History

randomly draw 50000 voxels for glDrawElements test

Stephen Birarda committed 13 years ago

Commit 0fbb5341 parent 4203fdd3 files · +82 −17Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+76−4
@@ -8,6 +8,28 @@ · 7 unmodified lines
88
99 #include "VoxelSystem.h"
1010
11+const float MAX_UNIT_ANY_AXIS = 20.0f;
12+const float CUBE_WIDTH = 0.05f;
13+const int VERTICES_PER_VOXEL = 8;
14+const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
15+const int INDICES_PER_VOXEL = 3 * 12;
16+
17+GLfloat identityVertices[] = { -1, -1, 1,
18+ 1, -1, 1,
19+ 1, -1, -1,
20+ -1, -1, -1,
21+ 1, 1, 1,
22+ -1, 1, 1,
23+ -1, 1, -1,
24+ 1, 1, -1 };
25+
26+GLubyte identityIndices[] = { 0,1,2, 0,2,3,
27+ 0,4,1, 0,4,5,
28+ 0,3,6, 0,5,6,
29+ 1,2,4, 2,4,7,
30+ 2,3,6, 2,6,7,
31+ 4,5,6, 4,6,7 };
32+
1133
1234 bool onSphereShell(float radius, float scale, glm::vec3 * position) {
1335 float vRadius = glm::length(*position);
@@ -18,6 +40,49 @@ void VoxelSystem::init() { · 4 unmodified lines
1840 root = new Voxel;
1941 }
2042
43+void VoxelSystem::init(int numberOfRandomVoxels) {
44+ // create the arrays needed to pass to glDrawElements later
45+ // position / color are random for now
46+
47+ voxelsRendered = numberOfRandomVoxels;
48+
49+ // there are 3 points for each vertices, 24 vertices in each cube
50+ verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels];
51+
52+ // there are 12 triangles in each cube, with three indices for each triangle
53+ indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels];
54+
55+ // new seed based on time now so voxels are different each time
56+ srand((unsigned)time(0));
57+
58+ for (int n = 0; n < numberOfRandomVoxels; n++) {
59+ // pick a random point for the center of the cube
60+ 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))
64+ );
65+
66+ GLfloat *currentVerticesPos = verticesArray + (n * VERTEX_POINTS_PER_VOXEL);
67+
68+ // fill the vertices array
69+ for (int v = 0; v < VERTEX_POINTS_PER_VOXEL; v++) {
70+ currentVerticesPos[v] = position[v % 3] + (identityVertices[v] * CUBE_WIDTH);
71+ }
72+
73+ // fill the indices array
74+ int voxelIndexOffset = n * INDICES_PER_VOXEL;
75+ GLuint *currentIndicesPos = indicesArray + voxelIndexOffset;
76+ int startIndex = (n * VERTICES_PER_VOXEL);
77+
78+ for (int i = 0; i < INDICES_PER_VOXEL; i++) {
79+ // add indices for this side of the cube
80+ currentIndicesPos[i] = startIndex + identityIndices[i];
81+ }
82+ }
83+
84+}
85+
2186 //
2287 // Recursively initialize the voxel tree
2388 //
@@ -118,11 +183,18 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { · 94 unmodified lines
118183 return vRendered;
119184 }
120185
121−void VoxelSystem::simulate(float deltaTime) {
186+void VoxelSystem::render() {
187+ glEnableClientState(GL_VERTEX_ARRAY);
188+ glVertexPointer(3, GL_FLOAT, 0, verticesArray);
189+
190+ // draw a cube
191+ glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, indicesArray);
122192
193+ // deactivate vertex arrays after drawing
194+ glDisableClientState(GL_VERTEX_ARRAY);
123195 }
124196
125−
126−
127−
197+void VoxelSystem::simulate(float deltaTime) {
198+
199+}
128200