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 vtkTexture;
48 class vtkTextureObject;
49 class vtkTransform;
50 
51 
52 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLPolyDataMapper : public vtkPolyDataMapper
53 {
54 public:
55  static vtkOpenGLPolyDataMapper* New();
57  void PrintSelf(ostream& os, vtkIndent indent) override;
58 
62  void RenderPiece(vtkRenderer *ren, vtkActor *act) override;
63 
65 
68  virtual void RenderPieceStart(vtkRenderer *ren, vtkActor *act);
69  virtual void RenderPieceDraw(vtkRenderer *ren, vtkActor *act);
70  virtual void RenderPieceFinish(vtkRenderer *ren, vtkActor *act);
72 
78  void ReleaseGraphicsResources(vtkWindow *) override;
79 
80  vtkGetMacro(PopulateSelectionSettings,int);
81  void SetPopulateSelectionSettings(int v) { this->PopulateSelectionSettings = v; };
82 
89  bool GetSupportsSelection() override { return true; }
90 
101  bool GetIsOpaque() override;
102 
103  // used by RenderPiece and functions it calls to reduce
104  // calls to get the input and allow for rendering of
105  // other polydata (not the input)
107 
109 
115  vtkSetStringMacro(PointIdArrayName);
116  vtkGetStringMacro(PointIdArrayName);
117  vtkSetStringMacro(CellIdArrayName);
118  vtkGetStringMacro(CellIdArrayName);
120 
122 
127  vtkSetStringMacro(ProcessIdArrayName);
128  vtkGetStringMacro(ProcessIdArrayName);
130 
132 
141  vtkSetStringMacro(CompositeIdArrayName);
142  vtkGetStringMacro(CompositeIdArrayName);
144 
145 
147 
154  void AddShaderReplacement(
155  vtkShader::Type shaderType, // vertex, fragment, etc
156  const std::string& originalValue,
157  bool replaceFirst, // do this replacement before the default
158  const std::string& replacementValue,
159  bool replaceAll);
160  void ClearShaderReplacement(
161  vtkShader::Type shaderType, // vertex, fragment, etc
162  const std::string& originalValue,
163  bool replaceFirst);
164  void ClearAllShaderReplacements(vtkShader::Type shaderType);
165  void ClearAllShaderReplacements();
167 
169 
175  vtkSetStringMacro(VertexShaderCode);
176  vtkGetStringMacro(VertexShaderCode);
177  vtkSetStringMacro(FragmentShaderCode);
178  vtkGetStringMacro(FragmentShaderCode);
179  vtkSetStringMacro(GeometryShaderCode);
180  vtkGetStringMacro(GeometryShaderCode);
182 
183  // the following is all extra stuff to work around the
184  // fact that gl_PrimitiveID does not work correctly on
185  // Apple devices with AMD graphics hardware. See apple
186  // bug ID 20747550
187  static vtkPolyData *HandleAppleBug(
188  vtkPolyData *poly,
189  std::vector<float> &buffData);
190 
195 
197 
201  {
202  this->HaveAppleBugForce = 1;
203  this->Modified();
204  }
206  {
207  this->HaveAppleBugForce = 2;
208  this->Modified();
209  }
211 
215  bool GetHaveAppleBug() { return this->HaveAppleBug; }
216 
218  vtkGetObjectMacro(VBOs, vtkOpenGLVertexBufferObjectGroup);
219 
223  void SetVBOShiftScaleMethod(int m);
224 
226  PrimitiveStart = 0,
227  PrimitivePoints = 0,
234  PrimitiveEnd
235  };
236 
240  static void MakeCellCellMap(std::vector<vtkIdType> &CellCellMap,
241  bool HaveAppleBug,
242  vtkPolyData *poly,
243  vtkCellArray **prims, int representation,
244  vtkPoints *points);
245 
258  const char* vertexAttributeName,
259  const char* dataArrayName,
260  int fieldAssociation,
261  int componentno = -1) override;
262 
263  // This method will Map the specified data array for use as
264  // a texture coordinate for texture tname. The actual
265  // attribute will be named tname_coord so as to not
266  // conflict with the texture sampler definition which will
267  // be tname.
269  const char *tname,
270  const char* dataArrayName, int fieldAssociation, int componentno = -1) override;
271 
272  // deprecated in favor of the const char * signature
273  VTK_LEGACY(void MapDataArrayToMultiTextureAttribute(
274  int unit,
275  const char* dataArrayName, int fieldAssociation, int componentno = -1) override);
276 
280  void RemoveVertexAttributeMapping(const char* vertexAttributeName) override;
281 
285  void RemoveAllVertexAttributeMappings() override;
286 
287 protected:
289  ~vtkOpenGLPolyDataMapper() override;
290 
292 
293  void MapDataArray(
294  const char* vertexAttributeName,
295  const char* dataArrayName,
296  const char *texturename,
297  int fieldAssociation,
298  int componentno);
299 
300  // what coordinate shoudl be used for this texture
301  std::string GetTextureCoordinateName(const char *tname);
302 
303  // the following is all extra stuff to work around the
304  // fact that gl_PrimitiveID does not work correctly on
305  // Apple devices with AMD graphics hardware. See apple
306  // bug ID 20747550
308  int HaveAppleBugForce; // 0 = default 1 = 0ff 2 = on
309  std::vector<float> AppleBugPrimIDs;
311 
315  void GetCoincidentParameters(
316  vtkRenderer *ren, vtkActor *actor, float &factor, float &offset);
317 
323  void ComputeBounds() override;
324 
329  virtual void UpdateShaders(
330  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
331 
335  virtual bool GetNeedToRebuildShaders(
336  vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
337 
341  virtual void BuildShaders(
342  std::map<vtkShader::Type, vtkShader *> shaders,
343  vtkRenderer *ren, vtkActor *act);
344 
348  virtual void GetShaderTemplate(
349  std::map<vtkShader::Type, vtkShader *> shaders,
350  vtkRenderer *ren, vtkActor *act);
351 
355  virtual void ReplaceShaderValues(
356  std::map<vtkShader::Type, vtkShader *> shaders,
357  vtkRenderer *ren, vtkActor *act);
358 
360 
364  virtual void ReplaceShaderRenderPass(
365  std::map<vtkShader::Type, vtkShader *> shaders,
366  vtkRenderer *ren, vtkActor *act, bool prePass);
367  virtual void ReplaceShaderColor(
368  std::map<vtkShader::Type, vtkShader *> shaders,
369  vtkRenderer *ren, vtkActor *act);
370  virtual void ReplaceShaderLight(
371  std::map<vtkShader::Type, vtkShader *> shaders,
372  vtkRenderer *ren, vtkActor *act);
373  virtual void ReplaceShaderTCoord(
374  std::map<vtkShader::Type, vtkShader *> shaders,
375  vtkRenderer *ren, vtkActor *act);
376  virtual void ReplaceShaderPicking(
377  std::map<vtkShader::Type, vtkShader *> shaders,
378  vtkRenderer *ren, vtkActor *act);
379  virtual void ReplaceShaderPrimID(
380  std::map<vtkShader::Type, vtkShader *> shaders,
381  vtkRenderer *ren, vtkActor *act);
382  virtual void ReplaceShaderNormal(
383  std::map<vtkShader::Type, vtkShader *> shaders,
384  vtkRenderer *ren, vtkActor *act);
385  virtual void ReplaceShaderClip(
386  std::map<vtkShader::Type, vtkShader *> shaders,
387  vtkRenderer *ren, vtkActor *act);
388  virtual void ReplaceShaderPositionVC(
389  std::map<vtkShader::Type, vtkShader *> shaders,
390  vtkRenderer *ren, vtkActor *act);
391  virtual void ReplaceShaderCoincidentOffset(
392  std::map<vtkShader::Type, vtkShader *> shaders,
393  vtkRenderer *ren, vtkActor *act);
394  virtual void ReplaceShaderDepth(
395  std::map<vtkShader::Type, vtkShader *> shaders,
396  vtkRenderer *ren, vtkActor *act);
398 
402  virtual void SetMapperShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
403 
407  virtual void SetLightingShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
408 
412  virtual void SetCameraShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
413 
417  virtual void SetPropertyShaderParameters(vtkOpenGLHelper &cellBO, vtkRenderer *ren, vtkActor *act);
418 
422  virtual void UpdateBufferObjects(vtkRenderer *ren, vtkActor *act);
423 
427  virtual bool GetNeedToRebuildBufferObjects(vtkRenderer *ren, vtkActor *act);
428 
432  virtual void BuildBufferObjects(vtkRenderer *ren, vtkActor *act);
433 
437  virtual void BuildIBO(vtkRenderer *ren, vtkActor *act, vtkPolyData *poly);
438 
439  // The VBO and its layout.
441 
442  // Structures for the various cell types we render.
443  vtkOpenGLHelper Primitives[PrimitiveEnd];
446 
447  // do we have wide lines that require special handling
448  virtual bool HaveWideLines(vtkRenderer *, vtkActor *);
449 
450  // do we have textures that require special handling
451  virtual bool HaveTextures(vtkActor *actor);
452 
453  // how many textures do we have
454  virtual unsigned int GetNumberOfTextures(vtkActor *actor);
455 
456  // populate a vector with the textures we have
457  // the order is always
458  // ColorInternalTexture
459  // Actors texture
460  // Properties textures
461  virtual std::vector<std::pair<vtkTexture *, std::string> > GetTextures(vtkActor *actor);
462 
463  // do we have textures coordinates that require special handling
464  virtual bool HaveTCoords(vtkPolyData *poly);
465 
466  // values we use to determine if we need to rebuild shaders
467  std::map<const vtkOpenGLHelper *, int> LastLightComplexity;
468  std::map<const vtkOpenGLHelper *, int> LastLightCount;
469  std::map<const vtkOpenGLHelper *, vtkTimeStamp> LightComplexityChanged;
470 
473 
474  // Caches the vtkOpenGLRenderPass::RenderPasses() information.
475  // Note: Do not dereference the pointers held by this object. There is no
476  // guarantee that they are still valid!
478 
479  // Check the renderpasses in actor's property keys to see if they've changed
480  // render stages:
481  vtkMTimeType GetRenderPassStageMTime(vtkActor *actor);
482 
484  vtkTimeStamp VBOBuildTime; // When was the OpenGL VBO updated?
485  std::string VBOBuildString; // used for determining whento rebuild the VBO
486  std::string IBOBuildString; // used for determining whento rebuild the IBOs
489 
492 
497  int ShiftScaleMethod; // for points
498 
499  // if set to true, tcoords will be passed to the
500  // VBO even if the mapper knows of no texture maps
501  // normally tcoords are only added to the VBO if the
502  // mapper has identified a texture map as well.
504 
505  void BuildCellTextures(
506  vtkRenderer *ren,
507  vtkActor *,
508  vtkCellArray *prims[4],
509  int representation);
510 
511  void AppendCellTextures(
512  vtkRenderer *ren,
513  vtkActor *,
514  vtkCellArray *prims[4],
515  int representation,
516  std::vector<unsigned char> &colors,
517  std::vector<float> &normals,
518  vtkPolyData *pd);
519 
527 
528  // additional picking indirection
533 
534  std::map<const vtkShader::ReplacementSpec, vtkShader::ReplacementValue>
536 
538  {
539  public:
544  };
545  std::map<std::string,ExtraAttributeValue> ExtraAttributes;
546 
551 
552  // are we currently drawing spheres/tubes
553  bool DrawingSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
554  bool DrawingTubes(vtkOpenGLHelper &cellBO, vtkActor *actor);
555  bool DrawingTubesOrSpheres(vtkOpenGLHelper &cellBO, vtkActor *actor);
556 
557  // get which opengl mode to use to draw the primitive
558  int GetOpenGLMode(int representation, int primType);
559 
560  // get how big to make the points when doing point picking
561  // typically 2 for points, 4 for lines, 6 for surface
562  int GetPointPickingPrimitiveSize(int primType);
563 
564  // a map from drawn triangles back to containing cell id
565  std::vector<unsigned int> CellCellMap;
566 
567 private:
569  void operator=(const vtkOpenGLPolyDataMapper&) = delete;
570 };
571 
572 #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: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.
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
handles properties associated with a texture map
Definition: vtkTexture.h:71
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
vtkOpenGLTexture * InternalColorTexture
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