Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
SceneOSG.h
1 //
2 // SceneOSG.h
3 //
4 // Copyright (c) 2001-2011 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef VTOSG_SCENEH
9 #define VTOSG_SCENEH
10 
11 #include <osgViewer/Viewer>
12 #include <osgViewer/Renderer>
13 #include <osg/Timer>
14 
15 #include "../core/Engine.h"
16 
17 #include "VisualImpactCalculatorOSG.h"
18 
19 #if OLD_OSG_SHADOWS
21 #endif
22 
28 
33 class vtWindow
34 {
35 public:
36  vtWindow();
37 
39  void SetBgColor(const RGBf &color) { m_BgColor = color; }
40  RGBf GetBgColor() { return m_BgColor; }
41 
42  void SetSize(int w, int h);
43  IPoint2 GetSize() { return m_Size; }
44 
45 protected:
46  RGBf m_BgColor;
47  IPoint2 m_Size;
48 };
49 
61 class vtScene
62 {
63 public:
64  vtScene();
65  ~vtScene();
66 
68  void SetRoot(vtGroup *pRoot);
69 
71  void SetGlobalWireframe(bool bWire);
73  bool GetGlobalWireframe();
74 
76  bool Init(int argc, char** argv, bool bStereo = false, int iStereoMode = 0);
77  void SetGraphicsContext(osg::GraphicsContext* pGraphicsContext);
78  osg::GraphicsContext* GetGraphicsContext();
79 
81  void Shutdown();
82 
83  void TimerRunning(bool bRun);
84  void UpdateBegin();
85  void UpdateEngines();
86  void UpdateWindow(vtWindow *window);
87  void PostDrawEngines();
88 
89  // backward compatibility
90  void DoUpdate();
91 
93  float GetFrameRate()
94  {
95  return 1.0 / m_fLastFrameTime;
96  }
97 
99  float GetTime()
100  {
101  return _timer.delta_s(_initialTick,_frameTick);
102  }
104  float GetFrameTime()
105  {
106  return m_fLastFrameTime;
107  }
108 
109  // View methods
110  bool CameraRay(const IPoint2 &win, FPoint3 &pos, FPoint3 &dir, vtWindow *pWindow = NULL);
111  void WorldToScreen(const FPoint3 &point, IPoint2 &result);
112 
114  void SetRootEngine(vtEngine *ptr) { m_pRootEngine = ptr; }
115 
117  vtEngine *GetRootEngine() { return m_pRootEngine; }
118 
120  void SetPostDrawEngine(vtEngine *ptr) { m_pRootEnginePostDraw = ptr; }
121 
123  vtEngine *GetPostDrawEngine() { return m_pRootEnginePostDraw; }
124 
126  void AddEngine(vtEngine *ptr);
127 
129  void TargetRemoved(osg::Referenced *tar);
130 
135  void SetCamera(vtCamera *cam) { m_pCamera = cam; }
137  vtCamera *GetCamera() { return m_pCamera; }
138 
139  void OnMouse(vtMouseEvent &event, vtWindow *pWindow = NULL);
140  void OnKey(int key, int flags, vtWindow *pWindow = NULL);
141  void SetKeyStates(bool *piKeyState) { m_piKeyState = piKeyState; }
142  bool GetKeyState(int key);
143 
144  void SetWindowSize(int w, int h, vtWindow *pWindow = NULL);
145  IPoint2 GetWindowSize(vtWindow *pWindow = NULL);
146 
147  bool GetWindowSizeFromOSG();
148 
150  vtGroup *GetRoot() { return m_pRoot; }
151  osg::StateSet *GetRootState() { return m_StateRoot->getOrCreateStateSet(); }
152 
153  // Windows
154  void AddWindow(vtWindow *pWindow) {
155  m_Windows.Append(pWindow);
156  }
157  vtWindow *GetWindow(uint i) {
158  if (m_Windows.GetSize() > i)
159  return m_Windows[i];
160  else
161  return NULL;
162  }
163 
164 #if OLD_OSG_SHADOWS
165  // Experimental:
166  // Object-terrain shadow casting, only for OSG
167  void SetShadowedNode(vtTransform *pLight, osg::Node *pShadowerNode,
168  osg::Node *pShadowed, int iRez, float fDarkness, int iTextureUnit,
169  const FSphere &ShadowSphere);
170  void UnsetShadowedNode(vtTransform *pTransform);
171  void UpdateShadowLightDirection(vtTransform *pLight);
172  void SetShadowDarkness(float fDarkness);
173  void SetShadowSphere(const FSphere &ShadowSphere, bool bForceRedraw);
174  void ShadowVisibleNode(osg::Node *node, bool bVis);
175  bool IsShadowVisibleNode(osg::Node *node);
176  void ComputeShadows();
177 #endif
178 
179  void SetHUD(vtHUD *hud) { m_pHUD = hud; }
180  vtHUD *GetHUD() { return m_pHUD; }
181 
182  // For backward compatibility
183  void SetBgColor(const RGBf &color) {
184  if (GetWindow(0))
185  GetWindow(0)->SetBgColor(color);
186  }
187 
188  bool IsStereo() const;;
189  void SetStereoSeparation(float fSep);
190  float GetStereoSeparation() const;
191  void SetStereoFusionDistance(float fDist);
192  float GetStereoFusionDistance();
193 
194  void ComputeViewMatrix(FMatrix4 &mat);
195 
196  // OSG access
197  osgViewer::Viewer *getViewer() { return m_pOsgViewer.get(); }
198 
199 #if VISUAL_IMPACT_CALCULATOR
200  // Visual Impact Calculation
201  virtual CVisualImpactCalculatorOSG& GetVisualImpactCalculator() { return m_VisualImpactCalculator; }
202 #endif
203 
204 protected:
205  void DoEngines(vtEngine *eng);
206 
207  vtArray<vtWindow*> m_Windows;
208  vtCamera *m_pCamera;
209  vtGroup *m_pRoot;
210  vtEnginePtr m_pRootEngine;
211  vtEngine *m_pRootEnginePostDraw;
212  bool *m_piKeyState;
213  vtHUD *m_pHUD;
214 
215  vtCameraPtr m_pDefaultCamera;
216  vtWindow *m_pDefaultWindow;
217 
218  osg::ref_ptr<osgViewer::Viewer> m_pOsgViewer;
219  osg::ref_ptr<osg::GraphicsContext> m_pGraphicsContext;
220 
221  osg::ref_ptr<osg::Group> m_StateRoot;
222 
223  osg::Timer _timer;
224  osg::Timer_t _initialTick;
225  osg::Timer_t _lastFrameTick;
226  osg::Timer_t _lastRunningTick;
227  osg::Timer_t _frameTick;
228  double m_fAccumulatedFrameTime, m_fLastFrameTime;
229 
230  bool m_bWinInfo;
231  bool m_bInitialized;
232  bool m_bWireframe;
233 #if OLD_OSG_SHADOWS
234  osg::ref_ptr<CStructureShadowsOSG> m_pStructureShadowsOSG;
235 #endif
236 #if VISUAL_IMPACT_CALCULATOR
237  CVisualImpactCalculatorOSG m_VisualImpactCalculator;
238 #endif
239 };
240 
241 // global
242 vtScene *vtGetScene();
243 float vtGetTime();
244 float vtGetFrameTime();
245 int vtGetMaxTextureSize();
246  // Group sg
248 
249 #endif // VTOSG_SCENEH
250