2 #include "OsgCameraManipulator.h"
3 #include "OsgMovableItem.h"
5 #include "OsgRigidBody.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"
18 namespace Visualization
21 OsgSimulationWindow::OsgSimulationWindow()
25 m_fltCameraPosX = m_fltCameraPosY = m_fltCameraPosZ = 0;
29 OsgSimulationWindow::~OsgSimulationWindow(
void)
33 CStdFPoint OsgSimulationWindow::GetCameraPosition()
35 osg::Vec3d vEye, vCenter, vUp;
37 m_osgViewer->getCamera()->getViewMatrixAsLookAt(vEye, vCenter, vUp, fltlookat);
39 CStdFPoint vPos(vEye.x(), vEye.y(), vEye.z());
48 osg::Matrix OsgSimulationWindow::GetScreenMatrix()
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());
57 osg::Viewport* OsgSimulationWindow::GetViewport()
59 return m_osgViewer->getCamera()->getViewport();
81 BodyPart *lpBody=NULL;
90 m_lpTrackBody = lpBody;
96 CStdFPoint vDefault(0, 0, 0);
103 osg::Vec3d position(oTarget.x, oTarget.y, oTarget.z);
105 osg::Vec3d vEye, vCenter, vUp;
107 m_osgViewer->getCamera()->getViewMatrixAsLookAt(vEye, vCenter, vUp, fltlookat);
111 pos.set(position[0]+50, position[1]+10, position[2]);
115 osg::Vec3d target(position[0], position[1], position[2]);
117 vUp = osg::Vec3d(0, 1, 0);
118 m_osgManip->setHomePosition(pos, target, vUp,
false );
125 osg::Vec3d vCameraPos(oCameraPos.x, oCameraPos.y, oCameraPos.z);
126 osg::Vec3d vTargetPos(oTarget.x, oTarget.y, oTarget.z);
132 osg::Vec3d eye, center, up;
133 m_osgManip->getHomePosition(eye, center, up);
135 up = osg::Vec3d(0, 1, 0);
136 m_osgManip->setHomePosition(vCameraPos, vTarget, up,
false );
144 CStdFPoint vTargetPos = m_lpTrackBody->AbsolutePosition();
153 CStdFPoint oPos = m_lpTrackBody->AbsolutePosition();
154 osg::Vec3d v(oPos.x, oPos.y, oPos.z);
155 m_osgManip->setCenter(v);
159 void OsgSimulationWindow::UpdateBackgroundColor()
162 if(m_osgViewer.valid())
163 m_osgViewer->getCamera()->setClearColor(osg::Vec4(vColor->r(), vColor->g(), vColor->b(), vColor->a()));
166 void OsgSimulationWindow::InitEmbedded(Simulator *lpSim,
OsgSimulator *lpVsSim)
169 m_osgViewer =
new osgViewer::Viewer;
171 osg::GraphicsContext::Traits *traits =
new osg::GraphicsContext::Traits();
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;
181 GetWindowRect(
m_HWND, &rect );
184 traits->width = rect.right - rect.left;
185 traits->height = rect.bottom - rect.top;
187 osg::GraphicsContext *gc = osg::GraphicsContext::createGraphicsContext(traits);
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);
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());
212 m_osgViewer->setSceneData(lpVsSim->OSGRoot());
219 m_osgViewer->setCameraManipulator(m_osgManip.get());
221 osgGA::GUIEventHandler *lpHandler =
new OsgDraggerHandler(lpSim, m_osgViewer.get(), osgViewport.get());
222 m_osgViewer->addEventHandler(lpHandler);
228 m_osgViewer->realize();
231 double fovy,aspectRatio,z1,z2;
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);
272 void OsgSimulationWindow::InitStandalone(Simulator *lpSim,
OsgSimulator *lpVsSim)
275 osg::ref_ptr<osg::GraphicsContext::Traits> traits =
new osg::GraphicsContext::Traits;
279 traits->height = 600;
280 traits->windowDecoration =
true;
281 traits->doubleBuffer =
true;
282 traits->sharedContext = 0;
284 osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
285 osgViewer::GraphicsWindow* gw =
dynamic_cast<osgViewer::GraphicsWindow*
>(gc.get());
288 osg::notify(osg::NOTICE)<<
"Error: unable to create graphics window."<<std::endl;
289 THROW_ERROR(Osg_Err_lUnableToConvertToVsSimulator, Osg_Err_strUnableToConvertToVsSimulator);
291 gw->setWindowName(
"AnimatSimulator");
293 m_osgViewer =
new osgViewer::Viewer;
297 m_osgViewer->setCameraManipulator(m_osgManip.get());
299 osgGA::GUIEventHandler *lpHandler =
new OsgDraggerHandler(lpSim, m_osgViewer.get());
300 m_osgViewer->addEventHandler(lpHandler);
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);
308 int inheritanceMask =
309 (osgUtil::SceneView::VariablesMask::ALL_VARIABLES &
310 ~osgUtil::SceneView::VariablesMask::CULL_MASK);
313 m_osgViewer->getCamera()->setInheritanceMask(inheritanceMask);
314 m_osgViewer->getCamera()->setCullMask(0x1);
316 m_osgViewer->realize();
321 SimulationWindow::Initialize();
325 THROW_ERROR(Osg_Err_lUnableToConvertToVsWinMgr, Osg_Err_strUnableToConvertToVsWinMgr);
329 THROW_ERROR(Osg_Err_lUnableToConvertToVsSimulator, Osg_Err_strUnableToConvertToVsSimulator);
332 InitEmbedded(
m_lpSim, lpVsSim);
334 InitStandalone(
m_lpSim, lpVsSim);
344 m_osgViewer->stopThreading();
346 m_lpTrackBody = NULL;
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;
371 THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"StimulusName: " + STR(
m_strName) +
" DataType: " + strDataType);
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.
std::string m_strLookAtStructureID
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.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void DistanceUnits(std::string strUnits)
Sets the distance units.
virtual CStdColor * BackgroundColor()
Gets the background color.
HWND m_HWND
Handle of the hwnd.
virtual void SetCameraPositionAndLookAt(CStdFPoint oCameraPos, CStdFPoint oTarget)
Manually sets a camera position and look at position.
virtual void Close()
Closes this window.
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.
std::string m_strLookAtBodyID
virtual SimulationWindowMgr * GetWindowMgr()
Gets the window manager.
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.
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.
virtual void Update()
Updates this window.
std::string m_strName
The name for this object.