CloudyDay
 All Classes Functions Variables Enumerations
CloudScene.h
1 #pragma once
2 #include "CloudGeometry.h"
3 #include "CloudLayerUpdate.h"
4 
5 #include "Skydome.h"
6 #include "Fog.h"
7 #include "WangCloud.h"
8 #include "CloudLayerUpdate.h"
9 #include "CloudLayerState.h"
10 
11 #define SHADOW_MAPPING
12 
13 namespace osgCloudyDay
14 {
18  class project : public osg::Uniform::Callback
19  {
20  public:
24  project() {}
28  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( Scene::GetProjectionMatrix_View() );}
29  };
33  class viewMatrix : public osg::Uniform::Callback
34  {
35  public:
43  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( Scene::GetViewCamera()->getViewMatrix() );}
44  };
48  class modelViewMatrix : public osg::Uniform::Callback
49  {
50  public:
58  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( Scene::GetViewCamera()->getViewMatrix() );}
59  };
63  class viewMatrixInv : public osg::Uniform::Callback
64  {
65  public:
73  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( osg::Matrix::inverse(Scene::GetViewCamera()->getViewMatrix()));}
74  };
78  class project_light : public osg::Uniform::Callback
79  {
80  public:
88  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( Scene::GetProjectionMatrix_Light() );}
89  };
93  class viewMatrix_light : public osg::Uniform::Callback
94  {
95  public:
103  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( Scene::GetLightCamera()->getViewMatrix() );}
104  };
108  class modelViewMatrix_light : public osg::Uniform::Callback
109  {
110  public:
118  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( Scene::GetLightCamera()->getViewMatrix() );}
119  };
123  class viewMatrixInv_light : public osg::Uniform::Callback
124  {
125  public:
133  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( osg::Matrix::inverse(Scene::GetProjectionMatrix_Light()) );}
134  };
135 
139  class CloudScene : public osg::Referenced
140  {
141  public:
146  {
147  CT_Stratus,
148  CT_AltStratus,
149  CT_StratoCumulusGenerator,
150  CT_Cumulus,
151  CT_Nimbostratus,
152  CT_Cumolonimbus,
153  CT_AltCumulusGenerator,
154  CT_Quantity
155  };
156 
157  enum WC_Uniform
158  {
159  WCU_LightPos,
160  WCU_TimeOfDay,
161  WCU_Fading,
162  WCU_Density,
163  WCU_AmbientLight_H0,
164  WCU_AmbientLight_H1,
165  WCU_AmbientLight_T0,
166  WCU_AmbientLight_T1,
167  WCU_DirectionalColor,
168  WCU_SunLightColor,
169  quantity
170  };
171 
172  public:
176  CloudScene();
177  //CloudScene(int m_category, osg::ref_ptr<osg::Vec3Array> vertices, osg::ref_ptr<osg::Vec4Array> rotation, osg::ref_ptr<osg::Vec4Array> center, osg::ref_ptr<osg::Vec4Array> ids, osg::ref_ptr<osg::Vec4Array> boxcenters, osg::ref_ptr<osg::Vec3Array> ambientlight_h, osg::ref_ptr<osg::Vec3Array> ambientlight_t, osg::ref_ptr<osg::Vec3Array> diffuselight);
178 
182  ~CloudScene(void);
183 
189  osg::ref_ptr<osg::Geometry> InsertCloud(WangCloud* cloud, int layerID);
190 
196  void AddCloud(WangCloud* cloud, int layerID);
202  void UpdateCloud(WangCloud* cloud, int at);
203 
209  void AddLayer(int layerID, int layer);
215  int GetType(int layer);
220  int GetNumberOfLayers();
221 
225  void Create();
229  static void DeleteStates();
230 
231 
232  CloudLayerUpdate* cloud_layer_update;
233 
238  void SetFog(Fog* fog);
239 
243  void Setup();
244 
248  void SetupAttributes(osg::ref_ptr<osg::StateSet> nodess4);
249 
253  static void LoadingTexture();
257  static void LoadingShader();
258 
263  static void SetStates(CloudLayerState* state);
268  static CloudLayerState* GetStates();
269 
274  void UpdateUniform(int type);
275 
280  int GetNumberOfClouds();
285  unsigned int GetNumberOfParticles();
286 
290  static void Initialize();
291 
292 
293  static float timeOfDay;
294  static float fading;
295  static float dens;
296  static osg::Vec3 ambientLight_h0;
297  static osg::Vec3 ambientLight_h1;
298  static osg::Vec3 ambientLight_t0;
299  static osg::Vec3 ambientLight_t1;
300  static osg::Matrix3 directionalColors;
301  static float time;
302  static osg::Vec3 sunLightColor;
303 
304  static osg::ref_ptr<osg::Program> cloudShadowMapProg;
305  static osg::ref_ptr<osg::Program> cloudBlurHoriProg;
306  static osg::ref_ptr<osg::Program> cloudBlurVertProg;
307  static osg::ref_ptr<osg::Program> cloudBlurLinearHoriProg;
308  static osg::ref_ptr<osg::Program> cloudBlurLinearVertProg;
309  static osg::ref_ptr<osg::Program> cloudProg;
310  static osg::ref_ptr<osg::Program> cloudShadowProg;
311  static osg::ref_ptr<osg::Texture2D> tex_clouds;
312 
313  static osg::FrameBufferObject* fbo_viewer;
314  static osg::FrameBufferObject* fbo_light;
315 
316 
317  osg::ref_ptr<osg::Uniform> project_lightuniform;
318  osg::ref_ptr<osg::Uniform> modelViewMatrix_lightuniform;
319  osg::ref_ptr<osg::Uniform> viewMatrix_lightuniform;
320  osg::ref_ptr<osg::Uniform> viewMatrixInv_lightuniform;
321  osg::ref_ptr<osg::Uniform> projectuniform;
322  osg::ref_ptr<osg::Uniform> modelViewMatrixuniform;
323  osg::ref_ptr<osg::Uniform> viewMatrixuniform;
324  osg::ref_ptr<osg::Uniform> viewMatrixInvuniform;
325 
326  osg::ref_ptr<osg::Geode> quadBillBoard;
327  osg::ref_ptr<osg::StateSet> nodess4;
328 
329  osg::ref_ptr<osg::Vec3Array> m_cloudcenter;
330 
331  osg::ref_ptr<osg::IntArray> m_start_indices;
332 
333  bool setup_attribute;
334  osg::ref_ptr<osg::Geode> geode;
335 
336  #ifdef SHADOW_MAPPING
337  osg::ref_ptr<osgCloudyDay::CloudGeometry> geometry;
338  #else
339  osg::ref_ptr<osg::Geometry> geometry;
340  #endif
341 
345  void RemoveScreenBillBoard();
346 
352  osg::Vec3 CalculatePosition(int m_type);
353 
354  int numClouds;
355 
363  osg::Geometry* createSphere(osg::Vec3 middlePoint, float radius, int vpr);
369  osg::ref_ptr<osg::Geometry> CreateBBForCloud(int cloudID);
370 
371  std::vector<WangCloud*> m_clouds;
372 
377  unsigned int GetLayer(int index);
378 
379  static bool m_backtofront;
380  static bool m_backtofront_old;
381 
382 
383 
387  static void CreateCloudCamera();
388 
393  static osg::ref_ptr<osg::Camera> GetCloudCamera();
394 
395 
396  static osg::ref_ptr<osg::Texture2D> fbo_light_texture; //SHADOW
397  static osg::ref_ptr<osg::Texture2D> fbo_cloud_texture; //CLOUD TEXTURE
398 
399  static osg::FrameBufferObject* fbo_cloud_viewer; //CLOUD FRAMEBUFFER
400  static osg::FrameBufferObject* fbo_shadow;
401  static osg::FrameBufferObject* fbo_vert_linear_blur;
402  static osg::FrameBufferObject* fbo_hori_linear_blur;
403  static osg::FrameBufferObject* fbo_vert_blur;
404  static osg::FrameBufferObject* fbo_hori_blur;
405  static osg::FrameBufferObject* fbo_hori_blur2;
406 
407 
408 
409  protected:
410  static osg::ref_ptr<osg::Camera> m_lightCloudCamera;
411 
418  void AddDrawElementsPoint(std::vector<unsigned int> index, unsigned int start, unsigned int count, int k);
419 
423  void InsertScreenBillBoard();
424  /*
425  * Random generator
426  * @return random value
427  */
428  inline float frand();
435  osg::Vec2 GetGaussianDistributedRandomNumber(osg::Vec2 m, float v);
440  void GenerateRandomSequence(int num_elements);
441  osg::Vec2 m_numOfClouds;
442  std::vector<int> sequence;
443 
444  unsigned int m_num_indices;
445 
446  std::vector<unsigned int> m_layerToNumberCloud;
447  std::vector<unsigned int> m_layerToCloudType;
448  std::vector<unsigned int> m_layerToCloudLayerID;
449  osg::ref_ptr<osg::IntArray> m_startLayerIndex;
450 
451  osg::ref_ptr<osg::Vec3Array> m_vertices;
452  osg::ref_ptr<osg::Vec3Array> m_bb_min;
453  osg::ref_ptr<osg::Vec3Array> m_bb_max;
454  osg::ref_ptr<osg::Vec4Array> m_rotation;
455  osg::ref_ptr<osg::Vec4Array> m_center;
456  osg::ref_ptr<osg::Vec4Array> m_ids;
457  osg::ref_ptr<osg::Vec4Array> m_box_centers;
458  osg::ref_ptr<osg::Vec4Array> m_color;
459 
460  osg::ref_ptr<osg::IntArray> m_numPrimitiveArrays;
461 
462  std::map<int, std::string> m_uniforms;
463 
464  osg::ref_ptr<osg::Vec3Array> m_ambientlight_h;
465  osg::ref_ptr<osg::Vec3Array> m_ambientlight_t;
466  osg::ref_ptr<osg::Vec3Array> m_diffuselight;
467 
468  static CloudLayerState* m_CloudLayerState;
469 
470  Fog* m_fog;
471  };
472 }