From 0fbb5345666429b008ec8d3b169b63dbbe9aaab7 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 6 Mar 2013 12:04:27 -0800 Subject: [PATCH 1/5] randomly draw 50000 voxels for glDrawElements test --- interface/src/VoxelSystem.cpp | 80 +++++++++++++++++++++++++++++++++-- interface/src/VoxelSystem.h | 4 ++ interface/src/main.cpp | 15 +------ 3 files changed, 82 insertions(+), 17 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index d654f71a5e..c20eb30fbd 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -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) { } - - - - diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index 8dc3e97c8c..43b5ff18e0 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -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 diff --git a/interface/src/main.cpp b/interface/src/main.cpp index e189b5c715..1a3f560089 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -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(); From e16cd4ffc354e628a291441cdeaf7c95ced2ca12 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 6 Mar 2013 13:56:11 -0800 Subject: [PATCH 2/5] use vertex buffer objects to speed up voxel rendering --- interface/src/VoxelSystem.cpp | 44 +++++++++++++++++++++++++++++------ interface/src/VoxelSystem.h | 4 ++-- interface/src/main.cpp | 2 +- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index c20eb30fbd..f93b667869 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -9,7 +9,7 @@ #include "VoxelSystem.h" const float MAX_UNIT_ANY_AXIS = 20.0f; -const float CUBE_WIDTH = 0.05f; +const float CUBE_WIDTH = 0.005f; const int VERTICES_PER_VOXEL = 8; const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; const int INDICES_PER_VOXEL = 3 * 12; @@ -47,10 +47,10 @@ void VoxelSystem::init(int numberOfRandomVoxels) { voxelsRendered = numberOfRandomVoxels; // there are 3 points for each vertices, 24 vertices in each cube - verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * numberOfRandomVoxels]; + GLfloat *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]; + GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * numberOfRandomVoxels]; // new seed based on time now so voxels are different each time srand((unsigned)time(0)); @@ -81,6 +81,27 @@ void VoxelSystem::init(int numberOfRandomVoxels) { } } + // generate new 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 + 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; } // @@ -184,14 +205,23 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { } void VoxelSystem::render() { - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(3, GL_FLOAT, 0, verticesArray); + // bind VBOs for vertices and indices array + glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); - // draw a cube - glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, indicesArray); + // draw the cubes + glEnableClientState(GL_VERTEX_ARRAY); + + glVertexPointer(3, GL_FLOAT, 0, 0); + + glDrawElements(GL_TRIANGLES, 36 * voxelsRendered, GL_UNSIGNED_INT, 0); // deactivate vertex arrays after drawing glDisableClientState(GL_VERTEX_ARRAY); + + // bind with 0 to switch back to normal operation + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } void VoxelSystem::simulate(float deltaTime) { diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index 43b5ff18e0..dc51164318 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -35,8 +35,8 @@ public: Voxel * root; private: int voxelsRendered; - GLfloat *verticesArray; - GLuint *indicesArray; + GLuint vboVerticesID; + GLuint vboIndicesID; }; #endif diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 1a3f560089..4dee7872ff 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -319,7 +319,7 @@ void initDisplay(void) void init(void) { - voxels.init(50000); + voxels.init(200000); myHead.setRenderYaw(start_yaw); From f27af9c9031edb7aacb6409d4fcce7cabc62194f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 6 Mar 2013 14:13:27 -0800 Subject: [PATCH 3/5] use fred's fancy new formula for distance attenuation --- mixer/src/main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mixer/src/main.cpp b/mixer/src/main.cpp index dd6ad8c974..4d47405a2b 100644 --- a/mixer/src/main.cpp +++ b/mixer/src/main.cpp @@ -101,7 +101,7 @@ void *sendBuffer(void *args) powf(agentPosition[1] - otherAgentPosition[1], 2) + powf(agentPosition[2] - otherAgentPosition[2], 2)); - distanceCoeffs[lowAgentIndex][highAgentIndex] = std::max(1.0f, powf((logf(DISTANCE_RATIO * distanceToAgent) / logf(3)), 2)); + distanceCoeffs[lowAgentIndex][highAgentIndex] = std::min(1.0f, powf(0.5, (logf(DISTANCE_RATIO * distanceToAgent) / logf(3)) - 1)); } @@ -149,11 +149,11 @@ void *sendBuffer(void *args) if (s < numSamplesDelay) { // pull the earlier sample for the delayed channel - int earlierSample = delaySamplePointer[s] / distanceCoeffs[lowAgentIndex][highAgentIndex]; + int earlierSample = delaySamplePointer[s] * distanceCoeffs[lowAgentIndex][highAgentIndex]; delayedChannel[s] = earlierSample; } - int16_t currentSample = (otherAgentBuffer->getNextOutput()[s] / distanceCoeffs[lowAgentIndex][highAgentIndex]); + int16_t currentSample = (otherAgentBuffer->getNextOutput()[s] * distanceCoeffs[lowAgentIndex][highAgentIndex]); goodChannel[s] = currentSample; if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { @@ -200,8 +200,8 @@ void *reportAliveToDS(void *args) { gettimeofday(&lastSend, NULL); *output = 'M'; - packSocket(output + 1, 895283510, htons(MIXER_LISTEN_PORT)); -// packSocket(output + 1, 788637888, htons(MIXER_LISTEN_PORT)); +// packSocket(output + 1, 895283510, htons(MIXER_LISTEN_PORT)); + packSocket(output + 1, 788637888, htons(MIXER_LISTEN_PORT)); agentList.getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7); double usecToSleep = 1000000 - (usecTimestampNow() - usecTimestamp(&lastSend)); From f004479e39bf33176c9d65651c898066f07cd360 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 6 Mar 2013 14:34:14 -0800 Subject: [PATCH 4/5] bump up to 300000 voxels, with color --- interface/src/VoxelSystem.cpp | 63 +++++++++++++++++++++++------------ interface/src/VoxelSystem.h | 1 + interface/src/main.cpp | 2 +- 3 files changed, 44 insertions(+), 22 deletions(-) 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); From 160e9f110073f4babdb4301d067ce67e282d8b6c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 6 Mar 2013 14:55:44 -0800 Subject: [PATCH 5/5] mixer needs to report its local address as EC2 box --- mixer/src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mixer/src/main.cpp b/mixer/src/main.cpp index 4d47405a2b..2d1ac025b5 100644 --- a/mixer/src/main.cpp +++ b/mixer/src/main.cpp @@ -200,8 +200,8 @@ void *reportAliveToDS(void *args) { gettimeofday(&lastSend, NULL); *output = 'M'; -// packSocket(output + 1, 895283510, htons(MIXER_LISTEN_PORT)); - packSocket(output + 1, 788637888, htons(MIXER_LISTEN_PORT)); + packSocket(output + 1, 895283510, htons(MIXER_LISTEN_PORT)); +// packSocket(output + 1, 788637888, htons(MIXER_LISTEN_PORT)); agentList.getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7); double usecToSleep = 1000000 - (usecTimestampNow() - usecTimestamp(&lastSend));