History

initial LOD drop off when moving back

Stephen Birarda committed 13 years ago

Commit 31862541 parent 80170bd5 files · +45 −9Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+37−6
@@ -51,6 +51,10 @@ VoxelSystem::~VoxelSystem() { · 50 unmodified lines
5151 pthread_mutex_destroy(&bufferWriteLock);
5252 }
5353
54+void VoxelSystem::setViewerHead(Head *newViewerHead) {
55+ viewerHead = newViewerHead;
56+}
57+
5458 //////////////////////////////////////////////////////////////////////////////////////////
5559 // Method: VoxelSystem::loadVoxelsFile()
5660 // Description: Loads HiFidelity encoded Voxels from a binary file. The current file
@@ -94,7 +98,8 @@ void VoxelSystem::setupNewVoxelsForDrawing() { · 37 unmodified lines
9498 writeVerticesEndPointer = writeVerticesArray;
9599 // call recursive function to populate in memory arrays
96100 // it will return the number of voxels added
97− voxelsRendered = treeToArrays(tree->rootNode);
101+ float treeRoot[3] = {0,0,0};
102+ voxelsRendered = treeToArrays(tree->rootNode, treeRoot);
98103
99104 // copy the newly written data to the arrays designated for reading
100105 copyWrittenDataToReadArrays();
@@ -114,16 +119,42 @@ void VoxelSystem::copyWrittenDataToReadArrays() { · 13 unmodified lines
114119 pthread_mutex_unlock(&bufferWriteLock);
115120 }
116121
117−int VoxelSystem::treeToArrays(VoxelNode *currentNode) {
122+int VoxelSystem::treeToArrays(VoxelNode *currentNode, float nodePosition[3]) {
118123 int voxelsAdded = 0;
119124
120− for (int i = 0; i < 8; i++) {
121− // check if there is a child here
122− if (currentNode->children[i] != NULL) {
123− voxelsAdded += treeToArrays(currentNode->children[i]);
125+ float halfUnitForVoxel = powf(0.5, *currentNode->octalCode) * (0.5 * TREE_SCALE);
126+ glm::vec3 viewerPosition = viewerHead->getPos();
127+
128+ float distanceToVoxelCenter = sqrtf(powf(viewerPosition[0] - nodePosition[0] - halfUnitForVoxel, 2) +
129+ powf(viewerPosition[1] - nodePosition[1] - halfUnitForVoxel, 2) +
130+ powf(viewerPosition[2] - nodePosition[2] - halfUnitForVoxel, 2));
131+
132+ if (distanceToVoxelCenter < boundaryDistanceForRenderLevel(*currentNode->octalCode + 1)) {
133+ for (int i = 0; i < 8; i++) {
134+ // check if there is a child here
135+ if (currentNode->children[i] != NULL) {
136+
137+ // calculate the child's position based on the parent position
138+ float childNodePosition[3];
139+
140+ for (int j = 0; j < 3; j++) {
141+ childNodePosition[j] = nodePosition[j];
142+
143+ if (oneAtBit(branchIndexWithDescendant(currentNode->octalCode,
144+ currentNode->children[i]->octalCode),
145+ (7 - j))) {
146+ childNodePosition[j] -= (powf(0.5, *currentNode->children[i]->octalCode) * TREE_SCALE);
147+ }
148+ }
149+
150+
151+ voxelsAdded += treeToArrays(currentNode->children[i], childNodePosition);
152+ }
124153 }
125154 }
126155
156+
157+
127158 // if we didn't get any voxels added then we're a leaf
128159 // add our vertex and color information to the interleaved array
129160 if (voxelsAdded == 0 && currentNode->color[3] == 1) {