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