]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/placement.cxx
Placement can contain multiple nodes.
[simgear.git] / simgear / scene / model / placement.cxx
1 // placement.cxx - manage the placment of a 3D model.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #include <simgear_config.h>
8 #endif
9
10 #include "placement.hxx"
11
12 #include <simgear/compiler.h>
13 #include <simgear/scene/util/OsgMath.hxx>
14 #include <simgear/scene/util/SGSceneUserData.hxx>
15
16 \f
17 ////////////////////////////////////////////////////////////////////////
18 // Implementation of SGModelPlacement.
19 ////////////////////////////////////////////////////////////////////////
20
21 SGModelPlacement::SGModelPlacement () :
22     _position(SGGeod::fromRad(0, 0)),
23     _roll_deg(0),
24     _pitch_deg(0),
25     _heading_deg(0),
26     _selector(new osg::Switch),
27     _transform(new osg::PositionAttitudeTransform)
28 {
29     _selector->addChild(_transform.get());
30 }
31
32 SGModelPlacement::~SGModelPlacement ()
33 {
34 }
35
36 void
37 SGModelPlacement::init( osg::Node * model )
38 {
39   // remove previous models (in case of reinit)
40   _transform->removeChild(0, _transform->getNumChildren());
41   if (model != 0) {
42       _transform->addChild(model);
43   }
44   _selector->setValue(0, 1);
45 }
46
47 void
48 SGModelPlacement::add( osg::Node* model )
49 {
50     if (model != 0) {
51         _transform->addChild(model);
52     }
53 }
54
55 void SGModelPlacement::clear()
56 {
57     _selector = NULL;
58     _transform = NULL;
59 }
60
61 void
62 SGModelPlacement::update()
63 {
64   // The cartesian position
65   SGVec3d position = SGVec3d::fromGeod(_position);
66   _transform->setPosition(toOsg(position));
67
68   // The orientation, composed from the horizontal local orientation and the
69   // orientation wrt the horizontal local frame
70   SGQuatd orient = SGQuatd::fromLonLat(_position);
71   orient *= SGQuatd::fromYawPitchRollDeg(_heading_deg, _pitch_deg, _roll_deg);
72   // Convert to the scenegraph orientation where we just rotate around
73   // the y axis 180 degrees.
74   orient *= SGQuatd::fromRealImag(0, SGVec3d(0, 1, 0));
75
76   _transform->setAttitude(toOsg(orient));
77 }
78
79 bool
80 SGModelPlacement::getVisible () const
81 {
82   return _selector->getValue(0);
83 }
84
85 void
86 SGModelPlacement::setVisible (bool visible)
87 {
88   _selector->setValue(0, visible);
89 }
90
91 void
92 SGModelPlacement::setPosition(const SGGeod& position)
93 {
94   _position = position;
95 }
96
97 void
98 SGModelPlacement::setRollDeg (double roll_deg)
99 {
100   _roll_deg = roll_deg;
101 }
102
103 void
104 SGModelPlacement::setPitchDeg (double pitch_deg)
105 {
106   _pitch_deg = pitch_deg;
107 }
108
109 void
110 SGModelPlacement::setHeadingDeg (double heading_deg)
111 {
112   _heading_deg = heading_deg;
113 }
114
115 void
116 SGModelPlacement::setOrientation (double roll_deg, double pitch_deg,
117                                   double heading_deg)
118 {
119   _roll_deg = roll_deg;
120   _pitch_deg = pitch_deg;
121   _heading_deg = heading_deg;
122 }
123
124 void
125 SGModelPlacement::setOrientation (const SGQuatd& orientation)
126 {
127   orientation.getEulerDeg(_heading_deg, _pitch_deg, _roll_deg);
128 }
129
130 void
131 SGModelPlacement::setReferenceTime(const double& referenceTime)
132 {
133   SGSceneUserData* userData;
134   userData = SGSceneUserData::getOrCreateSceneUserData(_transform.get());
135   SGSceneUserData::Velocity* vel = userData->getOrCreateVelocity();
136   vel->referenceTime = referenceTime;
137 }
138
139 void
140 SGModelPlacement::setBodyLinearVelocity(const SGVec3d& linear)
141 {
142   SGSceneUserData* userData;
143   userData = SGSceneUserData::getOrCreateSceneUserData(_transform.get());
144   SGSceneUserData::Velocity* vel = userData->getOrCreateVelocity();
145   vel->linear = SGVec3d(-linear[0], linear[1], -linear[2]);
146 }
147
148 void
149 SGModelPlacement::setBodyAngularVelocity(const SGVec3d& angular)
150 {
151   SGSceneUserData* userData;
152   userData = SGSceneUserData::getOrCreateSceneUserData(_transform.get());
153   SGSceneUserData::Velocity* vel = userData->getOrCreateVelocity();
154   vel->angular = SGVec3d(-angular[0], angular[1], -angular[2]);
155 }
156
157 // end of model.cxx