vtkOpenGLVertexBufferObject.cxx 14.5 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*=========================================================================

  Program:   Visualization Toolkit

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkOpenGLVertexBufferObject.h"
#include "vtkObjectFactory.h"

#include "vtkPoints.h"

#include "vtk_glew.h"

vtkStandardNewMacro(vtkOpenGLVertexBufferObject)

vtkOpenGLVertexBufferObject::vtkOpenGLVertexBufferObject()
{
  this->VertexCount = 0;
  this->Stride = 0;       // The size of a complete vertex + attributes
  this->VertexOffset = 0; // Offset of the vertex
  this->NormalOffset = 0; // Offset of the normal
  this->TCoordOffset = 0; // Offset of the texture coordinates
  this->TCoordComponents = 0; // Number of texture dimensions
  this->ColorOffset = 0;  // Offset of the color
  this->ColorComponents = 0; // Number of color components
  this->SetType(vtkOpenGLBufferObject::ArrayBuffer);
34 35 36
  this->CoordShiftAndScaleMethod = AUTO_SHIFT_SCALE;
  this->CoordShiftAndScaleEnabled = false;
  for (int i = 0; i < 3; ++i)
37
  {
38 39
    this->CoordShift[i] = 0.;
    this->CoordScale[i] = 1.;
40
  }
Ken Martin's avatar
Ken Martin committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

vtkOpenGLVertexBufferObject::~vtkOpenGLVertexBufferObject()
{
}

// we only instantiate some cases to avoid template explosion
#define vtkFloatDoubleTemplateMacro(call)  \
  vtkTemplateMacroCase(VTK_DOUBLE, double, call); \
  vtkTemplateMacroCase(VTK_FLOAT, float, call);

namespace {

// internal function called by AppendVBO
template<typename T, typename T2, typename T3>
void TemplatedAppendVBO3(vtkOpenGLVertexBufferObject *self,
  T* points, T2* normals, vtkIdType numPts,
  T3* tcoords, int textureComponents,
  unsigned char *colors, int colorComponents)
{
  // Figure out how big each block will be, currently 6 or 7 floats.
  int blockSize = 3;
  self->VertexOffset = 0;
  self->NormalOffset = 0;
  self->TCoordOffset = 0;
  self->TCoordComponents = 0;
  self->ColorComponents = 0;
  self->ColorOffset = 0;
  if (normals)
70
  {
Ken Martin's avatar
Ken Martin committed
71 72
    self->NormalOffset = sizeof(float) * blockSize;
    blockSize += 3;
73
  }
Ken Martin's avatar
Ken Martin committed
74
  if (tcoords)
75
  {
Ken Martin's avatar
Ken Martin committed
76 77 78
    self->TCoordOffset = sizeof(float) * blockSize;
    self->TCoordComponents = textureComponents;
    blockSize += textureComponents;
79
  }
Ken Martin's avatar
Ken Martin committed
80
  if (colors)
81
  {
Ken Martin's avatar
Ken Martin committed
82 83 84
    self->ColorComponents = colorComponents;
    self->ColorOffset = sizeof(float) * blockSize;
    ++blockSize;
85
  }
Ken Martin's avatar
Ken Martin committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99
  self->Stride = sizeof(float) * blockSize;

  // Create a buffer, and copy the data over.
  self->PackedVBO.resize(blockSize * (numPts + self->VertexCount));
  std::vector<float>::iterator it = self->PackedVBO.begin()
    + (self->VertexCount*self->Stride/sizeof(float));

  T *pointPtr;
  T2 *normalPtr;
  T3 *tcoordPtr;
  unsigned char *colorPtr;

  // TODO: optimize this somehow, lots of if statements in here
  for (vtkIdType i = 0; i < numPts; ++i)
100
  {
Ken Martin's avatar
Ken Martin committed
101 102 103 104 105 106 107 108 109 110
    pointPtr = points + i*3;
    normalPtr = normals + i*3;
    tcoordPtr = tcoords + i*textureComponents;
    colorPtr = colors + i*colorComponents;

    // Vertices
    *(it++) = *(pointPtr++);
    *(it++) = *(pointPtr++);
    *(it++) = *(pointPtr++);
    if (normals)
111
    {
Ken Martin's avatar
Ken Martin committed
112 113 114
      *(it++) = *(normalPtr++);
      *(it++) = *(normalPtr++);
      *(it++) = *(normalPtr++);
115
    }
Ken Martin's avatar
Ken Martin committed
116
    if (tcoords)
117
    {
Ken Martin's avatar
Ken Martin committed
118
      for (int j = 0; j < textureComponents; ++j)
119
      {
Ken Martin's avatar
Ken Martin committed
120 121
        *(it++) = *(tcoordPtr++);
      }
122
    }
Ken Martin's avatar
Ken Martin committed
123
    if (colors)
124
    {
Ken Martin's avatar
Ken Martin committed
125
      if (colorComponents == 4)
126
      {
Ken Martin's avatar
Ken Martin committed
127
        *(it++) = *reinterpret_cast<float *>(colorPtr);
128
      }
Ken Martin's avatar
Ken Martin committed
129
      else
130
      {
Ken Martin's avatar
Ken Martin committed
131 132 133 134 135 136
        vtkucfloat c;
        c.c[0] = *(colorPtr++);
        c.c[1] = *(colorPtr++);
        c.c[2] = *(colorPtr);
        c.c[3] =  255;
        *(it++) = c.f;
Ken Martin's avatar
Ken Martin committed
137 138
      }
    }
139
  }
Ken Martin's avatar
Ken Martin committed
140 141 142
  self->VertexCount += numPts;
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
// internal function called by AppendVBO
template<typename T, typename T2, typename T3>
void TemplatedAppendVBOShiftScale3(vtkOpenGLVertexBufferObject *self,
  T* points, T2* normals, vtkIdType numPts,
  T3* tcoords, int textureComponents,
  unsigned char* colors, int colorComponents,
  const double* shift, const double* scale)
{
  // Figure out how big each block will be, currently 6 or 7 floats.
  int blockSize = 3;
  self->VertexOffset = 0;
  self->NormalOffset = 0;
  self->TCoordOffset = 0;
  self->TCoordComponents = 0;
  self->ColorComponents = 0;
  self->ColorOffset = 0;
  if (normals)
160
  {
161 162
    self->NormalOffset = sizeof(float) * blockSize;
    blockSize += 3;
163
  }
164
  if (tcoords)
165
  {
166 167 168
    self->TCoordOffset = sizeof(float) * blockSize;
    self->TCoordComponents = textureComponents;
    blockSize += textureComponents;
169
  }
170
  if (colors)
171
  {
172 173 174
    self->ColorComponents = colorComponents;
    self->ColorOffset = sizeof(float) * blockSize;
    ++blockSize;
175
  }
176 177 178 179 180 181 182 183 184 185 186 187 188 189
  self->Stride = sizeof(float) * blockSize;

  // Create a buffer, and copy the data over.
  self->PackedVBO.resize(blockSize * (numPts + self->VertexCount));
  std::vector<float>::iterator it = self->PackedVBO.begin()
    + (self->VertexCount*self->Stride/sizeof(float));

  T *pointPtr;
  T2 *normalPtr;
  T3 *tcoordPtr;
  unsigned char *colorPtr;

  // TODO: optimize this somehow, lots of if statements in here
  for (vtkIdType i = 0; i < numPts; ++i)
190
  {
191 192 193 194 195 196 197 198 199 200
    pointPtr = points + i*3;
    normalPtr = normals + i*3;
    tcoordPtr = tcoords + i*textureComponents;
    colorPtr = colors + i*colorComponents;

    // Vertices
    *(it++) = (*(pointPtr++) - shift[0]) * scale[0];
    *(it++) = (*(pointPtr++) - shift[1]) * scale[1];
    *(it++) = (*(pointPtr++) - shift[2]) * scale[2];
    if (normals)
201
    {
202 203 204
      *(it++) = *(normalPtr++);
      *(it++) = *(normalPtr++);
      *(it++) = *(normalPtr++);
205
    }
206
    if (tcoords)
207
    {
208
      for (int j = 0; j < textureComponents; ++j)
209
      {
210 211
        *(it++) = *(tcoordPtr++);
      }
212
    }
213
    if (colors)
214
    {
215
      if (colorComponents == 4)
216
      {
217
        *(it++) = *reinterpret_cast<float *>(colorPtr);
218
      }
219
      else
220
      {
221 222 223 224 225 226 227 228
        vtkucfloat c;
        c.c[0] = *(colorPtr++);
        c.c[1] = *(colorPtr++);
        c.c[2] = *(colorPtr);
        c.c[3] =  255;
        *(it++) = c.f;
      }
    }
229
  }
230 231 232
  self->VertexCount += numPts;
}

Ken Martin's avatar
Ken Martin committed
233 234 235 236 237 238 239 240
//----------------------------------------------------------------------------
template<typename T, typename T2>
void TemplatedAppendVBO2(vtkOpenGLVertexBufferObject *self,
  T* points, T2 *normals, vtkIdType numPts,
  vtkDataArray *tcoords,
  unsigned char *colors, int colorComponents)
{
  if (tcoords)
241
  {
Ken Martin's avatar
Ken Martin committed
242
    switch(tcoords->GetDataType())
243
    {
Ken Martin's avatar
Ken Martin committed
244 245 246 247 248 249 250
      vtkFloatDoubleTemplateMacro(
        TemplatedAppendVBO3(self, points, normals,
                  numPts,
                  static_cast<VTK_TT*>(tcoords->GetVoidPointer(0)),
                  tcoords->GetNumberOfComponents(),
                  colors, colorComponents));
    }
251
  }
Ken Martin's avatar
Ken Martin committed
252
  else
253
  {
Ken Martin's avatar
Ken Martin committed
254 255 256
    TemplatedAppendVBO3(self, points, normals,
                        numPts, (float *)NULL, 0,
                        colors, colorComponents);
257
  }
Ken Martin's avatar
Ken Martin committed
258 259
}

260 261 262 263 264 265 266 267 268
//----------------------------------------------------------------------------
template<typename T, typename T2>
void TemplatedAppendVBOShiftScale2(vtkOpenGLVertexBufferObject* self,
  T* points, T2* normals, vtkIdType numPts,
  vtkDataArray* tcoords,
  unsigned char* colors, int colorComponents,
  const double* shift, const double* scale)
{
  if (tcoords)
269
  {
270
    switch(tcoords->GetDataType())
271
    {
272 273 274 275 276 277 278 279
      vtkFloatDoubleTemplateMacro(
        TemplatedAppendVBOShiftScale3(self, points, normals,
                  numPts,
                  static_cast<VTK_TT*>(tcoords->GetVoidPointer(0)),
                  tcoords->GetNumberOfComponents(),
                  colors, colorComponents,
                  shift, scale));
    }
280
  }
281
  else
282
  {
283 284 285 286
    TemplatedAppendVBOShiftScale3(self, points, normals,
                        numPts, (float *)NULL, 0,
                        colors, colorComponents,
                        shift, scale);
287
  }
288 289
}

Ken Martin's avatar
Ken Martin committed
290 291
//----------------------------------------------------------------------------
template<typename T>
292 293 294
void TemplatedAppendVBO(vtkOpenGLVertexBufferObject* self,
  T* points, vtkDataArray* normals, vtkIdType numPts,
  vtkDataArray* tcoords,
Ken Martin's avatar
Ken Martin committed
295 296 297
  unsigned char *colors, int colorComponents)
{
  if (normals)
298
  {
Ken Martin's avatar
Ken Martin committed
299
    switch(normals->GetDataType())
300
    {
Ken Martin's avatar
Ken Martin committed
301 302 303 304 305
      vtkFloatDoubleTemplateMacro(
        TemplatedAppendVBO2(self, points,
                  static_cast<VTK_TT*>(normals->GetVoidPointer(0)),
                  numPts, tcoords, colors, colorComponents));
    }
306
  }
Ken Martin's avatar
Ken Martin committed
307
  else
308
  {
Ken Martin's avatar
Ken Martin committed
309 310 311
    TemplatedAppendVBO2(self, points,
                        (float *)NULL,
                        numPts, tcoords, colors, colorComponents);
312
  }
Ken Martin's avatar
Ken Martin committed
313 314
}

315 316 317 318 319 320 321 322 323
//----------------------------------------------------------------------------
template<typename T>
void TemplatedAppendVBOShiftScale(vtkOpenGLVertexBufferObject* self,
  T* points, vtkDataArray* normals, vtkIdType numPts,
  vtkDataArray* tcoords,
  unsigned char *colors, int colorComponents,
  const double* shift, const double* scale)
{
  if (normals)
324
  {
325
    switch(normals->GetDataType())
326
    {
327 328 329 330 331
      vtkFloatDoubleTemplateMacro(
        TemplatedAppendVBOShiftScale2(self, points,
                  static_cast<VTK_TT*>(normals->GetVoidPointer(0)),
                  numPts, tcoords, colors, colorComponents, shift, scale));
    }
332
  }
333
  else
334
  {
335 336 337
    TemplatedAppendVBOShiftScale2(self, points,
                        (float *)NULL,
                        numPts, tcoords, colors, colorComponents, shift, scale);
338
  }
339 340
}

Ken Martin's avatar
Ken Martin committed
341 342
} // end anonymous namespace

343 344 345 346 347 348
void vtkOpenGLVertexBufferObject::SetCoordShiftAndScaleMethod(ShiftScaleMethod meth)
{
  vtkDebugMacro(
    << this->GetClassName() << " (" << this
    << "): setting CoordShiftAndScaleMethod to " << meth);
  if (this->CoordShiftAndScaleMethod != meth)
349
  {
350
    if (!this->PackedVBO.empty())
351
    {
352 353
      vtkErrorMacro("SetCoordShiftAndScaleMethod() called with non-empty VBO! Ignoring.");
      return;
354
    }
355 356 357

    this->CoordShiftAndScaleMethod = meth;
    this->Modified();
358
  }
359 360 361 362 363 364 365 366
}

void vtkOpenGLVertexBufferObject::SetCoordShift(double sx, double sy, double sz)
{
  vtkDebugMacro(
    << this->GetClassName() << " (" << this
    << "): setting CoordShift to (" << sx << "," << sy << "," << sz << ")");
  if ((this->CoordShift[0] != sx) || (this->CoordShift[1] != sy) || (this->CoordShift[2] != sz))
367
  {
368
    if (!this->PackedVBO.empty())
369
    {
370 371
      vtkErrorMacro("SetCoordShift() called with non-empty VBO! Ignoring.");
      return;
372
    }
373 374 375 376 377 378 379
    this->CoordShift[0] = sx;
    this->CoordShift[1] = sy;
    this->CoordShift[2] = sz;
    this->CoordShiftAndScaleEnabled =
      this->CoordShift[0] != 0.0 || this->CoordShift[1] != 0.0 || this->CoordShift[2] != 0.0 ||
      this->CoordScale[0] != 1.0 || this->CoordScale[1] != 1.0 || this->CoordScale[2] != 1.0;
    this->Modified();
380
  }
381 382 383 384 385 386 387 388 389 390 391 392 393
}

void vtkOpenGLVertexBufferObject::SetCoordShift(const double s[3])
{
  this->SetCoordShift(s[0], s[1], s[2]);
}

void vtkOpenGLVertexBufferObject::SetCoordScale(double sx, double sy, double sz)
{
  vtkDebugMacro(
    << this->GetClassName() << " (" << this
    << "): setting CoordScale to (" << sx << "," << sy << "," << sz << ")");
  if ((this->CoordScale[0] != sx) || (this->CoordScale[1] != sy) || (this->CoordScale[2] != sz))
394
  {
395
    if (!this->PackedVBO.empty())
396
    {
397 398
      vtkErrorMacro("SetCoordScale() called with non-empty VBO! Ignoring.");
      return;
399
    }
400 401 402 403 404 405 406 407

    this->CoordScale[0] = sx;
    this->CoordScale[1] = sy;
    this->CoordScale[2] = sz;
    this->CoordShiftAndScaleEnabled =
      this->CoordShift[0] != 0.0 || this->CoordShift[1] != 0.0 || this->CoordShift[2] != 0.0 ||
      this->CoordScale[0] != 1.0 || this->CoordScale[1] != 1.0 || this->CoordScale[2] != 1.0;
    this->Modified();
408
  }
409 410 411 412 413 414
}

void vtkOpenGLVertexBufferObject::SetCoordScale(const double s[3])
{
  this->SetCoordScale(s[0], s[1], s[2]);
}
Ken Martin's avatar
Ken Martin committed
415 416 417 418 419 420 421 422 423 424 425

// Take the points, and pack them into the VBO object supplied. This currently
// takes whatever the input type might be and packs them into a VBO using
// floats for the vertices and normals, and unsigned char for the colors (if
// the array is non-null).
void vtkOpenGLVertexBufferObject::AppendVBO(
  vtkPoints *points, unsigned int numPts,
  vtkDataArray *normals,
  vtkDataArray *tcoords,
  unsigned char *colors, int colorComponents)
{
426
  if (this->CoordShiftAndScaleEnabled)
427
  {
428
    switch(points->GetDataType())
429
    {
430 431 432 433 434 435
      vtkTemplateMacro(
        TemplatedAppendVBOShiftScale(
          this, static_cast<VTK_TT*>(points->GetVoidPointer(0)),
          normals, numPts, tcoords, colors, colorComponents,
          this->CoordShift, this->CoordScale));
    }
436
  }
437
  else
438
  {
439
    switch(points->GetDataType())
440
    {
441 442 443 444
      vtkTemplateMacro(
        TemplatedAppendVBO(
          this, static_cast<VTK_TT*>(points->GetVoidPointer(0)),
          normals, numPts, tcoords, colors, colorComponents));
Ken Martin's avatar
Ken Martin committed
445
    }
446
  }
Ken Martin's avatar
Ken Martin committed
447 448 449 450 451 452 453 454 455
}

// create a VBO, append the data to it, then upload it
void vtkOpenGLVertexBufferObject::CreateVBO(
  vtkPoints *points, unsigned int numPts,
  vtkDataArray *normals,
  vtkDataArray *tcoords,
  unsigned char *colors, int colorComponents)
{
456
  if (this->CoordShiftAndScaleMethod == AUTO_SHIFT_SCALE)
457
  {
458 459 460 461 462 463
    double bds[6];
    double shift[3];
    double scale[3];
    points->GetBounds(bds);
    bool needed = false;
    for (int i = 0; i < 3; ++i)
464
    {
465 466 467
      shift[i] = bds[2 * i]; //-0.5 * (bds[2 * i + 1] + bds[2 * i]);
      double delta = bds[2 * i + 1] - bds[2 * i];
      if (delta > 0.0 && fabs(shift[i]) / delta > 1.0e4)
468
      {
469 470
        needed = true;
        scale[i] = 1.0 / delta;
471
      }
472
      else
473
      {
474 475
        scale[i] = 1.0;
      }
476
    }
477
    if (needed)
478
    {
479 480 481
      this->SetCoordShift(shift);
      this->SetCoordScale(scale);
    }
482
  }
Ken Martin's avatar
Ken Martin committed
483
  // fast path
484 485 486
  if (
    !this->CoordShiftAndScaleEnabled && !tcoords && !normals &&
    !colors && points->GetDataType() == VTK_FLOAT)
487
  {
Ken Martin's avatar
Ken Martin committed
488 489 490 491 492 493 494 495 496 497 498 499
    int blockSize = 3;
    this->VertexOffset = 0;
    this->NormalOffset = 0;
    this->TCoordOffset = 0;
    this->TCoordComponents = 0;
    this->ColorComponents = 0;
    this->ColorOffset = 0;
    this->Stride = sizeof(float) * blockSize;
    this->VertexCount = numPts;
    this->Upload((float *)(points->GetVoidPointer(0)), numPts*3,
      vtkOpenGLBufferObject::ArrayBuffer);
    return;
500
  }
Ken Martin's avatar
Ken Martin committed
501 502 503

  // slower path
  this->VertexCount = 0;
504
  this->AppendVBO(points, numPts, normals, tcoords, colors, colorComponents);
Ken Martin's avatar
Ken Martin committed
505 506 507 508 509 510 511 512 513 514 515
  this->Upload(this->PackedVBO, vtkOpenGLBufferObject::ArrayBuffer);
  this->PackedVBO.resize(0);
  return;
}


//-----------------------------------------------------------------------------
void vtkOpenGLVertexBufferObject::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}