CloudyDay
 All Classes Functions Variables Enumerations
Scene.h
1 #pragma once
2 #include <GL/glew.h>
3 #include <GL/GL.h>
4 #include "SkydomeMie.h"
5 #include "SkydomeHimmel.h"
6 
7 #include "Fog.h"
8 
9 #include <osg/Matrix>
10 #include <osg/MatrixTransform>
11 #include <osg/Node>
12 #include <osg/Geometry>
13 #include <osg/Program>
14 #include <osg/Geode>
15 #include <osg/Texture2D>
16 #include <osg/Image>
17 #include <osg/BlendFunc>
18 #include <osg/CullFace>
19 #include <osgDB/ReadFile>
20 #include <osg/NodeVisitor>
21 #include <osg/Texture2D>
22 #include <osgDB/ReadFile>
23 #include <osgViewer/Renderer>
24 
25 #include <osg/Texture2D>
26 #include <osg/Texture3D>
27 
28 //#define CREATE_BOUNDINGBOX
29 #define SHADOW_MAPPING
30 
31 namespace osgCloudyDay
32 {
36  class Scene
37  {
38  public:
44  Scene(int width, int height);
45 
49  ~Scene(void);
50 
54  static void CreateViewCamera();
58  static void CreateLightCamera();
62  static void CreateViewDepthCamera();
63 
68  static osg::Matrix& GetViewMatrix_View();
73  static osg::Matrix& GetProjectionMatrix_View();
78  static osg::Matrix& GetProjectionMatrix_Light();
79 
84  static osg::ref_ptr<osg::Camera> GetLightCamera();
89  static osg::ref_ptr<osg::Camera> GetViewCamera();
94  static osg::ref_ptr<osg::Camera> GetViewCamera2();
99  static osg::ref_ptr<osg::Camera> GetViewDepthCamera();
100 
105  static void SetLightCamera(osg::ref_ptr<osg::Camera> c);
110  static void SetViewCamera(osg::ref_ptr<osg::Camera> c);
111 
116  static osg::ref_ptr<osg::Program> GetShadowProgram();
121  static osg::ref_ptr<osg::Program> GetShadingProgram();
126  static osg::ref_ptr<osg::Program> GetDebugProgram();
127 
132  static int GetWidth();
137  static int GetHeight();
138 
143  static osg::ref_ptr<osg::Texture2D> GetShadowTexture(); //SHADOW
144 
149  static osg::ref_ptr<osg::Texture2D> GetShadowDepthTexture(); //DEPTH SHADOW
154  static osg::ref_ptr<osg::Texture2D> GetSceneDepthTexture(); //DEPTH SCENE
159  static osg::ref_ptr<osg::Texture2D> GetSceneTexture(); //SCENE TEXTURE
160 
165  static osg::ref_ptr<osg::Texture2D> GetGoodRayTexture(); //GOOD RAYS HELEPR TEXTURE
170  static osg::ref_ptr<osg::Texture2D> GetHUDTexture(); //HUD TEXTURE
175  static osg::ref_ptr<osg::Texture2D> GetBlurTexture();
176 
177 
178  static osgCloudyDay::Skydome* m_skydome;
179  static osg::FrameBufferObject* fbo_vert_linear_blur2;
180  static osg::FrameBufferObject* fbo_hori_linear_blur2;
181  static osg::FrameBufferObject* fbo_scene_viewer;
182 
183  protected:
184  static osg::ref_ptr<osg::Texture2D> fbo_light_texture2; //SHADOW
185  static osg::ref_ptr<osg::Texture2D> fbo_goodrays; //GOOD RAYS HELEPR TEXTURE
186  static osg::ref_ptr<osg::Texture2D> fbo_scene_depth; //DEPTH SCENE
187  static osg::ref_ptr<osg::Texture2D> fbo_scene_texture; //SCENE TEXTURE
188  static osg::ref_ptr<osg::Texture2D> fbo_hud_texture; //HUD TEXTURE
189  static osg::ref_ptr<osg::Texture2D> fbo_blur_texture;
190  static osg::ref_ptr<osg::Texture2D> fbo_light_depth; //DEPTH SHADOW
191 
192  static int m_width;
193  static int m_height;
194 
195  static osg::ref_ptr<osg::Program> shadowProg;
196  static osg::ref_ptr<osg::Program> lightningProg;
197  static osg::ref_ptr<osg::Program> debugProg;
198 
199  static osg::ref_ptr<osg::Camera> m_ViewCameraDepthOnly;
200  static osg::ref_ptr<osg::Camera> m_ViewCamera;
201  static osg::ref_ptr<osg::Camera> m_LightCamera;
202  static osg::ref_ptr<osg::Camera> m_ViewDepthCamera;
203  };
204 
205  class CullCallBackViewCamera3 : public osg::NodeCallback
206 {
207  public:
209  {
210 
211  }
212 
213  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
214  {
215  //std::cerr << "DrawLightMatrix" << std::endl;
216  Scene::GetViewCamera()->setViewMatrix(Scene::GetViewDepthCamera()->getViewMatrix());
217  Scene::GetViewCamera()->setProjectionMatrix(Scene::GetViewDepthCamera()->getProjectionMatrix());
218 
219  traverse(node, nv);
220 
221  //Scene::GetViewCamera()->setViewMatrix(Scene::GetViewDepthCamera()->getViewMatrix());
222  //Scene::GetViewCamera()->setProjectionMatrix(Scene::GetViewDepthCamera()->getProjectionMatrix());
223  }
224 
225 };
226  class CullCallBackViewCamera2 : public osg::NodeCallback
227 {
228  public:
230  {
231 
232  }
233 
234  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
235  {
236  // std::cerr << "DepthViewMatrix" << std::endl;
237 
238  osg::Vec3 e;
239  osg::Vec3 c;
240  osg::Vec3 u;
241  Scene::GetViewDepthCamera()->getViewMatrixAsLookAt(e,c,u);
242  // std::cerr << "1eye: " << e.x() << " " << e.y() << " " << e.z() << std::endl;
243 
244  Scene::GetViewCamera2()->setViewMatrix(Scene::GetViewDepthCamera()->getViewMatrix());
245  Scene::GetViewCamera2()->setProjectionMatrix(Scene::GetViewDepthCamera()->getProjectionMatrix());
246  Scene::GetViewCamera()->setViewMatrix(Scene::GetViewDepthCamera()->getViewMatrix());
247  Scene::GetViewCamera()->setProjectionMatrix(Scene::GetViewDepthCamera()->getProjectionMatrix());
248  traverse(node, nv);
249  }
250 
251 };
252  class CullCallBackViewCamera5 : public osg::NodeCallback
253 {
254  public:
256  {
257 
258  }
259 
260  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
261  {
262  //std::cerr << "ViewMatrix Cull only Depth" << std::endl;
263  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
264  cv->getCurrentCamera()->setViewMatrix(Scene::GetViewMatrix_View());
265 
266  traverse(node, nv);
267  }
268 };
269  class CullCallBackViewCamera6 : public osg::NodeCallback
270 {
271  public:
273  {
274 
275  }
276 
277  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
278  {
279  //std::cerr << "ViewMatrix Cull Color" << std::endl;
280  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
281  cv->getCurrentCamera()->setViewMatrix(Scene::GetViewMatrix_View());
282  traverse(node, nv);
283  }
284 };
285  class CullCallBackViewCamera4 : public osg::NodeCallback
286 {
287  public:
289  {
290 
291  }
292 
293  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
294  {
295  //std::cerr << "DepthMatrix" << std::endl;
296  traverse(node, nv);
297  }
298 };
299  class CullCallBackViewCamera : public osg::NodeCallback
300 {
301  public:
303  {
304 
305  }
306 
307  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
308  {
309  // std::cerr << "ViewMatrix" << std::endl;
310  traverse(node, nv);
311  }
312 };
313 
314  class ViewerLightTerrainCallback : public osg::NodeCallback
315 {
316 public:
318  {
319 
320  }
321 
322  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
323  {
324  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
325  osg::ref_ptr<osg::Geode> geometry = dynamic_cast<osg::Geode*> (node);
326 
327  if(geometry && cv)
328  {
329  //std::cerr << "TERRAIN" << std::endl;
330  osg::Vec3f eye = osg::Vec3f();
331  osg::Vec3f center = osg::Vec3f();
332  osg::Vec3f up = osg::Vec3f();
333  Scene::GetLightCamera()->getViewMatrixAsLookAt(eye, center, up);
334 
335  //std::cout << "Eye: " << eye.x() << " " << eye.y() << " " << eye.z() << std::endl;
336  //std::cout << "Center: " << center.x() << " " << center.y() << " " << center.z() << std::endl;
337 
338 
339  osg::Matrixd view(cv->getCurrentCamera()->getViewMatrix());
340  osg::Matrixd invViewMatrix = osg::Matrixd::inverse(view);
341  osg::Matrixd modelview(*cv->getModelViewMatrix());
342  osg::Matrixd model = modelview*invViewMatrix;
343  osg::Matrixd proj(cv->getCurrentCamera()->getProjectionMatrix());
344 
345  geometry->getOrCreateStateSet()->getUniform("ModelMatrix")->set(model);
346  geometry->getOrCreateStateSet()->getUniform("ViewMatrix")->set(Scene::GetViewMatrix_View());
347  geometry->getOrCreateStateSet()->getUniform("ProjectionMatrix")->set(Scene::GetProjectionMatrix_View());
348 
349 #ifdef SHADOW_MAPPING
350  geometry->getOrCreateStateSet()->getUniform("light_proj_matrix")->set(Scene::GetProjectionMatrix_Light());
351  geometry->getOrCreateStateSet()->getUniform("light_mv_matrix")->set(Scene::GetLightCamera()->getViewMatrix());
352 #endif
353  }
354  traverse(node, nv);
355  }
356 };
357 
358  class ViewerLightCallback : public osg::NodeCallback
359 {
360 public:
361  ViewerLightCallback(void)
362  {
363 
364  }
365 
366  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
367  {
368  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
369  osg::ref_ptr<osg::Geode> geometry = dynamic_cast<osg::Geode*> (node);
370 
371  if(geometry && cv)
372  {
373  osg::Matrixd view(cv->getCurrentCamera()->getViewMatrix());
374  osg::Matrixd invViewMatrix = osg::Matrixd::inverse(view);
375  osg::Matrixd modelview(*cv->getModelViewMatrix());
376  osg::Matrixd model = modelview*invViewMatrix;
377  osg::Matrixd proj(cv->getCurrentCamera()->getProjectionMatrix());
378 
379  geometry->getOrCreateStateSet()->getUniform("ModelMatrix")->set(model);
380  geometry->getOrCreateStateSet()->getUniform("ViewMatrix")->set(Scene::GetViewMatrix_View());
381  geometry->getOrCreateStateSet()->getUniform("ProjectionMatrix")->set(Scene::GetProjectionMatrix_View());
382 
383  //geometry->getOrCreateStateSet()->getUniform("light_mv_matrix")->set(Scene::GetViewMatrix_Light());
384  //geometry->getOrCreateStateSet()->getUniform("light_proj_matrix")->set(Scene::GetProjectionMatrix_Light());
385 
386  //geometry->getOrCreateStateSet()->getUniform("ViewMatrix")->set(Scene::GetViewMatrix_View());
387  //geometry->getOrCreateStateSet()->getUniform("ViewMatrix")->set(camera_plane_cubemap[0]->getViewMatrix());
388  //geometry->getOrCreateStateSet()->getUniform("ProjectionMatrix")->set(Scene::GetProjectionMatrix_View());
389 
390  /*
391  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("v3LightPos", Scene::m_skydome->GetLightPosition()));
392  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("v3InvWavelength", osg::Vec3(1.f/powf(Scene::m_skydome->GetInverseWaveLength().x(), 4.f), 1.f/powf(Scene::m_skydome->GetInverseWaveLength().y(), 4.f), 1.f/powf(Scene::m_skydome->GetInverseWaveLength().z(), 4.f))));
393  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fOuterRadius", Scene::m_skydome->GetOtherRadius()));
394  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fOuterRadius2", powf(Scene::m_skydome->GetOtherRadius(),2.f)));
395  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fInnerRadius", Scene::m_skydome->GetInnerRadius()));
396  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fInnerRadius2", powf(Scene::m_skydome->GetInnerRadius(),2.f)));
397  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fKrESun", Scene::m_skydome->GetKr()*Scene::m_skydome->GetESun()));
398  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fKmESun", Scene::m_skydome->GetKm()*Scene::m_skydome->GetESun()));
399  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fKr4PI", Scene::m_skydome->GetKr()*4.f*(float)PI));
400  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fKm4PI", Scene::m_skydome->GetKm()*4.f*(float)PI));
401  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fScale", 1.f / (Scene::m_skydome->GetOtherRadius() - Scene::m_skydome->GetInnerRadius())));
402  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fScaleDepth", Scene::m_skydome->GetRayleighScaleDepth()));
403  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("fScaleOverScaleDepth", (1.f / (Scene::m_skydome->GetOtherRadius() - Scene::m_skydome->GetInnerRadius())) / Scene::m_skydome->GetRayleighScaleDepth()));
404  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("g", Scene::m_skydome->GetG()));
405  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("g2", (powf(Scene::m_skydome->GetG(),2.f))));
406 
407  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("color_tex", 0)); */
408 
409 
410  //std::cerr << "SCENE" << std::endl;
411 
412  //std::cout << "ath callback" << std::endl;
413 
414 #ifdef SHADOW_MAPPING
415  geometry->getOrCreateStateSet()->getUniform("light_proj_matrix")->set(Scene::GetProjectionMatrix_Light());
416  geometry->getOrCreateStateSet()->getUniform("light_mv_matrix")->set(Scene::GetLightCamera()->getViewMatrix());
417 #endif
418  }
419  traverse(node, nv);
420  }
421 };
422 }