History

fix byte normals, fix crashes in switching between voxel shader and normal mode

ZappoMan committed 13 years ago

Commit eb6af1a1 parent 2487a553 files · +65 −15Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+60−15
@@ -37,7 +37,7 @@ float identityVertices[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0, · 36 unmodified lines
3737 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1,
3838 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 };
3939
40−GLbyte identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1,
40+GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1,
4141 0,0,+1, 0,0,+1, 0,0,+1, 0,0,+1,
4242 0,-1,0, 0,-1,0, 0,+1,0, 0,+1,0,
4343 0,-1,0, 0,-1,0, 0,+1,0, 0,+1,0,
@@ -152,6 +152,10 @@ void VoxelSystem::setUseByteNormals(bool useByteNormals) { · 108 unmodified lines
152152 init();
153153 }
154154 pthread_mutex_unlock(&_bufferWriteLock);
155+
156+ if (wasInitialized) {
157+ forceRedrawEntireTree();
158+ }
155159 }
156160
157161
@@ -166,12 +170,17 @@ void VoxelSystem::setMaxVoxels(int maxVoxels) { · 8 unmodified lines
166170 init();
167171 }
168172 pthread_mutex_unlock(&_bufferWriteLock);
173+
174+ if (wasInitialized) {
175+ forceRedrawEntireTree();
176+ }
169177 }
170178
171179 void VoxelSystem::setUseVoxelShader(bool useVoxelShader) {
172180 pthread_mutex_lock(&_bufferWriteLock);
173181 bool wasInitialized = _initialized;
174182 if (wasInitialized) {
183+ clearAllNodesBufferIndex();
175184 cleanupVoxelMemory();
176185 }
177186 _useVoxelShader = useVoxelShader;
@@ -179,6 +188,10 @@ void VoxelSystem::setUseVoxelShader(bool useVoxelShader) { · 1 unmodified line
179188 init();
180189 }
181190 pthread_mutex_unlock(&_bufferWriteLock);
191+
192+ if (wasInitialized) {
193+ forceRedrawEntireTree();
194+ }
182195 }
183196
184197 void VoxelSystem::cleanupVoxelMemory() {
@@ -266,7 +279,7 @@ void VoxelSystem::initVoxelMemory() { · 81 unmodified lines
266279 // populate the normalsArray
267280 for (int n = 0; n < _maxVoxels; n++) {
268281 for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
269− *(normalsArrayEndPointer++) = identityNormals[i];
282+ *(normalsArrayEndPointer++) = (identityNormals[i] * CHAR_MAX);
270283 }
271284 }
272285
@@ -750,20 +763,24 @@ void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& s · 477 unmodified lines
750763 float voxelScale, const nodeColor& color) {
751764
752765 if (_useVoxelShader) {
753− VoxelShaderVBOData* writeVerticesAt = &_writeVoxelShaderData[nodeIndex];
754− writeVerticesAt->x = startVertex.x * TREE_SCALE;
755− writeVerticesAt->y = startVertex.y * TREE_SCALE;
756− writeVerticesAt->z = startVertex.z * TREE_SCALE;
757− writeVerticesAt->s = voxelScale * TREE_SCALE;
758− writeVerticesAt->r = color[RED_INDEX];
759− writeVerticesAt->g = color[GREEN_INDEX];
760− writeVerticesAt->b = color[BLUE_INDEX];
766+ if (_writeVoxelShaderData) {
767+ VoxelShaderVBOData* writeVerticesAt = &_writeVoxelShaderData[nodeIndex];
768+ writeVerticesAt->x = startVertex.x * TREE_SCALE;
769+ writeVerticesAt->y = startVertex.y * TREE_SCALE;
770+ writeVerticesAt->z = startVertex.z * TREE_SCALE;
771+ writeVerticesAt->s = voxelScale * TREE_SCALE;
772+ writeVerticesAt->r = color[RED_INDEX];
773+ writeVerticesAt->g = color[GREEN_INDEX];
774+ writeVerticesAt->b = color[BLUE_INDEX];
775+ }
761776 } else {
762− for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {
763− GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
764− GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
765− *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale);
766− *(writeColorsAt +j) = color[j % 3];
777+ if (_writeVerticesArray && _writeColorsArray) {
778+ for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) {
779+ GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
780+ GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL);
781+ *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale);
782+ *(writeColorsAt +j) = color[j % 3];
783+ }
767784 }
768785 }
769786 }
@@ -995,6 +1012,34 @@ void VoxelSystem::killLocalVoxels() { · 225 unmodified lines
9951012 }
9961013
9971014
1015+bool VoxelSystem::clearAllNodesBufferIndexOperation(VoxelNode* node, void* extraData) {
1016+ _nodeCount++;
1017+ node->setBufferIndex(GLBUFFER_INDEX_UNKNOWN);
1018+ return true;
1019+}
1020+
1021+void VoxelSystem::clearAllNodesBufferIndex() {
1022+ _nodeCount = 0;
1023+ _tree->recurseTreeWithOperation(clearAllNodesBufferIndexOperation);
1024+ qDebug("clearing buffer index of %d nodes\n", _nodeCount);
1025+ _tree->setDirtyBit();
1026+ setupNewVoxelsForDrawing();
1027+}
1028+
1029+bool VoxelSystem::forceRedrawEntireTreeOperation(VoxelNode* node, void* extraData) {
1030+ _nodeCount++;
1031+ node->setDirtyBit();
1032+ return true;
1033+}
1034+
1035+void VoxelSystem::forceRedrawEntireTree() {
1036+ _nodeCount = 0;
1037+ _tree->recurseTreeWithOperation(forceRedrawEntireTreeOperation);
1038+ qDebug("forcing redraw of %d nodes\n", _nodeCount);
1039+ _tree->setDirtyBit();
1040+ setupNewVoxelsForDrawing();
1041+}
1042+
9981043 bool VoxelSystem::randomColorOperation(VoxelNode* node, void* extraData) {
9991044 _nodeCount++;
10001045 if (node->isColored()) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t