diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index f93b667869..a46591d50f 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -9,9 +9,10 @@ #include "VoxelSystem.h" const float MAX_UNIT_ANY_AXIS = 20.0f; -const float CUBE_WIDTH = 0.005f; +const float CUBE_WIDTH = 0.01f; const int VERTICES_PER_VOXEL = 8; const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; +const int COLOR_VALUES_PER_VOXEL = 3 * VERTICES_PER_VOXEL; const int INDICES_PER_VOXEL = 3 * 12; GLfloat identityVertices[] = { -1, -1, 1, @@ -40,6 +41,10 @@ void VoxelSystem::init() { root = new Voxel; } +float randomFloat(float maximumValue) { + return ((float) rand() / ((float) RAND_MAX / maximumValue)); +} + void VoxelSystem::init(int numberOfRandomVoxels) { // create the arrays needed to pass to glDrawElements later // position / color are random for now @@ -49,6 +54,9 @@ void VoxelSystem::init(int numberOfRandomVoxels) { // there are 3 points for each vertices, 24 vertices in each cube GLfloat *verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels]; + // we need a color for each vertex in each voxel + GLfloat *colorsArray = new GLfloat[COLOR_VALUES_PER_VOXEL * numberOfRandomVoxels]; + // there are 12 triangles in each cube, with three indices for each triangle GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels]; @@ -58,17 +66,29 @@ void VoxelSystem::init(int numberOfRandomVoxels) { for (int n = 0; n < numberOfRandomVoxels; n++) { // pick a random point for the center of the cube glm::vec3 position = glm::vec3( - ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)), - ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)), - ((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)) + randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS, + randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS, + randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS ); + // fill the vertices array GLfloat *currentVerticesPos = verticesArray + (n * VERTEX_POINTS_PER_VOXEL); - // fill the vertices array for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) { currentVerticesPos[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH); } + + // fill the colors array + GLfloat *currentColorPos = colorsArray + (n * COLOR_VALUES_PER_VOXEL); + float voxelR = randomFloat(1); + float voxelG = randomFloat(1); + float voxelB = randomFloat(1); + + for (int c = 0; c < VERTICES_PER_VOXEL; c++) { + currentColorPos[0 + (c * 3)] = voxelR; + currentColorPos[1 + (c * 3)] = voxelG; + currentColorPos[2 + (c * 3)] = voxelB; + } // fill the indices array int voxelIndexOffset = n * INDICES_PER_VOXEL; @@ -81,27 +101,25 @@ void VoxelSystem::init(int numberOfRandomVoxels) { } } - // generate new VBO for the verticesArray + // VBO for the verticesArray glGenBuffers(1, &vboVerticesID); - - // bind VBO in order to use glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); - - // upload data to VBO glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * numberOfRandomVoxels, verticesArray, GL_STATIC_DRAW); - // generate new VBO for the indicesArray + // VBO for colorsArray + glGenBuffers(1, &vboColorsID); + glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); + glBufferData(GL_ARRAY_BUFFER, VERTICES_PER_VOXEL * sizeof(GLfloat) * numberOfRandomVoxels, colorsArray, GL_STATIC_DRAW); + + // VBO for the indicesArray glGenBuffers(1, &vboIndicesID); - - // bind VBO in order to use glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); - - // upload data to VBO glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberOfRandomVoxels * INDICES_PER_VOXEL * sizeof(GLuint), indicesArray, GL_STATIC_DRAW); // delete the verticesArray, indicesArray delete[] verticesArray; delete[] indicesArray; + delete[] colorsArray; } // @@ -205,19 +223,22 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { } void VoxelSystem::render() { - // bind VBOs for vertices and indices array - glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); - // draw the cubes - glEnableClientState(GL_VERTEX_ARRAY); - + glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); glVertexPointer(3, GL_FLOAT, 0, 0); + glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); + glColorPointer(3, GL_FLOAT, 0, 0); + glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, 0); - // deactivate vertex arrays after drawing + // deactivate vertex and color arrays after drawing glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); // bind with 0 to switch back to normal operation glBindBuffer(GL_ARRAY_BUFFER, 0); diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index dc51164318..d3a7cde77e 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -36,6 +36,7 @@ public: private: int voxelsRendered; GLuint vboVerticesID; + GLuint vboColorsID; GLuint vboIndicesID; }; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 4dee7872ff..2bc99d8dad 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -319,7 +319,7 @@ void initDisplay(void) void init(void) { - voxels.init(200000); + voxels.init(300000); myHead.setRenderYaw(start_yaw);