VTK
vtkglBufferObject.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 =========================================================================*/
14 #ifndef vtkglBufferObject_h
15 #define vtkglBufferObject_h
16 
17 #include "vtkRenderingOpenGL2Module.h"
18 #include "vtkStdString.h" // for std::string
19 
20 namespace vtkgl {
21 
29 class VTKRENDERINGOPENGL2_EXPORT BufferObject
30 {
31 public:
32  enum ObjectType {
34  ElementArrayBuffer
35  };
36 
37  BufferObject(ObjectType type = ArrayBuffer);
38  ~BufferObject();
39 
41  ObjectType GetType() const;
42 
44  int GetHandle() const;
45 
47  bool IsReady() const { return Dirty == false; }
48 
58  template <class T>
59  bool Upload(const T &array, ObjectType type);
60 
61  // non vector version
62  template <class T>
63  bool Upload(const T *array, size_t numElements, ObjectType type);
64 
70  bool Bind();
71 
75  bool Release();
76 
77 
78  // Description:
79  // Release any graphics resources that are being consumed by this class.
80  void ReleaseGraphicsResources();
81 
85  std::string GetError() const { return Error; }
86 
87 private:
88  bool UploadInternal(const void *buffer, size_t size, ObjectType objectType);
89 
90  struct Private;
91  Private *d;
92 
93  bool Dirty;
94  std::string Error;
95 };
96 
97 template <class T>
98 inline bool BufferObject::Upload(const T &array,
99  BufferObject::ObjectType objectType)
100 {
101  if (array.empty())
102  {
103  this->Error = "Refusing to upload empty array.";
104  return false;
105  }
106  return this->UploadInternal(&array[0],
107  array.size() * sizeof(typename T::value_type),
108  objectType);
109 }
110 
111 template <class T>
112 inline bool BufferObject::Upload(const T *array, size_t numElements,
113  BufferObject::ObjectType objectType)
114 {
115  if (!array)
116  {
117  this->Error = "Refusing to upload empty array.";
118  return false;
119  }
120  return this->UploadInternal(array,
121  numElements * sizeof(T),
122  objectType);
123 }
124 
125 }
126 
127 #endif
128 
129 // VTK-HeaderTest-Exclude: vtkglBufferObject.h
std::string GetError() const
Return a string describing errors.
GLsizeiptr size
Definition: vtkgl.h:11843
OpenGL buffer object.
GLuint GLuint GLsizei GLenum type
Definition: vtkgl.h:11315
GLuint buffer
Definition: vtkgl.h:11839
bool Upload(const T &array, ObjectType type)
Upload data to the buffer object.
Definition: vtkgl.h:11267
bool IsReady() const
Determine if the buffer object is ready to be used.
GLsizei const GLchar ** string
Definition: vtkgl.h:12011