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 vtkTexture;
45 class vtkTextureObject;
46 class vtkTransform;
47 
48 
49 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLPolyDataMapper : public vtkPolyDataMapper
50 {
51 public:
52  static vtkOpenGLPolyDataMapper* New();
54  void PrintSelf(ostream& os, vtkIndent indent) override;
55 
59  void RenderPiece(vtkRenderer *ren, vtkActor *act) override;
60 
62 
65  virtual void RenderPieceStart(vtkRenderer *ren, vtkActor *act);
66  virtual void RenderPieceDraw(vtkRenderer *ren, vtkActor *act);
67  virtual void RenderPieceFinish(vtkRenderer *ren, vtkActor *act);
69 
75  void ReleaseGraphicsResources(vtkWindow *) override;
76 
77  vtkGetMacro(PopulateSelectionSettings,int);
78  void SetPopulateSelectionSettings(int v) { this->PopulateSelectionSettings = v; };
79 
86  bool GetSupportsSelection() override { return true; }
87 
98  bool GetIsOpaque() override;
99 
100  // used by RenderPiece and functions it calls to reduce
101  // calls to get the input and allow for rendering of
102  // other polydata (not the input)
104 
106 
112  vtkSetStringMacro(PointIdArrayName);
113  vtkGetStringMacro(PointIdArrayName);
114  vtkSetStringMacro(CellIdArrayName);
115  vtkGetStringMacro(CellIdArrayName);
117 
119 
124  vtkSetStringMacro(ProcessIdArrayName);
125  vtkGetStringMacro(ProcessIdArrayName);
127 
129 
138  vtkSetStringMacro(CompositeIdArrayName);
139  vtkGetStringMacro(CompositeIdArrayName);
141 
142 
144 
151  void AddShaderReplacement(
152  vtkShader::Type shaderType, // vertex, fragment, etc
153  const std::string& originalValue,
154  bool replaceFirst, // do this replacement before the default
155  const std::string& replacementValue,
156  bool replaceAll);
157  void ClearShaderReplacement(
158  vtkShader::Type shaderType, // vertex, fragment, etc
159  const std::string& originalValue,
160  bool replaceFirst);
161  void ClearAllShaderReplacements(vtkShader::Type shaderType);
162  void ClearAllShaderReplacements();
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 
260  // This method will Map the specified data array for use as
261  // a texture coordinate for texture tname. The actual
262  // attribute will be named tname_coord so as to not
263  // conflict with the texture sampler definition which will
264  // be tname.
266  const char *tname,
267  const char* dataArrayName, int fieldAssociation, int componentno = -1) override;
268 
269  // deprecated in favor of the const char * signature
270  VTK_LEGACY(void MapDataArrayToMultiTextureAttribute(
271  int unit,
272  const char* dataArrayName, int fieldAssociation, int componentno = -1) override);
273 
277  void RemoveVertexAttributeMapping(const char* vertexAttributeName) override;
278 
282  void RemoveAllVertexAttributeMappings() override;
283 
284 protected:
286  ~vtkOpenGLPolyDataMapper() override;
287 
289 
290  void MapDataArray(
291  const char* vertexAttributeName,
292  const char* dataArrayName,
293  const char *texturename,
294  int fieldAssociation,
295  int componentno);
296 
297  // what coordinate shoudl be used for this texture
298  std::string GetTextureCoordinateName(const char *tname);
299 
300  // the following is all extra stuff to work around the
301  // fact that gl_PrimitiveID does not work correctly on
302  // Apple devices with AMD graphics hardware. See apple
303  // bug ID 20747550
305  int HaveAppleBugForce; // 0 = default 1 = 0ff 2 = on
306  std::vector<float> AppleBugPrimIDs;
308 
312  void GetCoincidentParameters(
313  vtkRenderer *ren, vtkActor *actor, float &factor, float &offset);
314 
320  void ComputeBounds() override;
321 
326  virtual void UpdateShaders(
327  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
328 
332  virtual bool GetNeedToRebuildShaders(
333  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
334 
338  virtual void BuildShaders(
339  std::map<vtkShader::Type, vtkShader *> shaders,
340  vtkRenderer *ren, vtkActor *act);
341 
345  virtual void GetShaderTemplate(
346  std::map<vtkShader::Type, vtkShader *> shaders,
347  vtkRenderer *ren, vtkActor *act);
348 
352  virtual void ReplaceShaderValues(
353  std::map<vtkShader::Type, vtkShader *> shaders,
354  vtkRenderer *ren, vtkActor *act);
355 
357 
361  virtual void ReplaceShaderRenderPass(
362  std::map<vtkShader::Type, vtkShader *> shaders,
363  vtkRenderer *ren, vtkActor *act, bool prePass);
364  virtual void ReplaceShaderColor(
365  std::map<vtkShader::Type, vtkShader *> shaders,
366  vtkRenderer *ren, vtkActor *act);
367  virtual void ReplaceShaderLight(
368  std::map<vtkShader::Type, vtkShader *> shaders,
369  vtkRenderer *ren, vtkActor *act);
370  virtual void ReplaceShaderTCoord(
371  std::map<vtkShader::Type, vtkShader *> shaders,
372  vtkRenderer *ren, vtkActor *act);
373  virtual void ReplaceShaderPicking(
374  std::map<vtkShader::Type, vtkShader *> shaders,
375  vtkRenderer *ren, vtkActor *act);
376  virtual void ReplaceShaderPrimID(
377  std::map<vtkShader::Type, vtkShader *> shaders,
378  vtkRenderer *ren, vtkActor *act);
379  virtual void ReplaceShaderNormal(
380  std::map<vtkShader::Type, vtkShader *> shaders,
381  vtkRenderer *ren, vtkActor *act);
382  virtual void ReplaceShaderClip(
383  std::map<vtkShader::Type, vtkShader *> shaders,
384  vtkRenderer *ren, vtkActor *act);
385  virtual void ReplaceShaderPositionVC(
386  std::map<vtkShader::Type, vtkShader *> shaders,
387  vtkRenderer *ren, vtkActor *act);
388  virtual void ReplaceShaderCoincidentOffset(
389  std::map<vtkShader::Type, vtkShader *> shaders,
390  vtkRenderer *ren, vtkActor *act);
391  virtual void ReplaceShaderDepth(
392  std::map<vtkShader::Type, vtkShader *> shaders,
393  vtkRenderer *ren, vtkActor *act);
395 
399  virtual void SetMapperShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
400 
404  virtual void SetLightingShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
405 
409  virtual void SetCameraShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
410 
414  virtual void SetPropertyShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
415 
419  virtual void UpdateBufferObjects(vtkRenderer *ren, vtkActor *act);
420 
424  virtual bool GetNeedToRebuildBufferObjects(vtkRenderer *ren, vtkActor *act);
425 
429  virtual void BuildBufferObjects(vtkRenderer *ren, vtkActor *act);
430 
434  virtual void BuildIBO(vtkRenderer *ren, vtkActor *act, vtkPolyData *poly);
435 
436  // The VBO and its layout.
438 
439  // Structures for the various cell types we render.
440  vtkOpenGLHelper Primitives[PrimitiveEnd];
443 
444  // do we have wide lines that require special handling
445  virtual bool HaveWideLines(vtkRenderer *, vtkActor *);
446 
447  // do we have textures that require special handling
448  virtual bool HaveTextures(vtkActor *actor);
449 
450  // how many textures do we have
451  virtual unsigned int GetNumberOfTextures(vtkActor *actor);
452 
453  // populate a vector with the textures we have
454  // the order is always
455  // ColorInternalTexture
456  // Actors texture
457  // Properties textures
458  virtual std::vector<std::pair<vtkTexture *, std::string> > GetTextures(vtkActor *actor);
459 
460  // do we have textures coordinates that require special handling
461  virtual bool HaveTCoords(vtkPolyData *poly);
462 
463  // values we use to determine if we need to rebuild shaders
464  std::map<const vtkOpenGLHelper *, int> LastLightComplexity;
465  std::map<const vtkOpenGLHelper *, int> LastLightCount;
466  std::map<const vtkOpenGLHelper *, vtkTimeStamp> LightComplexityChanged;
467 
470 
471  // Caches the vtkOpenGLRenderPass::RenderPasses() information.
472  // Note: Do not dereference the pointers held by this object. There is no
473  // guarantee that they are still valid!
475 
476  // Check the renderpasses in actor's property keys to see if they've changed
477  // render stages:
478  vtkMTimeType GetRenderPassStageMTime(vtkActor *actor);
479 
481  vtkTimeStamp VBOBuildTime; // When was the OpenGL VBO updated?
482  std::string VBOBuildString; // used for determining whento rebuild the VBO
483  std::string IBOBuildString; // used for determining whento rebuild the IBOs
486 
489 
494  int ShiftScaleMethod; // for points
495 
496  // if set to true, tcoords will be passed to the
497  // VBO even if the mapper knows of no texture maps
498  // normally tcoords are only added to the VBO if the
499  // mapper has identified a texture map as well.
501 
502  void BuildCellTextures(
503  vtkRenderer *ren,
504  vtkActor *,
505  vtkCellArray *prims[4],
506  int representation);
507 
508  void AppendCellTextures(
509  vtkRenderer *ren,
510  vtkActor *,
511  vtkCellArray *prims[4],
512  int representation,
513  std::vector<unsigned char> &colors,
514  std::vector<float> &normals,
515  vtkPolyData *pd);
516 
524 
525  // additional picking indirection
530 
531  std::map<const vtkShader::ReplacementSpec, vtkShader::ReplacementValue>
533 
535  {
536  public:
541  };
542  std::map<std::string,ExtraAttributeValue> ExtraAttributes;
543 
548 
549  // are we currently drawing spheres/tubes
550  bool DrawingSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
551  bool DrawingTubes(vtkOpenGLHelper &cellBO, vtkActor *actor);
552  bool DrawingTubesOrSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
553 
554  // get which opengl mode to use to draw the primitive
555  int GetOpenGLMode(int representation, int primType);
556 
557  // get how big to make the points when doing point picking
558  // typically 2 for points, 4 for lines, 6 for surface
559  int GetPointPickingPrimitiveSize(int primType);
560 
561  // a map from drawn triangles back to containing cell id
562  std::vector<unsigned int> CellCellMap;
563 
564 private:
566  void operator=(const vtkOpenGLPolyDataMapper&) = delete;
567 };
568 
569 #endif
void ForceHaveAppleBugOn()
Override the normal test for the apple bug.
std::map< std::string, ExtraAttributeValue > ExtraAttributes
PolyDataMapper using OpenGL to render.
represents an object (geometry & properties) in a rendered scene
Definition: vtkActor.h:45
vtkOpenGLBufferObject * CellScalarBuffer
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:35
virtual void MapDataArrayToMultiTextureAttribute(const char *textureName, const char *dataArrayName, int fieldAssociation, int componentno=-1)
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:45
record modification and/or execution time
Definition: vtkTimeStamp.h:32
vtkTextureObject * CellScalarTexture
vtkOpenGLBufferObject * CellNormalBuffer
vtkTextureObject * CellNormalTexture
abstract specification for renderers
Definition: vtkRenderer.h:57
std::map< const vtkShader::ReplacementSpec, vtkShader::ReplacementValue > UserShaderReplacements
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:54
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.
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:34
vtkOpenGLVertexBufferObjectGroup * VBOs
a simple class to control print indentation
Definition: vtkIndent.h:33
std::map< const vtkOpenGLHelper *, vtkTimeStamp > LightComplexityChanged
vtkNew< vtkMatrix4x4 > VBOShiftScale
handles properties associated with a texture map
Definition: vtkTexture.h:65
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:44
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:33
manage vertex buffer objects shared within a mapper
vtkOpenGLTexture * InternalColorTexture
vtkNew< vtkInformation > LastRenderPassInfo
Asynchronously measures GPU execution time for a single event.
represent and manipulate 3D points
Definition: vtkPoints.h:33
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:105
vtkOpenGLBufferObject * AppleBugPrimIDBuffer