VTK  9.3.20240328
vtkOpenGLVertexBufferObject.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
3 #ifndef vtkOpenGLVertexBufferObject_h
4 #define vtkOpenGLVertexBufferObject_h
5 
7 #include "vtkPolyDataMapper.h" // for ShiftScaleMethodType
8 #include "vtkRenderingOpenGL2Module.h" // for export macro
9 #include "vtkWeakPointer.h" // For vtkWeakPointer
10 
11 VTK_ABI_NAMESPACE_BEGIN
13 
21 // useful union for stuffing colors into a float
23  unsigned char c[4];
24  short s[2];
25  float f;
26 };
27 
28 class vtkCamera;
29 class vtkProp3D;
30 
31 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLVertexBufferObject : public vtkOpenGLBufferObject
32 {
33 public:
36  void PrintSelf(ostream& os, vtkIndent indent) override;
37 
38  // set the VBOs data to the provided data array and upload
39  // this can use a fast path of just passing the
40  // data array pointer to OpenGL if it is suitable
42 
43  // append a data array to this VBO, always
44  // copies the data from the data array
46 
47  // Get the mtime when this VBO was loaded
48  vtkGetMacro(UploadTime, vtkTimeStamp);
49 
51 
53 
56  static constexpr int DISABLE_SHIFT_SCALE = ShiftScaleMethod::DISABLE_SHIFT_SCALE;
57  static constexpr int AUTO_SHIFT_SCALE = ShiftScaleMethod::AUTO_SHIFT_SCALE;
58  static constexpr int ALWAYS_AUTO_SHIFT_SCALE = ShiftScaleMethod::ALWAYS_AUTO_SHIFT_SCALE;
59  static constexpr int MANUAL_SHIFT_SCALE = ShiftScaleMethod::MANUAL_SHIFT_SCALE;
60  static constexpr int AUTO_SHIFT = ShiftScaleMethod::AUTO_SHIFT;
61  static constexpr int NEAR_PLANE_SHIFT_SCALE = ShiftScaleMethod::NEAR_PLANE_SHIFT_SCALE;
62  static constexpr int FOCAL_POINT_SHIFT_SCALE = ShiftScaleMethod::FOCAL_POINT_SHIFT_SCALE;
64 
65  // Description:
66  // Get the shift and scale vectors computed by CreateVBO;
67  // or set the values CreateVBO and AppendVBO will use.
68  // Note that the "Set" methods **must** be called before the
69  // first time that CreateVBO or AppendVBO is invoked and
70  // should never be called afterwards.
71  //
72  // The CoordShiftAndScaleMethod describes how the shift
73  // and scale vectors are obtained (or that they should never
74  // be used).
75  // The GetCoordShiftAndScaleEnabled() method returns true if
76  // a shift and scale are currently being applied (or false if not).
77  //
78  // The "Get" methods are used by the mapper to modify the world
79  // and camera transformation matrices to match the scaling applied
80  // to coordinates in the VBO.
81  // CreateVBO only applies a shift and scale when the midpoint
82  // of the point bounding-box is distant from the origin by a
83  // factor of 10,000 or more relative to the size of the box
84  // along any axis.
85  //
86  // For example, if the x coordinates of the points range from
87  // 200,000 to 200,001 then the factor is
88  // 200,000.5 / (200,001 - 200,000) = 2x10^5, which is larger
89  // than 10,000 -- so the coordinates will be shifted and scaled.
90  //
91  // This is important as many OpenGL drivers use reduced precision
92  // to hold point coordinates.
93  //
94  // These methods are used by the mapper to determine the
95  // additional transform (if any) to apply to the rendering transform.
98  virtual void SetCoordShiftAndScaleMethod(int meth);
99  virtual void SetShift(const std::vector<double>& shift);
100  virtual void SetShift(double x, double y, double z);
101  virtual void SetScale(const std::vector<double>& scale);
102  virtual void SetScale(double x, double y, double z);
103  virtual const std::vector<double>& GetShift();
104  virtual const std::vector<double>& GetScale();
105 
106  // update the shift scale if needed
108 
109  // Allow all vertex adjustments to be enabled/disabled
110  //
111  // When smaller objects are positioned on the side of a larger scene,
112  // we don't want an individual mapper to try and center all its vertices.
113  //
114  // Complex scenes need to center the whole scene, not an individual mapper,
115  // so allow applications to turn all these shifts off and manage the
116  // float imprecision on their own.
118  static void GlobalCoordShiftAndScaleEnabledOn() { SetGlobalCoordShiftAndScaleEnabled(1); }
119  static void GlobalCoordShiftAndScaleEnabledOff() { SetGlobalCoordShiftAndScaleEnabled(0); }
121 
122  // Set/Get the DataType to use for the VBO
123  // As a side effect sets the DataTypeSize
124  void SetDataType(int v);
125  vtkGetMacro(DataType, int);
126 
127  // Get the size in bytes of the data type
128  vtkGetMacro(DataTypeSize, unsigned int);
129 
130  // How many tuples in the VBO
131  vtkGetMacro(NumberOfTuples, unsigned int);
132 
133  // How many components in the VBO
134  vtkGetMacro(NumberOfComponents, unsigned int);
135 
136  // Set/Get the VBO stride in bytes
137  vtkSetMacro(Stride, unsigned int);
138  vtkGetMacro(Stride, unsigned int);
139 
140  // Get the underlying VBO array
141  std::vector<float>& GetPackedVBO() { return this->PackedVBO; }
142 
143  // upload the current PackedVBO
144  // only used by mappers that skip the VBOGroup support
145  void UploadVBO();
146 
147  // VBOs may hold onto the cache, never the other way around
149 
150  // used by mappers that support camera based shift scale
151  virtual void SetCamera(vtkCamera* cam);
152  virtual void SetProp3D(vtkProp3D* prop3d);
153 
154 protected:
157 
158  std::vector<float> PackedVBO; // the data
159 
161 
162  unsigned int Stride; // The size of a complete tuple
163  unsigned int NumberOfComponents;
164  unsigned int NumberOfTuples;
165  int DataType;
166  unsigned int DataTypeSize;
167 
170  std::vector<double> Shift;
171  std::vector<double> Scale;
172 
174 
177 
178 private:
180  void operator=(const vtkOpenGLVertexBufferObject&) = delete;
181 
182  // Initialize static member that controls shifts and scales
183  static vtkTypeBool GlobalCoordShiftAndScaleEnabled;
184 };
185 
186 VTK_ABI_NAMESPACE_END
187 #endif
a virtual camera for 3D rendering
Definition: vtkCamera.h:150
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:154
a simple class to control print indentation
Definition: vtkIndent.h:108
OpenGL buffer object.
manage vertex buffer objects shared within a context
void UpdateShiftScale(vtkDataArray *da)
virtual bool GetCoordShiftAndScaleEnabled()
void SetCache(vtkOpenGLVertexBufferObjectCache *cache)
virtual void SetScale(double x, double y, double z)
virtual void SetCoordShiftAndScaleMethod(int meth)
static vtkTypeBool GetGlobalCoordShiftAndScaleEnabled()
~vtkOpenGLVertexBufferObject() override
void UploadDataArray(vtkDataArray *array)
virtual const std::vector< double > & GetScale()
virtual void SetScale(const std::vector< double > &scale)
virtual void SetShift(double x, double y, double z)
static void SetGlobalCoordShiftAndScaleEnabled(vtkTypeBool val)
virtual const std::vector< double > & GetShift()
virtual void SetShift(const std::vector< double > &shift)
virtual int GetCoordShiftAndScaleMethod()
static vtkOpenGLVertexBufferObject * New()
virtual void SetCamera(vtkCamera *cam)
void AppendDataArray(vtkDataArray *array)
virtual void SetProp3D(vtkProp3D *prop3d)
vtkWeakPointer< vtkProp3D > Prop3D
vtkOpenGLVertexBufferObjectCache * Cache
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkWeakPointer< vtkCamera > Camera
ShiftScaleMethodType
Methods for VBO coordinate shift+scale-computation.
represents an 3D object for placement in a rendered scene
Definition: vtkProp3D.h:88
record modification and/or execution time
Definition: vtkTimeStamp.h:44
@ scale
Definition: vtkX3D.h:229
OpenGL vertex buffer object.
int vtkTypeBool
Definition: vtkABI.h:64