VTK  9.7.1
vtkOpenGLLowMemoryPolyDataMapper.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
17 #ifndef vtkOpenGLLowMemoryPolyDataMapper_h
18 #define vtkOpenGLLowMemoryPolyDataMapper_h
19 
20 #include "vtkPolyDataMapper.h"
21 
22 #include "vtkCellGraphicsPrimitiveMap.h" // For CellTypeMapperOffsets
23 #include "vtkDrawTexturedElements.h" // For parent helper class
24 #include "vtkHardwareSelector.h" // For ivar
25 #include "vtkOpenGLShaderDeclaration.h" // For ivar
26 #include "vtkRenderingOpenGL2Module.h" // For export macro
27 #include "vtkShaderProgram.h" // For vtkWeakPtr<vtkShaderProgram>
28 #include "vtkWeakPtr.h" // For vtkWeakPtr
29 #include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
30 
31 #include <array> // for array
32 #include <set> // for set
33 #include <utility> // for pair
34 #include <vector> // for TextureInfo
35 
36 VTK_ABI_NAMESPACE_BEGIN
43 class vtkShaderProgram;
44 
45 class VTKRENDERINGOPENGL2_EXPORT VTK_MARSHALAUTO vtkOpenGLLowMemoryPolyDataMapper
46  : public vtkPolyDataMapper
47 #ifndef __VTK_WRAP__
49 #endif
50 {
51 public:
54  static vtkOverrideAttribute* CreateOverrideAttributes();
56  void PrintSelf(ostream& os, vtkIndent indent) override;
57 
61  void ShallowCopy(vtkAbstractMapper* m) override;
62 
68  MapperHashType GenerateHash(vtkPolyData* polydata) override;
69 
75 
76  vtkPolyData* CurrentInput = nullptr;
77  void RenderPiece(vtkRenderer* renderer, vtkActor* actor) override;
78  virtual void RenderPieceStart(vtkRenderer* renderer, vtkActor* actor);
79  virtual void RenderPieceDraw(vtkRenderer* renderer, vtkActor* actor);
80  virtual void RenderPieceFinish(vtkRenderer* renderer, vtkActor* actor);
81 
83  void ReleaseGraphicsResources(vtkWindow*) override;
84 
86  vtkGetMacro(PopulateSelectionSettings, bool);
88  void SetPopulateSelectionSettings(bool v) { this->PopulateSelectionSettings = v; }
89  void SetVBOShiftScaleMethod(int method) override;
90 
97  bool GetSupportsSelection() override { return true; }
98 
100 
111  vtkSetMacro(UseIndexedRendering, bool);
112  vtkGetMacro(UseIndexedRendering, bool);
113  vtkBooleanMacro(UseIndexedRendering, bool);
115 
117  virtual void ResetModsToDefault();
118  void AddMod(const std::string& className);
119  void AddMods(const std::vector<std::string>& classNames);
120  void RemoveMod(const std::string& className);
121  void RemoveAllMods();
122 
134  void MapDataArrayToVertexAttribute(const char* vertexAttributeName, const char* dataArrayName,
135  int fieldAssociation, int componentno = -1) override;
136 
137  // This method will Map the specified data array for use as
138  // a texture coordinate for texture tname. The actual
139  // attribute will be named tname_coord so as to not
140  // conflict with the texture sampler definition which will
141  // be tname.
142  void MapDataArrayToMultiTextureAttribute(const char* tname, const char* dataArrayName,
143  int fieldAssociation, int componentno = -1) override;
144 
148  void RemoveVertexAttributeMapping(const char* vertexAttributeName) override;
149 
153  void RemoveAllVertexAttributeMappings() override;
154 
160  vtkHardwareSelector* sel, std::vector<unsigned int>& pixeloffsets, vtkProp* prop) override;
161 
162 protected:
165 
167  {
168  // Color is obtained by mapping point scalar array. shader will interpolate colors.
169  Point,
170  // Color is obtained by mapping cell scalar array. entire cell will have the same color.
171  Cell,
172  // Similar to `Point`, in addition, mapper already interpolated the scalars and provided a
173  // ColorTextureCoordinate and a ColorTexture.
174  PointTexture,
175  // The color of the entire geometry is specified by the actor's vtkProperty instance.
176  Uniform
177  };
178 
180  {
181  // Uses point normals.
182  Point,
183  // Uses cell normals.
184  Cell,
185  // Shader computes a normal for the provoking vertex and passes it down to fragment shader.
186  Primitive
187  };
188 
189  ShaderColorSourceAttribute DetermineShaderColorSource(vtkPolyData* mesh);
190  ShaderNormalSourceAttribute DetermineShaderNormalSource(vtkActor* actor, vtkPolyData* mesh);
191 
192  void MapDataArray(const char* vertexAttributeName, const char* dataArrayName,
193  const char* texturename, int fieldAssociation, int componentno);
194  bool IsUpToDate(vtkRenderer* renderer, vtkActor* actor);
195  bool IsShaderUpToDate(vtkRenderer* renderer, vtkActor* actor);
196  virtual bool IsDataObjectUpToDate();
197  virtual bool IsShaderColorSourceUpToDate(vtkActor* actor);
198  virtual bool IsShaderNormalSourceUpToDate(vtkActor* actor);
199  void DeleteTextureBuffers();
200  virtual bool BindArraysToTextureBuffers(vtkRenderer* renderer, vtkActor* actor,
202  void InstallArrayTextureShaderDeclarations();
203  virtual void UpdateShaders(vtkRenderer* renderer, vtkActor* actor);
204  void ReplaceShaderValues(
205  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
206  void ReplaceShaderPosition(
207  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
208  void ReplaceShaderNormal(
209  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
210  virtual void ReplaceShaderColor(
211  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
212  void ReplaceShaderImplementationCustomUniforms(
213  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
214  void ReplaceShaderPointSize(
215  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
216  void ReplaceShaderWideLines(
217  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
218  void ReplaceShaderEdges(
219  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
220  void ReplaceShaderSelection(
221  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
222  void ReplaceShaderTCoord(
223  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
224  void ReplaceShaderClip(
225  vtkRenderer* renderer, vtkActor* actor, std::string& vsSource, std::string& fsSource);
226  void SetShaderParameters(vtkRenderer* renderer, vtkActor* actor);
227  // compute and set the maximum point and cell ID used in selection
228  void UpdateMaximumPointCellIds(vtkRenderer* ren, vtkActor* actor);
229 
230  bool GetCoordShiftAndScaleEnabled();
231  int GetCoordShiftAndScaleMethod();
232  void SetShiftValues(double x, double y, double z);
233  void SetScaleValues(double x, double y, double z);
234  void ComputeShiftScale(vtkRenderer* renderer, vtkActor* actor, vtkDataArray* arrays);
235  void ComputeCameraBasedShiftScale(vtkRenderer* renderer, vtkActor* actor, vtkPolyData* mesh);
236  void ComputeShiftScaleTransform(vtkRenderer* renderer, vtkActor* actor);
237  virtual void UpdateShiftScale(vtkRenderer* renderer, vtkActor* actor);
238 
239  virtual vtkDataArray* GetColors(vtkPolyData* mesh);
240  vtkDataArray* GetPointNormals(vtkPolyData* mesh);
241  vtkDataArray* GetPointTangents(vtkPolyData* mesh);
242  vtkDataArray* GetTextureCoordinates(vtkPolyData* mesh);
243  vtkDataArray* GetColorTextureCoordinates(vtkPolyData* mesh);
244  vtkDataArray* GetCellNormals(vtkPolyData* mesh);
245 
246  using TextureInfo = std::pair<vtkTexture*, std::string>;
247  bool HaveTextures(vtkActor* actor);
248  unsigned int GetNumberOfTextures(vtkActor* actor);
249  virtual std::vector<TextureInfo> GetTextures(vtkActor* actor);
250  std::pair<std::string, std::string> GetTextureCoordinateAndSamplerBufferNames(const char* tname);
251 
252  void UpdatePBRStateCache(vtkRenderer* renderer, vtkActor* actor);
253  void UpdateGLSLMods(vtkRenderer* renderer, vtkActor* actor);
254 
256  {
257  public:
262  };
263  std::map<std::string, ExtraAttributeValue> ExtraAttributes;
264 
266  ShaderColorSourceAttribute ShaderColorSource = ShaderColorSourceAttribute::Uniform;
267  ShaderNormalSourceAttribute ShaderNormalSource = ShaderNormalSourceAttribute::Primitive;
268 
272  std::vector<std::string> ModNames;
273  std::set<std::string> ModNamesUnique;
274  static std::vector<std::string> DefaultModNames;
275 
281  std::vector<vtkOpenGLShaderDeclaration> ShaderDecls;
282 
285  int LastSelectionState = vtkHardwareSelector::MIN_KNOWN_PASS - 1;
290 
292  std::array<double, 3> ShiftValues;
293  std::array<double, 3> ScaleValues;
294  bool CoordinateShiftAndScaleInUse = false;
295  vtkNew<vtkTransform> SSInverseTransform; // Inverse transform which can undo shift + scale.
296  vtkNew<vtkMatrix4x4> SSMatrix; // Transpose of the above inverse transform.
297 
299  {
301  vtkIdType NumberOfElements = 0;
302  bool CanRender = false;
303  bool UsesEdgeValueBuffer = false;
304  bool UsesCellMapBuffer = false;
305  friend std::ostream& operator<<(std::ostream& os, const CellGroupInformation& cg)
306  {
307  os << cg.Offsets << '\n'
308  << "NumberOfElements: " << cg.NumberOfElements << '\n'
309  << "CanRender: " << cg.CanRender << '\n'
310  << "UsesEdgeValueBuffer: " << cg.UsesEdgeValueBuffer << '\n'
311  << "UsesCellMapBuffer: " << cg.UsesCellMapBuffer << '\n';
312  return os;
313  }
314  };
319  {
320  std::unique_ptr<vtkOpenGLLowMemoryCellTypeAgent> Agent;
321  std::function<vtkCellGraphicsPrimitiveMap::PrimitiveDescriptor(vtkPolyData*)> GeneratorFunction;
322  std::vector<CellGroupInformation> CellGroups;
323  };
324  std::array<PrimitiveInformation, 4> Primitives;
325 
327  bool UseIndexedRendering = true;
328  bool DrawingVertices = false;
329  bool HasColors = false;
330  bool HasTangents = false;
331  bool HasPointNormals = false;
332  bool HasCellNormals = false;
333  bool HasPointTextureCoordinates = false;
334  // if set to true, tcoords will be passed to the
335  // VBO even if the mapper knows of no texture maps
336  // normally tcoords are only added to the VBO if the
337  // mapper has identified a texture map as well.
338  bool ForceTextureCoordinates = false;
339 
341  vtkOpenGLTexture* InternalColorTexture = nullptr;
342 
344  bool PopulateSelectionSettings = true;
345  bool PointPicking = false;
346 
348  bool HasAnisotropy = false;
349  bool HasClearCoat = false;
350  bool UsesNormalMap = false;
351  bool UsesCoatNormalMap = false;
352  bool UsesRotationMap = false;
354 
361  bool ShouldUseIndexedRendering(vtkRenderer* renderer, vtkActor* actor,
362  const CellGroupInformation& cellGroup, int numberOfPointsPerPrimitive,
363  int numberOfPseudoPrimitivesPerElement, bool inVertexVisibilityPass) const;
364 
372  {
373  // SetShaderParameters
374  int ViewportDimensions = -1;
375  int LineWidth = -1;
376  int RenderPointsAsSpheres = -1;
377  int RenderLinesAsTubes = -1;
378  int PointPicking = -1;
379  int VertexColor = -1;
380  int EdgeColor = -1;
381  int EdgeOpacity = -1;
382  int EdgeVisibility = -1;
383  int Wireframe = -1;
384  int EdgeWidth = -1;
385  int CameraParallel = -1;
386  int ZCalcR = -1;
387  int ZCalcS = -1;
388  int NumClipPlanes = -1;
389  int ClipPlanes = -1;
390  int MapperIndex = -1;
391  // cell-type agent
392  int CellType = -1;
393  int EnableLights = -1;
394  int VertexPass = -1;
395  int PrimitiveSize = -1;
396  int PointSize = -1;
397  int CellIdOffset = -1;
398  int VertexIdOffset = -1;
399  int EdgeValueBufferOffset = -1;
400  int PointIdOffset = -1;
401  int PrimitiveIdOffset = -1;
402  int UsesCellMap = -1;
403  int UsesEdgeValues = -1;
404  int UseIndexedPointId = -1;
405  } UniformLocs;
407  unsigned int CachedLocLinkCount = 0;
410  void UpdateUniformLocations();
411 
412 private:
414  void operator=(const vtkOpenGLLowMemoryPolyDataMapper&) = delete;
419 
420  vtkNew<vtkMatrix4x4> TempMatrix4;
421 };
422 
423 #define vtkOpenGLLowMemoryPolyDataMapper_OVERRIDE_ATTRIBUTES \
424  vtkOpenGLLowMemoryPolyDataMapper::CreateOverrideAttributes()
425 VTK_ABI_NAMESPACE_END
426 #endif // vtkOpenGLLowMemoryPolyDataMapper_h
abstract superclass for all actors, volumes and annotations
Definition: vtkProp.h:49
Attribute for vtkObjectFactory overrides.
void ShallowCopy(vtkAbstractMapper *m) override
Make a shallow copy of this mapper.
represents an object (geometry & properties) in a rendered scene
Definition: vtkActor.h:41
virtual MapperHashType GenerateHash(vtkPolyData *)
This hash integer is computed by concrete graphics implementation of this class.
std::unique_ptr< vtkOpenGLLowMemoryCellTypeAgent > Agent
virtual void MapDataArrayToMultiTextureAttribute(const char *textureName, const char *dataArrayName, int fieldAssociation, int componentno=-1)
virtual void RemoveAllVertexAttributeMappings()
Remove all vertex attributes.
std::function< vtkCellGraphicsPrimitiveMap::PrimitiveDescriptor(vtkPolyData *)> GeneratorFunction
vtkTimeStamp PBRStateTimeStamp
Decide whether a particular cell-group draw may use the indexed (glDrawElementsInstanced) fast path...
record modification and/or execution time
Definition: vtkTimeStamp.h:24
vtkCellGraphicsPrimitiveMap::CellTypeMapperOffsets Offsets
abstract specification for renderers
Definition: vtkRenderer.h:63
virtual void RenderPiece(vtkRenderer *, vtkActor *)
Implemented by sub classes.
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:469
int vtkIdType
Definition: vtkType.h:363
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:71
Maps VTK_LINE and VTK_POLY_LINE into GL_LINES and draws GL_LINES or GL_POINTS.
virtual void RemoveVertexAttributeMapping(const char *vertexAttributeName)
Remove a vertex attribute mapping.
PolyDataMapper using OpenGL to render surface meshes.
OpenGL texture map.
window superclass for vtkRenderWindow
Definition: vtkWindow.h:41
std::uintptr_t MapperHashType
a simple class to control print indentation
Definition: vtkIndent.h:28
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
Maps VTK_TRIANGLE and VTK_POLYGON into GL_TRIANGLES and draws GL_TRIANGLES or GL_LINES or GL_POINTS...
friend std::ostream & operator<<(std::ostream &os, const CellGroupInformation &cg)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
#define VTK_NEWINSTANCE
#define VTK_MARSHALEXCLUDE(reason)
std::array< PrimitiveInformation, 4 > Primitives
Hybrid surface/expansion dispatch switch. See the public accessors.
vtkWeakPtr< vtkShaderProgram > CachedLocProgram
Resolve UniformLocs against the current ShaderProgram if it changed or was relinked since the last ca...
abstract class specifies interface to map data
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
static std::vector< std::string > DefaultModNames
std::pair< vtkTexture *, std::string > TextureInfo
A base class for mappers or render responders that need to draw primitives via vertex-pulling.
bool GetSupportsSelection() override
WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE DO NOT USE THIS METHOD OUTSIDE OF THE RENDERI...
static vtkPolyDataMapper * New()
virtual vtkIdType GetMaximumNumberOfTriangles(vtkRenderer *)
Returns an estimate of the number of triangles that can be rendered by the underlying graphics implem...
std::map< std::string, ExtraAttributeValue > ExtraAttributes
#define VTK_MARSHALAUTO
Helps vtkOpenGLLowMemoryPolyDataMapper map and draw cell types from vtkPolyData as OpenGL graphics pr...
Maps VTK_VERTEX and VTK_POLY_VERTEX into GL_POINTS and draws GL_POINTS.
virtual void SetVBOShiftScaleMethod(int)
A convenience method for enabling/disabling the VBO's shift+scale transform.
std::vector< vtkOpenGLShaderDeclaration > ShaderDecls
void ReleaseGraphicsResources(vtkWindow *) override
Release any graphics resources that are being consumed by this mapper.
Definition: vtkMapper.h:105
The ShaderProgram uses one or more Shader objects.
#define VTK_MARSHAL_EXCLUDE_REASON_IS_INTERNAL