Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfVertexArrayObject.cpp
1 
2 #include "glfVertexArrayObject.hh"
3 #include "glfException.hh"
4 
5 #ifdef GL_VERSION_3_0
6 
7 using namespace BIAS;
8 using namespace std;
9 
10 glfVertexArrayObject::
11 glfVertexArrayObject() :
12 vertexArrayObjID_(0), boundRevertably_(false), previousBinding_(0)
13 {}
14 
15 glfVertexArrayObject::~glfVertexArrayObject()
16 {
17  glDeleteVertexArrays(1, &vertexArrayObjID_);
18  GLF_THROW_ON_OPENGL_ERROR_DEBUG;
19 }
20 
21  /*
22  \attention Call this after context is available and before any other member function !
23  */
24 void glfVertexArrayObject::
25 Create()
26 {
27 #ifdef BIAS_DEBUG
28  if(GLEW_VERSION_3_0 != GLEW_OK) GLF_THROW_EXCEPTION("glfVertexArrayObject is only functional with OpenGL 3.0 up!");
29 #endif
30  if(vertexArrayObjID_==0)
31  glGenVertexArrays(1, &vertexArrayObjID_);
32  GLF_THROW_ON_OPENGL_ERROR_DEBUG;
33 }
34 
35 void glfVertexArrayObject::
36 Bind()
37 {
38  BIASASSERT(vertexArrayObjID_!=0);
39  glBindVertexArray(vertexArrayObjID_);
40  boundRevertably_ = false;
41  GLF_THROW_ON_OPENGL_ERROR_DEBUG;
42 }
43 
44 
45 void glfVertexArrayObject::
46 BindRevertable()
47 {
48  BIASASSERT(vertexArrayObjID_!=0);
49  glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &previousBinding_);
50  boundRevertably_ = true;
51  glBindVertexArray(vertexArrayObjID_);
52  GLF_THROW_ON_OPENGL_ERROR_DEBUG;
53 
54 }
55 
56 void glfVertexArrayObject::
57 Revert()
58 {
59  if(boundRevertably_) {
60  glBindVertexArray(previousBinding_);
61  boundRevertably_ = false;
62  GLF_THROW_ON_OPENGL_ERROR_DEBUG;
63  } else {
64  BIASWARN("Unable to revert binding! Use BindRevertable() without subsequent call to Bind()!");
65  }
66 
67 }
68 
69 void glfVertexArrayObject::
70 UploadData(GLuint slotIndex,
71  GLint numValuesPerVertex,
72  float* data, unsigned int numValues,
73  GLenum usageHint)
74 {
75  BIASASSERT(vertexArrayObjID_!=0);
76  BIASASSERT(numValuesPerVertex>=1 && numValuesPerVertex<=4);
77  map<GLuint, glfArrayBufferObject>::iterator it = internalArrayBufferObjects_.find(slotIndex);
78  if(it != internalArrayBufferObjects_.end()) {
79  it->second.UploadData(data, numValues, usageHint);
80 
81  } else {
82  internalArrayBufferObjects_[slotIndex].Create();
83  internalArrayBufferObjects_[slotIndex].UploadData(data, numValues*numValuesPerVertex, usageHint);
84 
85  }
86 
87  BindRevertable();
88  internalArrayBufferObjects_[slotIndex].Bind(); //BindRevertable() ?
89  glVertexAttribPointer(slotIndex, numValuesPerVertex, GL_FLOAT, GL_FALSE, 0, 0);
90 
91  glEnableVertexAttribArray(slotIndex);
92  Revert();
93  // internalArrayBufferObjects_[slotIndex].Revert(); // ??
94 
95 }
96 
97 
98 glfVertexArrayObject& glfVertexArrayObject::
99 operator=(const glfVertexArrayObject& b)
100 { return *this; }
101 
102 #endif