#include "ObjectPlacer.h" #include "Matrix4x4.h" #include "SceneGraph.h" namespace GtpVisibilityPreprocessor { // --------------------------------------------------------------------------- // ObjectPlacer implementation // --------------------------------------------------------------------------- ObjectPlacer::ObjectPlacer() : mCurrentObject(0) { } void ObjectPlacer::AddObject(SceneGraphLeaf *obj) { mDynamicObjects.push_back(obj); } void ObjectPlacer::NextObject() { mCurrentObject = (mCurrentObject + 1) % (int)mDynamicObjects.size(); } void ObjectPlacer::PreviousObject() { mCurrentObject = (mCurrentObject - 1) % (int)mDynamicObjects.size(); } SceneGraphLeaf *ObjectPlacer::GetCurrentObject() { if (mCurrentObject >= mDynamicObjects.size()) return NULL; return mDynamicObjects[mCurrentObject]; } void ObjectPlacer::PlaceObject(const Vector3 &pt) { Vector3 p = pt; SceneGraphLeaf *obj = mDynamicObjects[mCurrentObject]; // set pivot to ground of object p.y -= obj->GetOriginalBox().Min().y; Matrix4x4 trafo = TranslationMatrix(p); obj->LoadTransform(trafo); } /*void TestApp::Placer(const String &parm) { if (parm.Empty()) { OUT1("Object placement: place/load /removelast/next/previous/save "); return; } ASM_RET(viewer->GetRoot(), "No scene"); if (!placer) placer = new ObjectPlacer; //-- make sure the placer visualization (which shows the already placed objects) is in the viewer SearchNodeByNameVisitor search("placervis"); search.Apply(viewer->GetRoot(), SearchNodeByNameVisitor::FIRST); BranchGroup *placervis = search.GetNode(); if (!placervis) { placervis = placer->GetPlacedNodeVisualization(); viewer->GetRoot()->addChild(placervis); placervis->setName("placervis"); } if (parm == "place") { int x, y; input_module->GetMousePos(x, y); Point3f pos = viewer->UnProject(x, y); if (viewer && viewer->GetRoot() && viewer->GetRoot()->getBoundingBox()->intersects(pos)) { placer->AddObject(pos); OUT1(x << " " << y << " " << pos); } else { OUT1("Point out of bounds"); } } else if (parm == "removelast") { placer->RemoveLastObject(); } else if (parm == "next") { placer->NextObject(); } else if (parm == "previous") { placer->PreviousObject(); } else if (parm.Left(5) == "save ") { String filename = parm.Right(parm.size()-5); if (!filename.Empty()) placer->SaveObjects(filename); } else if (parm.Left(5) == "clear") { placer->Clear(); } else if (parm.Left(5) == "load ") { String fn = sysutils::FindFile(parm.Right(parm.size()-5), settings->get_ObjectPath()); ASM_ELSE(!fn.empty(), "Couldn't find file " << fn) { OUT1("Loading library " << fn << "..."); SceneBasePtr root = ReadSceneGraph(fn, 0, 0, 1); BranchGroup *library = new BranchGroup; ASM_ELSE((root != NULL), "No scene found in file...") { SearchNodeByNameVisitor search("start"); search.Apply(root, SearchNodeByNameVisitor::FIRST); BranchGroup *local_root = search.GetNode(); if (local_root == NULL) { OUT1("Note: BranchGroup named \"start\" not found - using root node"); local_root = root; } placer->SetLibrary(local_root); BranchGroup *viz = placer->GetLibraryVisualization(); mRenderArea->SetSceneRoot(viz); mRenderArea->SetCamera(new Camera); mRenderArea->GetCamera()->ViewAll(viz->getBoundingSphere()); } } } else OUT1("Unknown placer command"); } */ }