vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Cloud.h
Go to the documentation of this file.
1 
9 #ifndef _VRECKO_CLOUDS_CLOUD_H
10 #define _VRECKO_CLOUDS_CLOUD_H
11 
12 #include <osg/Vec4>
13 #include <osg/Geometry>
14 #include <osg/Texture2D>
15 
16 #include <iostream>
17 #include <vector>
18 #include <algorithm>
19 #include <queue>
20 #include <set>
21 #include <cmath>
22 
23 #include "BoundingVol.h"
24 
25 #ifndef PI
26 #define PI 3.1415926535
27 #endif
28 
33 {
35  double Fovy, Aspect, Near, Far;
36 };
37 
41 struct SunLight
42 {
43  osg::Vec4f ambientLight,
44  sunColor;
45  osg::Vec3f position;
46 };
47 
51 struct Particle
52 {
53  float size;
54  osg::Vec3f position;
55  osg::Vec4f color;
56 };
57 
61 struct CloudParticle : public Particle
62 {
63  float distToCam;
66 };
67 
71 class Cloud
72 {
73 public:
74  AABoundingBox boundingBox; // Bounding box (sphere, actually) of the cloud
75  std::vector<CloudParticle> cloudParticle; // Contains all of the cloud's particles
76 private:
77 
78  float distToCam;
79  bool screenImpostor; // Indicates whether the camera is in the cloud and impostor is rendered on the whole screen
80 
81  osg::ref_ptr<osg::Geometry> cloudGeometry; // Cloud geometry itself, contains all of the particles and is connected to the scene graph
82  osg::ref_ptr<osg::StateSet> cloudState; // State of the cloud
83 
84  osg::Vec3f center; // Contains position of the center of the cloud (a vector used as a point in 3D)
85  osg::Vec3f last_cam;
86  osg::Vec3f right, up;
87 
88  static osg::Vec3f cameraPosition, cameraView, cameraUp; // Current camera data
89  static osg::Matrix viewMatrix; // Curent view matrix
90 
91  static osg::ref_ptr<osg::Texture2D> cloudParticleTexture; // Texture with cloud particles
92 
93  // Lighting related attributes
94  static const int SplatBufferSize;
95  static float Albedo;
96  static float Extinction;
97  static float Alpha;
98  static SunLight sunLight;
99  static unsigned int lightTexture;
100 
101  // Priority queue and cloud update related attributes
102  static std::priority_queue<Cloud *, std::vector<Cloud *> > cloudQueue;
103  static std::vector<Cloud*> cloudVector;
104  static std::set<Cloud*> inQueueSet;
105  static unsigned int maxUpdatesParFrame;
106  static float translateErrorTolerance;
107 
108  static CloudSceneInfo sceneInfo; // Contains the information about the scene and projection - view struct CloudSceneInfo
109 
110 public:
111  Cloud() { screenImpostor = false; }
112 
116  void CalculateBoundingBox(void);
117 
122  void ComputeUpRightVectors();
123 
128  void LightCloud(void);
129 
133  void RegularUpdate();
134 
140  void InitCloudGeometry(osg::ref_ptr<osg::Geometry> cloudGeometry);
141 
145  void UpdateCloudLotRocScale();
146 
150  void UpdateCloudColor();
151 
155  void UpdateCloud();
156 
160  void Delete(void);
161 
162  // Set methods
163  static void SetSunLight(SunLight& sun) { sunLight = sun; }
164  void SetLastCam(osg::Vec3f last_cam) { this->last_cam = last_cam; }
165 
166  // Static methods
167 
172  static void GenerateLightTexture(void);
173 
177  static void DestroyLightTexture(void);
178 
185  static bool AddToQueue(Cloud* cloudToUpdate);
186 
190  static void RealizeQueue(void);
191 
192  static void SetTranslateErrorTolerance(float tolerance) { translateErrorTolerance = abs(cos(tolerance/180*3.1415926f)); }
193  static void SetMaxCloudUpdates(unsigned int max) { maxUpdatesParFrame = max; }
194  static void SetSceneInfo(const CloudSceneInfo& info) { sceneInfo = info; }
195 
196  static void SetCloudParticleTexture(osg::ref_ptr<osg::Texture2D> newCloudParticleTexture) { cloudParticleTexture = newCloudParticleTexture; }
197 
205  static void SetCamera(const osg::Vec3f& newCameraPosition, const osg::Vec3f& newCameraView, const osg::Vec3f& newCameraUp);
206  static void SetViewMatrix(const osg::Matrix& newViewMatrix) { viewMatrix = newViewMatrix; }
207 
208 public:
209  // Functors
211  {
212  public:
213  bool operator () (CloudParticle particle1, CloudParticle particle2)
214  {
215  return particle1.distToCam < particle2.distToCam;
216  }
217  } SortAway;
218 };
219 
220 #endif