History

bump up to 300000 voxels, with color

Stephen Birarda committed 13 years ago

Commit f0044791 parent f27af9c3 files · +44 −22Browse files

Showing the changes to interface/src/VoxelSystem.cpp (1 of 3 files). Show all

Unified · Split

interface/src/VoxelSystem.cpp+42−21
@@ -9,9 +9,10 @@ · 8 unmodified lines
99 #include "VoxelSystem.h"
1010
1111 const float MAX_UNIT_ANY_AXIS = 20.0f;
12−const float CUBE_WIDTH = 0.005f;
12+const float CUBE_WIDTH = 0.01f;
1313 const int VERTICES_PER_VOXEL = 8;
1414 const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
15+const int COLOR_VALUES_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
1516 const int INDICES_PER_VOXEL = 3 * 12;
1617
1718 GLfloat identityVertices[] = { -1, -1, 1,
@@ -40,6 +41,10 @@ void VoxelSystem::init() { · 22 unmodified lines
4041 root = new Voxel;
4142 }
4243
44+float randomFloat(float maximumValue) {
45+ return ((float) rand() / ((float) RAND_MAX / maximumValue));
46+}
47+
4348 void VoxelSystem::init(int numberOfRandomVoxels) {
4449 // create the arrays needed to pass to glDrawElements later
4550 // position / color are random for now
@@ -49,6 +54,9 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 3 unmodified lines
4954 // there are 3 points for each vertices, 24 vertices in each cube
5055 GLfloat *verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels];
5156
57+ // we need a color for each vertex in each voxel
58+ GLfloat *colorsArray = new GLfloat[COLOR_VALUES_PER_VOXEL * numberOfRandomVoxels];
59+
5260 // there are 12 triangles in each cube, with three indices for each triangle
5361 GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels];
5462
@@ -58,17 +66,29 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 3 unmodified lines
5866 for (int n = 0; n < numberOfRandomVoxels; n++) {
5967 // pick a random point for the center of the cube
6068 glm::vec3 position = glm::vec3(
61− ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)),
62− ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)),
63− ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS))
69+ randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS,
70+ randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS,
71+ randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS
6472 );
6573
74+ // fill the vertices array
6675 GLfloat *currentVerticesPos = verticesArray + (n * VERTEX_POINTS_PER_VOXEL);
6776
68− // fill the vertices array
6977 for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) {
7078 currentVerticesPos[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH);
7179 }
80+
81+ // fill the colors array
82+ GLfloat *currentColorPos = colorsArray + (n * COLOR_VALUES_PER_VOXEL);
83+ float voxelR = randomFloat(1);
84+ float voxelG = randomFloat(1);
85+ float voxelB = randomFloat(1);
86+
87+ for (int c = 0; c < VERTICES_PER_VOXEL; c++) {
88+ currentColorPos[0 + (c * 3)] = voxelR;
89+ currentColorPos[1 + (c * 3)] = voxelG;
90+ currentColorPos[2 + (c * 3)] = voxelB;
91+ }
7292
7393 // fill the indices array
7494 int voxelIndexOffset = n * INDICES_PER_VOXEL;
@@ -81,27 +101,25 @@ void VoxelSystem::init(int numberOfRandomVoxels) { · 6 unmodified lines
81101 }
82102 }
83103
84− // generate new VBO for the verticesArray
104+ // VBO for the verticesArray
85105 glGenBuffers(1, &vboVerticesID);
86−
87− // bind VBO in order to use
88106 glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
89−
90− // upload data to VBO
91107 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * numberOfRandomVoxels, verticesArray, GL_STATIC_DRAW);
92108
93− // generate new VBO for the indicesArray
94− glGenBuffers(1, &vboIndicesID);
109+ // VBO for colorsArray
110+ glGenBuffers(1, &vboColorsID);
111+ glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
112+ glBufferData(GL_ARRAY_BUFFER, VERTICES_PER_VOXEL * sizeof(GLfloat) * numberOfRandomVoxels, colorsArray, GL_STATIC_DRAW);
95113
96− // bind VBO in order to use
114+ // VBO for the indicesArray
115+ glGenBuffers(1, &vboIndicesID);
97116 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
98−
99− // upload data to VBO
100117 glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberOfRandomVoxels * INDICES_PER_VOXEL * sizeof(GLuint), indicesArray, GL_STATIC_DRAW);
101118
102119 // delete the verticesArray, indicesArray
103120 delete[] verticesArray;
104121 delete[] indicesArray;
122+ delete[] colorsArray;
105123 }
106124
107125 //
@@ -205,19 +223,22 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { · 97 unmodified lines
205223 }
206224
207225 void VoxelSystem::render() {
208− // bind VBOs for vertices and indices array
209− glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
210− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
211−
212− // draw the cubes
213226 glEnableClientState(GL_VERTEX_ARRAY);
227+ glEnableClientState(GL_COLOR_ARRAY);
228+
229+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
214230
231+ glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
215232 glVertexPointer(3, GL_FLOAT, 0, 0);
216233
234+ glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
235+ glColorPointer(3, GL_FLOAT, 0, 0);
236+
217237 glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, 0);
218238
219− // deactivate vertex arrays after drawing
239+ // deactivate vertex and color arrays after drawing
220240 glDisableClientState(GL_VERTEX_ARRAY);
241+ glDisableClientState(GL_COLOR_ARRAY);
221242
222243 // bind with 0 to switch back to normal operation
223244 glBindBuffer(GL_ARRAY_BUFFER, 0);