History

#19197 Create basic visual studio

vincent committed 13 years ago

Commit 622053b2 parents 1c1b736 e5a31bc22 files · +925 −561Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+100−188
@@ -15,9 +15,10 @@ · 14 unmodified lines
1515 #include <fstream> // to load voxels from file
1616 #include <SharedUtil.h>
1717 #include <OctalCode.h>
18+#include <pthread.h>
1819 #include "VoxelSystem.h"
1920
20−const int MAX_VOXELS_PER_SYSTEM = 250000;
21+const int MAX_VOXELS_PER_SYSTEM = 1500000; //250000;
2122
2223 const int VERTICES_PER_VOXEL = 8;
2324 const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
@@ -42,12 +43,20 @@ GLubyte identityIndices[] = { 0,1,2, 0,2,3, · 18 unmodified lines
4243 VoxelSystem::VoxelSystem() {
4344 voxelsRendered = 0;
4445 tree = new VoxelTree();
46+ pthread_mutex_init(&bufferWriteLock, NULL);
4547 }
4648
4749 VoxelSystem::~VoxelSystem() {
48− delete[] verticesArray;
49− delete[] colorsArray;
50+ delete[] readVerticesArray;
51+ delete[] writeVerticesArray;
52+ delete[] readColorsArray;
53+ delete[] writeColorsArray;
5054 delete tree;
55+ pthread_mutex_destroy(&bufferWriteLock);
56+}
57+
58+void VoxelSystem::setViewerHead(Head *newViewerHead) {
59+ viewerHead = newViewerHead;
5160 }
5261
5362 //////////////////////////////////////////////////////////////////////////////////////////
@@ -57,65 +66,11 @@ VoxelSystem::~VoxelSystem() { · 3 unmodified lines
5766 // colors are set randomly
5867 // Complaints: Brad :)
5968 // To Do: Need to add color data to the file.
60−void VoxelSystem::loadVoxelsFile(char* fileName) {
61− int vCount = 0;
62−
63− std::ifstream file(fileName, std::ios::in|std::ios::binary);
64−
65− char octets;
66− unsigned int lengthInBytes;
69+void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) {
6770
68− int totalBytesRead = 0;
69− if(file.is_open())
70− {
71− bool bail = false;
72− while (!file.eof() && !bail) {
73− file.get(octets);
74− totalBytesRead++;
75− lengthInBytes = bytesRequiredForCodeLength(octets)-1; //(octets*3/8)+1;
76− unsigned char * voxelData = new unsigned char[lengthInBytes+1+3];
77− voxelData[0]=octets;
78− char byte;
79−
80− for (size_t i = 0; i < lengthInBytes; i++) {
81− file.get(byte);
82− totalBytesRead++;
83− voxelData[i+1] = byte;
84− }
85− // read color data
86− char red,green,blue;
87− file.get(red);
88− file.get(green);
89− file.get(blue);
90−
91− //printf("red:%d\n",red);
92− //printf("green:%d\n",green);
93− //printf("blue:%d\n",blue);
94− vCount++;
95− //printf("vCount:%d\n",vCount);
96−
97− //randomColorValue(65);
98− float rf = randFloatInRange(.5,1); // add a little bit of variance to colors so we can see the voxels
99− voxelData[lengthInBytes+1] = red * rf;
100− voxelData[lengthInBytes+2] = green * rf;
101− voxelData[lengthInBytes+3] = blue * rf;
102−
103− //printVoxelCode(voxelData);
104− tree->readCodeColorBufferToTree(voxelData);
105− delete voxelData;
106− }
107− file.close();
108− }
71+ tree->loadVoxelsFile(fileName,wantColorRandomizer);
10972
110− tree->pruneTree(tree->rootNode);
111−
112− // reset the verticesEndPointer so we're writing to the beginning of the array
113− verticesEndPointer = verticesArray;
114− // call recursive function to populate in memory arrays
115− // it will return the number of voxels added
116− voxelsRendered = treeToArrays(tree->rootNode);
117− // set the boolean if there are any voxels to be rendered so we re-fill the VBOs
118− voxelsToRender = (voxelsRendered > 0);
73+ copyWrittenDataToReadArrays();
11974 }
12075
12176 //////////////////////////////////////////////////////////////////////////////////////////
@@ -125,107 +80,10 @@ void VoxelSystem::loadVoxelsFile(char* fileName) { · 3 unmodified lines
12580 // mechanism to tell the system to redraw it's arrays after voxels are done
12681 // being added. This is a concept mostly only understood by VoxelSystem.
12782 // Complaints: Brad :)
128−void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bool solid)
129−{
130− // About the color of the sphere... we're going to make this sphere be a gradient
131− // between two RGB colors. We will do the gradient along the phi spectrum
132− unsigned char r1 = randomColorValue(165);
133− unsigned char g1 = randomColorValue(165);
134− unsigned char b1 = randomColorValue(165);
135− unsigned char r2 = randomColorValue(65);
136− unsigned char g2 = randomColorValue(65);
137− unsigned char b2 = randomColorValue(65);
138−
139− // we don't want them to match!!
140− if (r1==r2 && g1==g2 && b1==b2)
141− {
142− r2=r1/2;
143− g2=g1/2;
144− b2=b1/2;
145− }
146−
147− /**
148− std::cout << "creatSphere COLORS ";
149− std::cout << " r1=" << (int)r1;
150− std::cout << " g1=" << (int)g1;
151− std::cout << " b1=" << (int)b1;
152− std::cout << " r2=" << (int)r2;
153− std::cout << " g2=" << (int)g2;
154− std::cout << " b2=" << (int)b2;
155− std::cout << std::endl;
156− **/
157−
158− // Psuedocode for creating a sphere:
159− //
160− // for (theta from 0 to 2pi):
161− // for (phi from 0 to pi):
162− // x = xc+r*cos(theta)*sin(phi)
163− // y = yc+r*sin(theta)*sin(phi)
164− // z = zc+r*cos(phi)
165−
166− int t=0; // total points
83+void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bool solid, bool wantColorRandomizer) {
16784
168− // We want to make sure that as we "sweep" through our angles
169− // we use a delta angle that's small enough to not skip any voxels
170− // we can calculate theta from our desired arc length
171− //
172− // lenArc = ndeg/360deg * 2pi*R
173− // lenArc = theta/2pi * 2pi*R
174− // lenArc = theta*R
175− // theta = lenArc/R
176− // theta = g/r
177− float angleDelta = (s/r);
178−
179− // assume solid for now
180− float ri = 0.0;
181− if (!solid)
182− {
183− ri=r; // just the outer surface
184− }
185− // If you also iterate form the interior of the sphere to the radius, makeing
186− // larger and larger sphere's you'd end up with a solid sphere. And lots of voxels!
187− for (; ri <= r; ri+=s)
188− {
189− for (float theta=0.0; theta <= 2*M_PI; theta += angleDelta)
190− {
191− for (float phi=0.0; phi <= M_PI; phi += angleDelta)
192− {
193− t++; // total voxels
194− float x = xc+r*cos(theta)*sin(phi);
195− float y = yc+r*sin(theta)*sin(phi);
196− float z = zc+r*cos(phi);
197− /*
198− std::cout << " r=" << r;
199− std::cout << " theta=" << theta;
200− std::cout << " phi=" << phi;
201− std::cout << " x=" << x;
202− std::cout << " y=" << y;
203− std::cout << " z=" << z;
204− std::cout << " t=" << t;
205− std::cout << std::endl;
206− */
207−
208− // gradient color data
209− float gradient = (phi/M_PI);
210− unsigned char red = r1+((r2-r1)*gradient);
211− unsigned char green = g1+((g2-g1)*gradient);
212− unsigned char blue = b1+((b2-b1)*gradient);
213−
214− unsigned char* voxelData = pointToVoxel(x,y,z,s,red,green,blue);
215− tree->readCodeColorBufferToTree(voxelData);
216− delete voxelData;
217−
218− }
219− }
220− }
221−
222− // reset the verticesEndPointer so we're writing to the beginning of the array
223− verticesEndPointer = verticesArray;
224− // call recursive function to populate in memory arrays
225− // it will return the number of voxels added
226− voxelsRendered = treeToArrays(tree->rootNode);
227− // set the boolean if there are any voxels to be rendered so we re-fill the VBOs
228− voxelsToRender = (voxelsRendered > 0);
85+ tree->createSphere(r,xc,yc,zc,s,solid,wantColorRandomizer);
86+ setupNewVoxelsForDrawing();
22987 }
23088
23189
@@ -233,32 +91,74 @@ void VoxelSystem::parseData(void *data, int size) { · 1 unmodified line
23391 // output the bits received from the voxel server
23492 unsigned char *voxelData = (unsigned char *) data + 1;
23593
236− printf("Received a packet of %d bytes from VS\n", size);
237−
23894 // ask the VoxelTree to read the bitstream into the tree
23995 tree->readBitstreamToTree(voxelData, size - 1);
24096
97+ setupNewVoxelsForDrawing();
98+}
99+
100+void VoxelSystem::setupNewVoxelsForDrawing() {
241101 // reset the verticesEndPointer so we're writing to the beginning of the array
242− verticesEndPointer = verticesArray;
243−
102+ writeVerticesEndPointer = writeVerticesArray;
244103 // call recursive function to populate in memory arrays
245104 // it will return the number of voxels added
246− voxelsRendered = treeToArrays(tree->rootNode);
105+ float treeRoot[3] = {0,0,0};
106+ voxelsRendered = treeToArrays(tree->rootNode, treeRoot);
107+
108+ // copy the newly written data to the arrays designated for reading
109+ copyWrittenDataToReadArrays();
110+}
111+
112+void VoxelSystem::copyWrittenDataToReadArrays() {
113+ // lock on the buffer write lock so we can't modify the data when the GPU is reading it
114+ pthread_mutex_lock(&bufferWriteLock);
115+ // store a pointer to the current end so it doesn't change during copy
116+ GLfloat *endOfCurrentVerticesData = writeVerticesEndPointer;
117+ // copy the vertices and colors
118+ memcpy(readVerticesArray, writeVerticesArray, (endOfCurrentVerticesData - writeVerticesArray) * sizeof(GLfloat));
119+ memcpy(readColorsArray, writeColorsArray, (endOfCurrentVerticesData - writeVerticesArray) * sizeof(GLubyte));
247120
248− // set the boolean if there are any voxels to be rendered so we re-fill the VBOs
249− voxelsToRender = (voxelsRendered > 0);
121+ // set the read vertices end pointer to the correct spot so the GPU knows how much to pull
122+ readVerticesEndPointer = readVerticesArray + (endOfCurrentVerticesData - writeVerticesArray);
123+ pthread_mutex_unlock(&bufferWriteLock);
250124 }
251125
252−int VoxelSystem::treeToArrays(VoxelNode *currentNode) {
126+int VoxelSystem::treeToArrays(VoxelNode *currentNode, float nodePosition[3]) {
253127 int voxelsAdded = 0;
254128
255− for (int i = 0; i < 8; i++) {
256− // check if there is a child here
257− if (currentNode->children[i] != NULL) {
258− voxelsAdded += treeToArrays(currentNode->children[i]);
129+ float halfUnitForVoxel = powf(0.5, *currentNode->octalCode) * (0.5 * TREE_SCALE);
130+ glm::vec3 viewerPosition = viewerHead->getPos();
131+
132+ float distanceToVoxelCenter = sqrtf(powf(viewerPosition[0] - nodePosition[0] - halfUnitForVoxel, 2) +
133+ powf(viewerPosition[1] - nodePosition[1] - halfUnitForVoxel, 2) +
134+ powf(viewerPosition[2] - nodePosition[2] - halfUnitForVoxel, 2));
135+
136+ if (distanceToVoxelCenter < boundaryDistanceForRenderLevel(*currentNode->octalCode + 1)) {
137+ for (int i = 0; i < 8; i++) {
138+ // check if there is a child here
139+ if (currentNode->children[i] != NULL) {
140+
141+ // calculate the child's position based on the parent position
142+ float childNodePosition[3];
143+
144+ for (int j = 0; j < 3; j++) {
145+ childNodePosition[j] = nodePosition[j];
146+
147+ if (oneAtBit(branchIndexWithDescendant(currentNode->octalCode,
148+ currentNode->children[i]->octalCode),
149+ (7 - j))) {
150+ childNodePosition[j] -= (powf(0.5, *currentNode->children[i]->octalCode) * TREE_SCALE);
151+ }
152+ }
153+
154+
155+ voxelsAdded += treeToArrays(currentNode->children[i], childNodePosition);
156+ }
259157 }
260158 }
261159
160+
161+
262162 // if we didn't get any voxels added then we're a leaf
263163 // add our vertex and color information to the interleaved array
264164 if (voxelsAdded == 0 && currentNode->color[3] == 1) {
@@ -268,10 +168,10 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode) { · 3 unmodified lines
268168 // populate the array with points for the 8 vertices
269169 // and RGB color for each added vertex
270170 for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {
271− *verticesEndPointer = startVertex[j % 3] + (identityVertices[j] * voxelScale);
272− *(colorsArray + (verticesEndPointer - verticesArray)) = currentNode->color[j % 3];
171+ *writeVerticesEndPointer = startVertex[j % 3] + (identityVertices[j] * voxelScale);
172+ *(writeColorsArray + (writeVerticesEndPointer - writeVerticesArray)) = currentNode->color[j % 3];
273173
274− verticesEndPointer++;
174+ writeVerticesEndPointer++;
275175 }
276176
277177 voxelsAdded++;
@@ -289,8 +189,10 @@ VoxelSystem* VoxelSystem::clone() const { · 11 unmodified lines
289189
290190 void VoxelSystem::init() {
291191 // prep the data structures for incoming voxel data
292− verticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
293− colorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
192+ writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
193+ readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
194+ writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
195+ readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
294196
295197 GLuint *indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
296198
@@ -321,7 +223,9 @@ void VoxelSystem::init() { · 24 unmodified lines
321223 // VBO for the indicesArray
322224 glGenBuffers(1, &vboIndicesID);
323225 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
324− glBufferData(GL_ELEMENT_ARRAY_BUFFER, INDICES_PER_VOXEL * sizeof(GLuint) * MAX_VOXELS_PER_SYSTEM, indicesArray, GL_STATIC_DRAW);
226+ glBufferData(GL_ELEMENT_ARRAY_BUFFER,
227+ INDICES_PER_VOXEL * sizeof(GLuint) * MAX_VOXELS_PER_SYSTEM,
228+ indicesArray, GL_STATIC_DRAW);
325229
326230 // delete the indices array that is no longer needed
327231 delete[] indicesArray;
@@ -331,16 +235,24 @@ void VoxelSystem::render() { · 3 unmodified lines
331235
332236 glPushMatrix();
333237
334− if (voxelsToRender) {
335− glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
336− glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);
337− glBufferSubData(GL_ARRAY_BUFFER, 0, (verticesEndPointer - verticesArray) * sizeof(GLfloat), verticesArray);
338−
339− glBindBuffer(GL_ARRAY_BUFFER, vboColorsID);
340− glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);
341− glBufferSubData(GL_ARRAY_BUFFER, 0, (verticesEndPointer - verticesArray) * sizeof(GLubyte), colorsArray);
342−
343− voxelsToRender = false;
238+
239+ if (readVerticesEndPointer != readVerticesArray) {
240+ // try to lock on the buffer write
241+ // just avoid pulling new data if it is currently being written
242+ if (pthread_mutex_trylock(&bufferWriteLock) == 0) {
243+
244+ 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);
247+
248+ 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);
251+
252+ readVerticesEndPointer = readVerticesArray;
253+
254+ pthread_mutex_unlock(&bufferWriteLock);
255+ }
344256 }
345257
346258 // tell OpenGL where to find vertex and color information