2 #include "VsMouseSpring.h"
3 #include "VsCameraManipulator.h"
4 #include "VsMovableItem.h"
7 #include "VsRigidBody.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"
19 namespace Visualization
22 VsSimulationWindow::VsSimulationWindow()
26 m_fltCameraPosX = m_fltCameraPosY = m_fltCameraPosZ = 0;
30 VsSimulationWindow::~VsSimulationWindow(
void)
34 CStdFPoint VsSimulationWindow::GetCameraPosition()
36 osg::Vec3d vEye, vCenter, vUp;
38 m_osgViewer->getCamera()->getViewMatrixAsLookAt(vEye, vCenter, vUp, fltlookat);
40 CStdFPoint vPos(vEye.x(), vEye.y(), vEye.z());
49 osg::Matrix VsSimulationWindow::GetScreenMatrix()
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());
58 osg::Viewport* VsSimulationWindow::GetViewport()
60 return m_osgViewer->getCamera()->getViewport();
82 BodyPart *lpBody=NULL;
91 m_lpTrackBody = lpBody;
97 CStdFPoint vDefault(0, 0, 0);
104 VxVector3 position(oTarget.x, oTarget.y, oTarget.z);
106 osg::Vec3d vEye, vCenter, vUp;
108 m_osgViewer->getCamera()->getViewMatrixAsLookAt(vEye, vCenter, vUp, fltlookat);
112 pos.set(position.v[0]+50, position.v[1]+10, position.v[2]);
116 osg::Vec3d target(position.v[0], position.v[1], position.v[2]);
118 vUp = osg::Vec3d(0, 1, 0);
119 m_osgManip->setHomePosition(pos, target, vUp,
false );
126 osg::Vec3d vCameraPos(oCameraPos.x, oCameraPos.y, oCameraPos.z);
127 osg::Vec3d vTargetPos(oTarget.x, oTarget.y, oTarget.z);
133 osg::Vec3d eye, center, up;
134 m_osgManip->getHomePosition(eye, center, up);
136 up = osg::Vec3d(0, 1, 0);
137 m_osgManip->setHomePosition(vCameraPos, vTarget, up,
false );
145 CStdFPoint vTargetPos = m_lpTrackBody->AbsolutePosition();
154 CStdFPoint oPos = m_lpTrackBody->AbsolutePosition();
155 VxVector3 position(oPos.x, oPos.y, oPos.z);
157 osg::Vec3 v(position[0], position[1], position[2]);
158 m_osgManip->setCenter(v);
162 void VsSimulationWindow::UpdateBackgroundColor()
165 if(m_osgViewer.valid())
166 m_osgViewer->getCamera()->setClearColor(osg::Vec4(vColor->r(), vColor->g(), vColor->b(), vColor->a()));
169 void VsSimulationWindow::InitEmbedded(Simulator *lpSim,
VsSimulator *lpVsSim)
171 m_osgViewer =
new osgViewer::Viewer;
173 osg::GraphicsContext::Traits *traits =
new osg::GraphicsContext::Traits();
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;
183 GetWindowRect(
m_HWND, &rect );
186 traits->width = rect.right - rect.left;
187 traits->height = rect.bottom - rect.top;
189 osg::GraphicsContext *gc = osg::GraphicsContext::createGraphicsContext(traits);
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);
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());
214 m_osgViewer->setSceneData(lpVsSim->OSGRoot());
221 m_osgViewer->setCameraManipulator(m_osgManip.get());
223 osgGA::GUIEventHandler *lpHandler =
new VsDraggerHandler(lpSim, m_osgViewer.get(), osgViewport.get());
224 m_osgViewer->addEventHandler(lpHandler);
230 m_osgViewer->realize();
233 double fovy,aspectRatio,z1,z2;
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);
240 void VsSimulationWindow::InitStandalone(Simulator *lpSim,
VsSimulator *lpVsSim)
243 m_osgViewer =
new osgViewer::Viewer;
247 m_osgViewer->setCameraManipulator(m_osgManip.get());
249 osgGA::GUIEventHandler *lpHandler =
new VsDraggerHandler(lpSim, m_osgViewer.get());
250 m_osgViewer->addEventHandler(lpHandler);
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());
259 int inheritanceMask =
260 (osgUtil::SceneView::VariablesMask::ALL_VARIABLES &
261 ~osgUtil::SceneView::VariablesMask::CULL_MASK);
264 m_osgViewer->getCamera()->setInheritanceMask(inheritanceMask);
265 m_osgViewer->getCamera()->setCullMask(0x1);
271 m_osgViewer->realize();
289 SimulationWindow::Initialize();
293 THROW_ERROR(Vs_Err_lUnableToConvertToVsWinMgr, Vs_Err_strUnableToConvertToVsWinMgr);
297 THROW_ERROR(Vs_Err_lUnableToConvertToVsSimulator, Vs_Err_strUnableToConvertToVsSimulator);
300 InitEmbedded(
m_lpSim, lpVsSim);
302 InitStandalone(
m_lpSim, lpVsSim);
312 m_osgViewer->stopThreading();
314 m_lpTrackBody = NULL;
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;
339 THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"StimulusName: " + STR(
m_strName) +
" DataType: " + strDataType);
std::string m_strLookAtStructureID
virtual void Close()
Closes this window.
Simulator * m_lpSim
The pointer to a Simulation.
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.
virtual CStdColor * BackgroundColor()
Gets the background color.
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.
std::string m_strLookAtBodyID
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.
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 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 Update()
Updates this window.
virtual void SetCameraLookAt(CStdFPoint oTarget, bool bResetEyePos)
Manually sets a camera look at position.
std::string m_strName
The name for this object.
Declares the vortex structure class.