From 145c6484f49c0b7879fe17f1b92b0281f04cf3e9 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 9 May 2013 13:33:23 -0700 Subject: [PATCH 1/8] Fixed issue with mouse pressed state, use mouse buttons to add/delete voxels (numbers now switch between modes). --- interface/src/main.cpp | 52 +++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 4b70f20402..80adef9e37 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -174,6 +174,10 @@ int mouseY = 0; // Mouse location at start of last down click int mousePressed = 0; // true if mouse has been pressed (clear when finished) +// The current mode for mouse interaction +enum MouseMode { ADD_VOXELS, DELETE_VOXELS }; +MouseMode mouseMode = ADD_VOXELS; + Menu menu; // main menu int menuOn = 1; // Whether to show onscreen menu @@ -1662,8 +1666,8 @@ void key(unsigned char k, int x, int y) { if (k == '^') ::shiftPaintingColor(); // shifts randomize color between R,G,B dominant if (k == '-') ::sendVoxelServerEraseAll(); // sends erase all command to voxel server if (k == '%') ::sendVoxelServerAddScene(); // sends add scene command to voxel server - if (k == '1') ::addVoxelUnderCursor(); - if (k == '2') ::deleteVoxelUnderCursor(); + if (k == '1') ::mouseMode = ADD_VOXELS; + if (k == '2') ::mouseMode = DELETE_VOXELS; if (k == 'n' || k == 'N') { noiseOn = !noiseOn; // Toggle noise @@ -1767,9 +1771,7 @@ void idle(void) { myAvatar.setHandMovementValues(handControl.getValues()); // tell my avatar if the mouse is being pressed... - if (mousePressed) { - myAvatar.setMousePressed(mousePressed); - } + myAvatar.setMousePressed(mousePressed); // walking triggers the handControl to stop if (myAvatar.getMode() == AVATAR_MODE_WALKING) { @@ -1889,17 +1891,35 @@ glm::vec3 getGravity(glm::vec3 pos) { } void mouseFunc(int button, int state, int x, int y) { - if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { - if (state == GLUT_DOWN && !menu.mouseClick(x, y)) { - mouseX = x; - mouseY = y; - mousePressed = 1; - } else if (state == GLUT_UP) { - mouseX = x; - mouseY = y; - mousePressed = 0; - } - } + mouseX = x; + mouseY = y; + + switch (button) { + case GLUT_LEFT_BUTTON: + if (state == GLUT_DOWN && !menu.mouseClick(x, y)) { + mousePressed = 1; + if (::mouseMode == ADD_VOXELS) { + addVoxelUnderCursor(); + + } else { // ::mouseMode == DELETE_VOXELS + deleteVoxelUnderCursor(); + } + } else { + mousePressed = 0; + } + break; + + case GLUT_RIGHT_BUTTON: + if (state == GLUT_DOWN) { + if (::mouseMode == ADD_VOXELS) { + deleteVoxelUnderCursor(); + + } else { // ::mouseMode == DELETE_VOXELS + addVoxelUnderCursor(); + } + } + break; + } } void motionFunc(int x, int y) { From c4110830576bbc4d6330ea25c4cc035a6aaa3ed4 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 9 May 2013 14:30:50 -0700 Subject: [PATCH 2/8] Working on adding an "add voxel in front of avatar" command. --- interface/src/main.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 80adef9e37..a6ffe14ad1 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -1487,6 +1487,20 @@ void setupPaintingVoxel() { shiftPaintingColor(); } +void addVoxel(VoxelDetail& detail) { + unsigned char* bufferOut; + int sizeOut; + + if (createVoxelEditMessage(PACKET_HEADER_SET_VOXEL, 0, 1, &detail, bufferOut, sizeOut)){ + AgentList::getInstance()->broadcastToAgents(bufferOut, sizeOut, &AGENT_TYPE_VOXEL, 1); + delete bufferOut; + } +} + +void addVoxelInFrontOfAvatar() { + +} + void addVoxelUnderCursor() { glm::vec3 origin, direction; viewFrustum.computePickRay(mouseX / (float)WIDTH, mouseY / (float)HEIGHT, origin, direction); @@ -1521,13 +1535,7 @@ void addVoxelUnderCursor() { detail.z += detail.s; break; } - unsigned char* bufferOut; - int sizeOut; - - if (createVoxelEditMessage(PACKET_HEADER_SET_VOXEL, 0, 1, &detail, bufferOut, sizeOut)){ - AgentList::getInstance()->broadcastToAgents(bufferOut, sizeOut, &AGENT_TYPE_VOXEL, 1); - delete bufferOut; - } + addVoxel(detail); } } @@ -1668,6 +1676,7 @@ void key(unsigned char k, int x, int y) { if (k == '%') ::sendVoxelServerAddScene(); // sends add scene command to voxel server if (k == '1') ::mouseMode = ADD_VOXELS; if (k == '2') ::mouseMode = DELETE_VOXELS; + if (k == '3') addVoxelInFrontOfAvatar(); if (k == 'n' || k == 'N') { noiseOn = !noiseOn; // Toggle noise From ecd12e0ac4188115c723ea4b594c01b661682bab Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 9 May 2013 16:09:07 -0700 Subject: [PATCH 3/8] Show the voxels to be added/deleted as wireframe cubes. --- interface/src/main.cpp | 169 +++++++++++++++++++++++------------------ 1 file changed, 95 insertions(+), 74 deletions(-) diff --git a/interface/src/main.cpp b/interface/src/main.cpp index a6ffe14ad1..5bbee6672c 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -175,8 +175,9 @@ int mouseY = 0; int mousePressed = 0; // true if mouse has been pressed (clear when finished) // The current mode for mouse interaction -enum MouseMode { ADD_VOXELS, DELETE_VOXELS }; -MouseMode mouseMode = ADD_VOXELS; +enum MouseMode { ADD_VOXEL_MODE, DELETE_VOXEL_MODE }; +MouseMode mouseMode = ADD_VOXEL_MODE; +VoxelDetail mouseVoxel; // details of the voxel under the mouse cursor Menu menu; // main menu int menuOn = 1; // Whether to show onscreen menu @@ -374,6 +375,16 @@ void reset_sensors() { } } +void sendVoxelEditMessage(PACKET_HEADER header, VoxelDetail& detail) { + unsigned char* bufferOut; + int sizeOut; + + if (createVoxelEditMessage(header, 0, 1, &detail, bufferOut, sizeOut)){ + AgentList::getInstance()->broadcastToAgents(bufferOut, sizeOut, &AGENT_TYPE_VOXEL, 1); + delete bufferOut; + } +} + // // Using gyro data, update both view frustum and avatar head position // @@ -482,17 +493,11 @@ void updateAvatar(float deltaTime) { ::paintingVoxel.y = avatarPos.y / 10.0; ::paintingVoxel.z = avatarPos.z / 10.0; - unsigned char* bufferOut; - int sizeOut; - if (::paintingVoxel.x >= 0.0 && ::paintingVoxel.x <= 1.0 && ::paintingVoxel.y >= 0.0 && ::paintingVoxel.y <= 1.0 && ::paintingVoxel.z >= 0.0 && ::paintingVoxel.z <= 1.0) { - if (createVoxelEditMessage(PACKET_HEADER_SET_VOXEL, 0, 1, &::paintingVoxel, bufferOut, sizeOut)){ - AgentList::getInstance()->broadcastToAgents(bufferOut, sizeOut, &AGENT_TYPE_VOXEL, 1); - delete bufferOut; - } + sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, ::paintingVoxel); } } } @@ -745,6 +750,20 @@ void displaySide(Camera& whichCamera) { voxels.render(); } + // indicate what we'll be adding/removing in mouse mode, if anything + if (::mouseVoxel.s != 0) { + glPushMatrix(); + glColor3ub(::mouseVoxel.red, ::mouseVoxel.green, ::mouseVoxel.blue); + glScalef(TREE_SCALE, TREE_SCALE, TREE_SCALE); + glTranslatef(::mouseVoxel.x + ::mouseVoxel.s*0.5f, + ::mouseVoxel.y + ::mouseVoxel.s*0.5f, + ::mouseVoxel.z + ::mouseVoxel.s*0.5f); + glLineWidth(4.0f); + glutWireCube(::mouseVoxel.s); + glLineWidth(1.0f); + glPopMatrix(); + } + if (::renderAvatarsOn) { // Render avatars of other agents AgentList* agentList = AgentList::getInstance(); @@ -1487,73 +1506,31 @@ void setupPaintingVoxel() { shiftPaintingColor(); } -void addVoxel(VoxelDetail& detail) { - unsigned char* bufferOut; - int sizeOut; - - if (createVoxelEditMessage(PACKET_HEADER_SET_VOXEL, 0, 1, &detail, bufferOut, sizeOut)){ - AgentList::getInstance()->broadcastToAgents(bufferOut, sizeOut, &AGENT_TYPE_VOXEL, 1); - delete bufferOut; - } -} - void addVoxelInFrontOfAvatar() { + VoxelDetail detail; + glm::vec3 position = (myAvatar.getPosition() + myAvatar.getCameraDirection()) * (1.0f / TREE_SCALE); + detail.s = 1.0f / 1024; + + detail.x = detail.s * floor(position.x / detail.s); + detail.y = detail.s * floor(position.y / detail.s); + detail.z = detail.s * floor(position.z / detail.s); + detail.red = 128; + detail.green = 128; + detail.blue = 128; + + sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, detail); } void addVoxelUnderCursor() { - glm::vec3 origin, direction; - viewFrustum.computePickRay(mouseX / (float)WIDTH, mouseY / (float)HEIGHT, origin, direction); - - VoxelDetail detail; - float distance; - BoxFace face; - if (voxels.findRayIntersection(origin, direction, detail, distance, face)) { - // use the face to determine the side on which to create a neighbor - switch (face) { - case MIN_X_FACE: - detail.x -= detail.s; - break; - - case MAX_X_FACE: - detail.x += detail.s; - break; - - case MIN_Y_FACE: - detail.y -= detail.s; - break; - - case MAX_Y_FACE: - detail.y += detail.s; - break; - - case MIN_Z_FACE: - detail.z -= detail.s; - break; - - case MAX_Z_FACE: - detail.z += detail.s; - break; - } - addVoxel(detail); + if (::mouseVoxel.s != 0) { + sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, ::mouseVoxel); } } void deleteVoxelUnderCursor() { - glm::vec3 origin, direction; - viewFrustum.computePickRay(mouseX / (float)WIDTH, mouseY / (float)HEIGHT, origin, direction); - - VoxelDetail detail; - float distance; - BoxFace face; - if (voxels.findRayIntersection(origin, direction, detail, distance, face)) { - unsigned char* bufferOut; - int sizeOut; - - if (createVoxelEditMessage(PACKET_HEADER_ERASE_VOXEL, 0, 1, &detail, bufferOut, sizeOut)){ - AgentList::getInstance()->broadcastToAgents(bufferOut, sizeOut, &AGENT_TYPE_VOXEL, 1); - delete bufferOut; - } + if (::mouseVoxel.s != 0) { + sendVoxelEditMessage(PACKET_HEADER_ERASE_VOXEL, ::mouseVoxel); } } @@ -1674,8 +1651,8 @@ void key(unsigned char k, int x, int y) { if (k == '^') ::shiftPaintingColor(); // shifts randomize color between R,G,B dominant if (k == '-') ::sendVoxelServerEraseAll(); // sends erase all command to voxel server if (k == '%') ::sendVoxelServerAddScene(); // sends add scene command to voxel server - if (k == '1') ::mouseMode = ADD_VOXELS; - if (k == '2') ::mouseMode = DELETE_VOXELS; + if (k == '1') ::mouseMode = ADD_VOXEL_MODE; + if (k == '2') ::mouseMode = DELETE_VOXEL_MODE; if (k == '3') addVoxelInFrontOfAvatar(); if (k == 'n' || k == 'N') { @@ -1764,6 +1741,28 @@ void* networkReceive(void* args) { return NULL; } +glm::vec3 getFaceVector(BoxFace face) { + switch (face) { + case MIN_X_FACE: + return glm::vec3(-1, 0, 0); + + case MAX_X_FACE: + return glm::vec3(1, 0, 0); + + case MIN_Y_FACE: + return glm::vec3(0, -1, 0); + + case MAX_Y_FACE: + return glm::vec3(0, 1, 0); + + case MIN_Z_FACE: + return glm::vec3(0, 0, -1); + + case MAX_Z_FACE: + return glm::vec3(0, 0, 1); + } +} + void idle(void) { timeval check; gettimeofday(&check, NULL); @@ -1781,7 +1780,29 @@ void idle(void) { // tell my avatar if the mouse is being pressed... myAvatar.setMousePressed(mousePressed); - + + // check what's under the mouse and update the mouse voxel + glm::vec3 origin, direction; + viewFrustum.computePickRay(mouseX / (float)WIDTH, mouseY / (float)HEIGHT, origin, direction); + + float distance; + BoxFace face; + ::mouseVoxel.s = 0.0f; + if (voxels.findRayIntersection(origin, direction, ::mouseVoxel, distance, face)) { + if (::mouseMode == ADD_VOXEL_MODE) { + // use the face to determine the side on which to create a neighbor + glm::vec3 offset = getFaceVector(face); + ::mouseVoxel.x += offset.x * ::mouseVoxel.s; + ::mouseVoxel.y += offset.y * ::mouseVoxel.s; + ::mouseVoxel.z += offset.z * ::mouseVoxel.s; + + } else { + // red indicates deletion + ::mouseVoxel.red = 255; + ::mouseVoxel.green = ::mouseVoxel.blue = 0; + } + } + // walking triggers the handControl to stop if (myAvatar.getMode() == AVATAR_MODE_WALKING) { handControl.stop(); @@ -1907,10 +1928,10 @@ void mouseFunc(int button, int state, int x, int y) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN && !menu.mouseClick(x, y)) { mousePressed = 1; - if (::mouseMode == ADD_VOXELS) { + if (::mouseMode == ADD_VOXEL_MODE) { addVoxelUnderCursor(); - } else { // ::mouseMode == DELETE_VOXELS + } else { // ::mouseMode == DELETE_VOXEL_MODE deleteVoxelUnderCursor(); } } else { @@ -1920,10 +1941,10 @@ void mouseFunc(int button, int state, int x, int y) { case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { - if (::mouseMode == ADD_VOXELS) { + if (::mouseMode == ADD_VOXEL_MODE) { deleteVoxelUnderCursor(); - } else { // ::mouseMode == DELETE_VOXELS + } else { // ::mouseMode == DELETE_VOXEL_MODE addVoxelUnderCursor(); } } From 78b37fe920df08f14af3bbfa94785dfd771af930 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 9 May 2013 16:26:51 -0700 Subject: [PATCH 4/8] Starting on coloring support. --- interface/src/main.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 5bbee6672c..2eb761d65d 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -175,7 +175,7 @@ int mouseY = 0; int mousePressed = 0; // true if mouse has been pressed (clear when finished) // The current mode for mouse interaction -enum MouseMode { ADD_VOXEL_MODE, DELETE_VOXEL_MODE }; +enum MouseMode { ADD_VOXEL_MODE, DELETE_VOXEL_MODE, COLOR_VOXEL_MODE }; MouseMode mouseMode = ADD_VOXEL_MODE; VoxelDetail mouseVoxel; // details of the voxel under the mouse cursor @@ -1653,7 +1653,8 @@ void key(unsigned char k, int x, int y) { if (k == '%') ::sendVoxelServerAddScene(); // sends add scene command to voxel server if (k == '1') ::mouseMode = ADD_VOXEL_MODE; if (k == '2') ::mouseMode = DELETE_VOXEL_MODE; - if (k == '3') addVoxelInFrontOfAvatar(); + if (k == '3') ::mouseMode = COLOR_VOXEL_MODE; + if (k == '4') addVoxelInFrontOfAvatar(); if (k == 'n' || k == 'N') { noiseOn = !noiseOn; // Toggle noise @@ -1796,7 +1797,12 @@ void idle(void) { ::mouseVoxel.y += offset.y * ::mouseVoxel.s; ::mouseVoxel.z += offset.z * ::mouseVoxel.s; - } else { + } else if (::mouseMode == COLOR_VOXEL_MODE) { + ::mouseVoxel.red = 0; + ::mouseVoxel.green = 255; + ::mouseVoxel.blue = 0; + + } else { // ::mouseMode == DELETE_VOXEL_MODE // red indicates deletion ::mouseVoxel.red = 255; ::mouseVoxel.green = ::mouseVoxel.blue = 0; @@ -1928,9 +1934,9 @@ void mouseFunc(int button, int state, int x, int y) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN && !menu.mouseClick(x, y)) { mousePressed = 1; - if (::mouseMode == ADD_VOXEL_MODE) { + if (::mouseMode == ADD_VOXEL_MODE || ::mouseMode == COLOR_VOXEL_MODE) { addVoxelUnderCursor(); - + } else { // ::mouseMode == DELETE_VOXEL_MODE deleteVoxelUnderCursor(); } @@ -1941,12 +1947,7 @@ void mouseFunc(int button, int state, int x, int y) { case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { - if (::mouseMode == ADD_VOXEL_MODE) { - deleteVoxelUnderCursor(); - - } else { // ::mouseMode == DELETE_VOXEL_MODE - addVoxelUnderCursor(); - } + deleteVoxelUnderCursor(); } break; } From 9dc09de75eb99107ad84b3ea549c2fa35ee5e128 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 10 May 2013 09:32:56 -0700 Subject: [PATCH 5/8] make VoxelSystem::deleteVoxelAt() work properly --- interface/src/VoxelSystem.cpp | 27 +++++++++++++++++++++++---- libraries/voxels/src/VoxelNode.cpp | 1 + libraries/voxels/src/VoxelNode.h | 7 +++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index b5dab29029..592a70ca26 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -232,7 +232,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { bool inChildBoundary = (distanceToNode <= childBoundary); shouldRender = (node->isLeaf() && inChildBoundary) || (inBoundary && !inChildBoundary); } - node->setShouldRender(shouldRender); + node->setShouldRender(shouldRender && !node->deleteMe()); // let children figure out their renderness for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { if (node->getChildAtIndex(i)) { @@ -244,6 +244,15 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { } else { voxelsUpdated += updateNodeInArraysAsPartialVBO(node); } + + // If the node has been asked to be deleted, but we've gotten to here, after updateNodeInArraysXXX() + // then it means our VBOs are "clean" and our vertices have been removed or not added. So we can now + // safely remove the node from the tree and actually delete it. + // otherwise honor our calculated shouldRender + if (node->deleteMe()) { + _tree->deleteVoxelCodeFromTree(node->getOctalCode()); + } + node->clearDirtyBit(); // always clear the dirty bit, even if it doesn't need to be rendered return voxelsUpdated; } @@ -901,9 +910,19 @@ void VoxelSystem::collectStatsForTreesAndVBOs() { void VoxelSystem::deleteVoxelAt(float x, float y, float z, float s) { - //printLog("VoxelSystem::deleteVoxelAt(%f,%f,%f,%f)\n",x,y,z,s); - _tree->deleteVoxelAt(x, y, z, s); - setupNewVoxelsForDrawing(); + printLog("VoxelSystem::deleteVoxelAt(%f,%f,%f,%f)\n",x,y,z,s); + + VoxelNode* node = _tree->getVoxelAt(x, y, z, s); + if (node) { + // tell the node we want it deleted + node->pleaseDeleteMe(); + + // tree is now dirty + _tree->setDirtyBit(); + + // redraw! + setupNewVoxelsForDrawing(); // do we even need to do this? Or will the next network receive kick in? + } }; VoxelNode* VoxelSystem::getVoxelAt(float x, float y, float z, float s) const { diff --git a/libraries/voxels/src/VoxelNode.cpp b/libraries/voxels/src/VoxelNode.cpp index 5af7f09ea2..5070a6442d 100644 --- a/libraries/voxels/src/VoxelNode.cpp +++ b/libraries/voxels/src/VoxelNode.cpp @@ -44,6 +44,7 @@ void VoxelNode::init(unsigned char * octalCode) { _glBufferIndex = GLBUFFER_INDEX_UNKNOWN; _isDirty = true; _shouldRender = false; + _deleteMe = false; calculateAABox(); } diff --git a/libraries/voxels/src/VoxelNode.h b/libraries/voxels/src/VoxelNode.h index 51af8f6379..4ac083bd0f 100644 --- a/libraries/voxels/src/VoxelNode.h +++ b/libraries/voxels/src/VoxelNode.h @@ -27,6 +27,7 @@ private: glBufferIndex _glBufferIndex; bool _isDirty; bool _shouldRender; + bool _deleteMe; AABox _box; unsigned char* _octalCode; VoxelNode* _children[8]; @@ -66,9 +67,15 @@ public: glBufferIndex getBufferIndex() const { return _glBufferIndex; }; bool isKnownBufferIndex() const { return (_glBufferIndex != GLBUFFER_INDEX_UNKNOWN); }; void setBufferIndex(glBufferIndex index) { _glBufferIndex = index; }; + + // Used by VoxelSystem for rendering in/out of view and LOD void setShouldRender(bool shouldRender); bool getShouldRender() const { return _shouldRender; } + // Used by VoxelSystem to mark a node as to be deleted on next render pass + void pleaseDeleteMe() { _deleteMe = true; }; + bool deleteMe() const { return _deleteMe; } + #ifndef NO_FALSE_COLOR // !NO_FALSE_COLOR means, does have false color void setFalseColor(colorPart red, colorPart green, colorPart blue); void setFalseColored(bool isFalseColored); From f6fcdd78b21e3ee29eae11a6c2f4a0c3145dc70f Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Fri, 10 May 2013 09:53:53 -0700 Subject: [PATCH 6/8] Adding/removing voxels at different sizes. --- interface/src/main.cpp | 21 ++++++++++++++++++++- libraries/voxels/src/VoxelTree.cpp | 3 ++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 2eb761d65d..57a4b457fb 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -178,6 +178,7 @@ int mousePressed = 0; // true if mouse has been pressed (clear when finished) enum MouseMode { ADD_VOXEL_MODE, DELETE_VOXEL_MODE, COLOR_VOXEL_MODE }; MouseMode mouseMode = ADD_VOXEL_MODE; VoxelDetail mouseVoxel; // details of the voxel under the mouse cursor +float mouseVoxelScale = 1.0f / 1024.0f; // the scale for adding/removing voxels Menu menu; // main menu int menuOn = 1; // Whether to show onscreen menu @@ -1510,7 +1511,7 @@ void addVoxelInFrontOfAvatar() { VoxelDetail detail; glm::vec3 position = (myAvatar.getPosition() + myAvatar.getCameraDirection()) * (1.0f / TREE_SCALE); - detail.s = 1.0f / 1024; + detail.s = ::mouseVoxelScale; detail.x = detail.s * floor(position.x / detail.s); detail.y = detail.s * floor(position.y / detail.s); @@ -1655,6 +1656,8 @@ void key(unsigned char k, int x, int y) { if (k == '2') ::mouseMode = DELETE_VOXEL_MODE; if (k == '3') ::mouseMode = COLOR_VOXEL_MODE; if (k == '4') addVoxelInFrontOfAvatar(); + if (k == '5') ::mouseVoxelScale /= 2; + if (k == '6') ::mouseVoxelScale *= 2; if (k == 'n' || k == 'N') { noiseOn = !noiseOn; // Toggle noise @@ -1790,6 +1793,22 @@ void idle(void) { BoxFace face; ::mouseVoxel.s = 0.0f; if (voxels.findRayIntersection(origin, direction, ::mouseVoxel, distance, face)) { + // find the nearest voxel with the desired scale + if (::mouseVoxelScale > ::mouseVoxel.s) { + ::mouseVoxel.x = ::mouseVoxelScale * floorf(::mouseVoxel.x / ::mouseVoxelScale); + ::mouseVoxel.y = ::mouseVoxelScale * floorf(::mouseVoxel.y / ::mouseVoxelScale); + ::mouseVoxel.z = ::mouseVoxelScale * floorf(::mouseVoxel.z / ::mouseVoxelScale); + ::mouseVoxel.s = ::mouseVoxelScale; + + } else if (::mouseVoxelScale < ::mouseVoxel.s) { + glm::vec3 pt = (origin + direction * distance) / (float)TREE_SCALE - + getFaceVector(face) * (::mouseVoxelScale * 0.5f); + ::mouseVoxel.x = ::mouseVoxelScale * floorf(pt.x / ::mouseVoxelScale); + ::mouseVoxel.y = ::mouseVoxelScale * floorf(pt.y / ::mouseVoxelScale); + ::mouseVoxel.z = ::mouseVoxelScale * floorf(pt.z / ::mouseVoxelScale); + ::mouseVoxel.s = ::mouseVoxelScale; + } + if (::mouseMode == ADD_VOXEL_MODE) { // use the face to determine the side on which to create a neighbor glm::vec3 offset = getFaceVector(face); diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index bb55f01bc2..14cc92881e 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -620,7 +620,8 @@ bool findRayOperation(VoxelNode* node, void* extraData) { if (!node->isLeaf()) { return true; // recurse on children } - if (!args->found || distance < args->distance) { + distance *= TREE_SCALE; + if (node->getShouldRender() && (!args->found || distance < args->distance)) { args->node = node; args->distance = distance; args->face = face; From 3af8a1b8f443555093697562f4b1043bf95dba94 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 10 May 2013 10:03:28 -0700 Subject: [PATCH 7/8] code review fixes --- interface/src/VoxelSystem.cpp | 6 +++--- libraries/voxels/src/VoxelNode.cpp | 2 +- libraries/voxels/src/VoxelNode.h | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 592a70ca26..e473961c8e 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -232,7 +232,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { bool inChildBoundary = (distanceToNode <= childBoundary); shouldRender = (node->isLeaf() && inChildBoundary) || (inBoundary && !inChildBoundary); } - node->setShouldRender(shouldRender && !node->deleteMe()); + node->setShouldRender(shouldRender && !node->isStagedForDeletion()); // let children figure out their renderness for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { if (node->getChildAtIndex(i)) { @@ -249,7 +249,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { // then it means our VBOs are "clean" and our vertices have been removed or not added. So we can now // safely remove the node from the tree and actually delete it. // otherwise honor our calculated shouldRender - if (node->deleteMe()) { + if (node->isStagedForDeletion()) { _tree->deleteVoxelCodeFromTree(node->getOctalCode()); } @@ -915,7 +915,7 @@ void VoxelSystem::deleteVoxelAt(float x, float y, float z, float s) { VoxelNode* node = _tree->getVoxelAt(x, y, z, s); if (node) { // tell the node we want it deleted - node->pleaseDeleteMe(); + node->stageForDeletion(); // tree is now dirty _tree->setDirtyBit(); diff --git a/libraries/voxels/src/VoxelNode.cpp b/libraries/voxels/src/VoxelNode.cpp index 5070a6442d..19e6a12aa5 100644 --- a/libraries/voxels/src/VoxelNode.cpp +++ b/libraries/voxels/src/VoxelNode.cpp @@ -44,7 +44,7 @@ void VoxelNode::init(unsigned char * octalCode) { _glBufferIndex = GLBUFFER_INDEX_UNKNOWN; _isDirty = true; _shouldRender = false; - _deleteMe = false; + _isStagedForDeletion = false; calculateAABox(); } diff --git a/libraries/voxels/src/VoxelNode.h b/libraries/voxels/src/VoxelNode.h index 4ac083bd0f..c07cb528e6 100644 --- a/libraries/voxels/src/VoxelNode.h +++ b/libraries/voxels/src/VoxelNode.h @@ -27,7 +27,7 @@ private: glBufferIndex _glBufferIndex; bool _isDirty; bool _shouldRender; - bool _deleteMe; + bool _isStagedForDeletion; AABox _box; unsigned char* _octalCode; VoxelNode* _children[8]; @@ -73,8 +73,8 @@ public: bool getShouldRender() const { return _shouldRender; } // Used by VoxelSystem to mark a node as to be deleted on next render pass - void pleaseDeleteMe() { _deleteMe = true; }; - bool deleteMe() const { return _deleteMe; } + void stageForDeletion() { _isStagedForDeletion = true; }; + bool isStagedForDeletion() const { return _isStagedForDeletion; } #ifndef NO_FALSE_COLOR // !NO_FALSE_COLOR means, does have false color void setFalseColor(colorPart red, colorPart green, colorPart blue); From 501aac39da2a9733579c25574744712d441528a6 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Fri, 10 May 2013 10:46:04 -0700 Subject: [PATCH 8/8] Use isColored, not getShouldRender. --- libraries/voxels/src/VoxelTree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 14cc92881e..0120c7bfe1 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -621,7 +621,7 @@ bool findRayOperation(VoxelNode* node, void* extraData) { return true; // recurse on children } distance *= TREE_SCALE; - if (node->getShouldRender() && (!args->found || distance < args->distance)) { + if (node->isColored() && (!args->found || distance < args->distance)) { args->node = node; args->distance = distance; args->face = face;