Point Cloud Library (PCL)  1.7.1
vtkVertexBufferObject.h
1  /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkPixelBufferObject.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 // .NAME vtkVertexBufferObject - abstracts an OpenGL vertex buffer object.
16 // .SECTION Description
17 // Provides low-level access to GPU memory. Used to pass raw data to GPU.
18 // The data is uploaded into a vertex buffer.
19 // .SECTION See Also
20 // OpenGL Vertex Buffer Object Extension Spec (ARB_vertex_buffer_object):
21 // http://www.opengl.org/registry/specs/ARB/vertex_buffer_object.txt
22 // .SECTION Caveats
23 // Since most GPUs don't support double format all double data is converted to
24 // float and then uploaded.
25 // DON'T PLAY WITH IT YET.
26 
27 #ifndef __vtkVertexBufferObject_h
28 #define __vtkVertexBufferObject_h
29 
30 #include <vector>
31 
32 #include "vtkObject.h"
33 #include "vtkWeakPointer.h"
34 
35 #include "vtkgl.h" // Needed for gl data types exposed in API
36 #include <pcl/pcl_macros.h>
37 
38 class vtkCellArray;
39 class vtkDataArray;
40 class vtkObject;
41 class vtkPoints;
42 class vtkUnsignedCharArray;
43 class vtkOpenGLExtensionManager;
44 class vtkRenderWindow;
45 
46 class PCL_EXPORTS vtkVertexBufferObject : public vtkObject
47 {
48 public:
49 
50  static vtkVertexBufferObject* New();
51  vtkTypeMacro(vtkVertexBufferObject, vtkObject);
52  void PrintSelf(ostream& os, vtkIndent indent);
53 
54  // Description:
55  // Get/Set the context. Context must be a vtkOpenGLRenderWindow.
56  // This does not increase the reference count of the
57  // context to avoid reference loops.
58  // SetContext() may raise an error is the OpenGL context does not support the
59  // required OpenGL extensions.
60  void SetContext(vtkRenderWindow* context);
61  vtkRenderWindow* GetContext();
62 
63  //BTX
64  // Usage values.
65  enum
66  {
67  StreamDraw=0,
76  NumberOfUsages
77  };
78  //ETX
79 
80  // Description:
81  // Usage is a performance hint.
82  // Valid values are:
83  // - StreamDraw specified once by A, used few times S
84  // - StreamRead specified once by R, queried a few times by A
85  // - StreamCopy specified once by R, used a few times S
86  // - StaticDraw specified once by A, used many times S
87  // - StaticRead specificed once by R, queried many times by A
88  // - StaticCopy specified once by R, used many times S
89  // - DynamicDraw respecified repeatedly by A, used many times S
90  // - DynamicRead respecified repeatedly by R, queried many times by A
91  // - DynamicCopy respecified repeatedly by R, used many times S
92  // A: the application
93  // S: as the source for GL drawing and image specification commands.
94  // R: reading data from the GL
95  // Initial value is StaticDraw, as in OpenGL spec.
96  vtkGetMacro(Usage, int);
97  vtkSetMacro(Usage, int);
98 
99  int GetAttributeIndex();
100  void SetUserDefinedAttribute(int index, bool normalized=false, int stride=0);
101  void ResetUserDefinedAttribute();
102 
103  void SetAttributeNormalized(bool normalized);
104 
105  // Description:
106  // Set point data
107  bool Upload(vtkPoints *points);
108 
109  // Description:
110  // Set indice data
111  bool Upload(vtkCellArray *verts);
112 
113  // Description:
114  // Set indice data
115  bool Upload(unsigned int *indices, unsigned int count);
116 
117  // Description:
118  // Set color data
119  bool Upload(vtkUnsignedCharArray *colors);
120 
121  // Description:
122  // Set color data
123  bool Upload(vtkDataArray *array);
124  bool Upload(vtkDataArray *array, int attributeType, int arrayType);
125  bool UploadNormals(vtkDataArray *normals);
126  bool UploadColors(vtkDataArray *colors);
127 
128 
129  // Description:
130  // Get the size of the data loaded into the GPU. Size is in the number of
131  // elements of the uploaded Type.
132  vtkGetMacro(Size, unsigned int);
133 
134  // Description:
135  // Get the size of the data loaded into the GPU. Size is in the number of
136  // elements of the uploaded Type.
137  vtkGetMacro(Count, unsigned int);
138 
139  // Description:
140  // Get the openGL buffer handle.
141  vtkGetMacro(Handle, unsigned int);
142 
143  // Description:
144  // Inactivate the buffer.
145  void UnBind();
146 
147  // Description:
148  // Make the buffer active.
149  void Bind();
150 
151  // Description:
152  // Allocate the memory. size is in number of bytes. type is a VTK type.
153 // void Allocate(unsigned int size, int type);
154 
155 //BTX
156 
157  // Description:
158  // Release the memory allocated without destroying the PBO handle.
159  void ReleaseMemory();
160 
161  // Description:
162  // Returns if the context supports the required extensions.
163  static bool IsSupported(vtkRenderWindow* renWin);
164 
165 //ETX
166 //BTX
167 protected:
170 
171  // Description:
172  // Loads all required OpenGL extensions. Must be called every time a new
173  // context is set.
174  bool LoadRequiredExtensions(vtkOpenGLExtensionManager* mgr);
175 
176  // Description:
177  // Create the pixel buffer object.
178  void CreateBuffer();
179 
180  // Description:
181  // Destroys the pixel buffer object.
182  void DestroyBuffer();
183 
184  // Description:
185  // Uploads data to buffer object
186  bool Upload(GLvoid* data);
187 
188  // Description:
189  // Get the openGL buffer handle.
190  vtkGetMacro(ArrayType, unsigned int);
191 
192  int Usage;
193  unsigned int Size;
194  unsigned int Count;
195  unsigned int Handle;
196  unsigned int ArrayType;
197  unsigned int BufferTarget;
198 
204 
205  vtkWeakPointer<vtkRenderWindow> Context;
206 
207 
208 private:
209  vtkVertexBufferObject(const vtkVertexBufferObject&); // Not implemented.
210  void operator=(const vtkVertexBufferObject&); // Not implemented.
211 
212  // Helper to get data type sizes when passing to opengl
213  int GetDataTypeSize(int type);
214  //ETX
215 };
216 
217 #endif
218 
219