-
Notifications
You must be signed in to change notification settings - Fork 2
/
GLvbo.cpp
192 lines (173 loc) · 4.68 KB
/
GLvbo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright © 2008-2012 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#include <cassert>
#include "GLvbo.h"
#include "utils.h"
// Include GLM
#include "glm/glm.hpp"
//#define UINT_MAX (unsigned int (-1))
CGLvbo::CGLvbo(const int nElements, const void *pVertexBuf, const void *pNormalBuf, const void *pUVBuf, GLenum nUsage)
{
assert(nullptr!=pVertexBuf);
assert(0<nElements);
assert( nUsage>=0x88E0 && nUsage<=0x88EA );
mVAO = UINT_MAX;
mVertObjId = UINT_MAX;
mNormObjId = UINT_MAX;
mUVObjId = UINT_MAX;
Init(nElements, pVertexBuf, pNormalBuf, pUVBuf, nUsage);
}
CGLvbo::~CGLvbo()
{
Cleanup();
}
// private
void CGLvbo::Init(const int nElements, const void *pVertexBuf, const void *pNormalBuf, const void *pUVBuf, GLenum nUsage)
{
assert(nullptr!=pVertexBuf);
assert(0<nElements);
assert( nUsage>=0x88E0 && nUsage<=0x88EA );
Cleanup();
checkGLError();
glGenVertexArrays(1, &mVAO);
glBindVertexArray(mVAO);
// the vertex buffer is NOT optional
glGenBuffers(1, &mVertObjId);
glBindBuffer(GL_ARRAY_BUFFER, mVertObjId);
glBufferData(GL_ARRAY_BUFFER, nElements * sizeof(glm::vec3), pVertexBuf, nUsage);
checkGLError();
if(pNormalBuf) {
glGenBuffers(1, &mNormObjId);
glBindBuffer(GL_ARRAY_BUFFER, mNormObjId);
glBufferData(GL_ARRAY_BUFFER, nElements * sizeof(glm::vec3), pNormalBuf, nUsage);
checkGLError();
}
if(pUVBuf) {
glGenBuffers(1, &mUVObjId);
glBindBuffer(GL_ARRAY_BUFFER, mUVObjId);
glBufferData(GL_ARRAY_BUFFER, nElements * sizeof(glm::vec2), pUVBuf, nUsage);
checkGLError();
}
// LOG_GLERROR();
}
void CGLvbo::Cleanup()
{
checkGLError();
if(mVertObjId != UINT_MAX)
{
const GLboolean glbIsBuffer = glIsBuffer(mVertObjId);
if(glbIsBuffer==GL_TRUE) {
glDeleteBuffers(1, &mVertObjId);
mVertObjId = UINT_MAX;
}
checkGLError();
}
if(mNormObjId != UINT_MAX)
{
const GLboolean glbIsBuffer = glIsBuffer(mNormObjId);
if(glbIsBuffer==GL_TRUE) {
glDeleteBuffers(1, &mNormObjId);
mNormObjId = UINT_MAX;
}
checkGLError();
}
if(mUVObjId != UINT_MAX)
{
const GLboolean glbIsBuffer = glIsBuffer(mUVObjId);
if(glbIsBuffer==GL_TRUE) {
glDeleteBuffers(1, &mUVObjId);
mUVObjId = UINT_MAX;
}
checkGLError();
}
if(mVAO != UINT_MAX)
{
const GLboolean glbIsVA = glIsVertexArray(mVAO);
if(glbIsVA==GL_TRUE) {
glDeleteVertexArrays(1, &mVAO);
mVAO = UINT_MAX;
}
checkGLError();
}
}
void CGLvbo::Bind() const
{
glBindVertexArray(mVAO);
checkGLError();
GLuint attributeIndex = 0;
if(mVertObjId < UINT_MAX)
{
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(attributeIndex);
checkGLError();
glBindBuffer(GL_ARRAY_BUFFER, mVertObjId);
checkGLError();
glVertexAttribPointer(
attributeIndex, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
nullptr // array buffer offset
);
checkGLError();
++attributeIndex;
}
if(mNormObjId < UINT_MAX)
{
// 2nd attribute buffer : normals
glEnableVertexAttribArray(attributeIndex);
checkGLError();
glBindBuffer(GL_ARRAY_BUFFER, mNormObjId);
checkGLError();
glVertexAttribPointer(
attributeIndex, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
nullptr // array buffer offset
);
checkGLError();
++attributeIndex;
}
if(mUVObjId < UINT_MAX)
{
// 3rd attribute buffer : UVs
glEnableVertexAttribArray(attributeIndex);
checkGLError();
glBindBuffer(GL_ARRAY_BUFFER, mUVObjId);
checkGLError();
glVertexAttribPointer(
attributeIndex, // attribute
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
nullptr // array buffer offset
);
checkGLError();
++attributeIndex;
}
}
void CGLvbo::Release() const
{
GLuint attributeIndex = 0;
if(mVertObjId != UINT_MAX) {
glDisableVertexAttribArray(attributeIndex);
checkGLError();
++attributeIndex;
}
if(mNormObjId != UINT_MAX) {
glDisableVertexAttribArray(attributeIndex);
checkGLError();
++attributeIndex;
}
if(mUVObjId != UINT_MAX) {
glDisableVertexAttribArray(attributeIndex);
checkGLError();
++attributeIndex;
}
glBindVertexArray(0);
checkGLError();
}