VTK
vtkOpenGLPolyDataMapper.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4 
5  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
6  All rights reserved.
7  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without even
10  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  PURPOSE. See the above copyright notice for more information.
12 
13 =========================================================================*/
21 #ifndef vtkOpenGLPolyDataMapper_h
22 #define vtkOpenGLPolyDataMapper_h
23 
24 #include "vtkNew.h" // For vtkNew
25 #include "vtkRenderingOpenGL2Module.h" // For export macro
26 #include "vtkNew.h" // for ivars
27 #include "vtkPolyDataMapper.h"
28 #include "vtkShader.h" // for methods
29 #include "vtkOpenGLHelper.h" // used for ivars
30 
31 #include <vector> //for ivars
32 #include <map> //for methods
33 
34 class vtkCellArray;
36 class vtkMatrix4x4;
37 class vtkMatrix3x3;
39 class vtkOpenGLTexture;
43 class vtkPoints;
44 class vtkTextureObject;
45 class vtkTransform;
46 
47 
48 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLPolyDataMapper : public vtkPolyDataMapper
49 {
50 public:
51  static vtkOpenGLPolyDataMapper* New();
53  void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
54 
58  void RenderPiece(vtkRenderer *ren, vtkActor *act) VTK_OVERRIDE;
59 
61 
64  virtual void RenderPieceStart(vtkRenderer *ren, vtkActor *act);
65  virtual void RenderPieceDraw(vtkRenderer *ren, vtkActor *act);
66  virtual void RenderPieceFinish(vtkRenderer *ren, vtkActor *act);
68 
74  void ReleaseGraphicsResources(vtkWindow *) VTK_OVERRIDE;
75 
76  vtkGetMacro(PopulateSelectionSettings,int);
77  void SetPopulateSelectionSettings(int v) { this->PopulateSelectionSettings = v; };
78 
85  bool GetSupportsSelection() VTK_OVERRIDE { return true; }
86 
97  bool GetIsOpaque() VTK_OVERRIDE;
98 
99  // used by RenderPiece and functions it calls to reduce
100  // calls to get the input and allow for rendering of
101  // other polydata (not the input)
102  vtkPolyData *CurrentInput;
103 
105 
111  vtkSetStringMacro(PointIdArrayName);
112  vtkGetStringMacro(PointIdArrayName);
113  vtkSetStringMacro(CellIdArrayName);
114  vtkGetStringMacro(CellIdArrayName);
116 
118 
123  vtkSetStringMacro(ProcessIdArrayName);
124  vtkGetStringMacro(ProcessIdArrayName);
126 
128 
137  vtkSetStringMacro(CompositeIdArrayName);
138  vtkGetStringMacro(CompositeIdArrayName);
140 
141 
143 
150  void AddShaderReplacement(
151  vtkShader::Type shaderType, // vertex, fragment, etc
152  const std::string& originalValue,
153  bool replaceFirst, // do this replacement before the default
154  const std::string& replacementValue,
155  bool replaceAll);
156  void ClearShaderReplacement(
157  vtkShader::Type shaderType, // vertex, fragment, etc
158  const std::string& originalValue,
159  bool replaceFirst);
161 
163 
169  vtkSetStringMacro(VertexShaderCode);
170  vtkGetStringMacro(VertexShaderCode);
171  vtkSetStringMacro(FragmentShaderCode);
172  vtkGetStringMacro(FragmentShaderCode);
173  vtkSetStringMacro(GeometryShaderCode);
174  vtkGetStringMacro(GeometryShaderCode);
176 
177  // the following is all extra stuff to work around the
178  // fact that gl_PrimitiveID does not work correctly on
179  // Apple devices with AMD graphics hardware. See apple
180  // bug ID 20747550
181  static vtkPolyData *HandleAppleBug(
182  vtkPolyData *poly,
183  std::vector<float> &buffData);
184 
188  void ShallowCopy(vtkAbstractMapper *m);
189 
191 
194  void ForceHaveAppleBugOff()
195  {
196  this->HaveAppleBugForce = 1;
197  this->Modified();
198  }
200  {
201  this->HaveAppleBugForce = 2;
202  this->Modified();
203  }
205 
209  bool GetHaveAppleBug() { return this->HaveAppleBug; }
210 
212  vtkGetObjectMacro(VBOs, vtkOpenGLVertexBufferObjectGroup);
213 
217  void SetVBOShiftScaleMethod(int m);
218 
220  PrimitiveStart = 0,
221  PrimitivePoints = 0,
228  PrimitiveEnd
229  };
230 
234  static void MakeCellCellMap(std::vector<vtkIdType> &CellCellMap,
235  bool HaveAppleBug,
236  vtkPolyData *poly,
237  vtkCellArray **prims, int representation,
238  vtkPoints *points);
239 
240 protected:
242  ~vtkOpenGLPolyDataMapper() VTK_OVERRIDE;
243 
245 
246  // the following is all extra stuff to work around the
247  // fact that gl_PrimitiveID does not work correctly on
248  // Apple devices with AMD graphics hardware. See apple
249  // bug ID 20747550
250  bool HaveAppleBug;
251  int HaveAppleBugForce; // 0 = default 1 = 0ff 2 = on
252  std::vector<float> AppleBugPrimIDs;
253  vtkOpenGLBufferObject *AppleBugPrimIDBuffer;
254 
258  void GetCoincidentParameters(
259  vtkRenderer *ren, vtkActor *actor, float &factor, float &offset);
260 
266  void ComputeBounds() VTK_OVERRIDE;
267 
272  virtual void UpdateShaders(
273  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
274 
278  virtual bool GetNeedToRebuildShaders(
279  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
280 
284  virtual void BuildShaders(
285  std::map<vtkShader::Type, vtkShader *> shaders,
286  vtkRenderer *ren, vtkActor *act);
287 
291  virtual void GetShaderTemplate(
292  std::map<vtkShader::Type, vtkShader *> shaders,
293  vtkRenderer *ren, vtkActor *act);
294 
298  virtual void ReplaceShaderValues(
299  std::map<vtkShader::Type, vtkShader *> shaders,
300  vtkRenderer *ren, vtkActor *act);
301 
303 
307  virtual void ReplaceShaderRenderPass(
308  std::map<vtkShader::Type, vtkShader *> shaders,
309  vtkRenderer *ren, vtkActor *act, bool prePass);
310  virtual void ReplaceShaderColor(
311  std::map<vtkShader::Type, vtkShader *> shaders,
312  vtkRenderer *ren, vtkActor *act);
313  virtual void ReplaceShaderLight(
314  std::map<vtkShader::Type, vtkShader *> shaders,
315  vtkRenderer *ren, vtkActor *act);
316  virtual void ReplaceShaderTCoord(
317  std::map<vtkShader::Type, vtkShader *> shaders,
318  vtkRenderer *ren, vtkActor *act);
319  virtual void ReplaceShaderPicking(
320  std::map<vtkShader::Type, vtkShader *> shaders,
321  vtkRenderer *ren, vtkActor *act);
322  virtual void ReplaceShaderPrimID(
323  std::map<vtkShader::Type, vtkShader *> shaders,
324  vtkRenderer *ren, vtkActor *act);
325  virtual void ReplaceShaderNormal(
326  std::map<vtkShader::Type, vtkShader *> shaders,
327  vtkRenderer *ren, vtkActor *act);
328  virtual void ReplaceShaderClip(
329  std::map<vtkShader::Type, vtkShader *> shaders,
330  vtkRenderer *ren, vtkActor *act);
331  virtual void ReplaceShaderPositionVC(
332  std::map<vtkShader::Type, vtkShader *> shaders,
333  vtkRenderer *ren, vtkActor *act);
334  virtual void ReplaceShaderCoincidentOffset(
335  std::map<vtkShader::Type, vtkShader *> shaders,
336  vtkRenderer *ren, vtkActor *act);
337  virtual void ReplaceShaderDepth(
338  std::map<vtkShader::Type, vtkShader *> shaders,
339  vtkRenderer *ren, vtkActor *act);
341 
345  virtual void SetMapperShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
346 
350  virtual void SetLightingShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
351 
355  virtual void SetCameraShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
356 
360  virtual void SetPropertyShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
361 
365  virtual void UpdateBufferObjects(vtkRenderer *ren, vtkActor *act);
366 
370  virtual bool GetNeedToRebuildBufferObjects(vtkRenderer *ren, vtkActor *act);
371 
375  virtual void BuildBufferObjects(vtkRenderer *ren, vtkActor *act);
376 
380  virtual void BuildIBO(vtkRenderer *ren, vtkActor *act, vtkPolyData *poly);
381 
382  // The VBO and its layout.
384 
385  // Structures for the various cell types we render.
386  vtkOpenGLHelper Primitives[PrimitiveEnd];
387  vtkOpenGLHelper *LastBoundBO;
388  bool DrawingEdgesOrVertices;
389 
390  // do we have wide lines that require special handling
391  virtual bool HaveWideLines(vtkRenderer *, vtkActor *);
392 
393  // do we have textures that require special handling
394  virtual bool HaveTextures(vtkActor *actor);
395 
396  // how many textures do we have
397  virtual unsigned int GetNumberOfTextures(vtkActor *actor);
398 
399  // populate a vector with the textures we have
400  // the order is always
401  // ColorInternalTexture
402  // Actors texture
403  // Properies textures
404  virtual std::vector<vtkTexture *> GetTextures(vtkActor *actor);
405 
406  // do we have textures coordinates that require special handling
407  virtual bool HaveTCoords(vtkPolyData *poly);
408 
409  // values we use to determine if we need to rebuild shaders
410  std::map<const vtkOpenGLHelper *, int> LastLightComplexity;
411  std::map<const vtkOpenGLHelper *, int> LastLightCount;
412  std::map<const vtkOpenGLHelper *, vtkTimeStamp> LightComplexityChanged;
413 
414  int LastSelectionState;
415  vtkTimeStamp SelectionStateChanged;
416 
417  // Caches the vtkOpenGLRenderPass::RenderPasses() information.
418  // Note: Do not dereference the pointers held by this object. There is no
419  // guarantee that they are still valid!
420  vtkNew<vtkInformation> LastRenderPassInfo;
421 
422  // Check the renderpasses in actor's property keys to see if they've changed
423  // render stages:
424  vtkMTimeType GetRenderPassStageMTime(vtkActor *actor);
425 
426  bool UsingScalarColoring;
427  vtkTimeStamp VBOBuildTime; // When was the OpenGL VBO updated?
428  std::string VBOBuildString; // used for determining whento rebuild the VBO
429  std::string IBOBuildString; // used for determining whento rebuild the IBOs
430  std::string CellTextureBuildString;
431  vtkOpenGLTexture* InternalColorTexture;
432 
433  int PopulateSelectionSettings;
434  int PrimitiveIDOffset;
435 
436  vtkMatrix4x4* TempMatrix4;
437  vtkMatrix3x3* TempMatrix3;
438  vtkNew<vtkTransform> VBOInverseTransform;
439  vtkNew<vtkMatrix4x4> VBOShiftScale;
440  int ShiftScaleMethod; // for points
441 
442  // if set to true, tcoords will be passed to the
443  // VBO even if the mapper knows of no texture maps
444  // normally tcoords are only added to the VBO if the
445  // mapper has indentified a texture map as well.
446  bool ForceTextureCoordinates;
447 
448  void BuildCellTextures(
449  vtkRenderer *ren,
450  vtkActor *,
451  vtkCellArray *prims[4],
452  int representation);
453 
454  void AppendCellTextures(
455  vtkRenderer *ren,
456  vtkActor *,
457  vtkCellArray *prims[4],
458  int representation,
459  std::vector<unsigned char> &colors,
460  std::vector<float> &normals,
461  vtkPolyData *pd);
462 
463  bool HavePickScalars;
464  vtkTextureObject *CellScalarTexture;
465  vtkOpenGLBufferObject *CellScalarBuffer;
466  bool HaveCellScalars;
467  vtkTextureObject *CellNormalTexture;
468  vtkOpenGLBufferObject *CellNormalBuffer;
469  bool HaveCellNormals;
470 
471  // aditional picking indirection
472  char* PointIdArrayName;
473  char* CellIdArrayName;
474  char* ProcessIdArrayName;
475  char* CompositeIdArrayName;
476 
478  {
479  public:
483  bool operator<(const ReplacementSpec &v1) const
484  {
485  if (this->OriginalValue != v1.OriginalValue) { return this->OriginalValue < v1.OriginalValue; }
486  if (this->ShaderType != v1.ShaderType) { return this->ShaderType < v1.ShaderType; }
487  return (this->ReplaceFirst < v1.ReplaceFirst);
488  }
489  bool operator>(const ReplacementSpec &v1) const
490  {
491  if (this->OriginalValue != v1.OriginalValue) { return this->OriginalValue > v1.OriginalValue; }
492  if (this->ShaderType != v1.ShaderType) { return this->ShaderType > v1.ShaderType; }
493  return (this->ReplaceFirst > v1.ReplaceFirst);
494  }
495  };
497  {
498  public:
501  };
502 
503  std::map<const ReplacementSpec,ReplacementValue> UserShaderReplacements;
504 
509 
510  // are we currently drawing spheres/tubes
511  bool DrawingSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
512  bool DrawingTubes(vtkOpenGLHelper &cellBO, vtkActor *actor);
513  bool DrawingTubesOrSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
514 
515  // get why opengl mode to use to draw the primitive
516  int GetOpenGLMode(int representation, int primType);
517 
518  // get how big to make the points when doing point picking
519  // typically 2 for points, 4 for lines, 6 for surface
520  int GetPointPickingPrimitiveSize(int primType);
521 
522  // a map from drawn triangles back to containing cell id
523  std::vector<unsigned int> CellCellMap;
524 
525 private:
526  vtkOpenGLPolyDataMapper(const vtkOpenGLPolyDataMapper&) VTK_DELETE_FUNCTION;
527  void operator=(const vtkOpenGLPolyDataMapper&) VTK_DELETE_FUNCTION;
528 };
529 
530 #endif
void ForceHaveAppleBugOn()
Override the normal test for the apple bug.
a PolyDataMapper for the OpenGL library
represents an object (geometry & properties) in a rendered scene
Definition: vtkActor.h:45
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:35
std::vector< unsigned int > CellCellMap
Store vtkAlgorithm input/output information.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:300
Type
Available shader types.
Definition: vtkShader.h:46
record modification and/or execution time
Definition: vtkTimeStamp.h:32
abstract specification for renderers
Definition: vtkRenderer.h:57
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:54
std::map< const ReplacementSpec, ReplacementValue > UserShaderReplacements
virtual bool GetIsOpaque()
Returns if the mapper does not expect to have translucent geometry.
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:79
virtual void RenderPiece(vtkRenderer *ren, vtkActor *act)=0
Implemented by sub classes.
bool GetSupportsSelection() override
WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE DO NOT USE THIS METHOD OUTSIDE OF THE RENDERI...
OpenGL texture map.
window superclass for vtkRenderWindow
Definition: vtkWindow.h:34
bool operator<(const ReplacementSpec &v1) const
a simple class to control print indentation
Definition: vtkIndent.h:33
handles properties associated with a texture map
Definition: vtkTexture.h:64
vtkGetStringMacro(ExtensionsString)
Returns a string listing all available extensions.
virtual void Modified()
Update the modification time for this object.
vtkOpenGLRenderTimer * TimerQuery
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
bool operator>(const ReplacementSpec &v1) const
abstract class specifies interface to map data
abstracts an OpenGL texture object.
map vtkPolyData to graphics primitives
object to represent cell connectivity
Definition: vtkCellArray.h:44
Vertex or Fragment shader, combined into a ShaderProgram.
Definition: vtkShader.h:37
Allocate and hold a VTK object.
Definition: vtkNew.h:61
bool GetHaveAppleBug()
Get the value of HaveAppleBug.
static vtkPolyDataMapper * New()
OpenGL buffer object.
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:33
manage vertex buffer objects shared within a mapper
Asynchronously measures GPU execution time for a single event.
represent and manipulate 3D points
Definition: vtkPoints.h:33
void ReleaseGraphicsResources(vtkWindow *) override
Release any graphics resources that are being consumed by this mapper.
Definition: vtkMapper.h:113