AnimatLab  2
Test
OsgLight.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "OsgMovableItem.h"
9 #include "OsgBody.h"
10 #include "OsgRigidBody.h"
11 #include "OsgJoint.h"
12 #include "OsgStructure.h"
13 #include "OsgLight.h"
14 #include "OsgUserData.h"
15 #include "OsgUserDataVisitor.h"
16 #include "OsgDragger.h"
17 #include "OsgSimulator.h"
18 
19 namespace OsgAnimatSim
20 {
21  namespace Environment
22  {
23 
31 {
32  m_lpThisLI = NULL;
33  SetThisPointers();
34 }
35 
43 {
44  try
45  {
46  DeleteGraphics();
47  DeletePhysics(false);
48  }
49  catch(...)
50  {Std_TraceMsg(0, "Caught Error in desctructor of VsBox\r\n", "", -1, false, true);}
51 }
52 
53 void OsgLight::SetThisPointers()
54 {
55  OsgMovableItem::SetThisPointers();
56 
57  m_lpThisLI = dynamic_cast<Light *>(this);
58  if(!m_lpThisLI)
59  THROW_TEXT_ERROR(Osg_Err_lThisPointerNotDefined, Osg_Err_strThisPointerNotDefined, "m_lpThisLI, " + m_lpThisAB->Name());
60 }
61 
62 osg::MatrixTransform *OsgLight::ParentOSG()
63 {
64  return GetOsgSimulator()->OSGRoot();
65 }
66 
67 void OsgLight::Enabled(bool bVal)
68 {
70 
71  if(GetOsgSimulator() && GetOsgSimulator()->OSGRoot())
72  {
73  osg::StateSet *rootStateSet = GetOsgSimulator()->OSGRoot()->getOrCreateStateSet();
74 
75  if(bVal)
76  rootStateSet->setMode( GetGlLight(), osg::StateAttribute::ON );
77  else
78  rootStateSet->setMode( GetGlLight(), osg::StateAttribute::OFF );
79  }
80 }
81 
82 void OsgLight::Position(CStdFPoint &oPoint, bool bUseScaling, bool bFireChangeEvent, bool bUpdateMatrix)
83 {
84  Light::Position(oPoint, bUseScaling, bFireChangeEvent, bUpdateMatrix);
85 
86  //Reposition the light.
87  if(m_osgLight.valid())
88  {
89  osg::Vec4 position(m_oPosition.x, m_oPosition.y, m_oPosition.z, 1);
90  m_osgLight->setPosition(position);
91  }
92 }
93 
94 void OsgLight::Ambient(CStdColor &aryColor)
95 {
96  Light::Ambient(aryColor);
97 
98  //Reposition the light.
99  if(m_osgLight.valid())
100  {
101  osg::Vec4 color(m_vAmbient.r(), m_vAmbient.g(), m_vAmbient.b(), 1.0);
102  m_osgLight->setAmbient(color);
103  }
104 }
105 
106 void OsgLight::Diffuse(CStdColor &aryColor)
107 {
108  Light::Diffuse(aryColor);
109 
110  //Reposition the light.
111  if(m_osgLight.valid())
112  {
113  osg::Vec4 color(m_vDiffuse.r(), m_vDiffuse.g(), m_vDiffuse.b(), 1.0);
114  m_osgLight->setDiffuse(color);
115  }
116 }
117 
118 void OsgLight::Specular(CStdColor &aryColor)
119 {
120  Light::Specular(aryColor);
121 
122  //Reposition the light.
123  if(m_osgLight.valid())
124  {
125  osg::Vec4 specular(m_vSpecular.r(), m_vSpecular.g(), m_vSpecular.b(), 1.0);
126  m_osgLight->setSpecular(specular);
127  }
128 }
129 
130 void OsgLight::SetAttenuation()
131 {
132  if(m_osgLight.valid())
133  {
134  m_osgLight->setConstantAttenuation(m_fltConstantAttenRatio);
135 
137  {
138  float fltAtten = 1/m_fltLinearAttenDistance;
139  m_osgLight->setLinearAttenuation(fltAtten);
140  }
141  else
142  m_osgLight->setLinearAttenuation(0);
143 
145  {
146  float fltAtten = 1/m_fltQuadraticAttenDistance;
147  m_osgLight->setQuadraticAttenuation(fltAtten);
148  }
149  else
150  m_osgLight->setQuadraticAttenuation(0);
151  }
152 }
153 
154 int OsgLight::GetGlLight()
155 {
156  switch (m_iLightNum)
157  {
158  case 0:
159  return GL_LIGHT0;
160  case 1:
161  return GL_LIGHT1;
162  case 2:
163  return GL_LIGHT2;
164  case 3:
165  return GL_LIGHT3;
166  case 4:
167  return GL_LIGHT4;
168  case 5:
169  return GL_LIGHT5;
170  case 6:
171  return GL_LIGHT6;
172  case 7:
173  return GL_LIGHT7;
174  default:
175  return GL_LIGHT0;
176  }
177 }
178 
179 void OsgLight::SetupLighting()
180 {
181  // Set up lighting.
182  osg::Vec4 ambient(m_vAmbient.r(), m_vAmbient.g(), m_vAmbient.b(), 1.0);
183  osg::Vec4 diffuse(m_vDiffuse.r(), m_vDiffuse.g(), m_vDiffuse.b(), 1.0);
184  osg::Vec4 specular(m_vSpecular.r(), m_vSpecular.g(), m_vSpecular.b(), 1.0);
185  osg::Vec4 position(m_oPosition.x, m_oPosition.y, m_oPosition.z, 1);
186 
187  m_osgLight = new osg::Light(m_iLightNum);
188  m_osgLight->setAmbient(ambient);
189  m_osgLight->setDiffuse(diffuse);
190  m_osgLight->setSpecular(specular);
191  m_osgLight->setPosition(position);
192  SetAttenuation();
193 
194  m_osgLightSource = new osg::LightSource;
195  m_osgLightSource->setLight(m_osgLight.get());
196  GetOsgSimulator()->OSGRoot()->addChild(m_osgLightSource.get());
197 
199 }
200 
201 
202 void OsgLight::SetupGraphics()
203 {
204  OsgMovableItem::SetupGraphics();
205 
206  SetupLighting();
207 }
208 
209 void OsgLight::CreateGraphicsGeometry()
210 {
212 }
213 
214 void OsgLight::DeleteGraphics()
215 {
216  OsgMovableItem::DeleteGraphics();
217 
218  if(m_osgLightSource.valid() && GetOsgSimulator() && GetOsgSimulator()->OSGRoot())
219  {
220  if(GetOsgSimulator()->OSGRoot()->containsNode(m_osgLightSource.get()))
221  GetOsgSimulator()->OSGRoot()->removeChild(m_osgLightSource.get());
222 
223  osg::StateSet *rootStateSet = GetOsgSimulator()->OSGRoot()->getOrCreateStateSet();
224  rootStateSet->setMode( GetGlLight(), osg::StateAttribute::OFF );
225  }
226 
227  m_osgLight.release();
228  m_osgLightSource.release();
229 }
230 
231 void OsgLight::Create()
232 {
233  CreateGeometry();
234  CreateItem();
235 
236  Light::Create();
237 }
238 
240 {
241  OsgMovableItem::Physics_ResetSimulation();
242 
243  Light::ResetSimulation();
244 }
245 
246 void OsgLight::Physics_Resize()
247 {
248  //First lets get rid of the current current geometry and then put new geometry in place.
249  if(m_osgNode.valid())
250  {
251  osg::Geode *osgGroup = NULL;
252  if(m_osgGeometryRotationMT.valid())
253  osgGroup = dynamic_cast<osg::Geode *>(m_osgGeometryRotationMT->getChild(0));
254  else
255  osgGroup = dynamic_cast<osg::Geode *>(m_osgNode.get());
256 
257  if(!osgGroup)
258  THROW_TEXT_ERROR(Osg_Err_lNodeNotGeode, Osg_Err_strNodeNotGeode, m_lpThisAB->Name());
259 
260  if(osgGroup && osgGroup->containsDrawable(m_osgGeometry.get()))
261  osgGroup->removeDrawable(m_osgGeometry.get());
262 
263  m_osgGeometry.release();
264 
265  //Create a new box geometry with the new sizes.
266  CreateGraphicsGeometry();
267  m_osgGeometry->setName(m_lpThisAB->Name() + "_Geometry");
268 
269  //Add it to the geode.
270  osgGroup->addDrawable(m_osgGeometry.get());
271 
272  //Now lets re-adjust the gripper size.
273  if(m_osgDragger.valid())
274  m_osgDragger->SetupMatrix();
275 
276  //Reset the user data for the new parts.
277  if(m_osgNodeGroup.valid())
278  {
279  osg::ref_ptr<OsgUserDataVisitor> osgVisitor = new OsgUserDataVisitor(this);
280  osgVisitor->traverse(*m_osgNodeGroup);
281  }
282  }
283 
284  SetAttenuation();
285 }
286 
287 void OsgLight::Physics_SetColor()
288 {
289  SetColor(*m_lpThisMI->Ambient(), *m_lpThisMI->Diffuse(), *m_lpThisMI->Specular(), m_lpThisMI->Shininess());
290 }
291 
292  } // Environment
293 } //OsgAnimatSim
virtual bool Enabled()
Tells whether this light is enabled.
Definition: Light.cpp:78
osg::ref_ptr< osg::MatrixTransform > m_osgGeometryRotationMT
CStdColor m_vAmbient
The ambient color to apply to this part. It is specified as red, green, blue, and alpha...
Definition: MovableItem.h:79
Declares the vortex Light class.
float m_fltQuadraticAttenDistance
The quadratic attenuation distance.
Definition: Light.h:45
float m_fltLinearAttenDistance
The linear attenuation distance.
Definition: Light.h:42
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual CStdColor * Ambient()
Gets the ambient color value.
virtual CStdColor * Specular()
Gets the specular color.
int m_iLightNum
Zero-based index of the light number. OSG only allows 8 lights.
Definition: Light.h:36
virtual float Radius()
Gets the radius.
Definition: Light.cpp:122
virtual int LongtitudeSegments()
Gets the longtitude segments.
Definition: Light.cpp:183
CStdColor m_vDiffuse
The diffuse color to apply to this part. It is specified as red, green, blue, and alpha...
Definition: MovableItem.h:82
float m_fltConstantAttenRatio
The constant attenuation ratio.
Definition: Light.h:39
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
virtual CStdFPoint Position()
Gets the local position. (m_oPosition)
CStdColor m_vSpecular
The specular color to apply to this part. It is specified as red, green, blue, and alpha...
Definition: MovableItem.h:85
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
CStdFPoint m_oPosition
These are rotation and position coords relative to the parent if this is a body part.
Definition: MovableItem.h:36
virtual int LatitudeSegments()
Gets the latitude segments.
Definition: Light.cpp:158
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: OsgLight.cpp:239
virtual CStdColor * Diffuse()
Gets the diffuse color.
virtual ~OsgLight()
Destructor.
Definition: OsgLight.cpp:42
Light(void)
Default constructor.
Definition: Light.cpp:47
Declares the vortex structure class.
OsgLight()
Default constructor.
Definition: OsgLight.cpp:30
osg::Geometry ANIMAT_OSG_PORT * CreateSphereGeometry(int latres, int longres, float radius)