]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/placement.cxx
Merge branch 'frohlich/weak' into next
[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/SGSceneUserData.hxx>
14
15 \f
16 ////////////////////////////////////////////////////////////////////////
17 // Implementation of SGModelPlacement.
18 ////////////////////////////////////////////////////////////////////////
19
20 SGModelPlacement::SGModelPlacement () :
21     _position(SGGeod::fromRad(0, 0)),
22     _roll_deg(0),
23     _pitch_deg(0),
24     _heading_deg(0),
25     _selector(new osg::Switch),
26     _transform(new osg::PositionAttitudeTransform)
27 {
28 }
29
30 SGModelPlacement::~SGModelPlacement ()
31 {
32 }
33
34 void
35 SGModelPlacement::init( osg::Node * model )
36 {
37   if (model != 0) {
38       _transform->addChild(model);
39   }
40   _selector->addChild(_transform.get());
41   _selector->setValue(0, 1);
42 }
43
44 void
45 SGModelPlacement::update()
46 {
47   // The cartesian position
48   SGVec3d position = SGVec3d::fromGeod(_position);
49   _transform->setPosition(toOsg(position));
50
51   // The orientation, composed from the horizontal local orientation and the
52   // orientation wrt the horizontal local frame
53   SGQuatd orient = SGQuatd::fromLonLat(_position);
54   orient *= SGQuatd::fromYawPitchRollDeg(_heading_deg, _pitch_deg, _roll_deg);
55   // Convert to the scenegraph orientation where we just rotate around
56   // the y axis 180 degrees.
57   orient *= SGQuatd::fromRealImag(0, SGVec3d(0, 1, 0));
58
59   _transform->setAttitude(toOsg(orient));
60 }
61
62 bool
63 SGModelPlacement::getVisible () const
64 {
65   return _selector->getValue(0);
66 }
67
68 void
69 SGModelPlacement::setVisible (bool visible)
70 {
71   _selector->setValue(0, visible);
72 }
73
74 void
75 SGModelPlacement::setLongitudeDeg (double lon_deg)
76 {
77   _position.setLongitudeDeg(lon_deg);
78 }
79
80 void
81 SGModelPlacement::setLatitudeDeg (double lat_deg)
82 {
83   _position.setLatitudeDeg(lat_deg);
84 }
85
86 void
87 SGModelPlacement::setElevationFt (double elev_ft)
88 {
89   _position.setElevationFt(elev_ft);
90 }
91
92 void
93 SGModelPlacement::setPosition (double lon_deg, double lat_deg, double elev_ft)
94 {
95   _position = SGGeod::fromDegFt(lon_deg, lat_deg, elev_ft);
96 }
97
98 void
99 SGModelPlacement::setPosition(const SGGeod& position)
100 {
101   _position = position;
102 }
103
104 void
105 SGModelPlacement::setRollDeg (double roll_deg)
106 {
107   _roll_deg = roll_deg;
108 }
109
110 void
111 SGModelPlacement::setPitchDeg (double pitch_deg)
112 {
113   _pitch_deg = pitch_deg;
114 }
115
116 void
117 SGModelPlacement::setHeadingDeg (double heading_deg)
118 {
119   _heading_deg = heading_deg;
120 }
121
122 void
123 SGModelPlacement::setOrientation (double roll_deg, double pitch_deg,
124                                   double heading_deg)
125 {
126   _roll_deg = roll_deg;
127   _pitch_deg = pitch_deg;
128   _heading_deg = heading_deg;
129 }
130
131 void
132 SGModelPlacement::setOrientation (const SGQuatd& orientation)
133 {
134   orientation.getEulerDeg(_heading_deg, _pitch_deg, _roll_deg);
135 }
136
137 void
138 SGModelPlacement::setReferenceTime(const double& referenceTime)
139 {
140   SGSceneUserData* userData;
141   userData = SGSceneUserData::getOrCreateSceneUserData(_transform);
142   SGSceneUserData::Velocity* vel = userData->getOrCreateVelocity();
143   vel->referenceTime = referenceTime;
144 }
145
146 void
147 SGModelPlacement::setBodyLinearVelocity(const SGVec3d& linear)
148 {
149   SGSceneUserData* userData;
150   userData = SGSceneUserData::getOrCreateSceneUserData(_transform);
151   SGSceneUserData::Velocity* vel = userData->getOrCreateVelocity();
152   vel->linear = SGVec3d(-linear[0], linear[1], -linear[2]);
153 }
154
155 void
156 SGModelPlacement::setBodyAngularVelocity(const SGVec3d& angular)
157 {
158   SGSceneUserData* userData;
159   userData = SGSceneUserData::getOrCreateSceneUserData(_transform);
160   SGSceneUserData::Velocity* vel = userData->getOrCreateVelocity();
161   vel->angular = SGVec3d(-angular[0], angular[1], -angular[2]);
162 }
163
164 // end of model.cxx