[2693] | 1 | #include "ObjectPlacer.h"
|
---|
[2694] | 2 | #include "Matrix4x4.h"
|
---|
| 3 | #include "SceneGraph.h"
|
---|
[2693] | 4 |
|
---|
| 5 |
|
---|
[2694] | 6 |
|
---|
| 7 | namespace GtpVisibilityPreprocessor
|
---|
| 8 | {
|
---|
| 9 |
|
---|
[2693] | 10 | // ---------------------------------------------------------------------------
|
---|
| 11 | // ObjectPlacer implementation
|
---|
| 12 | // ---------------------------------------------------------------------------
|
---|
[2694] | 13 |
|
---|
[2693] | 14 | ObjectPlacer::ObjectPlacer()
|
---|
[2694] | 15 | : mCurrentObject(0)
|
---|
[2693] | 16 | {
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 |
|
---|
[2694] | 20 | void ObjectPlacer::AddObject(SceneGraphLeaf *obj)
|
---|
[2693] | 21 | {
|
---|
[2694] | 22 | mDynamicObjects.push_back(obj);
|
---|
[2693] | 23 | }
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | void ObjectPlacer::NextObject()
|
---|
| 27 | {
|
---|
[2694] | 28 | mCurrentObject = (mCurrentObject + 1) % (int)mDynamicObjects.size();
|
---|
[2693] | 29 | }
|
---|
| 30 |
|
---|
[2694] | 31 |
|
---|
[2693] | 32 | void ObjectPlacer::PreviousObject()
|
---|
| 33 | {
|
---|
[2694] | 34 | mCurrentObject = (mCurrentObject - 1) % (int)mDynamicObjects.size();
|
---|
[2693] | 35 | }
|
---|
| 36 |
|
---|
| 37 |
|
---|
[2694] | 38 | SceneGraphLeaf *ObjectPlacer::GetCurrentObject()
|
---|
| 39 | {
|
---|
| 40 | if (mCurrentObject >= mDynamicObjects.size())
|
---|
| 41 | return NULL;
|
---|
[2693] | 42 |
|
---|
[2694] | 43 | return mDynamicObjects[mCurrentObject];
|
---|
[2693] | 44 | }
|
---|
| 45 |
|
---|
| 46 |
|
---|
[2694] | 47 | void ObjectPlacer::PlaceObject(const Vector3 &pt)
|
---|
[2693] | 48 | {
|
---|
[2694] | 49 | Vector3 p = pt;
|
---|
[2693] | 50 |
|
---|
[2694] | 51 | SceneGraphLeaf *obj = mDynamicObjects[mCurrentObject];
|
---|
[2693] | 52 |
|
---|
[2694] | 53 | // set pivot to ground of object
|
---|
| 54 | p.y -= obj->GetOriginalBox().Min().y;
|
---|
| 55 | Matrix4x4 trafo = TranslationMatrix(p);
|
---|
[2693] | 56 |
|
---|
[2694] | 57 | obj->LoadTransform(trafo);
|
---|
[2693] | 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
[2694] | 61 | /*void TestApp::Placer(const String &parm)
|
---|
[2693] | 62 | {
|
---|
| 63 | if (parm.Empty())
|
---|
| 64 | {
|
---|
| 65 | OUT1("Object placement: place/load <libaryfile>/removelast/next/previous/save <objectfile>");
|
---|
| 66 | return;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | ASM_RET(viewer->GetRoot(), "No scene");
|
---|
| 70 |
|
---|
| 71 | if (!placer)
|
---|
| 72 | placer = new ObjectPlacer;
|
---|
| 73 |
|
---|
| 74 | //-- make sure the placer visualization (which shows the already placed objects) is in the viewer
|
---|
| 75 | SearchNodeByNameVisitor search("placervis");
|
---|
| 76 | search.Apply(viewer->GetRoot(), SearchNodeByNameVisitor::FIRST);
|
---|
| 77 | BranchGroup *placervis = search.GetNode();
|
---|
| 78 | if (!placervis)
|
---|
| 79 | {
|
---|
| 80 | placervis = placer->GetPlacedNodeVisualization();
|
---|
| 81 | viewer->GetRoot()->addChild(placervis);
|
---|
| 82 | placervis->setName("placervis");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | if (parm == "place")
|
---|
| 86 | {
|
---|
| 87 | int x, y;
|
---|
| 88 | input_module->GetMousePos(x, y);
|
---|
| 89 |
|
---|
| 90 | Point3f pos = viewer->UnProject(x, y);
|
---|
| 91 | if (viewer && viewer->GetRoot() && viewer->GetRoot()->getBoundingBox()->intersects(pos))
|
---|
| 92 | {
|
---|
| 93 | placer->AddObject(pos);
|
---|
| 94 | OUT1(x << " " << y << " " << pos);
|
---|
| 95 | }
|
---|
| 96 | else
|
---|
| 97 | {
|
---|
| 98 | OUT1("Point out of bounds");
|
---|
| 99 | }
|
---|
| 100 | } else if (parm == "removelast") {
|
---|
| 101 | placer->RemoveLastObject();
|
---|
| 102 | } else if (parm == "next") {
|
---|
| 103 | placer->NextObject();
|
---|
| 104 | } else if (parm == "previous") {
|
---|
| 105 | placer->PreviousObject();
|
---|
| 106 | } else if (parm.Left(5) == "save ") {
|
---|
| 107 | String filename = parm.Right(parm.size()-5);
|
---|
| 108 | if (!filename.Empty())
|
---|
| 109 | placer->SaveObjects(filename);
|
---|
| 110 | } else if (parm.Left(5) == "clear") {
|
---|
| 111 | placer->Clear();
|
---|
| 112 | } else if (parm.Left(5) == "load ") {
|
---|
| 113 | String fn = sysutils::FindFile(parm.Right(parm.size()-5), settings->get_ObjectPath());
|
---|
| 114 |
|
---|
| 115 | ASM_ELSE(!fn.empty(), "Couldn't find file " << fn)
|
---|
| 116 | {
|
---|
| 117 | OUT1("Loading library " << fn << "...");
|
---|
| 118 | SceneBasePtr<BranchGroup> root = ReadSceneGraph(fn, 0, 0, 1);
|
---|
| 119 |
|
---|
| 120 | BranchGroup *library = new BranchGroup;
|
---|
| 121 | ASM_ELSE((root != NULL), "No scene found in file...")
|
---|
| 122 | {
|
---|
| 123 | SearchNodeByNameVisitor search("start");
|
---|
| 124 | search.Apply(root, SearchNodeByNameVisitor::FIRST);
|
---|
| 125 | BranchGroup *local_root = search.GetNode();
|
---|
| 126 |
|
---|
| 127 | if (local_root == NULL)
|
---|
| 128 | {
|
---|
| 129 | OUT1("Note: BranchGroup named \"start\" not found - using root node");
|
---|
| 130 | local_root = root;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | placer->SetLibrary(local_root);
|
---|
| 134 | BranchGroup *viz = placer->GetLibraryVisualization();
|
---|
| 135 | mRenderArea->SetSceneRoot(viz);
|
---|
| 136 | mRenderArea->SetCamera(new Camera);
|
---|
| 137 | mRenderArea->GetCamera()->ViewAll(viz->getBoundingSphere());
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | } else
|
---|
| 141 | OUT1("Unknown placer command");
|
---|
| 142 | }
|
---|
[2694] | 143 | */
|
---|
[2693] | 144 |
|
---|
| 145 | } |
---|