randomly draw 50000 voxels for glDrawElements test

This commit is contained in:
Stephen Birarda 2013-03-06 12:04:27 -08:00
parent 4203fddb88
commit 0fbb534566
3 changed files with 82 additions and 17 deletions

View file

@ -8,6 +8,28 @@
#include "VoxelSystem.h"
const float MAX_UNIT_ANY_AXIS = 20.0f;
const float CUBE_WIDTH = 0.05f;
const int VERTICES_PER_VOXEL = 8;
const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
const int INDICES_PER_VOXEL = 3 * 12;
GLfloat identityVertices[] = { -1, -1, 1,
1, -1, 1,
1, -1, -1,
-1, -1, -1,
1, 1, 1,
-1, 1, 1,
-1, 1, -1,
1, 1, -1 };
GLubyte identityIndices[] = { 0,1,2, 0,2,3,
0,4,1, 0,4,5,
0,3,6, 0,5,6,
1,2,4, 2,4,7,
2,3,6, 2,6,7,
4,5,6, 4,6,7 };
bool onSphereShell(float radius, float scale, glm::vec3 * position) {
float vRadius = glm::length(*position);
@ -18,6 +40,49 @@ void VoxelSystem::init() {
root = new Voxel;
}
void VoxelSystem::init(int numberOfRandomVoxels) {
// create the arrays needed to pass to glDrawElements later
// position / color are random for now
voxelsRendered = numberOfRandomVoxels;
// there are 3 points for each vertices, 24 vertices in each cube
verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels];
// there are 12 triangles in each cube, with three indices for each triangle
indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels];
// new seed based on time now so voxels are different each time
srand((unsigned)time(0));
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))
);
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 indices array
int voxelIndexOffset = n * INDICES_PER_VOXEL;
GLuint *currentIndicesPos = indicesArray + voxelIndexOffset;
int startIndex = (n * VERTICES_PER_VOXEL);
for (int i = 0; i < INDICES_PER_VOXEL; i++) {
// add indices for this side of the cube
currentIndicesPos[i] = startIndex + identityIndices[i];
}
}
}
//
// Recursively initialize the voxel tree
//
@ -118,11 +183,18 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) {
return vRendered;
}
void VoxelSystem::render() {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, verticesArray);
// draw a cube
glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, indicesArray);
// deactivate vertex arrays after drawing
glDisableClientState(GL_VERTEX_ARRAY);
}
void VoxelSystem::simulate(float deltaTime) {
}

View file

@ -26,13 +26,17 @@ class VoxelSystem {
public:
void simulate(float deltaTime);
int render(Voxel * voxel, float scale, glm::vec3 * distance);
void render();
void init();
void init(int numberOfRandomVoxels);
int initVoxels(Voxel * root, float scale, glm::vec3 * position);
void setVoxelsRendered(int v) {voxelsRendered = v;};
int getVoxelsRendered() {return voxelsRendered;};
Voxel * root;
private:
int voxelsRendered;
GLfloat *verticesArray;
GLuint *indicesArray;
};
#endif

View file

@ -319,10 +319,7 @@ void initDisplay(void)
void init(void)
{
voxels.init();
glm::vec3 position(0,0,0);
int voxelsMade = voxels.initVoxels(NULL, 10.0, &position);
std::cout << voxelsMade << " voxels made. \n";
voxels.init(50000);
myHead.setRenderYaw(start_yaw);
@ -575,15 +572,7 @@ void display(void)
if (!display_head) cloud.render();
// Draw voxels
glPushMatrix();
glTranslatef(WORLD_SIZE/2.0, WORLD_SIZE/2.0, WORLD_SIZE/2.0);
glm::vec3 distance(5.0 + location[0], 5.0 + location[1], 5.0 + location[2]);
//std::cout << "length: " << glm::length(distance) << "\n";
int voxelsRendered = voxels.render(NULL, 10.0, &distance);
voxels.setVoxelsRendered(voxelsRendered);
//glColor4f(0,0,1,0.5);
//glutSolidCube(10.0);
glPopMatrix();
voxels.render();
// Draw field vectors
if (display_field) field.render();