CloudyDay
 All Classes Functions Variables Enumerations
Night.h
1 #pragma once
2 #include "Scene.h"
3 #include <osg/Geometry>
4 #include <osg/State>
5 #include <osg/ArrayDispatchers>
6 #include <osg/PrimitiveSet>
7 #include <osg/Shader>
8 #include <osg/Program>
9 
10 #include <osg/ClearNode>
11 #include <osgViewer/Viewer>
12 #include <osg/GL>
13 #include <osgUtil/RenderStage>
14 #include <osg/PolygonOffset>
15 #include <osg/BlendFunc>
16 #include <osg/BlendEquation>
17 #include <osg/Depth>
18 
19 #include <osg/Shader>
20 #include <osg/Vec3>
21 #include <osg/Matrix>
22 
23 namespace osgCloudyDay
24 {
28  class NightLightCallback : public osg::NodeCallback
29  {
30  public:
31  NightLightCallback(void)
32  {
33 
34  }
35 
36  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
37  {
38  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
39  osg::ref_ptr<osg::Geode> geometry = dynamic_cast<osg::Geode*> (node);
40 
41  if(geometry && cv)
42  {
43  //std::cerr << "NIGHT" << std::endl;
44  osg::Matrixd view(cv->getCurrentCamera()->getViewMatrix());
45  osg::Matrixd invViewMatrix = osg::Matrixd::inverse(view);
46  osg::Matrixd modelview(*cv->getModelViewMatrix());
47  osg::Matrixd model = modelview*invViewMatrix;
48  osg::Matrixd proj(cv->getCurrentCamera()->getProjectionMatrix());
49 
50  geometry->getOrCreateStateSet()->getUniform("ModelMatrix")->set(model);
51  geometry->getOrCreateStateSet()->getUniform("ViewMatrix")->set(view);
52  geometry->getOrCreateStateSet()->getUniform("ProjectionMatrix")->set(proj);
53 
54  geometry->getOrCreateStateSet()->getUniform("light_mv_matrix")->set(osgCloudyDay::Scene::GetLightCamera()->getViewMatrix());
55 
56 
57  #ifdef SHADOW_MAPPING
58  geometry->getOrCreateStateSet()->getUniform("light_proj_matrix")->set(osgCloudyDay::Scene::GetProjectionMatrix_Light());
59  geometry->getOrCreateStateSet()->getUniform("light_mv_matrix")->set(osgCloudyDay::Scene::GetLightCamera()->getViewMatrix());
60  #endif
61  }
62  traverse(node, nv);
63  }
64  };
65 
69  class Night
70  {
71  public:
72  Night();
73  ~Night();
74 
75  void Initialize();
76  void SetArrays();
77  static void CreateShader();
78  void SetUniforms(osg::ref_ptr<osg::StateSet> nodess4);
79 
80  static void CreateTexture();
81 
82  static osg::ref_ptr<osg::Program> m_cloudProg;
83  static osg::ref_ptr<osg::Texture2D> tex_tree;
84 
85  osg::Geode* GetGeode();
86 
87  protected:
88  osg::ref_ptr<osg::Geode> geode;
89  osg::ref_ptr<osg::Geometry> geometry;
90  osg::ref_ptr<osg::Vec3Array> m_vertices;
91  osg::ref_ptr<osg::Vec4Array> m_information;
92  osg::ref_ptr<osg::UIntArray> m_indices;
93 
94  int m_num_stars;
95  };
96 }