From 785e55e06f8d4f0df98c101fb17b0a0ce252a6a8 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 9 May 2013 09:56:35 -0700 Subject: [PATCH] Working on voxel editing. --- interface/src/VoxelSystem.cpp | 15 +++++++++++++ interface/src/VoxelSystem.h | 3 +++ interface/src/main.cpp | 41 ++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 3d5880c305..d1605166c2 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -613,3 +613,18 @@ void VoxelSystem::removeOutOfView() { _nodeCount = 0; _tree->recurseTreeWithOperation(removeOutOfViewOperation,(void*)this); } + +bool VoxelSystem::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelDetail& detail, float& distance) { + VoxelNode* node; + if (!_tree->findRayIntersection(origin, direction, node, distance)) { + return false; + } + detail.x = node->getCorner().x; + detail.y = node->getCorner().y; + detail.z = node->getCorner().z; + detail.s = node->getScale(); + detail.red = node->getColor()[0]; + detail.green = node->getColor()[1]; + detail.blue = node->getColor()[2]; + return true; +} diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index f3968004b0..4b998dd942 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -11,6 +11,7 @@ #include "InterfaceConfig.h" #include +#include #include #include #include @@ -64,6 +65,8 @@ public: void removeOutOfView(); bool hasViewChanged(); + bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, VoxelDetail& detail, float& distance); + private: int _callsToTreesToArrays; VoxelNodeBag _removedVoxels; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index abbb67cdbc..309d79151d 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -70,7 +70,6 @@ #include "Oscilloscope.h" #include "UDPSocket.h" #include "SerialInterface.h" -#include #include #include #include @@ -1457,6 +1456,44 @@ void setupPaintingVoxel() { shiftPaintingColor(); } +void addVoxelUnderCursor() { + glm::vec3 origin, direction; + viewFrustum.computePickRay(mouseX / (float)WIDTH, mouseY / (float)HEIGHT, origin, direction); + + VoxelDetail detail; + float distance; + if (voxels.findRayIntersection(origin, direction, detail, distance)) { + // get the hit location relative to the center of the voxel + float half = detail.s * 0.5f; + glm::vec3 hit = origin + distance*direction - glm::vec3(detail.x + half, detail.y + half, detail.z + half); + + 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 deleteVoxelUnderCursor() { + glm::vec3 origin, direction; + viewFrustum.computePickRay(mouseX / (float)WIDTH, mouseY / (float)HEIGHT, origin, direction); + + VoxelDetail detail; + float distance; + if (voxels.findRayIntersection(origin, direction, detail, distance)) { + 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; + } + } +} + const float KEYBOARD_YAW_RATE = 0.8; const float KEYBOARD_PITCH_RATE = 0.6; const float KEYBOARD_STRAFE_RATE = 0.03; @@ -1574,6 +1611,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 == 'n' || k == 'N') { noiseOn = !noiseOn; // Toggle noise