1 | #include "ObjectPlacer.h"
|
---|
2 | #include "Matrix4x4.h"
|
---|
3 | #include "SceneGraph.h"
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace GtpVisibilityPreprocessor
|
---|
8 | {
|
---|
9 |
|
---|
10 | // ---------------------------------------------------------------------------
|
---|
11 | // ObjectPlacer implementation
|
---|
12 | // ---------------------------------------------------------------------------
|
---|
13 |
|
---|
14 | ObjectPlacer::ObjectPlacer()
|
---|
15 | : mCurrentObject(0)
|
---|
16 | {
|
---|
17 | }
|
---|
18 |
|
---|
19 |
|
---|
20 | void ObjectPlacer::AddObject(SceneGraphLeaf *obj)
|
---|
21 | {
|
---|
22 | mDynamicObjects.push_back(obj);
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | void ObjectPlacer::NextObject()
|
---|
27 | {
|
---|
28 | mCurrentObject = (mCurrentObject + 1) % (int)mDynamicObjects.size();
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | void ObjectPlacer::PreviousObject()
|
---|
33 | {
|
---|
34 | mCurrentObject = (mCurrentObject - 1) % (int)mDynamicObjects.size();
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | SceneGraphLeaf *ObjectPlacer::GetCurrentObject()
|
---|
39 | {
|
---|
40 | if (mCurrentObject >= mDynamicObjects.size())
|
---|
41 | return NULL;
|
---|
42 |
|
---|
43 | return mDynamicObjects[mCurrentObject];
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | void ObjectPlacer::PlaceObject(const Vector3 &pt)
|
---|
48 | {
|
---|
49 | Vector3 p = pt;
|
---|
50 |
|
---|
51 | SceneGraphLeaf *obj = mDynamicObjects[mCurrentObject];
|
---|
52 |
|
---|
53 | // set pivot to ground of object
|
---|
54 | p.y -= obj->GetOriginalBox().Min().y;
|
---|
55 | Matrix4x4 trafo = TranslationMatrix(p);
|
---|
56 |
|
---|
57 | obj->LoadTransform(trafo);
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | /*void TestApp::Placer(const String &parm)
|
---|
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 | }
|
---|
143 | */
|
---|
144 |
|
---|
145 | } |
---|