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 #include "vtkStateStorage.h" // used for ivars
34 
35 #include <vector> //for ivars
36 #include <map> //for methods
37 
38 class vtkCellArray;
40 class vtkMatrix4x4;
41 class vtkMatrix3x3;
43 class vtkOpenGLTexture;
47 class vtkPoints;
48 class vtkTexture;
49 class vtkTextureObject;
50 class vtkTransform;
51 
52 
53 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLPolyDataMapper : public vtkPolyDataMapper
54 {
55 public:
56  static vtkOpenGLPolyDataMapper* New();
58  void PrintSelf(ostream& os, vtkIndent indent) override;
59 
63  void RenderPiece(vtkRenderer *ren, vtkActor *act) override;
64 
66 
69  virtual void RenderPieceStart(vtkRenderer *ren, vtkActor *act);
70  virtual void RenderPieceDraw(vtkRenderer *ren, vtkActor *act);
71  virtual void RenderPieceFinish(vtkRenderer *ren, vtkActor *act);
73 
79  void ReleaseGraphicsResources(vtkWindow *) override;
80 
81  vtkGetMacro(PopulateSelectionSettings,int);
82  void SetPopulateSelectionSettings(int v) { this->PopulateSelectionSettings = v; };
83 
90  bool GetSupportsSelection() override { return true; }
91 
102  bool GetIsOpaque() override;
103 
104  // used by RenderPiece and functions it calls to reduce
105  // calls to get the input and allow for rendering of
106  // other polydata (not the input)
108 
110 
116  vtkSetStringMacro(PointIdArrayName);
117  vtkGetStringMacro(PointIdArrayName);
118  vtkSetStringMacro(CellIdArrayName);
119  vtkGetStringMacro(CellIdArrayName);
121 
123 
128  vtkSetStringMacro(ProcessIdArrayName);
129  vtkGetStringMacro(ProcessIdArrayName);
131 
133 
142  vtkSetStringMacro(CompositeIdArrayName);
143  vtkGetStringMacro(CompositeIdArrayName);
145 
146 
148 
155  void AddShaderReplacement(
156  vtkShader::Type shaderType, // vertex, fragment, etc
157  const std::string& originalValue,
158  bool replaceFirst, // do this replacement before the default
159  const std::string& replacementValue,
160  bool replaceAll);
161  void ClearShaderReplacement(
162  vtkShader::Type shaderType, // vertex, fragment, etc
163  const std::string& originalValue,
164  bool replaceFirst);
165  void ClearAllShaderReplacements(vtkShader::Type shaderType);
166  void ClearAllShaderReplacements();
168 
170 
176  vtkSetStringMacro(VertexShaderCode);
177  vtkGetStringMacro(VertexShaderCode);
178  vtkSetStringMacro(FragmentShaderCode);
179  vtkGetStringMacro(FragmentShaderCode);
180  vtkSetStringMacro(GeometryShaderCode);
181  vtkGetStringMacro(GeometryShaderCode);
183 
184  // the following is all extra stuff to work around the
185  // fact that gl_PrimitiveID does not work correctly on
186  // Apple Macs with AMD graphics hardware (before macOS 10.11).
187  // See <rdar://20747550>.
188  static vtkPolyData *HandleAppleBug(
189  vtkPolyData *poly,
190  std::vector<float> &buffData);
191 
195  void ShallowCopy(vtkAbstractMapper *m) override;
196 
198 
202  {
203  this->HaveAppleBugForce = 1;
204  this->Modified();
205  }
207  {
208  this->HaveAppleBugForce = 2;
209  this->Modified();
210  }
212 
216  bool GetHaveAppleBug() { return this->HaveAppleBug; }
217 
219  vtkGetObjectMacro(VBOs, vtkOpenGLVertexBufferObjectGroup);
220 
224  void SetVBOShiftScaleMethod(int m);
225 
227  PrimitiveStart = 0,
228  PrimitivePoints = 0,
235  PrimitiveEnd
236  };
237 
238  void UpdateCellMaps(
239  bool HaveAppleBug,
240  vtkPolyData *poly,
241  vtkCellArray **prims, int representation,
242  vtkPoints *points);
243 
247  static void MakeCellCellMap(
248  std::vector<vtkIdType> &cellCellMap,
249  bool HaveAppleBug,
250  vtkPolyData *poly,
251  vtkCellArray **prims, int representation,
252  vtkPoints *points);
253 
266  const char* vertexAttributeName,
267  const char* dataArrayName,
268  int fieldAssociation,
269  int componentno = -1) override;
270 
271  // This method will Map the specified data array for use as
272  // a texture coordinate for texture tname. The actual
273  // attribute will be named tname_coord so as to not
274  // conflict with the texture sampler definition which will
275  // be tname.
277  const char *tname,
278  const char* dataArrayName, int fieldAssociation, int componentno = -1) override;
279 
283  void RemoveVertexAttributeMapping(const char* vertexAttributeName) override;
284 
288  void RemoveAllVertexAttributeMappings() override;
289 
295  std::vector<unsigned int> &pixeloffsets,
296  vtkProp *prop) override;
297 
298 protected:
300  ~vtkOpenGLPolyDataMapper() override;
301 
303 
304  void MapDataArray(
305  const char* vertexAttributeName,
306  const char* dataArrayName,
307  const char *texturename,
308  int fieldAssociation,
309  int componentno);
310 
311  // what coordinate should be used for this texture
312  std::string GetTextureCoordinateName(const char *tname);
313 
314  // the following is all extra stuff to work around the
315  // fact that gl_PrimitiveID does not work correctly on
316  // Apple Macs with AMD graphics hardware (before macOS 10.11).
317  // See <rdar://20747550>.
319  int HaveAppleBugForce; // 0 = default 1 = 0ff 2 = on
320  std::vector<float> AppleBugPrimIDs;
322 
326  void GetCoincidentParameters(
327  vtkRenderer *ren, vtkActor *actor, float &factor, float &offset);
328 
334  void ComputeBounds() override;
335 
340  virtual void UpdateShaders(
341  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
342 
346  virtual bool GetNeedToRebuildShaders(
347  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
348 
352  virtual void BuildShaders(
353  std::map<vtkShader::Type, vtkShader *> shaders,
354  vtkRenderer *ren, vtkActor *act);
355 
359  virtual void GetShaderTemplate(
360  std::map<vtkShader::Type, vtkShader *> shaders,
361  vtkRenderer *ren, vtkActor *act);
362 
366  virtual void ReplaceShaderValues(
367  std::map<vtkShader::Type, vtkShader *> shaders,
368  vtkRenderer *ren, vtkActor *act);
369 
371 
375  virtual void ReplaceShaderRenderPass(
376  std::map<vtkShader::Type, vtkShader *> shaders,
377  vtkRenderer *ren, vtkActor *act, bool prePass);
378  virtual void ReplaceShaderColor(
379  std::map<vtkShader::Type, vtkShader *> shaders,
380  vtkRenderer *ren, vtkActor *act);
381  virtual void ReplaceShaderLight(
382  std::map<vtkShader::Type, vtkShader *> shaders,
383  vtkRenderer *ren, vtkActor *act);
384  virtual void ReplaceShaderTCoord(
385  std::map<vtkShader::Type, vtkShader *> shaders,
386  vtkRenderer *ren, vtkActor *act);
387  virtual void ReplaceShaderPicking(
388  std::map<vtkShader::Type, vtkShader *> shaders,
389  vtkRenderer *ren, vtkActor *act);
390  virtual void ReplaceShaderPrimID(
391  std::map<vtkShader::Type, vtkShader *> shaders,
392  vtkRenderer *ren, vtkActor *act);
393  virtual void ReplaceShaderNormal(
394  std::map<vtkShader::Type, vtkShader *> shaders,
395  vtkRenderer *ren, vtkActor *act);
396  virtual void ReplaceShaderClip(
397  std::map<vtkShader::Type, vtkShader *> shaders,
398  vtkRenderer *ren, vtkActor *act);
399  virtual void ReplaceShaderPositionVC(
400  std::map<vtkShader::Type, vtkShader *> shaders,
401  vtkRenderer *ren, vtkActor *act);
402  virtual void ReplaceShaderCoincidentOffset(
403  std::map<vtkShader::Type, vtkShader *> shaders,
404  vtkRenderer *ren, vtkActor *act);
405  virtual void ReplaceShaderDepth(
406  std::map<vtkShader::Type, vtkShader *> shaders,
407  vtkRenderer *ren, vtkActor *act);
409 
413  virtual void SetMapperShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
414 
418  virtual void SetLightingShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
419 
423  virtual void SetCameraShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
424 
428  virtual void SetPropertyShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
429 
433  virtual void UpdateBufferObjects(vtkRenderer *ren, vtkActor *act);
434 
438  virtual bool GetNeedToRebuildBufferObjects(vtkRenderer *ren, vtkActor *act);
439 
443  virtual void BuildBufferObjects(vtkRenderer *ren, vtkActor *act);
444 
448  virtual void BuildIBO(vtkRenderer *ren, vtkActor *act, vtkPolyData *poly);
449 
450  // The VBO and its layout.
452 
453  // Structures for the various cell types we render.
454  vtkOpenGLHelper Primitives[PrimitiveEnd];
457 
458  // do we have wide lines that require special handling
459  virtual bool HaveWideLines(vtkRenderer *, vtkActor *);
460 
461  // do we have textures that require special handling
462  virtual bool HaveTextures(vtkActor *actor);
463 
464  // how many textures do we have
465  virtual unsigned int GetNumberOfTextures(vtkActor *actor);
466 
467  // populate a vector with the textures we have
468  // the order is always
469  // ColorInternalTexture
470  // Actors texture
471  // Properties textures
472  virtual std::vector<std::pair<vtkTexture *, std::string> > GetTextures(vtkActor *actor);
473 
474  // do we have textures coordinates that require special handling
475  virtual bool HaveTCoords(vtkPolyData *poly);
476 
477  // values we use to determine if we need to rebuild shaders
478  std::map<const vtkOpenGLHelper *, int> LastLightComplexity;
479  std::map<const vtkOpenGLHelper *, int> LastLightCount;
480  std::map<const vtkOpenGLHelper *, vtkTimeStamp> LightComplexityChanged;
481 
484 
485  // Caches the vtkOpenGLRenderPass::RenderPasses() information.
486  // Note: Do not dereference the pointers held by this object. There is no
487  // guarantee that they are still valid!
489 
490  // Check the renderpasses in actor's property keys to see if they've changed
491  // render stages:
492  vtkMTimeType GetRenderPassStageMTime(vtkActor *actor);
493 
495  vtkTimeStamp VBOBuildTime; // When was the OpenGL VBO updated?
496  vtkStateStorage VBOBuildState; // used for determining when to rebuild the VBO
497  vtkStateStorage IBOBuildState; // used for determining whento rebuild the IBOs
499  vtkStateStorage TempState; // can be used to avoid constant allocs/deallocs
501 
504 
509  int ShiftScaleMethod; // for points
510 
511  // if set to true, tcoords will be passed to the
512  // VBO even if the mapper knows of no texture maps
513  // normally tcoords are only added to the VBO if the
514  // mapper has identified a texture map as well.
516 
517  virtual void BuildCellTextures(
518  vtkRenderer *ren,
519  vtkActor *,
520  vtkCellArray *prims[4],
521  int representation);
522 
523  void AppendCellTextures(
524  vtkRenderer *ren,
525  vtkActor *,
526  vtkCellArray *prims[4],
527  int representation,
528  std::vector<unsigned char> &colors,
529  std::vector<float> &normals,
530  vtkPolyData *pd);
531 
538 
539  // additional picking indirection
544 
545  std::map<const vtkShader::ReplacementSpec, vtkShader::ReplacementValue>
547 
549  {
550  public:
555  };
556  std::map<std::string,ExtraAttributeValue> ExtraAttributes;
557 
562 
563  // are we currently drawing spheres/tubes
564  bool DrawingSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
565  bool DrawingTubes(vtkOpenGLHelper &cellBO, vtkActor *actor);
566  bool DrawingTubesOrSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
567 
568  // get which opengl mode to use to draw the primitive
569  int GetOpenGLMode(int representation, int primType);
570 
571  // get how big to make the points when doing point picking
572  // typically 2 for points, 4 for lines, 6 for surface
573  int GetPointPickingPrimitiveSize(int primType);
574 
575  // used to occasionally invoke timers
576  unsigned int TimerQueryCounter;
577 
578  // stores the mapping from vtk cells to gl_PrimitiveId
579  std::vector<vtkIdType> CellCellMap;
580  std::vector<vtkIdType> PointCellMap;
582 
583  // compute and set the maximum point and cell ID used in selection
584  virtual void UpdateMaximumPointCellIds(vtkRenderer* ren, vtkActor *actor);
585 
586 private:
588  void operator=(const vtkOpenGLPolyDataMapper&) = delete;
589 };
590 
591 #endif
abstract superclass for all actors, volumes and annotations
Definition: vtkProp.h:52
void ForceHaveAppleBugOn()
Override the normal test for the apple bug.
std::map< std::string, ExtraAttributeValue > ExtraAttributes
PolyDataMapper using OpenGL to render.
void ShallowCopy(vtkAbstractMapper *m) override
Make a shallow copy of this mapper.
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 MapDataArrayToMultiTextureAttribute(const char *textureName, const char *dataArrayName, int fieldAssociation, int componentno=-1)
virtual void RemoveAllVertexAttributeMappings()
Remove all vertex attributes.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:302
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 void ProcessSelectorPixelBuffers(vtkHardwareSelector *, std::vector< unsigned int > &, vtkProp *)
allows a mapper to update a selections color buffers Called from a prop which in turn is called from ...
Definition: vtkMapper.h:496
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 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
handles properties associated with a texture map
Definition: vtkTexture.h:71
virtual void Modified()
Update the modification time for this object.
vtkOpenGLRenderTimer * TimerQuery
virtual void ComputeBounds()
Called in GetBounds().
abstract class specifies interface to map data
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()
std::vector< vtkIdType > PointCellMap
OpenGL buffer object.
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:36
manage vertex buffer objects shared within a mapper
vtkOpenGLTexture * InternalColorTexture
vtkNew< vtkInformation > LastRenderPassInfo
Asynchronously measures GPU execution time for a single event.
Class to make storing and comparing state quick and easy.
std::vector< vtkIdType > CellCellMap
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.
vtkOpenGLBufferObject * AppleBugPrimIDBuffer