bump up to 300000 voxels, with color

This commit is contained in:
Stephen Birarda 2013-03-06 14:34:14 -08:00
parent f27af9c903
commit f004479e39
3 changed files with 44 additions and 22 deletions

View file

@ -9,9 +9,10 @@
#include "VoxelSystem.h" #include "VoxelSystem.h"
const float MAX_UNIT_ANY_AXIS = 20.0f; 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 VERTICES_PER_VOXEL = 8;
const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; 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; const int INDICES_PER_VOXEL = 3 * 12;
GLfloat identityVertices[] = { -1, -1, 1, GLfloat identityVertices[] = { -1, -1, 1,
@ -40,6 +41,10 @@ void VoxelSystem::init() {
root = new Voxel; root = new Voxel;
} }
float randomFloat(float maximumValue) {
return ((float) rand() / ((float) RAND_MAX / maximumValue));
}
void VoxelSystem::init(int numberOfRandomVoxels) { void VoxelSystem::init(int numberOfRandomVoxels) {
// create the arrays needed to pass to glDrawElements later // create the arrays needed to pass to glDrawElements later
// position / color are random for now // 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 // there are 3 points for each vertices, 24 vertices in each cube
GLfloat *verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels]; 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 // there are 12 triangles in each cube, with three indices for each triangle
GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels]; GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels];
@ -58,17 +66,29 @@ void VoxelSystem::init(int numberOfRandomVoxels) {
for (int n = 0; n < numberOfRandomVoxels; n++) { for (int n = 0; n < numberOfRandomVoxels; n++) {
// pick a random point for the center of the cube // pick a random point for the center of the cube
glm::vec3 position = glm::vec3( glm::vec3 position = glm::vec3(
((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)), randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS,
((float) rand() / ((float) RAND_MAX / MAX_UNIT_ANY_AXIS)), randomFloat(MAX_UNIT_ANY_AXIS * 2) - MAX_UNIT_ANY_AXIS,
((float) rand() / ((float) RAND_MAX / 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); GLfloat *currentVerticesPos = verticesArray + (n * VERTEX_POINTS_PER_VOXEL);
// fill the vertices array
for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) { for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) {
currentVerticesPos[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH); 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 // fill the indices array
int voxelIndexOffset = n * INDICES_PER_VOXEL; 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); glGenBuffers(1, &vboVerticesID);
// bind VBO in order to use
glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
// upload data to VBO
glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * numberOfRandomVoxels, verticesArray, GL_STATIC_DRAW); 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); glGenBuffers(1, &vboIndicesID);
// bind VBO in order to use
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); 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); glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberOfRandomVoxels * INDICES_PER_VOXEL * sizeof(GLuint), indicesArray, GL_STATIC_DRAW);
// delete the verticesArray, indicesArray // delete the verticesArray, indicesArray
delete[] verticesArray; delete[] verticesArray;
delete[] indicesArray; delete[] indicesArray;
delete[] colorsArray;
} }
// //
@ -205,19 +223,22 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) {
} }
void VoxelSystem::render() { void VoxelSystem::render() {
// bind VBOs for vertices and indices array glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
// draw the cubes glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0); 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); 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_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
// bind with 0 to switch back to normal operation // bind with 0 to switch back to normal operation
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);

View file

@ -36,6 +36,7 @@ public:
private: private:
int voxelsRendered; int voxelsRendered;
GLuint vboVerticesID; GLuint vboVerticesID;
GLuint vboColorsID;
GLuint vboIndicesID; GLuint vboIndicesID;
}; };

View file

@ -319,7 +319,7 @@ void initDisplay(void)
void init(void) void init(void)
{ {
voxels.init(200000); voxels.init(300000);
myHead.setRenderYaw(start_yaw); myHead.setRenderYaw(start_yaw);