diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index d654f71a5e..a46591d50f 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -8,6 +8,29 @@ #include "VoxelSystem.h" +const float MAX_UNIT_ANY_AXIS = 20.0f; +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, + 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 +41,87 @@ 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 + + voxelsRendered = 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]; + + // 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( + 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); + + 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; + 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]; + } + } + + // VBO for the verticesArray + glGenBuffers(1, &vboVerticesID); + glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); + glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * numberOfRandomVoxels, verticesArray, GL_STATIC_DRAW); + + // 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); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); + 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; +} + // // Recursively initialize the voxel tree // @@ -118,11 +222,30 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { return vRendered; } +void VoxelSystem::render() { + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID); + + 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 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); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} + void VoxelSystem::simulate(float deltaTime) { } - - - - diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index 8dc3e97c8c..d3a7cde77e 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -26,13 +26,18 @@ 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; + GLuint vboVerticesID; + GLuint vboColorsID; + GLuint vboIndicesID; }; #endif diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 30e75321dd..ec2be98786 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(300000); myHead.setRenderYaw(start_yaw); @@ -577,15 +574,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(); diff --git a/mixer/src/main.cpp b/mixer/src/main.cpp index dd6ad8c974..2d1ac025b5 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) {