From 9957a0bae075b3b60465b6a1eeecae3cd5c85c95 Mon Sep 17 00:00:00 2001 From: Riku Avelar Date: Tue, 6 Dec 2016 17:06:55 -0500 Subject: [PATCH] Hierarchy Transform (Wonky Delete) --- src/viewer/simpleViewer.cpp | 98 +++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 31 deletions(-) diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 411d15c..38d05d8 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -56,9 +56,12 @@ namespace const int numTriSphere = numColSphere*(numRowSphere-1)*2 + 2*numColSphere; const GLuint particleLimit = 1024; + QMap mapColorToPosition; + QMap mapColorToShape; + QRgb currentPickerColor; int m_lDirectionLocation; - int m_normalMatrixLoc; + int m_normalMatrixLoc; QVector3D sun = QVector3D(0,-1,0); QMatrix4x4 sunRotate; @@ -435,7 +438,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) { SceneGroup* container = new SceneGroup; container->transform = selectedGeom.position; container->addChild(c); - root.addChild(container); + selectedGeom.shape->getParent()->addChild(container); startSwingAnimation(); } else{ @@ -1049,6 +1052,27 @@ void Viewer::visit(Cube &s) QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); QColor* faceColor = new QColor; + if(isPickingActivated) { + // Register self for picking + QColor c; + c.setRgb(currentPickerColor); + s.setColor(c); + for (int j = 0; j<3; j++) + { + for(int i = -1; i<=1; i+=2) + { + QMatrix4x4 currentTransform = QMatrix4x4(); + int components[3] = {0, 0, 0}; + components[j] = i; + //std::cout << "Setting " << startColor << " to {" << components[0] << ", " << components[0] << ", " << components[1] << ", " << components[2] << "}\n"; + currentTransform.translate(components[1], -components[2], -components[0]); + mapColorToPosition.insert(currentPickerColor, currentTransform); + mapColorToShape.insert(currentPickerColor, &s); + currentPickerColor++; + } + } + } + glBindVertexArray(m_VAOs[VAO_Cube]); glActiveTexture(GL_TEXTURE0); @@ -1211,6 +1235,16 @@ void Viewer::deleteSelected() { // selection), // selectedObj.shape->getParent()->getChildren()->end()); + // Get Children if available, and attach to parent + if(selectedObj.shape->getParent()->getChildren()->size() > 1) { + for(int i = 0; i < selectedObj.shape->getParent()->getChildren()->size(); i++) { + if(selectedObj.shape != selectedObj.shape->getParent()->getChildren()->at(i)) { + selectedObj.shape->getParent()->getParent()->addChild(selectedObj.shape->getParent()->getChildren()->at(i)); + } + } + } + + selectedObj.shape->getParent()->getParent()->getChildren()->erase( std::remove(selectedObj.shape->getParent()->getParent()->getChildren()->begin(), selectedObj.shape->getParent()->getParent()->getChildren()->end(), @@ -1301,8 +1335,10 @@ PickedGeom Viewer::pickGeom(int x, int y){ isPickingActivated = true; - QMap mapColorToPosition; - QMap mapColorToShape; + mapColorToPosition.clear(); + mapColorToShape.clear(); + currentPickerColor = 1; + QColor c; QRgb startColor = 1; // alpha must be 100%, glReadPixels doesn't resturn alpha unsigned char pixelData[3]; @@ -1310,34 +1346,34 @@ PickedGeom Viewer::pickGeom(int x, int y){ // Give each cube a color, let each cube manage their face-color assignment //std::cout << "Iterating through " << root.getChildren()->size() << " items"<size(); i++) - { - //std::cout << " iterating... " << i << endl; - SceneGroup* current = dynamic_cast(root.childAt(i)); - Shape* currentCube; +// for(int i = 0; isize(); i++) +// { +// //std::cout << " iterating... " << i << endl; +// SceneGroup* current = dynamic_cast(root.childAt(i)); +// Shape* currentCube; - if(current->getChildren()->size()) - { - currentCube = dynamic_cast(current->childAt(0)); - } +// if(current->getChildren()->size()) +// { +// currentCube = dynamic_cast(current->childAt(0)); +// } - c.setRgb(startColor); - currentCube->setColor(c); - if(currentCube) for (int j = 0; j<3; j++) - { - for(int i = -1; i<=1; i+=2) - { - QMatrix4x4 currentTransform = QMatrix4x4(current->transform); - int components[3] = {0, 0, 0}; - components[j] = i; - //std::cout << "Setting " << startColor << " to {" << components[0] << ", " << components[0] << ", " << components[1] << ", " << components[2] << "}\n"; - currentTransform.translate(components[1], -components[2], -components[0]); - mapColorToPosition.insert(startColor, currentTransform); - mapColorToShape.insert(startColor, currentCube); - startColor++; - } - } - } +// c.setRgb(startColor); +// currentCube->setColor(c); +// if(currentCube) for (int j = 0; j<3; j++) +// { +// for(int i = -1; i<=1; i+=2) +// { +// QMatrix4x4 currentTransform = QMatrix4x4(current->transform); +// int components[3] = {0, 0, 0}; +// components[j] = i; +// //std::cout << "Setting " << startColor << " to {" << components[0] << ", " << components[0] << ", " << components[1] << ", " << components[2] << "}\n"; +// currentTransform.translate(components[1], -components[2], -components[0]); +// mapColorToPosition.insert(startColor, currentTransform); +// mapColorToShape.insert(startColor, currentCube); +// startColor++; +// } +// } +// } root.accept(*this); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); @@ -1366,7 +1402,7 @@ PickedGeom Viewer::pickGeom(int x, int y){ unsigned int pickedInt = pickedColor->rgb() % 0x1000000; // rgb returns ARGB. go figure. std::cout << "Picked " << pickedInt << endl; - QMatrix4x4 pickedPosition = mapColorToPosition.value(pickedInt, identityMatrix); + QMatrix4x4 pickedPosition = mapColorToPosition.value(pickedInt, identityMatrix); Shape* pickedShape = mapColorToShape.value(pickedInt); mapColorToPosition.clear();