forked from amundhov/legendary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvboobject.cpp
135 lines (112 loc) · 4.05 KB
/
vboobject.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
#include <cstdlib>
#include <fstream>
#include <GL/glew.h>
#include "msg.h"
#include "vboobject.h"
VboObject::VboObject()
{
loc.x = 0.0;
loc.y = 0.0;
loc.z = 0.0;
reference_count = 0;
VBO = 0;
IBO = 0;
VBO_size_vertices = 0;
VBO_size_normals = 0;
VBO_size_colours = 0;
VBO_size_coords = 0;
IBO_size_indices = 0;
IBO_indices = 0;
INDEX_SIZE = GL_UNSIGNED_SHORT;
reference_count++;
}
VboObject::~VboObject() {
if (!--reference_count)
freeBO();
}
void VboObject::locate(float x, float y, float z) {
loc.x = x;
loc.y = y;
loc.z = z;
}
void VboObject::genBO() {
vec3 *vertices = getVertices();
unsigned short int *indices = getIndices();
unsigned char *colours = getColours();
float *coords = getCoords();
vec3 *normals = getNormals();
LOG("Vert size: " << VBO_size_vertices);
LOG("Color size:" << VBO_size_colours);
LOG("coord size:" << VBO_size_coords);
LOG("indices:" << IBO_indices);
const int VBO_total_size = VBO_size_colours + VBO_size_coords + VBO_size_normals + VBO_size_vertices;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, VBO_total_size, vertices, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, VBO_size_vertices, vertices);
if ( normals != NULL ) glBufferSubData(GL_ARRAY_BUFFER, VBO_size_vertices, VBO_size_normals, normals);
if ( colours != NULL ) glBufferSubData(GL_ARRAY_BUFFER, VBO_size_vertices+VBO_size_normals, VBO_size_colours, colours);
if ( coords != NULL ) glBufferSubData(GL_ARRAY_BUFFER, VBO_size_vertices+VBO_size_normals+VBO_size_colours, VBO_size_coords, coords);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, IBO_size_indices, indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
if ( vertices != NULL ) delete[] vertices;
if ( indices != NULL ) delete[] indices;
if ( colours != NULL ) delete[] colours;
if ( coords != NULL ) delete[] coords;
if ( normals != NULL ) delete[] normals;
vertices = NULL;
indices = NULL;
colours = NULL;
coords = NULL;
normals = NULL;
LOG("Generated the Buffer Objects.");
LOG("VBO: " << VBO);
LOG("IBO: " << IBO);
}
void VboObject::freeBO() {
if (VBO) glDeleteBuffers(1, &VBO);
if (IBO) glDeleteBuffers(1, &IBO);
}
void VboObject::draw() {
static float i=0;
i+=1.00;
// Set an identity matrix
const GLfloat transform[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
loc.x, loc.y, loc.z, 1
};
glLoadMatrixf(transform);
// Bind our buffers
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
// Enable shit
glEnableClientState(GL_VERTEX_ARRAY);
if (VBO_size_normals) glEnableClientState(GL_NORMAL_ARRAY);
if (VBO_size_colours) glEnableClientState(GL_COLOR_ARRAY);
if (VBO_size_coords) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Rotate to base
glRotatef(i, 1, 1, 1);
drawElements();
// Return to previous state
glDisableClientState(GL_VERTEX_ARRAY);
if (VBO_size_normals) glDisableClientState(GL_NORMAL_ARRAY);
if (VBO_size_colours) glDisableClientState(GL_COLOR_ARRAY);
if (VBO_size_coords) glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
/* Default implementation draws all vertices in one batch */
void VboObject::drawElements() {
// Point to our vertices
glVertexPointer(3, GL_FLOAT, 0, 0);
if (VBO_size_normals) glNormalPointer(GL_FLOAT,0,(GLvoid*)VBO_size_vertices);
if (VBO_size_colours) glColorPointer(3, GL_UNSIGNED_BYTE, 0, (GLvoid *)(VBO_size_vertices+VBO_size_normals));
if (VBO_size_coords) glTexCoordPointer(2, GL_FLOAT, 0, (GLvoid *)(VBO_size_vertices+VBO_size_normals+VBO_size_colours));
glDrawElements(GL_TRIANGLES, IBO_indices, INDEX_SIZE, 0);
}
unsigned int VboObject::reference_count = 0;