mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 15:09:24 +02:00
randomly draw 50000 voxels for glDrawElements test
This commit is contained in:
parent
4203fddb88
commit
0fbb534566
3 changed files with 82 additions and 17 deletions
|
@ -8,6 +8,28 @@
|
||||||
|
|
||||||
#include "VoxelSystem.h"
|
#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) {
|
bool onSphereShell(float radius, float scale, glm::vec3 * position) {
|
||||||
float vRadius = glm::length(*position);
|
float vRadius = glm::length(*position);
|
||||||
|
@ -18,6 +40,49 @@ void VoxelSystem::init() {
|
||||||
root = new Voxel;
|
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
|
// Recursively initialize the voxel tree
|
||||||
//
|
//
|
||||||
|
@ -118,11 +183,18 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) {
|
||||||
return vRendered;
|
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) {
|
void VoxelSystem::simulate(float deltaTime) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,13 +26,17 @@ class VoxelSystem {
|
||||||
public:
|
public:
|
||||||
void simulate(float deltaTime);
|
void simulate(float deltaTime);
|
||||||
int render(Voxel * voxel, float scale, glm::vec3 * distance);
|
int render(Voxel * voxel, float scale, glm::vec3 * distance);
|
||||||
|
void render();
|
||||||
void init();
|
void init();
|
||||||
|
void init(int numberOfRandomVoxels);
|
||||||
int initVoxels(Voxel * root, float scale, glm::vec3 * position);
|
int initVoxels(Voxel * root, float scale, glm::vec3 * position);
|
||||||
void setVoxelsRendered(int v) {voxelsRendered = v;};
|
void setVoxelsRendered(int v) {voxelsRendered = v;};
|
||||||
int getVoxelsRendered() {return voxelsRendered;};
|
int getVoxelsRendered() {return voxelsRendered;};
|
||||||
Voxel * root;
|
Voxel * root;
|
||||||
private:
|
private:
|
||||||
int voxelsRendered;
|
int voxelsRendered;
|
||||||
|
GLfloat *verticesArray;
|
||||||
|
GLuint *indicesArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -319,10 +319,7 @@ void initDisplay(void)
|
||||||
|
|
||||||
void init(void)
|
void init(void)
|
||||||
{
|
{
|
||||||
voxels.init();
|
voxels.init(50000);
|
||||||
glm::vec3 position(0,0,0);
|
|
||||||
int voxelsMade = voxels.initVoxels(NULL, 10.0, &position);
|
|
||||||
std::cout << voxelsMade << " voxels made. \n";
|
|
||||||
|
|
||||||
myHead.setRenderYaw(start_yaw);
|
myHead.setRenderYaw(start_yaw);
|
||||||
|
|
||||||
|
@ -575,15 +572,7 @@ void display(void)
|
||||||
if (!display_head) cloud.render();
|
if (!display_head) cloud.render();
|
||||||
|
|
||||||
// Draw voxels
|
// Draw voxels
|
||||||
glPushMatrix();
|
voxels.render();
|
||||||
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();
|
|
||||||
|
|
||||||
// Draw field vectors
|
// Draw field vectors
|
||||||
if (display_field) field.render();
|
if (display_field) field.render();
|
||||||
|
|
Loading…
Reference in a new issue