CloudyDay
 All Classes Functions Variables Enumerations
Rain.h
1 #pragma once
2 #include "Scene.h"
3 #include <osg/Geode>
4 #include <osg/Shader>
5 #include <osg/Texture2D>
6 
7 namespace osgCloudyDay
8 {
12  class Rain
13  {
14  public:
18  Rain(void);
22  ~Rain(void);
23 
27  void Initialize();
28 
32  static void CreateShader();
36  static void CreateTexture();
37 
42  osg::ref_ptr<osg::Geode> GetGeode();
43 
44  protected:
45  osg::ref_ptr<osg::Geode> m_geode;
46  float frand();
47  static osg::ref_ptr<osg::Program> m_rain_shader;
48  static osg::ref_ptr<osg::Texture3D> m_rain_texture;
49 
50  std::vector<unsigned int> m_index;
51  osg::ref_ptr<osg::Vec3Array> m_vertices;
52  osg::ref_ptr<osg::Vec4Array> m_velocity;
53  osg::ref_ptr<osg::Vec4Array> m_information;
54  };
55 
59  class RainCallback : public osg::NodeCallback
60  {
61  public:
66  {
67 
68  }
69 
73  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
74  {
75  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
76  osg::ref_ptr<osg::Geode> geometry = dynamic_cast<osg::Geode*> (node);
77 
78  float fspeed = 2500.f;
79 
80  if(geometry && cv)
81  {
82  osg::ref_ptr<osg::Geometry> g = static_cast<osg::Geometry*> (geometry->getDrawable(0));
83  osg::ref_ptr<osg::Vec3Array> pos = static_cast<osg::Vec3Array*>(g->getVertexAttribArray(0));
84  osg::ref_ptr<osg::Vec4Array> vel = static_cast<osg::Vec4Array*>(g->getVertexAttribArray(1));
85  for(unsigned int i = 0; i < pos->size(); i++)
86  {
87  pos->at(i).x() = pos->at(i).x() + 0.05f * vel->at(i).x() * fspeed;
88  pos->at(i).y() = pos->at(i).y() + 0.05f * vel->at(i).y() * fspeed;
89  pos->at(i).z() = pos->at(i).z() + 0.05f * vel->at(i).z() * fspeed;
90  vel->at(i).w() -= 0.05f;
91 
92  if(vel->at(i).w() < 0.f)
93  {
94  pos->at(i).z() = fspeed;
95  vel->at(i).w() = 1.f;
96  }
97  }
98  g->getVertexAttribArray(0)->dirty();
99  g->getVertexAttribArray(1)->dirty();
100 
101  osg::Matrixd view(cv->getCurrentCamera()->getViewMatrix());
102  osg::Matrixd invViewMatrix = osg::Matrixd::inverse(view);
103  osg::Matrixd modelview(*cv->getModelViewMatrix());
104  osg::Matrixd model = modelview*invViewMatrix;
105  osg::Matrixd proj(cv->getCurrentCamera()->getProjectionMatrix());
106 
107  geometry->getOrCreateStateSet()->getUniform("ModelMatrix")->set(model);
108  geometry->getOrCreateStateSet()->getUniform("ViewMatrix")->set(view);
109  geometry->getOrCreateStateSet()->getUniform("ProjectionMatrix")->set(proj);
110 
111  #ifdef SHADOW_MAPPING
112  geometry->getOrCreateStateSet()->getUniform("light_proj_matrix")->set(Scene::GetProjectionMatrix_Light());
113  geometry->getOrCreateStateSet()->getUniform("light_mv_matrix")->set(Scene::GetLightCamera()->getViewMatrix());
114  #endif
115  }
116  traverse(node, nv);
117  }
118  };
119 }