AnimatLab  2
Test
OsgSimulationWindow.cpp
1 #include "StdAfx.h"
2 #include "OsgCameraManipulator.h"
3 #include "OsgMovableItem.h"
4 #include "OsgBody.h"
5 #include "OsgRigidBody.h"
6 #include "OsgJoint.h"
7 #include "OsgStructure.h"
8 #include "OsgLight.h"
9 #include "OsgUserData.h"
10 #include "OsgSimulationWindow.h"
11 #include "OsgMouseSpring.h"
12 #include "OsgDraggerHandler.h"
13 #include "OsgDragger.h"
14 #include "OsgSimulator.h"
15 
16 namespace OsgAnimatSim
17 {
18  namespace Visualization
19  {
20 
21 OsgSimulationWindow::OsgSimulationWindow()
22 {
23  m_lpWinMgr = NULL;
24  m_lpTrackBody = NULL;
25  m_fltCameraPosX = m_fltCameraPosY = m_fltCameraPosZ = 0;
26  m_bEyePosSet = false;
27 }
28 
29 OsgSimulationWindow::~OsgSimulationWindow(void)
30 {
31 }
32 
33 CStdFPoint OsgSimulationWindow::GetCameraPosition()
34 {
35  osg::Vec3d vEye, vCenter, vUp;
36  float fltlookat=0;
37  m_osgViewer->getCamera()->getViewMatrixAsLookAt(vEye, vCenter, vUp, fltlookat);
38 
39  CStdFPoint vPos(vEye.x(), vEye.y(), vEye.z());
40 
41  m_fltCameraPosX = vEye.x()*m_lpSim->DistanceUnits();
42  m_fltCameraPosY = vEye.y()*m_lpSim->DistanceUnits();
43  m_fltCameraPosZ = vEye.z()*m_lpSim->DistanceUnits();
44 
45  return vPos;
46 }
47 
48 osg::Matrix OsgSimulationWindow::GetScreenMatrix()
49 {
50  osg::Matrix matrix = m_osgViewer->getCamera()->getViewMatrix() * m_osgViewer->getCamera()->getProjectionMatrix();
51  if(m_osgViewer->getCamera()->getViewport())
52  matrix.postMult(m_osgViewer->getCamera()->getViewport()->computeWindowMatrix());
53 
54  return matrix;
55 }
56 
57 osg::Viewport* OsgSimulationWindow::GetViewport()
58 {
59  return m_osgViewer->getCamera()->getViewport();
60 }
61 
63 {
64  if(m_lpTrackBody)
65  TrackCamera();
66 
67  m_osgViewer->frame();
68 }
69 
71 {
72  if(m_bTrackCamera)
73  {
74  m_lpTrackBody = NULL;
75 
77  {
78  Structure *lpStructure = m_lpSim->FindStructureFromAll(m_strLookAtStructureID);
79 
80  OsgBody *lpOsgBody = NULL;
81  BodyPart *lpBody=NULL;
83  lpBody = dynamic_cast<BodyPart *>(lpStructure->FindNode(m_strLookAtBodyID));
84 
85  if(lpBody)
86  SetCameraLookAt(lpBody->GetCurrentPosition(), bResetEyePos);
87  else
88  SetCameraLookAt(lpStructure->Position(), bResetEyePos);
89 
90  m_lpTrackBody = lpBody;
91  }
92  }
93  else
94  {
95  m_lpTrackBody = NULL;
96  CStdFPoint vDefault(0, 0, 0);
97  SetCameraLookAt(vDefault, bResetEyePos);
98  }
99 }
100 
101 void OsgSimulationWindow::SetCameraLookAt(CStdFPoint oTarget, bool bResetEyePos)
102 {
103  osg::Vec3d position(oTarget.x, oTarget.y, oTarget.z);
104 
105  osg::Vec3d vEye, vCenter, vUp;
106  float fltlookat=0;
107  m_osgViewer->getCamera()->getViewMatrixAsLookAt(vEye, vCenter, vUp, fltlookat);
108 
109  osg::Vec3d pos;
110  if(bResetEyePos)
111  pos.set(position[0]+50, position[1]+10, position[2]);
112  else
113  pos = vEye;
114 
115  osg::Vec3d target(position[0], position[1], position[2]);
116 
117  vUp = osg::Vec3d(0, 1, 0);
118  m_osgManip->setHomePosition(pos, target, vUp, false );
119  m_osgManip->home(0);
120 }
121 
122 
123 void OsgSimulationWindow::SetCameraPositionAndLookAt(CStdFPoint oCameraPos, CStdFPoint oTarget)
124 {
125  osg::Vec3d vCameraPos(oCameraPos.x, oCameraPos.y, oCameraPos.z);
126  osg::Vec3d vTargetPos(oTarget.x, oTarget.y, oTarget.z);
127  SetCameraPositionAndLookAt(vCameraPos, vTargetPos);
128 }
129 
130 void OsgSimulationWindow::SetCameraPositionAndLookAt(osg::Vec3d vCameraPos, osg::Vec3d vTarget)
131 {
132  osg::Vec3d eye, center, up;
133  m_osgManip->getHomePosition(eye, center, up);
134 
135  up = osg::Vec3d(0, 1, 0);
136  m_osgManip->setHomePosition(vCameraPos, vTarget, up, false );
137  m_osgManip->home(0);
138 }
139 
140 void OsgSimulationWindow::SetCameraPostion(CStdFPoint vCameraPos)
141 {
142  if(m_lpTrackBody)
143  {
144  CStdFPoint vTargetPos = m_lpTrackBody->AbsolutePosition();
145  SetCameraPositionAndLookAt(vCameraPos, vTargetPos);
146  }
147 }
148 
150 {
151  if(m_lpTrackBody)
152  {
153  CStdFPoint oPos = m_lpTrackBody->AbsolutePosition();
154  osg::Vec3d v(oPos.x, oPos.y, oPos.z);
155  m_osgManip->setCenter(v);
156  }
157 }
158 
159 void OsgSimulationWindow::UpdateBackgroundColor()
160 {
161  CStdColor *vColor = m_lpSim->BackgroundColor();
162  if(m_osgViewer.valid())
163  m_osgViewer->getCamera()->setClearColor(osg::Vec4(vColor->r(), vColor->g(), vColor->b(), vColor->a()));
164 }
165 
166 void OsgSimulationWindow::InitEmbedded(Simulator *lpSim, OsgSimulator *lpVsSim)
167 {
168 #ifdef WIN32
169  m_osgViewer = new osgViewer::Viewer;
170 
171  osg::GraphicsContext::Traits *traits = new osg::GraphicsContext::Traits();
172 
173  traits->inheritedWindowData = new osgViewer::GraphicsWindowWin32::WindowData( m_HWND );
174  traits->setInheritedWindowPixelFormat = true;
175  traits->doubleBuffer = true;
176  traits->windowDecoration = false;
177  traits->sharedContext = NULL;
178  traits->supportsResize = true;
179 
180  RECT rect;
181  GetWindowRect( m_HWND, &rect );
182  traits->x = 0;
183  traits->y = 0;
184  traits->width = rect.right - rect.left;
185  traits->height = rect.bottom - rect.top;
186 
187  osg::GraphicsContext *gc = osg::GraphicsContext::createGraphicsContext(traits);
188 
189  osg::ref_ptr<osg::Camera> osgCamera = new osg::Camera;
190  osgCamera->setGraphicsContext(gc);
191  osg::ref_ptr<osg::Viewport> osgViewport = new osg::Viewport(traits->x, traits->y, traits->width, traits->height);
192  osgCamera->setViewport(osgViewport.get());
193  osgCamera->setDrawBuffer(GL_BACK);
194  osgCamera->setReadBuffer(GL_BACK);
195 
196  CStdColor *vColor = lpSim->BackgroundColor();
197  m_osgViewer->getCamera()->setClearColor(osg::Vec4(vColor->r(), vColor->g(), vColor->b(), vColor->a()));
198  m_osgViewer->addSlave(osgCamera.get());
199 
200 
201  // //gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
202  // gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
203 
204  //osg::Camera *cam = m_osgViewer->getCamera();
205  //cam->setName("MyCam");
206  //cam->setGraphicsContext( gc );
207  //cam->setViewport( new osg::Viewport( traits->x, traits->y, traits->width, traits->height ) );
208  //float *vColor = lpSim->BackgroundColor();
209  //cam->setClearColor(osg::Vec4(vColor[0], vColor[1], vColor[2], vColor[3]));
210  //m_osgViewer->setName("MyTest");
211 
212  m_osgViewer->setSceneData(lpVsSim->OSGRoot());
213 
214  //m_osgViewer->addEventHandler( new osgViewer::WindowSizeHandler );
215  //m_osgViewer->addEventHandler( new osgViewer::ThreadingHandler );
216 
217  m_osgManip = new OsgCameraManipulator(lpSim, m_osgViewer.get(), osgViewport.get());
218  //m_osgManip = new osgGA::TrackballManipulator();
219  m_osgViewer->setCameraManipulator(m_osgManip.get());
220 
221  osgGA::GUIEventHandler *lpHandler = new OsgDraggerHandler(lpSim, m_osgViewer.get(), osgViewport.get());
222  m_osgViewer->addEventHandler(lpHandler);
223 /*
224  m_osgStatsHandler = new VsStatsHandler;
225  m_osgStatsHandler->setStatsHandler(osgViewer::StatsHandler::StatsType::FRAME_RATE);
226  m_osgViewer->addEventHandler( m_osgStatsHandler );*/
227 
228  m_osgViewer->realize();
229 
230  // correct aspect ratio
231  double fovy,aspectRatio,z1,z2;
232 
233  m_osgViewer->getCamera()->getProjectionMatrixAsPerspective(fovy,aspectRatio, z1,z2);
234  aspectRatio=double(traits->width)/double(traits->height);
235  m_osgViewer->getCamera()->setProjectionMatrixAsPerspective(fovy,aspectRatio,z1,z2);
236 #endif
237 }
238 
239 /*
240 void OsgSimulationWindow::InitStandalone(Simulator *lpSim, OsgSimulator *lpVsSim)
241 {
242 
243  m_osgViewer = new osgViewer::Viewer;
244 
245  //m_osgManip = new osgGA::TrackballManipulator;
246  m_osgManip = new OsgCameraManipulator(lpSim, m_osgViewer.get());
247  m_osgViewer->setCameraManipulator(m_osgManip.get());
248 
249  osgGA::GUIEventHandler *lpHandler = new OsgDraggerHandler(lpSim, m_osgViewer.get());
250  m_osgViewer->addEventHandler(lpHandler);
251 
252  // Create the window and run the threads.
253  //m_osgViewer->apply(new osgViewer::SingleScreen(0));
254  //m_osgViewer->setUpViewInWindow(m_ptPosition.x, m_ptPosition.y, m_ptSize.x, m_ptSize.y);
255 
256  CStdColor *vColor = lpSim->BackgroundColor();
257  m_osgViewer->getCamera()->setClearColor(osg::Vec4(vColor->r(), vColor->g(), vColor->b(), vColor->a()));
258  m_osgViewer->setSceneData(lpVsSim->OSGRoot());
259 
260  int inheritanceMask =
261  (osgUtil::SceneView::VariablesMask::ALL_VARIABLES &
262  ~osgUtil::SceneView::VariablesMask::CULL_MASK);
263 
264  // set mask for upper camera
265  m_osgViewer->getCamera()->setInheritanceMask(inheritanceMask);
266  m_osgViewer->getCamera()->setCullMask(0x1);
267 
268  m_osgViewer->realize();
269 }
270 */
271 
272 void OsgSimulationWindow::InitStandalone(Simulator *lpSim, OsgSimulator *lpVsSim)
273 {
274  // create the window to draw to.
275  osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
276  traits->x = 200;
277  traits->y = 200;
278  traits->width = 800;
279  traits->height = 600;
280  traits->windowDecoration = true;
281  traits->doubleBuffer = true;
282  traits->sharedContext = 0;
283 
284  osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
285  osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
286  if (!gw)
287  {
288  osg::notify(osg::NOTICE)<<"Error: unable to create graphics window."<<std::endl;
289  THROW_ERROR(Osg_Err_lUnableToConvertToVsSimulator, Osg_Err_strUnableToConvertToVsSimulator);
290  }
291  gw->setWindowName("AnimatSimulator");
292 
293  m_osgViewer = new osgViewer::Viewer;
294 
295  //m_osgManip = new osgGA::TrackballManipulator;
296  m_osgManip = new OsgCameraManipulator(lpSim, m_osgViewer.get());
297  m_osgViewer->setCameraManipulator(m_osgManip.get());
298 
299  osgGA::GUIEventHandler *lpHandler = new OsgDraggerHandler(lpSim, m_osgViewer.get());
300  m_osgViewer->addEventHandler(lpHandler);
301 
302  CStdColor *vColor = lpSim->BackgroundColor();
303  m_osgViewer->getCamera()->setGraphicsContext(gc.get());
304  m_osgViewer->getCamera()->setClearColor(osg::Vec4(vColor->r(), vColor->g(), vColor->b(), vColor->a()));
305  m_osgViewer->setSceneData(lpVsSim->OSGRoot());
306  m_osgViewer->getCamera()->setViewport(0,0,800,600);
307 
308  int inheritanceMask =
309  (osgUtil::SceneView::VariablesMask::ALL_VARIABLES &
310  ~osgUtil::SceneView::VariablesMask::CULL_MASK);
311 
312  // set mask for upper camera
313  m_osgViewer->getCamera()->setInheritanceMask(inheritanceMask);
314  m_osgViewer->getCamera()->setCullMask(0x1);
315 
316  m_osgViewer->realize();
317 }
318 
320 {
321  SimulationWindow::Initialize();
322 
323  m_lpWinMgr = dynamic_cast<OsgSimulationWindowMgr *>(m_lpSim->GetWindowMgr());
324  if(!m_lpWinMgr)
325  THROW_ERROR(Osg_Err_lUnableToConvertToVsWinMgr, Osg_Err_strUnableToConvertToVsWinMgr);
326 
327  OsgSimulator *lpVsSim = dynamic_cast<OsgSimulator *>(m_lpSim);
328  if(!lpVsSim)
329  THROW_ERROR(Osg_Err_lUnableToConvertToVsSimulator, Osg_Err_strUnableToConvertToVsSimulator);
330 
331  if(m_HWND)
332  InitEmbedded(m_lpSim, lpVsSim);
333  else
334  InitStandalone(m_lpSim, lpVsSim);
335 
336  SetupTrackCamera(true);
337 }
338 
339 
341 {
342  //Stop threading so we can add this new window to the composite viewer.
343  //m_osgViewer->setDone(true);
344  m_osgViewer->stopThreading();
345 
346  m_lpTrackBody = NULL; //Do not delete this item.
347 }
348 
350 {
351 }
352 
354 {
355 }
356 
357 float *OsgSimulationWindow::GetDataPointer(const std::string &strDataType)
358 {
359  float *lpData=NULL;
360  std::string strType = Std_CheckString(strDataType);
361 
362  GetCameraPosition();
363 
364  if(strType == "CAMERAPOSITIONX")
365  lpData = &m_fltCameraPosX;
366  else if(strType == "CAMERAPOSITIONY")
367  lpData = &m_fltCameraPosY;
368  else if(strType == "CAMERAPOSITIONZ")
369  lpData = &m_fltCameraPosZ;
370  else
371  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "StimulusName: " + STR(m_strName) + " DataType: " + strDataType);
372 
373  return lpData;
374 }
375 
376 
377  }// end Visualization
378 }// end OsgAnimatSim
bool m_bEyePosSet
true if we have set the eye position at least once.
virtual void OnLoseFocus()
Called by the GUI when this window loses the focus.
virtual void Initialize()
Initializes this object.
virtual void OnGetFocus()
Called by the GUI when this window gets the focus.
Declares the vortex Light class.
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void DistanceUnits(std::string strUnits)
Sets the distance units.
Definition: Simulator.cpp:1717
Vortex base body class.
Definition: OsgBody.h:26
virtual CStdColor * BackgroundColor()
Gets the background color.
Definition: Simulator.cpp:1460
HWND m_HWND
Handle of the hwnd.
virtual void SetCameraPositionAndLookAt(CStdFPoint oCameraPos, CStdFPoint oTarget)
Manually sets a camera position and look at position.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
virtual void SetCameraPostion(CStdFPoint vCameraPos)
Manually sets a camera position.
virtual void SetupTrackCamera(bool bResetEyePos)
Sets up the camera tracking.
virtual SimulationWindowMgr * GetWindowMgr()
Gets the window manager.
Definition: Simulator.cpp:450
virtual void TrackCamera()
Implements code to do the camera tracking.
virtual Structure * FindStructureFromAll(std::string strStructureID, bool bThrowError=true)
Searches for the first structure with the specified ID.
Definition: Simulator.cpp:3920
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
virtual void SetCameraLookAt(CStdFPoint oTarget, bool bResetEyePos)
Manually sets a camera look at position.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
Declares the vortex structure class.
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61