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 =========================================================================*/
24 #ifndef vtkOpenGLPolyDataMapper_h
25 #define vtkOpenGLPolyDataMapper_h
26 
27 #include "vtkNew.h" // For vtkNew
28 #include "vtkRenderingOpenGL2Module.h" // For export macro
29 #include "vtkNew.h" // for ivars
30 #include "vtkPolyDataMapper.h"
31 #include "vtkShader.h" // for methods
32 #include "vtkOpenGLHelper.h" // used for ivars
33 
34 #include <vector> //for ivars
35 #include <map> //for methods
36 
37 class vtkCellArray;
39 class vtkMatrix4x4;
40 class vtkMatrix3x3;
42 class vtkOpenGLTexture;
46 class vtkPoints;
47 class vtkTextureObject;
48 class vtkTransform;
49 
50 
51 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLPolyDataMapper : public vtkPolyDataMapper
52 {
53 public:
54  static vtkOpenGLPolyDataMapper* New();
56  void PrintSelf(ostream& os, vtkIndent indent) override;
57 
61  void RenderPiece(vtkRenderer *ren, vtkActor *act) override;
62 
64 
67  virtual void RenderPieceStart(vtkRenderer *ren, vtkActor *act);
68  virtual void RenderPieceDraw(vtkRenderer *ren, vtkActor *act);
69  virtual void RenderPieceFinish(vtkRenderer *ren, vtkActor *act);
71 
77  void ReleaseGraphicsResources(vtkWindow *) override;
78 
79  vtkGetMacro(PopulateSelectionSettings,int);
80  void SetPopulateSelectionSettings(int v) { this->PopulateSelectionSettings = v; };
81 
88  bool GetSupportsSelection() override { return true; }
89 
100  bool GetIsOpaque() override;
101 
102  // used by RenderPiece and functions it calls to reduce
103  // calls to get the input and allow for rendering of
104  // other polydata (not the input)
106 
108 
114  vtkSetStringMacro(PointIdArrayName);
115  vtkGetStringMacro(PointIdArrayName);
116  vtkSetStringMacro(CellIdArrayName);
117  vtkGetStringMacro(CellIdArrayName);
119 
121 
126  vtkSetStringMacro(ProcessIdArrayName);
127  vtkGetStringMacro(ProcessIdArrayName);
129 
131 
140  vtkSetStringMacro(CompositeIdArrayName);
141  vtkGetStringMacro(CompositeIdArrayName);
143 
144 
146 
153  void AddShaderReplacement(
154  vtkShader::Type shaderType, // vertex, fragment, etc
155  const std::string& originalValue,
156  bool replaceFirst, // do this replacement before the default
157  const std::string& replacementValue,
158  bool replaceAll);
159  void ClearShaderReplacement(
160  vtkShader::Type shaderType, // vertex, fragment, etc
161  const std::string& originalValue,
162  bool replaceFirst);
164 
166 
172  vtkSetStringMacro(VertexShaderCode);
173  vtkGetStringMacro(VertexShaderCode);
174  vtkSetStringMacro(FragmentShaderCode);
175  vtkGetStringMacro(FragmentShaderCode);
176  vtkSetStringMacro(GeometryShaderCode);
177  vtkGetStringMacro(GeometryShaderCode);
179 
180  // the following is all extra stuff to work around the
181  // fact that gl_PrimitiveID does not work correctly on
182  // Apple devices with AMD graphics hardware. See apple
183  // bug ID 20747550
184  static vtkPolyData *HandleAppleBug(
185  vtkPolyData *poly,
186  std::vector<float> &buffData);
187 
192 
194 
198  {
199  this->HaveAppleBugForce = 1;
200  this->Modified();
201  }
203  {
204  this->HaveAppleBugForce = 2;
205  this->Modified();
206  }
208 
212  bool GetHaveAppleBug() { return this->HaveAppleBug; }
213 
215  vtkGetObjectMacro(VBOs, vtkOpenGLVertexBufferObjectGroup);
216 
220  void SetVBOShiftScaleMethod(int m);
221 
223  PrimitiveStart = 0,
224  PrimitivePoints = 0,
231  PrimitiveEnd
232  };
233 
237  static void MakeCellCellMap(std::vector<vtkIdType> &CellCellMap,
238  bool HaveAppleBug,
239  vtkPolyData *poly,
240  vtkCellArray **prims, int representation,
241  vtkPoints *points);
242 
255  const char* vertexAttributeName,
256  const char* dataArrayName,
257  int fieldAssociation,
258  int componentno = -1) override;
259 
263  void RemoveVertexAttributeMapping(const char* vertexAttributeName) override;
264 
268  void RemoveAllVertexAttributeMappings() override;
269 
270 protected:
272  ~vtkOpenGLPolyDataMapper() override;
273 
275 
276  // the following is all extra stuff to work around the
277  // fact that gl_PrimitiveID does not work correctly on
278  // Apple devices with AMD graphics hardware. See apple
279  // bug ID 20747550
281  int HaveAppleBugForce; // 0 = default 1 = 0ff 2 = on
282  std::vector<float> AppleBugPrimIDs;
284 
288  void GetCoincidentParameters(
289  vtkRenderer *ren, vtkActor *actor, float &factor, float &offset);
290 
296  void ComputeBounds() override;
297 
302  virtual void UpdateShaders(
303  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
304 
308  virtual bool GetNeedToRebuildShaders(
309  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
310 
314  virtual void BuildShaders(
315  std::map<vtkShader::Type, vtkShader *> shaders,
316  vtkRenderer *ren, vtkActor *act);
317 
321  virtual void GetShaderTemplate(
322  std::map<vtkShader::Type, vtkShader *> shaders,
323  vtkRenderer *ren, vtkActor *act);
324 
328  virtual void ReplaceShaderValues(
329  std::map<vtkShader::Type, vtkShader *> shaders,
330  vtkRenderer *ren, vtkActor *act);
331 
333 
337  virtual void ReplaceShaderRenderPass(
338  std::map<vtkShader::Type, vtkShader *> shaders,
339  vtkRenderer *ren, vtkActor *act, bool prePass);
340  virtual void ReplaceShaderColor(
341  std::map<vtkShader::Type, vtkShader *> shaders,
342  vtkRenderer *ren, vtkActor *act);
343  virtual void ReplaceShaderLight(
344  std::map<vtkShader::Type, vtkShader *> shaders,
345  vtkRenderer *ren, vtkActor *act);
346  virtual void ReplaceShaderTCoord(
347  std::map<vtkShader::Type, vtkShader *> shaders,
348  vtkRenderer *ren, vtkActor *act);
349  virtual void ReplaceShaderPicking(
350  std::map<vtkShader::Type, vtkShader *> shaders,
351  vtkRenderer *ren, vtkActor *act);
352  virtual void ReplaceShaderPrimID(
353  std::map<vtkShader::Type, vtkShader *> shaders,
354  vtkRenderer *ren, vtkActor *act);
355  virtual void ReplaceShaderNormal(
356  std::map<vtkShader::Type, vtkShader *> shaders,
357  vtkRenderer *ren, vtkActor *act);
358  virtual void ReplaceShaderClip(
359  std::map<vtkShader::Type, vtkShader *> shaders,
360  vtkRenderer *ren, vtkActor *act);
361  virtual void ReplaceShaderPositionVC(
362  std::map<vtkShader::Type, vtkShader *> shaders,
363  vtkRenderer *ren, vtkActor *act);
364  virtual void ReplaceShaderCoincidentOffset(
365  std::map<vtkShader::Type, vtkShader *> shaders,
366  vtkRenderer *ren, vtkActor *act);
367  virtual void ReplaceShaderDepth(
368  std::map<vtkShader::Type, vtkShader *> shaders,
369  vtkRenderer *ren, vtkActor *act);
371 
375  virtual void SetMapperShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
376 
380  virtual void SetLightingShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
381 
385  virtual void SetCameraShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
386 
390  virtual void SetPropertyShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
391 
395  virtual void UpdateBufferObjects(vtkRenderer *ren, vtkActor *act);
396 
400  virtual bool GetNeedToRebuildBufferObjects(vtkRenderer *ren, vtkActor *act);
401 
405  virtual void BuildBufferObjects(vtkRenderer *ren, vtkActor *act);
406 
410  virtual void BuildIBO(vtkRenderer *ren, vtkActor *act, vtkPolyData *poly);
411 
412  // The VBO and its layout.
414 
415  // Structures for the various cell types we render.
416  vtkOpenGLHelper Primitives[PrimitiveEnd];
419 
420  // do we have wide lines that require special handling
421  virtual bool HaveWideLines(vtkRenderer *, vtkActor *);
422 
423  // do we have textures that require special handling
424  virtual bool HaveTextures(vtkActor *actor);
425 
426  // how many textures do we have
427  virtual unsigned int GetNumberOfTextures(vtkActor *actor);
428 
429  // populate a vector with the textures we have
430  // the order is always
431  // ColorInternalTexture
432  // Actors texture
433  // Properies textures
434  virtual std::vector<vtkTexture *> GetTextures(vtkActor *actor);
435 
436  // do we have textures coordinates that require special handling
437  virtual bool HaveTCoords(vtkPolyData *poly);
438 
439  // values we use to determine if we need to rebuild shaders
440  std::map<const vtkOpenGLHelper *, int> LastLightComplexity;
441  std::map<const vtkOpenGLHelper *, int> LastLightCount;
442  std::map<const vtkOpenGLHelper *, vtkTimeStamp> LightComplexityChanged;
443 
446 
447  // Caches the vtkOpenGLRenderPass::RenderPasses() information.
448  // Note: Do not dereference the pointers held by this object. There is no
449  // guarantee that they are still valid!
451 
452  // Check the renderpasses in actor's property keys to see if they've changed
453  // render stages:
454  vtkMTimeType GetRenderPassStageMTime(vtkActor *actor);
455 
457  vtkTimeStamp VBOBuildTime; // When was the OpenGL VBO updated?
458  std::string VBOBuildString; // used for determining whento rebuild the VBO
459  std::string IBOBuildString; // used for determining whento rebuild the IBOs
461  vtkOpenGLTexture* InternalColorTexture;
462 
465 
470  int ShiftScaleMethod; // for points
471 
472  // if set to true, tcoords will be passed to the
473  // VBO even if the mapper knows of no texture maps
474  // normally tcoords are only added to the VBO if the
475  // mapper has indentified a texture map as well.
477 
478  void BuildCellTextures(
479  vtkRenderer *ren,
480  vtkActor *,
481  vtkCellArray *prims[4],
482  int representation);
483 
484  void AppendCellTextures(
485  vtkRenderer *ren,
486  vtkActor *,
487  vtkCellArray *prims[4],
488  int representation,
489  std::vector<unsigned char> &colors,
490  std::vector<float> &normals,
491  vtkPolyData *pd);
492 
500 
501  // aditional picking indirection
506 
507  std::map<const vtkShader::ReplacementSpec, vtkShader::ReplacementValue>
509 
511  {
512  public:
516  };
517  std::map<std::string,ExtraAttributeValue> ExtraAttributes;
518 
523 
524  // are we currently drawing spheres/tubes
525  bool DrawingSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
526  bool DrawingTubes(vtkOpenGLHelper &cellBO, vtkActor *actor);
527  bool DrawingTubesOrSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
528 
529  // get which opengl mode to use to draw the primitive
530  int GetOpenGLMode(int representation, int primType);
531 
532  // get how big to make the points when doing point picking
533  // typically 2 for points, 4 for lines, 6 for surface
534  int GetPointPickingPrimitiveSize(int primType);
535 
536  // a map from drawn triangles back to containing cell id
537  std::vector<unsigned int> CellCellMap;
538 
539 private:
541  void operator=(const vtkOpenGLPolyDataMapper&) = delete;
542 };
543 
544 #endif
void ForceHaveAppleBugOn()
Override the normal test for the apple bug.
std::map< std::string, ExtraAttributeValue > ExtraAttributes
a PolyDataMapper for the OpenGL library
represents an object (geometry & properties) in a rendered scene
Definition: vtkActor.h:51
vtkOpenGLBufferObject * CellScalarBuffer
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:41
virtual void RemoveAllVertexAttributeMappings()
Remove all vertex attributes.
std::vector< unsigned int > CellCellMap
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:300
Type
Available shader types.
Definition: vtkShader.h:48
record modification and/or execution time
Definition: vtkTimeStamp.h:35
vtkTextureObject * CellScalarTexture
vtkOpenGLBufferObject * CellNormalBuffer
vtkTextureObject * CellNormalTexture
abstract specification for renderers
Definition: vtkRenderer.h:63
std::map< const vtkShader::ReplacementSpec, vtkShader::ReplacementValue > UserShaderReplacements
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:60
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:85
virtual void RenderPiece(vtkRenderer *ren, vtkActor *act)=0
Implemented by sub classes.
virtual void RemoveVertexAttributeMapping(const char *vertexAttributeName)
Remove a vertex attribute mapping.
bool GetSupportsSelection() override
WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE DO NOT USE THIS METHOD OUTSIDE OF THE RENDERI...
std::vector< float > AppleBugPrimIDs
OpenGL texture map.
window superclass for vtkRenderWindow
Definition: vtkWindow.h:37
vtkOpenGLVertexBufferObjectGroup * VBOs
a simple class to control print indentation
Definition: vtkIndent.h:39
std::map< const vtkOpenGLHelper *, vtkTimeStamp > LightComplexityChanged
vtkNew< vtkMatrix4x4 > VBOShiftScale
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.
virtual void ComputeBounds()
Called in GetBounds().
abstract class specifies interface to map data
void ShallowCopy(vtkAbstractMapper *m)
Make a shallow copy of this mapper.
std::map< const vtkOpenGLHelper *, int > LastLightComplexity
abstracts an OpenGL texture object.
virtual void MapDataArrayToVertexAttribute(const char *vertexAttributeName, const char *dataArrayName, int fieldAssociation, int componentno=-1)
Select a data array from the point/cell data and map it to a generic vertex attribute.
map vtkPolyData to graphics primitives
object to represent cell connectivity
Definition: vtkCellArray.h:50
vtkGenericOpenGLResourceFreeCallback * ResourceCallback
vtkNew< vtkTransform > VBOInverseTransform
bool GetHaveAppleBug()
Get the value of HaveAppleBug.
static vtkPolyDataMapper * New()
OpenGL buffer object.
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:36
manage vertex buffer objects shared within a mapper
vtkNew< vtkInformation > LastRenderPassInfo
Asynchronously measures GPU execution time for a single event.
represent and manipulate 3D points
Definition: vtkPoints.h:39
std::map< const vtkOpenGLHelper *, int > LastLightCount
void ForceHaveAppleBugOff()
Override the normal test for the apple bug.
void ReleaseGraphicsResources(vtkWindow *) override
Release any graphics resources that are being consumed by this mapper.
Definition: vtkMapper.h:111
vtkOpenGLBufferObject * AppleBugPrimIDBuffer