Showing the changes to interface/src/VoxelSystem.cpp (1 of 61 files). Show all
Unified · Split
| @@ -14,11 +14,12 @@ · 13 unmodified lines | |||
| 14 | #include <iostream> // to load voxels from file | 14 | #include <iostream> // to load voxels from file |
| 15 | #include <fstream> // to load voxels from file | 15 | #include <fstream> // to load voxels from file |
| 16 | #include <SharedUtil.h> | 16 | #include <SharedUtil.h> |
| 17 | #include <PacketHeaders.h> | ||
| 17 | #include <OctalCode.h> | 18 | #include <OctalCode.h> |
| 18 | #include <pthread.h> | 19 | #include <pthread.h> |
| 19 | #include "VoxelSystem.h" | 20 | #include "VoxelSystem.h" |
| 20 | 21 | ||
| 21 | const int MAX_VOXELS_PER_SYSTEM = 1500000; //250000; | 22 | const int MAX_VOXELS_PER_SYSTEM = 250000; |
| 22 | 23 | ||
| 23 | const int VERTICES_PER_VOXEL = 8; | 24 | const int VERTICES_PER_VOXEL = 8; |
| 24 | const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; | 25 | const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; |
| @@ -86,13 +87,69 @@ void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bo · 61 unmodified lines | |||
| 86 | setupNewVoxelsForDrawing(); | 87 | setupNewVoxelsForDrawing(); |
| 87 | } | 88 | } |
| 88 | 89 | ||
| 90 | long int VoxelSystem::getVoxelsCreated() { | ||
| 91 | return tree->voxelsCreated; | ||
| 92 | } | ||
| 93 | |||
| 94 | long int VoxelSystem::getVoxelsCreatedRunningAverage() { | ||
| 95 | return tree->voxelsCreatedStats.getRunningAverage(); | ||
| 96 | } | ||
| 97 | |||
| 98 | long int VoxelSystem::getVoxelsColored() { | ||
| 99 | return tree->voxelsColored; | ||
| 100 | } | ||
| 101 | |||
| 102 | long int VoxelSystem::getVoxelsColoredRunningAverage() { | ||
| 103 | return tree->voxelsColoredStats.getRunningAverage(); | ||
| 104 | } | ||
| 105 | |||
| 106 | long int VoxelSystem::getVoxelsBytesRead() { | ||
| 107 | return tree->voxelsBytesRead; | ||
| 108 | } | ||
| 109 | |||
| 110 | long int VoxelSystem::getVoxelsBytesReadRunningAverage() { | ||
| 111 | return tree->voxelsBytesReadStats.getRunningAverage(); | ||
| 112 | } | ||
| 113 | |||
| 89 | 114 | ||
| 90 | void VoxelSystem::parseData(void *data, int size) { | 115 | void VoxelSystem::parseData(void *data, int size) { |
| 91 | // output the bits received from the voxel server | 116 | |
| 117 | unsigned char command = *(unsigned char*)data; | ||
| 92 | unsigned char *voxelData = (unsigned char *) data + 1; | 118 | unsigned char *voxelData = (unsigned char *) data + 1; |
| 93 | 119 | ||
| 94 | // ask the VoxelTree to read the bitstream into the tree | 120 | switch(command) { |
| 95 | tree->readBitstreamToTree(voxelData, size - 1); | 121 | case PACKET_HEADER_VOXEL_DATA: |
| 122 | // ask the VoxelTree to read the bitstream into the tree | ||
| 123 | tree->readBitstreamToTree(voxelData, size - 1); | ||
| 124 | break; | ||
| 125 | case PACKET_HEADER_ERASE_VOXEL: | ||
| 126 | // ask the tree to read the "remove" bitstream | ||
| 127 | tree->processRemoveVoxelBitstream((unsigned char*)data,size); | ||
| 128 | break; | ||
| 129 | case PACKET_HEADER_Z_COMMAND: | ||
| 130 | |||
| 131 | // the Z command is a special command that allows the sender to send high level semantic | ||
| 132 | // requests, like erase all, or add sphere scene, different receivers may handle these | ||
| 133 | // messages differently | ||
| 134 | char* packetData =(char*)data; | ||
| 135 | char* command = &packetData[1]; // start of the command | ||
| 136 | int commandLength = strlen(command); // commands are null terminated strings | ||
| 137 | int totalLength = 1+commandLength+1; | ||
| 138 | |||
| 139 | printf("got Z message len(%d)= %s\n",size,command); | ||
| 140 | |||
| 141 | while (totalLength <= size) { | ||
| 142 | if (0==strcmp(command,(char*)"erase all")) { | ||
| 143 | printf("got Z message == erase all\n"); | ||
| 144 | tree->eraseAllVoxels(); | ||
| 145 | } | ||
| 146 | if (0==strcmp(command,(char*)"add scene")) { | ||
| 147 | printf("got Z message == add scene - NOT SUPPORTED ON INTERFACE\n"); | ||
| 148 | } | ||
| 149 | totalLength += commandLength+1; | ||
| 150 | } | ||
| 151 | break; | ||
| 152 | } | ||
| 96 | 153 | ||
| 97 | setupNewVoxelsForDrawing(); | 154 | setupNewVoxelsForDrawing(); |
| 98 | } | 155 | } |
| @@ -128,12 +185,35 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, float nodePosition[3]) { · 29 unmodified lines | |||
| 128 | 185 | ||
| 129 | float halfUnitForVoxel = powf(0.5, *currentNode->octalCode) * (0.5 * TREE_SCALE); | 186 | float halfUnitForVoxel = powf(0.5, *currentNode->octalCode) * (0.5 * TREE_SCALE); |
| 130 | glm::vec3 viewerPosition = viewerHead->getPos(); | 187 | glm::vec3 viewerPosition = viewerHead->getPos(); |
| 188 | |||
| 189 | // XXXBHG - Note: It appears as if the X and Z coordinates of Head or Agent are flip-flopped relative to the | ||
| 190 | // coords of the voxel space. This flip flop causes LOD behavior to be extremely odd. This is my temporary hack | ||
| 191 | // to fix this behavior. To disable this swap, set swapXandZ to false. | ||
| 192 | bool swapXandZ=true; | ||
| 193 | float viewerX = swapXandZ ? viewerPosition[2] : viewerPosition[0]; | ||
| 194 | float viewerZ = swapXandZ ? viewerPosition[0] : viewerPosition[2]; | ||
| 195 | |||
| 196 | // debugging code. | ||
| 197 | //printf("treeToArrays() halfUnitForVoxel=%f\n",halfUnitForVoxel); | ||
| 198 | //printf("treeToArrays() viewerPosition {x,y,z or [0],[1],[2]} ={%f,%f,%f}\n", | ||
| 199 | // viewerPosition[0],viewerPosition[1],viewerPosition[2]); | ||
| 200 | //printf("treeToArrays() nodePosition {x,y,z or [0],[1],[2]} = {%f,%f,%f}\n", | ||
| 201 | // nodePosition[0],nodePosition[1],nodePosition[2]); | ||
| 202 | //float* vertices = firstVertexForCode(currentNode->octalCode); | ||
| 203 | //printf("treeToArrays() firstVerticesForCode(currentNode->octalCode)={x,y,z or [0],[1],[2]} = {%f,%f,%f}\n", | ||
| 204 | // vertices[0],vertices[1],vertices[2]); | ||
| 205 | //delete []vertices; | ||
| 131 | 206 | ||
| 132 | float distanceToVoxelCenter = sqrtf(powf(viewerPosition[0] - nodePosition[0] - halfUnitForVoxel, 2) + | 207 | float distanceToVoxelCenter = sqrtf(powf(viewerX - nodePosition[0] - halfUnitForVoxel, 2) + |
| 133 | powf(viewerPosition[1] - nodePosition[1] - halfUnitForVoxel, 2) + | 208 | powf(viewerPosition[1] - nodePosition[1] - halfUnitForVoxel, 2) + |
| 134 | powf(viewerPosition[2] - nodePosition[2] - halfUnitForVoxel, 2)); | 209 | powf(viewerZ - nodePosition[2] - halfUnitForVoxel, 2)); |
| 135 | 210 | ||
| 136 | if (distanceToVoxelCenter < boundaryDistanceForRenderLevel(*currentNode->octalCode + 1)) { | 211 | int boundaryPosition = boundaryDistanceForRenderLevel(*currentNode->octalCode + 1); |
| 212 | //printf("treeToArrays() distanceToVoxelCenter=%f boundaryPosition=%d\n",distanceToVoxelCenter,boundaryPosition); | ||
| 213 | |||
| 214 | bool alwaysDraw = false; // XXXBHG - temporary debug code. Flip this to true to disable LOD blurring | ||
| 215 | |||
| 216 | if (alwaysDraw || distanceToVoxelCenter < boundaryPosition) { | ||
| 137 | for (int i = 0; i < 8; i++) { | 217 | for (int i = 0; i < 8; i++) { |
| 138 | // check if there is a child here | 218 | // check if there is a child here |
| 139 | if (currentNode->children[i] != NULL) { | 219 | if (currentNode->children[i] != NULL) { |
| @@ -150,8 +230,6 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, float nodePosition[3]) { · 10 unmodified lines | |||
| 150 | childNodePosition[j] -= (powf(0.5, *currentNode->children[i]->octalCode) * TREE_SCALE); | 230 | childNodePosition[j] -= (powf(0.5, *currentNode->children[i]->octalCode) * TREE_SCALE); |
| 151 | } | 231 | } |
| 152 | } | 232 | } |
| 153 | |||
| 154 | |||
| 155 | voxelsAdded += treeToArrays(currentNode->children[i], childNodePosition); | 233 | voxelsAdded += treeToArrays(currentNode->children[i], childNodePosition); |
| 156 | } | 234 | } |
| 157 | } | 235 | } |
| @@ -189,11 +267,11 @@ VoxelSystem* VoxelSystem::clone() const { · 31 unmodified lines | |||
| 189 | 267 | ||
| 190 | void VoxelSystem::init() { | 268 | void VoxelSystem::init() { |
| 191 | // prep the data structures for incoming voxel data | 269 | // prep the data structures for incoming voxel data |
| 192 | writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | 270 | writeVerticesEndPointer = writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; |
| 193 | readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | 271 | readVerticesEndPointer = readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; |
| 194 | writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | 272 | writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; |
| 195 | readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | 273 | readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; |
| 196 | 274 | ||
| 197 | GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; | 275 | GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM]; |
| 198 | 276 | ||
| 199 | // populate the indicesArray | 277 | // populate the indicesArray |
| @@ -235,18 +313,15 @@ void VoxelSystem::render() { · 35 unmodified lines | |||
| 235 | 313 | ||
| 236 | glPushMatrix(); | 314 | glPushMatrix(); |
| 237 | 315 | ||
| 238 | |||
| 239 | if (readVerticesEndPointer != readVerticesArray) { | 316 | if (readVerticesEndPointer != readVerticesArray) { |
| 240 | // try to lock on the buffer write | 317 | // try to lock on the buffer write |
| 241 | // just avoid pulling new data if it is currently being written | 318 | // just avoid pulling new data if it is currently being written |
| 242 | if (pthread_mutex_trylock(&bufferWriteLock) == 0) { | 319 | if (pthread_mutex_trylock(&bufferWriteLock) == 0) { |
| 243 | 320 | ||
| 244 | glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); | 321 | glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID); |
| 245 | glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW); | ||
| 246 | glBufferSubData(GL_ARRAY_BUFFER, 0, (readVerticesEndPointer - readVerticesArray) * sizeof(GLfloat), readVerticesArray); | 322 | glBufferSubData(GL_ARRAY_BUFFER, 0, (readVerticesEndPointer - readVerticesArray) * sizeof(GLfloat), readVerticesArray); |
| 247 | 323 | ||
| 248 | glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); | 324 | glBindBuffer(GL_ARRAY_BUFFER, vboColorsID); |
| 249 | glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW); | ||
| 250 | glBufferSubData(GL_ARRAY_BUFFER, 0, (readVerticesEndPointer - readVerticesArray) * sizeof(GLubyte), readColorsArray); | 325 | glBufferSubData(GL_ARRAY_BUFFER, 0, (readVerticesEndPointer - readVerticesArray) * sizeof(GLubyte), readColorsArray); |
| 251 | 326 | ||
| 252 | readVerticesEndPointer = readVerticesArray; | 327 | readVerticesEndPointer = readVerticesArray; |