ADTF Display Toolbox
Loading...
Searching...
No Matches
baseobject.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <osg/Array>
10#include <osg/ShapeDrawable>
11#include <osg/Switch>
12
13#include <osg/PositionAttitudeTransform>
14#include <osg/Geode>
15
16#include <a_utils/a_utils.h>
17#include <memory>
18
19namespace adtf
20{
21
22namespace disptb
23{
24
25namespace mixinlib
26{
27
28namespace endor
29{
30
41class cBaseObject : public osg::Switch
42{
43protected:
45 tUInt32 m_nId;
46
47 // object
49 osg::ref_ptr<osg::PositionAttitudeTransform> m_pTransform;
51 osg::ref_ptr<osg::ShapeDrawable> m_pDrawable;
53 osg::ref_ptr<osg::Box> m_pBox;
54
55 // history
57 osg::ref_ptr<osg::Geometry> m_pHistoryGeom;
59 osg::ref_ptr<osg::DrawArrays> m_pHistoryArray;
60
61 // motion vector
63 osg::ref_ptr<osg::Geometry> m_pMotionGeom;
65 osg::ref_ptr<osg::DrawArrays> m_pMotionArray;
66
67 // payload data
69 osg::Vec3d m_vPos;
70
71 // position history
73 osg::ref_ptr<osg::Vec3Array> m_pPositionHistory;
76
77 // motion vector data
79 osg::ref_ptr<osg::Vec3Array> m_pMotionVector;
80
81 // color array for both history an motion vector
83 osg::ref_ptr<osg::Vec4Array> m_pColorArray;
84
86 osg::ref_ptr<osg::Switch> m_pSelectionSwitch;
87 osg::ref_ptr<osg::Geode> m_pSelectionGeode;
88 osg::ref_ptr<osg::ShapeDrawable> m_pSelectionDrawable;
89 osg::ref_ptr<osg::Box> m_pSelectionBox;
91
92 // state data
101
102public:
107 cBaseObject(tUInt32 nId):
108 m_nId(nId),
110 m_bHistory(tTrue),
111 m_bMotionVector(tTrue),
112 m_bSelected(tFalse),
113 m_bUpdated(tFalse)
114 {
115 m_pPositionHistory = new osg::Vec3Array;
116 m_pMotionVector = new osg::Vec3Array;
117 m_pMotionVector->resize(2);
118 m_pColorArray = new osg::Vec4Array;
119 m_pColorArray->resize(1);
120 m_pColorArray->at(0).x() = -1.0; // to make sure that the next SetColor will initialize the state
121
122 // this will move and rotate the shape
123 m_pTransform = new osg::PositionAttitudeTransform;
124 addChild(m_pTransform.get());
125
126 // now we create a osg::Geode and create our box
127 osg::Geode* pGeode = new osg::Geode;
128 m_pTransform->addChild(pGeode);
129 m_pDrawable = new osg::ShapeDrawable;
130 pGeode->addDrawable(m_pDrawable);
131 m_pBox = new osg::Box();
132 m_pDrawable->setShape(m_pBox.get());
133 m_pDrawable->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
134
135 // prepare selection bounding box
136 m_pSelectionSwitch = new osg::Switch;
137 m_pSelectionGeode = new osg::Geode;
138 m_pSelectionDrawable = new osg::ShapeDrawable;
139 m_pSelectionDrawable->setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 0.4f));
140 m_pSelectionBox = new osg::Box;
141 m_pSelectionDrawable->setShape(m_pSelectionBox);
142
143 m_pSelectionGeode->addDrawable(m_pSelectionDrawable);
144 m_pSelectionGeode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
145 m_pSelectionGeode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
146
147 m_pSelectionSwitch->addChild(m_pSelectionGeode);
148 m_pSelectionSwitch->setAllChildrenOff();
149
150 m_pTransform->addChild(m_pSelectionSwitch);
151
152 // this will be the history trail
153 m_pHistoryArray = new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP);
154 m_pHistoryGeom = new osg::Geometry;
155 m_pHistoryGeom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
156 m_pHistoryGeom->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::OFF);
157 osg::Geode* pHistoryGeode = new osg::Geode;
158
159 m_pHistoryGeom->addPrimitiveSet(m_pHistoryArray.get());
160 m_pHistoryGeom->setVertexArray(m_pPositionHistory.get());
161 m_pHistoryGeom->setColorArray(m_pColorArray.get(), osg::Array::BIND_OVERALL);
162 // it changes almost every frame and is only one line so we do not need to use display lists.
163 m_pHistoryGeom->setUseDisplayList(tFalse);
164 pHistoryGeode->addDrawable(m_pHistoryGeom.get());
165
166 addChild(pHistoryGeode);
167
168 // this will be the motion vector
169 m_pMotionArray = new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP);
170 m_pMotionGeom = new osg::Geometry;
171 m_pMotionGeom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
172 m_pMotionGeom->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::OFF);
173 osg::Geode* pMotionGeode = new osg::Geode;
174
175 m_pMotionGeom->addPrimitiveSet(m_pMotionArray.get());
176 m_pMotionGeom->setVertexArray(m_pMotionVector.get());
177 m_pMotionArray->setCount(2);
178 m_pMotionGeom->setColorArray(m_pColorArray.get(), osg::Array::BIND_OVERALL);
179 // it changes almost every frame and is only one line so we do not need to use display lists.
180 m_pMotionGeom->setUseDisplayList(tFalse);
181 pMotionGeode->addDrawable(m_pMotionGeom.get());
182
183 addChild(pMotionGeode);
184
185 SetColor(1.0, 1.0, 1.0, 1.0);
186 }
187
191 virtual ~cBaseObject()
192 {
193 }
194
199 tUInt32 GetId()
200 {
201 return m_nId;
202 }
203
209 tResult Update()
210 {
211 m_bUpdated = tTrue;
212 RETURN_NOERROR;
213 }
214
219 tBool IsUpdated()
220 {
221 return m_bUpdated;
222 }
223
228 tResult Reset()
229 {
230 m_bUpdated = tFalse;
231 RETURN_NOERROR;
232 }
233
245 virtual tResult SetHistory(tBool bEnabled, tUInt32 nCount = 0)
246 {
247 if (!bEnabled && m_bHistory)
248 {
249 // if it hase been enabled clear the data.
250 m_pPositionHistory->clear();
251 }
252
253 m_bHistory = bEnabled;
254 m_nHistorySize = nCount;
255
256 // enable/disable the history node
257 setValue(1, m_bHistory);
258
259 RETURN_NOERROR;
260 }
261
267 virtual tResult SetMotionVector(tBool bEnabled)
268 {
269 m_bMotionVector = bEnabled;
270 setValue(2, m_bMotionVector);
271 RETURN_NOERROR;
272 }
273
279 virtual tResult SetPosition(tFloat64 fPosX, tFloat64 fPosY, tFloat64 fPosZ)
280 {
281 // store the position vector
282 m_vPos.x() = fPosX;
283 m_vPos.y() = fPosY;
284 m_vPos.z() = fPosZ;
285
286 m_pTransform->setPosition(m_vPos);
287
288 // update the history data
289 if (m_bHistory)
290 {
291 m_pPositionHistory->push_back(m_vPos);
293 {
294 // this is expensive!!!
295 m_pPositionHistory->erase(m_pPositionHistory->begin());
296 }
297
298 // update the amount of history data
299 m_pHistoryArray->setCount(m_pPositionHistory->size());
300 // make sure the the display list and the bounding sphere is recomputed during rendering.
301 m_pPositionHistory->dirty();
302 m_pHistoryGeom->dirtyBound();
303 }
304
305 RETURN_NOERROR;
306 }
307
312 osg::Vec3d GetPosition()
313 {
314 return m_pTransform->getPosition();
315 }
316
322 virtual tResult SetRotation(tFloat64 fRotX, tFloat64 fRotY, tFloat64 fRotZ)
323 {
324 m_pTransform->setAttitude(
325 osg::Quat(fRotX, osg::Vec3(1, 0, 0), fRotY, osg::Vec3(0, 1, 0), fRotZ, osg::Vec3(0, 0, 1)));
326 RETURN_NOERROR;
327 }
328
333 osg::Vec3d GetRotation()
334 {
335 return m_pTransform->getAttitude().asVec3();
336 }
337
343 virtual tResult SetSize(tFloat64 fSizeX, tFloat64 fSizeY, tFloat64 fSizeZ)
344 {
345 // update the size of the object
346 auto oNewDimensions = osg::Vec3d(fSizeX / 2.0, fSizeY / 2.0, fSizeZ / 2.0);
347 if (oNewDimensions != m_pBox->getHalfLengths())
348 {
349 m_pBox->setHalfLengths(oNewDimensions);
350 m_pDrawable->build();
351 m_pDrawable->dirtyGLObjects();
352 m_pDrawable->dirtyBound();
353
354 // update selection bounding sphere if neccessary
355 if (m_bSelected)
356 {
357 RETURN_IF_FAILED(UpdateSelection());
358 }
359 }
360 RETURN_NOERROR;
361 }
362
367 osg::Vec3d GetSize()
368 {
369 return m_pBox->getHalfLengths() * 2;
370 }
371
378 virtual tResult SetColor(tFloat64 fRed, tFloat64 fGreen, tFloat64 fBlue, tFloat64 fAlpha)
379 {
380 // store the color
381 osg::Vec4 oColor(fRed, fGreen, fBlue, fAlpha);
382 if (m_pColorArray->at(0) != oColor)
383 {
384 m_pDrawable->setColor(oColor);
385 m_pColorArray->at(0) = oColor;
386
387 if (fAlpha < 1.0)
388 {
389 m_pDrawable->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
390 m_pDrawable->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
391 }
392 else
393 {
394 m_pDrawable->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::OFF);
395 m_pDrawable->getOrCreateStateSet()->setRenderingHint(osg::StateSet::DEFAULT_BIN);
396 }
397
398 // make sure the display list is updated
399 m_pDrawable->dirtyDisplayList();
400 }
401 RETURN_NOERROR;
402 }
403
410 virtual tResult SetMotion(tFloat64 fMotionX, tFloat64 fMotionY, tFloat64 fMotionZ)
411 {
412 // calculate its end vertices
413 m_pMotionVector->at(0) = m_vPos;
414 m_pMotionVector->at(1) = osg::Vec3(fMotionX, fMotionY, fMotionZ) + m_vPos;
415 m_pMotionVector->dirty();
416 // make sure that the bounding sphere is recomputed during rendering.
417 m_pMotionGeom->dirtyBound();
418 RETURN_NOERROR;
419 }
420
425 osg::Vec3d GetMotion()
426 {
427 return m_pMotionVector->at(1) - m_vPos;
428 }
429
435 virtual tResult SetSelected(tBool bSelected)
436 {
437 if (bSelected && !m_bSelected)
438 {
439 RETURN_IF_FAILED(UpdateSelection());
440 }
441
442 // enable/disable the sphere
443 m_pSelectionSwitch->setValue(0, bSelected);
444 m_bSelected = bSelected;
445
446 RETURN_NOERROR;
447 }
448
453 virtual tBool IsSelected()
454 {
455 return m_bSelected;
456 }
457
458protected:
463 virtual tResult UpdateSelection()
464 {
465 // update the size of the selection box
466 osg::Vec3 vSize = m_pBox->getHalfLengths();
467 vSize += osg::Vec3(0.1f, 0.1f, 0.1f);
468 m_pSelectionBox->setHalfLengths(vSize);
469 m_pSelectionDrawable->dirtyDisplayList();
470
471 RETURN_NOERROR;
472 }
473};
474
475} // namespace endor
476
478using endor::cBaseObject;
479
480} // namespace mixinlib
481
482} // namespace disptb
483
484} // namespace adtf
cBaseObject(tUInt32 nId)
Constructor.
Definition baseobject.h:107
tBool IsUpdated()
Returns whether or not this object has been updated since the last Reset call.
Definition baseobject.h:219
virtual tResult SetHistory(tBool bEnabled, tUInt32 nCount=0)
Enables or disable the history trail of this object.
Definition baseobject.h:245
osg::Vec3d GetMotion()
Returns the current motion vector.
Definition baseobject.h:425
tResult Reset()
Resets the updated property to false.
Definition baseobject.h:228
virtual tResult SetRotation(tFloat64 fRotX, tFloat64 fRotY, tFloat64 fRotZ)
Sets the rotation (attitude) of the object.
Definition baseobject.h:322
osg::ref_ptr< osg::ShapeDrawable > m_pDrawable
The drawable for the box.
Definition baseobject.h:51
tBool m_bUpdated
Whether or not this object has been updated.
Definition baseobject.h:100
virtual tResult SetMotionVector(tBool bEnabled)
Enables or disables the motion vector.
Definition baseobject.h:267
tBool m_bSelected
Whether or not this object is selected.
Definition baseobject.h:98
virtual tResult SetMotion(tFloat64 fMotionX, tFloat64 fMotionY, tFloat64 fMotionZ)
Sets the motion vector of the object.
Definition baseobject.h:410
osg::ref_ptr< osg::Vec3Array > m_pMotionVector
The start and end point of the motion vector.
Definition baseobject.h:79
osg::ref_ptr< osg::DrawArrays > m_pMotionArray
This will do the actual drawing of the motion vector.
Definition baseobject.h:65
osg::Vec3d GetRotation()
Returns the current rotation.
Definition baseobject.h:333
virtual tBool IsSelected()
Returns the selected property of the object.
Definition baseobject.h:453
osg::ref_ptr< osg::Geometry > m_pHistoryGeom
Geometry object used for rendering the history trail.
Definition baseobject.h:57
tUInt32 GetId()
Returns the id of the object.
Definition baseobject.h:199
osg::Vec3d GetSize()
Returns the current size.
Definition baseobject.h:367
osg::ref_ptr< osg::Vec3Array > m_pPositionHistory
This will store old positions for drawing the history trail.
Definition baseobject.h:73
osg::ref_ptr< osg::DrawArrays > m_pHistoryArray
This will do the actual drawing of the history trail.
Definition baseobject.h:59
tResult Update()
Base Implementation sets the updated property to true.
Definition baseobject.h:209
osg::Vec3d m_vPos
The current position of the object.
Definition baseobject.h:69
virtual tResult SetSelected(tBool bSelected)
Gives the object a chance to change its appearance when being selected or unselected.
Definition baseobject.h:435
virtual tResult SetSize(tFloat64 fSizeX, tFloat64 fSizeY, tFloat64 fSizeZ)
Sets the dimensions of the object.
Definition baseobject.h:343
virtual tResult SetColor(tFloat64 fRed, tFloat64 fGreen, tFloat64 fBlue, tFloat64 fAlpha)
Sets the color of the object.
Definition baseobject.h:378
virtual tResult UpdateSelection()
Used to update the selection sphere.
Definition baseobject.h:463
osg::ref_ptr< osg::PositionAttitudeTransform > m_pTransform
The transform that applies the translation and rotation to the box.
Definition baseobject.h:49
virtual tResult SetPosition(tFloat64 fPosX, tFloat64 fPosY, tFloat64 fPosZ)
Sets the position of the object.
Definition baseobject.h:279
tBool m_bHistory
Whether or not to show the history trail.
Definition baseobject.h:94
osg::Vec3d GetPosition()
Returns the current position.
Definition baseobject.h:312
osg::ref_ptr< osg::Geometry > m_pMotionGeom
Geometry object used for rendering the motion vector.
Definition baseobject.h:63
tBool m_bMotionVector
Whether or not to show the motion vector.
Definition baseobject.h:96
osg::ref_ptr< osg::Vec4Array > m_pColorArray
The color array used for drawing the history trail and the motion vector.
Definition baseobject.h:83
osg::ref_ptr< osg::Box > m_pBox
The box that represents the object.
Definition baseobject.h:53
tUInt32 m_nHistorySize
The maximum amount of positions in the m_pPositionHistory array.
Definition baseobject.h:75
Namespace for functionality provided by the Mixin Library.
Definition baseobject.h:26
Namespace for functionality provided by the Display Toolbox.
Main namespace.