vtkOpenGLGlyph3DHelper.cxx 20.2 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
/*=========================================================================

  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 "vtkOpenGLGlyph3DHelper.h"

Ken Martin's avatar
Ken Martin committed
16
#include "vtkOpenGLHelper.h"
Ken Martin's avatar
Ken Martin committed
17

18
#include "vtkBitArray.h"
Ken Martin's avatar
Ken Martin committed
19
#include "vtkCamera.h"
20 21
#include "vtkDataObject.h"
#include "vtkHardwareSelector.h"
Ken Martin's avatar
Ken Martin committed
22 23 24 25
#include "vtkMath.h"
#include "vtkMatrix3x3.h"
#include "vtkMatrix4x4.h"
#include "vtkNew.h"
26
#include "vtkObjectFactory.h"
Ken Martin's avatar
Ken Martin committed
27
#include "vtkOpenGLBufferObject.h"
28
#include "vtkOpenGLError.h"
Ken Martin's avatar
Ken Martin committed
29
#include "vtkOpenGLIndexBufferObject.h"
Ken Martin's avatar
Ken Martin committed
30
#include "vtkOpenGLRenderWindow.h"
31
#include "vtkOpenGLRenderer.h"
Ken Martin's avatar
Ken Martin committed
32
#include "vtkOpenGLShaderCache.h"
Ken Martin's avatar
Ken Martin committed
33
#include "vtkOpenGLVertexArrayObject.h"
Ken Martin's avatar
Ken Martin committed
34
#include "vtkOpenGLVertexBufferObject.h"
35
#include "vtkProperty.h"
36 37
#include "vtkShader.h"
#include "vtkShaderProgram.h"
38
#include "vtkTransform.h"
39

Ken Martin's avatar
Ken Martin committed
40
#include "vtkGlyph3DVS.h"
41

Ken Martin's avatar
Ken Martin committed
42 43 44 45 46 47
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkOpenGLGlyph3DHelper)

//-----------------------------------------------------------------------------
vtkOpenGLGlyph3DHelper::vtkOpenGLGlyph3DHelper()
{
Ken Martin's avatar
Ken Martin committed
48 49 50
  this->NormalMatrixBuffer = vtkOpenGLBufferObject::New();
  this->MatrixBuffer = vtkOpenGLBufferObject::New();
  this->ColorBuffer = vtkOpenGLBufferObject::New();
51 52
  this->ModelTransformMatrix = NULL;
  this->ModelNormalMatrix = NULL;
53
  this->ModelColor = NULL;
54 55
  this->UseFastPath = false;
  this->UsingInstancing = false;
Ken Martin's avatar
Ken Martin committed
56
}
Ken Martin's avatar
Ken Martin committed
57

Ken Martin's avatar
Ken Martin committed
58 59 60 61 62 63 64 65 66
//-----------------------------------------------------------------------------
vtkOpenGLGlyph3DHelper::~vtkOpenGLGlyph3DHelper()
{
  this->NormalMatrixBuffer->Delete();
  this->NormalMatrixBuffer = 0;
  this->MatrixBuffer->Delete();
  this->MatrixBuffer = 0;
  this->ColorBuffer->Delete();
  this->ColorBuffer = 0;
Ken Martin's avatar
Ken Martin committed
67 68
}

Ken Martin's avatar
Ken Martin committed
69

70 71 72 73 74
// ---------------------------------------------------------------------------
// Description:
// Release any graphics resources that are being consumed by this mapper.
void vtkOpenGLGlyph3DHelper::ReleaseGraphicsResources(vtkWindow *window)
{
Ken Martin's avatar
Ken Martin committed
75 76 77
  this->NormalMatrixBuffer->ReleaseGraphicsResources();
  this->MatrixBuffer->ReleaseGraphicsResources();
  this->ColorBuffer->ReleaseGraphicsResources();
78 79 80
  this->Superclass::ReleaseGraphicsResources(window);
}

81
//-----------------------------------------------------------------------------
82 83 84
void vtkOpenGLGlyph3DHelper::GetShaderTemplate(
    std::map<vtkShader::Type, vtkShader *> shaders,
    vtkRenderer *ren, vtkActor *actor)
85
{
86
  this->Superclass::GetShaderTemplate(shaders,ren,actor);
87

88
  shaders[vtkShader::Vertex]->SetSource(vtkGlyph3DVS);
89 90
}

91 92 93
void vtkOpenGLGlyph3DHelper::ReplaceShaderPositionVC(
  std::map<vtkShader::Type, vtkShader *> shaders,
  vtkRenderer *ren, vtkActor *actor)
94
{
95 96
  std::string VSSource = shaders[vtkShader::Vertex]->GetSource();

97
  if (this->LastLightComplexity[this->LastBoundBO] > 0)
98 99
    {
    // we use vertex instead of vertexMC
Ken Martin's avatar
Ken Martin committed
100
    vtkShaderProgram::Substitute(VSSource,
101
      "//VTK::PositionVC::Impl",
102
      "vertexVCVSOutput = MCVCMatrix * vertex;\n"
Ken Martin's avatar
Ken Martin committed
103
      "  gl_Position = MCDCMatrix * vertex;\n");
104
    }
105 106
  else
    {
Ken Martin's avatar
Ken Martin committed
107
    vtkShaderProgram::Substitute(VSSource,
108 109 110
      "//VTK::PositionVC::Impl",
      "gl_Position = MCDCMatrix * vertex;\n");
    }
111

112 113 114 115 116 117 118 119 120 121 122 123
  shaders[vtkShader::Vertex]->SetSource(VSSource);

  this->Superclass::ReplaceShaderPositionVC(shaders,ren,actor);
}

void vtkOpenGLGlyph3DHelper::ReplaceShaderColor(
  std::map<vtkShader::Type, vtkShader *> shaders,
  vtkRenderer *ren, vtkActor *actor)
{
  std::string VSSource = shaders[vtkShader::Vertex]->GetSource();
  std::string FSSource = shaders[vtkShader::Fragment]->GetSource();

124
  // deal with color
125
  if (this->UsingInstancing)
126
    {
127 128 129 130
    vtkShaderProgram::Substitute(VSSource,
      "//VTK::Color::Dec",
      "attribute vec4 glyphColor;\n"
      "varying vec4 vertexColorVSOutput;");
131 132 133
    }
  else
    {
Ken Martin's avatar
Ken Martin committed
134
    vtkShaderProgram::Substitute(VSSource,
135 136 137
      "//VTK::Color::Dec",
      "uniform vec4 glyphColor;\n"
      "varying vec4 vertexColorVSOutput;");
138
    }
Ken Martin's avatar
Ken Martin committed
139
  vtkShaderProgram::Substitute(VSSource,"//VTK::Color::Impl",
140
    "vertexColorVSOutput =  glyphColor;");
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

  // crate the material/color property declarations, and VS implementation
  // these are always defined
  std::string colorDec =
    "uniform float opacityUniform; // the fragment opacity\n"
    "uniform vec3 ambientColorUniform; // intensity weighted color\n"
    "uniform vec3 diffuseColorUniform; // intensity weighted color\n";
  // add some if we have a backface property
  if (actor->GetBackfaceProperty())
    {
    colorDec +=
      "uniform float opacityUniformBF; // the fragment opacity\n"
      "uniform vec3 ambientColorUniformBF; // intensity weighted color\n"
      "uniform vec3 diffuseColorUniformBF; // intensity weighted color\n";
    }
  // add more for specular
157
  if (this->LastLightComplexity[this->LastBoundBO])
158 159 160 161 162 163 164 165 166 167 168
    {
    colorDec +=
      "uniform vec3 specularColorUniform; // intensity weighted color\n"
      "uniform float specularPowerUniform;\n";
    if (actor->GetBackfaceProperty())
      {
      colorDec +=
        "uniform vec3 specularColorUniformBF; // intensity weighted color\n"
        "uniform float specularPowerUniformBF;\n";
      }
    }
169
  colorDec += "varying vec4 vertexColorVSOutput;\n";
Ken Martin's avatar
Ken Martin committed
170
  vtkShaderProgram::Substitute(FSSource,"//VTK::Color::Dec", colorDec);
171 172 173 174 175 176 177 178

  // now handle the more complex fragment shader implementation
  // the following are always defined variables.  We start
  // by assiging a default value from the uniform
  std::string colorImpl =
    "vec3 ambientColor;\n"
    "  vec3 diffuseColor;\n"
    "  float opacity;\n";
179
  if (this->LastLightComplexity[this->LastBoundBO])
180 181 182 183 184 185 186
    {
    colorImpl +=
      "  vec3 specularColor;\n"
      "  float specularPower;\n";
    }
  if (actor->GetBackfaceProperty())
    {
187
    if (this->LastLightComplexity[this->LastBoundBO])
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      {
      colorImpl +=
        "  if (int(gl_FrontFacing) == 0) {\n"
        "    ambientColor = ambientColorUniformBF;\n"
        "    diffuseColor = diffuseColorUniformBF;\n"
        "    specularColor = specularColorUniformBF;\n"
        "    specularPower = specularPowerUniformBF;\n"
        "    opacity = opacityUniformBF; }\n"
        "  else {\n"
        "    ambientColor = ambientColorUniform;\n"
        "    diffuseColor = diffuseColorUniform;\n"
        "    specularColor = specularColorUniform;\n"
        "    specularPower = specularPowerUniform;\n"
        "    opacity = opacityUniform; }\n";
      }
    else
      {
      colorImpl +=
        "  if (int(gl_FrontFacing) == 0) {\n"
        "    ambientColor = ambientColorUniformBF;\n"
        "    diffuseColor = diffuseColorUniformBF;\n"
        "    opacity = opacityUniformBF; }\n"
        "  else {\n"
        "    ambientColor = ambientColorUniform;\n"
        "    diffuseColor = diffuseColorUniform;\n"
        "    opacity = opacityUniform; }\n";
      }
    }
  else
    {
    colorImpl +=
      "    ambientColor = ambientColorUniform;\n"
      "    diffuseColor = diffuseColorUniform;\n"
      "    opacity = opacityUniform;\n";
222
    if (this->LastLightComplexity[this->LastBoundBO])
223 224 225 226 227 228 229 230
      {
      colorImpl +=
        "    specularColor = specularColorUniform;\n"
        "    specularPower = specularPowerUniform;\n";
      }
    }

  // now handle scalar coloring
231
  if (this->ScalarMaterialMode == VTK_MATERIALMODE_AMBIENT ||
Ken Martin's avatar
Ken Martin committed
232 233
        (this->ScalarMaterialMode == VTK_MATERIALMODE_DEFAULT &&
         actor->GetProperty()->GetAmbient() > actor->GetProperty()->GetDiffuse()))
234
    {
235 236 237 238
    vtkShaderProgram::Substitute(FSSource,"//VTK::Color::Impl",
      colorImpl +
      "  ambientColor = vertexColorVSOutput.rgb;\n"
      "  opacity = vertexColorVSOutput.a;");
239 240
    }
  else if (this->ScalarMaterialMode == VTK_MATERIALMODE_DIFFUSE ||
Ken Martin's avatar
Ken Martin committed
241 242
        (this->ScalarMaterialMode == VTK_MATERIALMODE_DEFAULT &&
         actor->GetProperty()->GetAmbient() <= actor->GetProperty()->GetDiffuse()))
243
    {
244 245 246 247
    vtkShaderProgram::Substitute(FSSource,"//VTK::Color::Impl",
      colorImpl +
      "  diffuseColor = vertexColorVSOutput.rgb;\n"
      "  opacity = vertexColorVSOutput.a;");
248 249 250
    }
  else
    {
251 252 253 254 255
    vtkShaderProgram::Substitute(FSSource,"//VTK::Color::Impl",
      colorImpl +
      "  diffuseColor = vertexColorVSOutput.rgb;\n"
      "  ambientColor = vertexColorVSOutput.rgb;\n"
      "  opacity = vertexColorVSOutput.a;");
256 257
    }

258
  if (this->UsingInstancing)
259
    {
Ken Martin's avatar
Ken Martin committed
260
    vtkShaderProgram::Substitute(VSSource,
261 262
       "//VTK::Glyph::Dec",
       "attribute mat4 GCMCMatrix;");
263 264 265
    }
  else
    {
Ken Martin's avatar
Ken Martin committed
266
    vtkShaderProgram::Substitute(VSSource,
267 268
      "//VTK::Glyph::Dec",
      "uniform mat4 GCMCMatrix;");
269
    }
Ken Martin's avatar
Ken Martin committed
270
  vtkShaderProgram::Substitute(VSSource,
271 272
    "//VTK::Glyph::Impl",
    "vec4 vertex = GCMCMatrix * vertexMC;\n");
273

274 275 276 277 278 279 280 281 282 283 284 285 286
  shaders[vtkShader::Vertex]->SetSource(VSSource);
  shaders[vtkShader::Fragment]->SetSource(FSSource);

  this->Superclass::ReplaceShaderColor(shaders,ren,actor);
}

void vtkOpenGLGlyph3DHelper::ReplaceShaderNormal(
  std::map<vtkShader::Type, vtkShader *> shaders,
  vtkRenderer *ren, vtkActor *actor)
{
  std::string VSSource = shaders[vtkShader::Vertex]->GetSource();
  std::string FSSource = shaders[vtkShader::Fragment]->GetSource();

287
  // new code for normal matrix if we have normals
Ken Martin's avatar
Ken Martin committed
288
  if (this->VBO->NormalOffset)
289
    {
290
    if (this->UsingInstancing)
291
      {
Ken Martin's avatar
Ken Martin committed
292
      vtkShaderProgram::Substitute(VSSource,
293 294 295 296 297
         "//VTK::Normal::Dec",
         "uniform mat3 normalMatrix;\n"
         "attribute vec3 normalMC;\n"
         "attribute mat3 glyphNormalMatrix;\n"
         "varying vec3 normalVCVSOutput;");
298 299 300
      }
    else
      {
Ken Martin's avatar
Ken Martin committed
301
      vtkShaderProgram::Substitute(VSSource,
302 303 304 305 306
         "//VTK::Normal::Dec",
         "uniform mat3 normalMatrix;\n"
         "attribute vec3 normalMC;\n"
         "uniform mat3 glyphNormalMatrix;\n"
         "varying vec3 normalVCVSOutput;");
307
      }
Ken Martin's avatar
Ken Martin committed
308
    vtkShaderProgram::Substitute(VSSource, "//VTK::Normal::Impl",
309
      "normalVCVSOutput = normalMatrix * glyphNormalMatrix * normalMC;");
310 311
    }

312 313 314 315 316 317 318 319 320 321 322 323 324
  shaders[vtkShader::Vertex]->SetSource(VSSource);
  shaders[vtkShader::Fragment]->SetSource(FSSource);

  this->Superclass::ReplaceShaderNormal(shaders,ren,actor);
}


void vtkOpenGLGlyph3DHelper::ReplaceShaderClip(
  std::map<vtkShader::Type, vtkShader *> shaders,
  vtkRenderer *ren, vtkActor *actor)
{
  std::string VSSource = shaders[vtkShader::Vertex]->GetSource();

325 326 327 328 329 330 331 332 333 334 335
  // override one part of the clipping code
  if (this->GetNumberOfClippingPlanes())
    {
    // add all the clipping planes
    int numClipPlanes = this->GetNumberOfClippingPlanes();
    if (numClipPlanes > 6)
      {
      vtkErrorMacro(<< "OpenGL has a limit of 6 clipping planes");
      numClipPlanes = 6;
      }

Ken Martin's avatar
Ken Martin committed
336
    vtkShaderProgram::Substitute(VSSource,
Ken Martin's avatar
Ken Martin committed
337 338 339 340 341
       "//VTK::Clip::Impl",
       "for (int planeNum = 0; planeNum < numClipPlanes; planeNum++)\n"
       "    {\n"
       "    clipDistances[planeNum] = dot(clipPlanes[planeNum], vertex);\n"
       "    }\n");
342 343
    }

344
  shaders[vtkShader::Vertex]->SetSource(VSSource);
345

346
  this->Superclass::ReplaceShaderClip(shaders,ren,actor);
347
}
Ken Martin's avatar
Ken Martin committed
348

Ken Martin's avatar
Ken Martin committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
void vtkOpenGLGlyph3DHelper::ReplaceShaderPicking(
  std::map<vtkShader::Type, vtkShader *> shaders,
  vtkRenderer *, vtkActor *)
{
  std::string FSSource = shaders[vtkShader::Fragment]->GetSource();

  if (this->LastSelectionState >= vtkHardwareSelector::MIN_KNOWN_PASS)
    {
    vtkShaderProgram::Substitute(FSSource, "//VTK::Picking::Dec",
      "uniform vec3 mapperIndex;");
    vtkShaderProgram::Substitute(FSSource,
      "//VTK::Picking::Impl",
      "  gl_FragData[0] = vec4(mapperIndex,1.0);\n");
    }
  shaders[vtkShader::Fragment]->SetSource(FSSource);
}

Ken Martin's avatar
Ken Martin committed
366 367 368 369 370 371 372 373 374
void vtkOpenGLGlyph3DHelper::GlyphRender(
  vtkRenderer* ren,
  vtkActor* actor,
  vtkIdType numPts,
  std::vector<unsigned char> &colors,
  std::vector<float> &matrices,
  std::vector<float> &normalMatrices,
  std::vector<vtkIdType> &pickIds,
  unsigned long pointMTime)
Ken Martin's avatar
Ken Martin committed
375
{
376 377 378 379
  // we always tell our triangle VAO to emulate unless we
  // have opngl 3.2 to be safe
  // this is because it seems that GLEW_ARB_vertex_array_object
  // does not always handle the attributes for GLEW_ARB_instanced_arrays
Ken Martin's avatar
Ken Martin committed
380
  this->Tris.VAO->SetForceEmulation(
381 382
    !vtkOpenGLRenderWindow::GetContextSupportsOpenGL32());

383
  this->CurrentInput = this->GetInput();
384 385 386 387 388 389
  this->UsingInstancing = false;

  vtkHardwareSelector* selector = ren->GetSelector();
  bool selecting_points = selector && (selector->GetFieldAssociation() ==
    vtkDataObject::FIELD_ASSOCIATION_POINTS);

390
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
391
  if (actor->GetProperty()->GetRepresentation() == VTK_SURFACE &&
392
      !selector && GLEW_ARB_instanced_arrays)
393 394
    {
    this->GlyphRenderInstances(ren, actor, numPts,
Ken Martin's avatar
Ken Martin committed
395
      colors, matrices, normalMatrices, pointMTime);
396 397
    return;
    }
398
#endif
399

Ken Martin's avatar
Ken Martin committed
400
  bool primed = false;
Ken Martin's avatar
Ken Martin committed
401

Ken Martin's avatar
Ken Martin committed
402 403 404 405
  for (vtkIdType inPtId = 0; inPtId < numPts; inPtId++)
    {
    if (selecting_points)
      {
406
      selector->RenderAttributeId(pickIds[inPtId]);
Ken Martin's avatar
Ken Martin committed
407 408 409 410
      }
    if (!primed)
      {
      this->RenderPieceStart(ren,actor);
411
      this->UpdateShaders(this->Tris, ren, actor);
Ken Martin's avatar
Ken Martin committed
412
      this->Tris.IBO->Bind();
Ken Martin's avatar
Ken Martin committed
413 414
      primed = true;
      }
Ken Martin's avatar
Ken Martin committed
415

Ken Martin's avatar
Ken Martin committed
416 417 418 419
    // handle the middle
    vtkShaderProgram *program = this->Tris.Program;

    // Apply the extra transform
420
    program->SetUniformMatrix4x4("GCMCMatrix", &(matrices[inPtId*16]));
Ken Martin's avatar
Ken Martin committed
421

Ken Martin's avatar
Ken Martin committed
422
    // for lit shaders set normal matrix
423
    if (this->LastLightComplexity[this->LastBoundBO] > 0 && this->VBO->NormalOffset &&
Ken Martin's avatar
Ken Martin committed
424
        !this->UsingInstancing)
Ken Martin's avatar
Ken Martin committed
425
      {
426
      program->SetUniformMatrix3x3("glyphNormalMatrix", &(normalMatrices[inPtId*9]));
Ken Martin's avatar
Ken Martin committed
427 428
      }

429
    program->SetUniform4uc("glyphColor", &(colors[inPtId*4]));
Ken Martin's avatar
Ken Martin committed
430

Ken Martin's avatar
Ken Martin committed
431
    if (selector)
Ken Martin's avatar
Ken Martin committed
432
      {
Ken Martin's avatar
Ken Martin committed
433
      program->SetUniform3f("mapperIndex", selector->GetPropColorValue());
Ken Martin's avatar
Ken Martin committed
434 435
      }

Ken Martin's avatar
Ken Martin committed
436 437 438 439
    // First we do the triangles, update the shader, set uniforms, etc.
    if (actor->GetProperty()->GetRepresentation() == VTK_POINTS)
      {
      glDrawRangeElements(GL_POINTS, 0,
Ken Martin's avatar
Ken Martin committed
440 441
                          static_cast<GLuint>(this->VBO->VertexCount - 1),
                          static_cast<GLsizei>(this->Tris.IBO->IndexCount),
Ken Martin's avatar
Ken Martin committed
442 443 444 445 446 447 448 449
                          GL_UNSIGNED_INT,
                          reinterpret_cast<const GLvoid *>(NULL));
      }
    if (actor->GetProperty()->GetRepresentation() == VTK_WIREFRAME)
      {
      // TODO wireframe of triangles is not lit properly right now
      // you either have to generate normals and send them down
      // or use a geometry shader.
450
      glDrawRangeElements(GL_LINES, 0,
Ken Martin's avatar
Ken Martin committed
451 452
                          static_cast<GLuint>(this->VBO->VertexCount - 1),
                          static_cast<GLsizei>(this->Tris.IBO->IndexCount),
453 454
                          GL_UNSIGNED_INT,
                          reinterpret_cast<const GLvoid *>(NULL));
Ken Martin's avatar
Ken Martin committed
455 456 457 458
      }
    if (actor->GetProperty()->GetRepresentation() == VTK_SURFACE)
      {
      glDrawRangeElements(GL_TRIANGLES, 0,
Ken Martin's avatar
Ken Martin committed
459 460
                          static_cast<GLuint>(this->VBO->VertexCount - 1),
                          static_cast<GLsizei>(this->Tris.IBO->IndexCount),
Ken Martin's avatar
Ken Martin committed
461 462 463
                          GL_UNSIGNED_INT,
                          reinterpret_cast<const GLvoid *>(NULL));
      }
Ken Martin's avatar
Ken Martin committed
464
    }
Ken Martin's avatar
Ken Martin committed
465
  if (primed)
Ken Martin's avatar
Ken Martin committed
466
    {
Ken Martin's avatar
Ken Martin committed
467
    this->Tris.IBO->Release();
Ken Martin's avatar
Ken Martin committed
468
    this->RenderPieceFinish(ren,actor);
Ken Martin's avatar
Ken Martin committed
469 470 471 472
    }
}

//-----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
473
void vtkOpenGLGlyph3DHelper::SetCameraShaderParameters(vtkOpenGLHelper &cellBO,
Ken Martin's avatar
Ken Martin committed
474 475 476 477 478
                                                    vtkRenderer* ren, vtkActor *actor)
{
  // do the superclass and then reset a couple values
  this->Superclass::SetCameraShaderParameters(cellBO,ren,actor);

479
  vtkShaderProgram *program = cellBO.Program;
Ken Martin's avatar
Ken Martin committed
480

481
  // Apply the extra transform
Ken Martin's avatar
Ken Martin committed
482 483
  if (this->ModelTransformMatrix)
    {
484 485
    program->SetUniformMatrix4x4("GCMCMatrix", this->ModelTransformMatrix);
    }
Ken Martin's avatar
Ken Martin committed
486 487

  // for lit shaders set normal matrix
488
  if (this->LastLightComplexity[&cellBO] > 0 && this->ModelNormalMatrix &&
Ken Martin's avatar
Ken Martin committed
489
     this->VBO->NormalOffset && !this->UsingInstancing)
Ken Martin's avatar
Ken Martin committed
490
    {
491
    program->SetUniformMatrix3x3("glyphNormalMatrix", this->ModelNormalMatrix);
Ken Martin's avatar
Ken Martin committed
492 493 494 495
    }
}

//-----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
496
void vtkOpenGLGlyph3DHelper::SetPropertyShaderParameters(vtkOpenGLHelper &cellBO,
497
                                                         vtkRenderer *ren, vtkActor *actor)
Ken Martin's avatar
Ken Martin committed
498 499 500 501
{
  // do the superclass and then reset a couple values
  this->Superclass::SetPropertyShaderParameters(cellBO,ren,actor);

502
  vtkShaderProgram *program = cellBO.Program;
Ken Martin's avatar
Ken Martin committed
503

504
  if (this->ModelColor)
Ken Martin's avatar
Ken Martin committed
505
    {
506
    program->SetUniform4uc("glyphColor", this->ModelColor);
Ken Martin's avatar
Ken Martin committed
507 508 509
    }
}

510
//-----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
511
void vtkOpenGLGlyph3DHelper::SetMapperShaderParameters(vtkOpenGLHelper &cellBO,
512 513 514 515 516 517 518 519 520 521 522
                                                         vtkRenderer *ren, vtkActor *actor)
{
  this->Superclass::SetMapperShaderParameters(cellBO,ren,actor);

  vtkHardwareSelector* selector = ren->GetSelector();
  if (selector && selector->GetCurrentPass() == vtkHardwareSelector::ID_LOW24)
    {
    cellBO.Program->SetUniform3f("mapperIndex", selector->GetPropColorValue());
    }
}

523
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
524 525
void vtkOpenGLGlyph3DHelper::GlyphRenderInstances(
    vtkRenderer* ren, vtkActor* actor, vtkIdType numPts,
526
    std::vector<unsigned char> &colors, std::vector<float> &matrices,
Ken Martin's avatar
Ken Martin committed
527
    std::vector<float> &normalMatrices,
528 529
    unsigned long pointMTime)
{
530
  this->UsingInstancing = true;
531
  this->RenderPieceStart(ren,actor);
532
  this->UpdateShaders(this->Tris, ren, actor);
533

Ken Martin's avatar
Ken Martin committed
534
  // do the superclass and then reset a couple values
Ken Martin's avatar
Ken Martin committed
535
  if (this->Tris.IBO->IndexCount &&   // we have points and one of
536
      (this->VBOBuildTime > this->InstanceBuffersLoadTime ||
Ken Martin's avatar
Ken Martin committed
537 538
      this->Tris.ShaderSourceTime > this->InstanceBuffersLoadTime ||
      pointMTime > this->InstanceBuffersLoadTime.GetMTime()))
539
    {
Ken Martin's avatar
Ken Martin committed
540
    this->Tris.VAO->Bind();
541
    // add 3 new BOs?
Ken Martin's avatar
Ken Martin committed
542 543 544
    this->MatrixBuffer->Bind();
    this->MatrixBuffer->Upload(matrices, vtkOpenGLBufferObject::ArrayBuffer);
    if (!this->Tris.VAO->AddAttributeMatrixWithDivisor(this->Tris.Program, this->MatrixBuffer,
545 546 547 548
        "GCMCMatrix", 0, 16*sizeof(float), VTK_FLOAT, 4, false, 1))
      {
      vtkErrorMacro(<< "Error setting 'GCMCMatrix' in shader VAO.");
      }
Ken Martin's avatar
Ken Martin committed
549
    this->MatrixBuffer->Release();
550

551
    if (this->VBO->NormalOffset && this->LastLightComplexity[this->LastBoundBO] > 0)
552
      {
Ken Martin's avatar
Ken Martin committed
553 554 555 556 557
      this->NormalMatrixBuffer->Bind();
      this->NormalMatrixBuffer->Upload(
        normalMatrices, vtkOpenGLBufferObject::ArrayBuffer);
      if (!this->Tris.VAO->AddAttributeMatrixWithDivisor(
            this->Tris.Program, this->NormalMatrixBuffer,
558 559 560 561
            "glyphNormalMatrix", 0, 9*sizeof(float), VTK_FLOAT, 3, false, 1))
        {
        vtkErrorMacro(<< "Error setting 'glyphNormalMatrix' in shader VAO.");
        }
Ken Martin's avatar
Ken Martin committed
562
      this->NormalMatrixBuffer->Release();
563 564
      }

Ken Martin's avatar
Ken Martin committed
565 566 567 568
    this->ColorBuffer->Bind();
    this->ColorBuffer->Upload(colors, vtkOpenGLBufferObject::ArrayBuffer);
    if (!this->Tris.VAO->AddAttributeArrayWithDivisor(
          this->Tris.Program, this->ColorBuffer,
569
          "glyphColor", 0, 4*sizeof(unsigned char), VTK_UNSIGNED_CHAR, 4, true, 1, false))
570 571 572
      {
      vtkErrorMacro(<< "Error setting 'diffuse color' in shader VAO.");
      }
Ken Martin's avatar
Ken Martin committed
573
    this->ColorBuffer->Release();
574 575 576 577

    this->InstanceBuffersLoadTime.Modified();
    }

Ken Martin's avatar
Ken Martin committed
578
  this->Tris.IBO->Bind();
579 580
#if GL_ES_VERSION_3_0 == 1
  glDrawElementsInstanced(GL_TRIANGLES,
Ken Martin's avatar
Ken Martin committed
581
                        static_cast<GLsizei>(this->Tris.IBO->IndexCount),
582 583 584 585
                        GL_UNSIGNED_INT,
                        reinterpret_cast<const GLvoid *>(NULL),
                        numPts);
#else
586
  if (GLEW_ARB_instanced_arrays)
587 588
    {
    glDrawElementsInstancedARB(GL_TRIANGLES,
Ken Martin's avatar
Ken Martin committed
589
                          static_cast<GLsizei>(this->Tris.IBO->IndexCount),
590 591 592 593
                          GL_UNSIGNED_INT,
                          reinterpret_cast<const GLvoid *>(NULL),
                          numPts);
    }
594
#endif
595 596
  vtkOpenGLCheckErrorMacro("failed after Render");

Ken Martin's avatar
Ken Martin committed
597
  this->Tris.IBO->Release();
598 599
  this->RenderPieceFinish(ren, actor);
}
600
#endif
Ken Martin's avatar
Ken Martin committed
601 602 603 604 605 606

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