CloudyDay
 All Classes Functions Variables Enumerations
Forest.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 ForestLightCallback : public osg::NodeCallback
29  {
30  public:
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  osg::Matrixd view(cv->getCurrentCamera()->getViewMatrix());
44  osg::Matrixd invViewMatrix = osg::Matrixd::inverse(view);
45  osg::Matrixd modelview(*cv->getModelViewMatrix());
46  osg::Matrixd model = modelview*invViewMatrix;
47  osg::Matrixd proj(cv->getCurrentCamera()->getProjectionMatrix());
48 
49  geometry->getOrCreateStateSet()->getUniform("ModelMatrix")->set(model);
50  geometry->getOrCreateStateSet()->getUniform("ViewMatrix")->set(view);
51  geometry->getOrCreateStateSet()->getUniform("ProjectionMatrix")->set(proj);
52 
53  geometry->getOrCreateStateSet()->getUniform("light_mv_matrix")->set(osgCloudyDay::Scene::GetLightCamera()->getViewMatrix());
54 
55 
56  #ifdef SHADOW_MAPPING
57  geometry->getOrCreateStateSet()->getUniform("light_proj_matrix")->set(osgCloudyDay::Scene::GetProjectionMatrix_Light());
58  geometry->getOrCreateStateSet()->getUniform("light_mv_matrix")->set(osgCloudyDay::Scene::GetLightCamera()->getViewMatrix());
59  #endif
60  }
61  traverse(node, nv);
62  }
63  };
64 
68  class Forest
69  {
70  public:
71  Forest(void);
72  ~Forest(void);
73 
74  void Initialize();
75 
76  static void CreateTexture();
77  static void CreateShader();
78  static void SetDefinitionOfTerrain(osg::Texture* definition);
79  static void SetHeightofTerrain(osg::Texture* heightmap);
80 
81  static osg::ref_ptr<osg::Program> m_forestProg;
82  static osg::ref_ptr<osg::Texture2D> tex_tree;
83  static float* heightmap_data;
84  static osg::Vec4* definition_data;
85  static int heightmap_height;
86  static int heightmap_width;
87 
88  osg::Geode* GetGeode();
89 
90  protected:
91  void SetArrays();
92  void SetUniforms(osg::ref_ptr<osg::StateSet> nodess4);
93 
94  osg::ref_ptr<osg::Geode> geode;
95  osg::ref_ptr<osg::Geometry> geometry;
96  osg::ref_ptr<osg::Vec3Array> m_vertices;
97  osg::ref_ptr<osg::UIntArray> m_indices;
98  };
99 }
100