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