CloudyDay
 All Classes Functions Variables Enumerations
CloudyDay.h
1 #pragma once
2 #include "Create2DCloud.h"
3 #include "ObjectModel.h"
4 #include "Ground.h"
5 #include "HUD.h"
6 #include "Blur.h"
7 #include "PostProcess.h"
8 #include "LuminanceCalculation.h"
9 #include "AtmosphereMie.h"
10 #include "AtmosphereHimmel.h"
11 #include "TerrainMIE.h"
12 #include "TerrainHimmel.h"
13 #include "TerrainGeometry.h"
14 #include "ReflectionCamera.h"
15 #include "CloudCreator.h"
16 #include "Atmosphereprecompute.h"
17 
18 #include "PerlinCloudState.h"
19 #include "CirrusCloudState.h"
20 #include "CirrusStratusCloudState.h"
21 #include "CirrusCumulusCloudState.h"
22 #include "AltStratusCloudState.h"
23 
24 #include "Night.h"
25 
26 #include "Fog.h"
27 
28 #include "ObjCloud.h"
29 #include "DataCloud.h"
30 
31 #include "Export.h"
32 #include "SkydomeMie.h"
33 #include "PerlinNoiseCloud.h"
34 #include "Cirrus.h"
35 #include "CirrusCumulus.h"
36 #include "CSGObject.h"
37 #include "Contrail.h"
38 #include "ContrailUpdate.h"
39 #include "LuminanceCalculation.h"
40 
41 #include <osg/PolygonMode>
42 #include <osg/ShapeDrawable>
43 #include <osg/Depth>
44 #include <osg/Program>
45 #include <osg/Shader>
46 #include <osg/Uniform>
47 
48 #include "Forest.h"
49 #include "Grass.h"
50 
51 #include "Rain.h"
52 
53 #include "BlurShadow.h"
54 #include "LightShaft.h"
55 #include "LightShaft2.h"
56 
57 #define TERRAIN
58 #define ATMOSPHERE
59 
60 namespace osgCloudyDay
61 {
62  class PostDrawCallBack : public osg::Camera::DrawCallback
63 {
64 public:
65  PostDrawCallBack(osgCloudyDay::CloudCreator*& cloudcreator) : m_cloudcreator(cloudcreator)
66  {
67  }
68 
69  virtual void operator () (const osg::Camera& /*camera*/) const
70  {
71  m_cloudcreator->UpdateAddClouds();
72  }
73 
74 protected:
75  osgCloudyDay::CloudCreator*& m_cloudcreator;
76 };
77 
78  class CullDebugGeometryCallback : public osg::NodeCallback
79 {
80  public:
82  {
83 
84  }
85 
86  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
87  {
88  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
89  osg::ref_ptr<osg::Geode> geometry = dynamic_cast<osg::Geode*> (node);
90 
91  std::cerr << "CullDebugGeometryCallback1" << std::endl;
92  if(geometry && cv)
93  {
94  std::cerr << "CullDebugGeometryCallback2" << std::endl;
95 
96  //geometry->getOrCreateStateSet()->getUniform("ModelMatrix")->set(model);
97  geometry->getOrCreateStateSet()->getUniform("viewMatrix")->set(Scene::GetViewMatrix_View());
98  geometry->getOrCreateStateSet()->getUniform("viewMatrixInv")->set(osg::Matrix::inverse(Scene::GetViewMatrix_View()));
99  geometry->getOrCreateStateSet()->getUniform("project")->set(Scene::GetProjectionMatrix_View());
100 
101 #ifdef SHADOW_MAPPING
102  geometry->getOrCreateStateSet()->getUniform("project_light")->set(Scene::GetProjectionMatrix_Light());
103  geometry->getOrCreateStateSet()->getUniform("inv_project_light")->set(osg::Matrix::inverse(Scene::GetProjectionMatrix_Light()));
104  geometry->getOrCreateStateSet()->getUniform("viewMatrix_light")->set(Scene::GetLightCamera()->getViewMatrix());
105  geometry->getOrCreateStateSet()->getUniform("viewMatrixInv_light")->set(osg::Matrix::inverse(Scene::GetLightCamera()->getViewMatrix()));
106  geometry->getOrCreateStateSet()->getUniform("modelViewMatrix_light")->set(Scene::GetLightCamera()->getViewMatrix());
107 #endif
108  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("color_tex", 0));
109  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("shadow_tex", 1));
110  geometry->getOrCreateStateSet()->addUniform(new osg::Uniform("depth_tex", 2));
111 
112  geometry->getOrCreateStateSet()->setTextureAttribute( 0, osgCloudyDay::CloudScene::tex_clouds );
113  geometry->getOrCreateStateSet()->setTextureAttribute( 1, Scene::GetBlurTexture());
114  geometry->getOrCreateStateSet()->setTextureAttribute( 2, Scene::GetShadowDepthTexture() );
115  }
116 
117  traverse(node, nv);
118 
119  //Scene::GetViewCamera()->setViewMatrix(Scene::GetViewDepthCamera()->getViewMatrix());
120  //Scene::GetViewCamera()->setProjectionMatrix(Scene::GetViewDepthCamera()->getProjectionMatrix());
121  }
122 
123 };
124 
125  class DebugGeometry : public osg::Geometry
126  {
127  public:
128  DebugGeometry()
129  {
130  }
131 
132  ~DebugGeometry()
133  {
134 
135  }
136 
137  void drawImplementation(osg::RenderInfo& renderInfo) const
138  {
139  if (_internalOptimizedGeometry.valid())
140  {
141  _internalOptimizedGeometry->drawImplementation(renderInfo);
142  return;
143  }
144 
145  osg::State& state = *renderInfo.getState();
146 
147  bool checkForGLErrors = state.getCheckForGLErrors()==osg::State::ONCE_PER_ATTRIBUTE;
148  if (checkForGLErrors) state.checkGLErrors("start of Geometry::drawImplementation()");
149 
150  bool useFastPath = areFastPathsUsed();
151  // useFastPath = false;
152 
153  bool usingVertexBufferObjects = _useVertexBufferObjects && state.isVertexBufferObjectSupported();
154  bool handleVertexAttributes = !_vertexAttribList.empty();
155 
156  osg::ArrayDispatchers& arrayDispatchers = state.getArrayDispatchers();
157 
158  arrayDispatchers.reset();
159  arrayDispatchers.setUseVertexAttribAlias(useFastPath && state.getUseVertexAttributeAliasing());
160  arrayDispatchers.setUseGLBeginEndAdapter(!useFastPath);
161 
162  arrayDispatchers.activateNormalArray(_normalData.binding, _normalData.array.get(), _normalData.indices.get());
163  arrayDispatchers.activateColorArray(_colorData.binding, _colorData.array.get(), _colorData.indices.get());
164  arrayDispatchers.activateSecondaryColorArray(_secondaryColorData.binding, _secondaryColorData.array.get(), _secondaryColorData.indices.get());
165  arrayDispatchers.activateFogCoordArray(_fogCoordData.binding, _fogCoordData.array.get(), _fogCoordData.indices.get());
166 
167  if (handleVertexAttributes)
168  {
169  for(unsigned int unit=0;unit<_vertexAttribList.size();++unit)
170  {
171  arrayDispatchers.activateVertexAttribArray(_vertexAttribList[unit].binding, unit, _vertexAttribList[unit].array.get(), _vertexAttribList[unit].indices.get());
172  }
173  }
174 
175  // dispatch any attributes that are bound overall
176  arrayDispatchers.dispatch(BIND_OVERALL,0);
177 
178  state.lazyDisablingOfVertexAttributes();
179 
180  if (useFastPath)
181  {
182  // set up arrays
183  if( _vertexData.array.valid() )
184  state.setVertexPointer(_vertexData.array.get());
185 
186  if (_normalData.binding==BIND_PER_VERTEX && _normalData.array.valid())
187  state.setNormalPointer(_normalData.array.get());
188 
189  if (_colorData.binding==BIND_PER_VERTEX && _colorData.array.valid())
190  state.setColorPointer(_colorData.array.get());
191 
192  if (_secondaryColorData.binding==BIND_PER_VERTEX && _secondaryColorData.array.valid())
193  state.setSecondaryColorPointer(_secondaryColorData.array.get());
194 
195  if (_fogCoordData.binding==BIND_PER_VERTEX && _fogCoordData.array.valid())
196  state.setFogCoordPointer(_fogCoordData.array.get());
197 
198  for(unsigned int unit=0;unit<_texCoordList.size();++unit)
199  {
200  const osg::Array* array = _texCoordList[unit].array.get();
201  if (array) state.setTexCoordPointer(unit,array);
202  }
203 
204  if( handleVertexAttributes )
205  {
206  for(unsigned int index = 0; index < _vertexAttribList.size(); ++index )
207  {
208  const osg::Array* array = _vertexAttribList[index].array.get();
209  const AttributeBinding ab = _vertexAttribList[index].binding;
210  if( ab == BIND_PER_VERTEX && array )
211  {
212  state.setVertexAttribPointer( index, array, _vertexAttribList[index].normalize );
213  }
214  }
215  }
216  }
217  else
218  {
219  for(unsigned int unit=0;unit<_texCoordList.size();++unit)
220  {
221  arrayDispatchers.activateTexCoordArray(BIND_PER_VERTEX, unit, _texCoordList[unit].array.get(), _texCoordList[unit].indices.get());
222  }
223 
224  arrayDispatchers.activateVertexArray(BIND_PER_VERTEX, _vertexData.array.get(), _vertexData.indices.get());
225  }
226 
227  state.applyDisablingOfVertexAttributes();
228 
229  bool bindPerPrimitiveSetActive = arrayDispatchers.active(BIND_PER_PRIMITIVE_SET);
230  bool bindPerPrimitiveActive = arrayDispatchers.active(BIND_PER_PRIMITIVE);
231 
232  if (checkForGLErrors) state.checkGLErrors("Geometry::drawImplementation() after vertex arrays setup.");
233 
234 
235  //std::cerr << std::endl << "DEBUG GEOMSTRY" << std::endl << std::endl;
236 
238  //
239  // draw the primitives themselves.
240  //
241 
242  for(unsigned int primitiveSetNum=0; primitiveSetNum!= _primitives.size() ; ++primitiveSetNum)
243  {
244  // dispatch any attributes that are bound per primitive
245  if (bindPerPrimitiveSetActive)
246  arrayDispatchers.dispatch(BIND_PER_PRIMITIVE_SET, primitiveSetNum);
247 
248  const osg::PrimitiveSet* primitiveset = _primitives[primitiveSetNum].get();
249 
250  //glDisable(GL_BLEND);
251  //glDisable(GL_DEPTH_TEST);
252  //glDisable(GL_CULL_FACE);
253 
254  osg::State& state = *renderInfo.getState();
255 
256  //::cerr << "DEBUG GEOMSTRY(project_light): " << state.getUniformLocation("project_light") << std::endl;
257 
258  //osg::Depth* depth2 = new osg::Depth(osg::Depth::ALWAYS, 0.0, 1.0, false); //MIT DEPTH => FEHLER
259  //depth2->apply(state);
260 
261 
262  //std::cerr << "HIER BIN ICH 1" << std::endl;
263  //sh_blur_linear_vert1->apply(state);
264  //state.applyTextureAttribute(0, Scene::fbo_glare_texture);
265  //Scene::fbo_V1_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
266  primitiveset->draw(state, usingVertexBufferObjects);
267 
268 
269  }
270 
271  // unbind the VBO's if any are used.
272  state.unbindVertexBufferObject();
273  state.unbindElementBufferObject();
274 
275  if (checkForGLErrors) state.checkGLErrors("end of Geometry::drawImplementation().");
276  }
277  };
278 
279  class CloudyDay
280  {
281  public:
282  CloudyDay(osgCloudyDay::TerrainConfig* config, bool add_rain=false, bool add_forest=false, bool add_grass=false, bool bloom=false, bool star=false);
286  ~CloudyDay(void);
287 
288  void Initialize(int width, int size, osgCloudyDay::CloudState*& clouds, std::vector<osgCloudyDay::Cloud2DState> cloud2dstates, std::vector<int> m_ids, std::vector<std::string> reflection_models, std::vector<std::string> reflection_tex, osgCloudyDay::Fog* fog, std::vector<std::string> luds);
289 
300  osg::Camera**& GetReflectionCamerasAt(int id);
301 
305  void UpdateLightForClouds();
306 
311  osg::ref_ptr<osg::Camera> GetHUD();
316  osg::ref_ptr<osg::Camera> GetBlurProcess();
321  osg::ref_ptr<osg::Camera> GetBlur2Process();
326  osg::ref_ptr<osg::Camera> GetPostProcess();
327 
328  osg::ref_ptr<osg::Node> GetObjectModel(int);
329  //osg::ref_ptr<osg::Node> GetAtmosphere();
330  //osg::ref_ptr<osg::Node> GetTerrain();
331 
332  static Scene* m_scene;
333 
338  osg::ref_ptr<osg::Camera> GetLightCamera();
343  osg::ref_ptr<osg::Camera> GetLightCloudCamera();
348  osg::ref_ptr<osg::Node> GetScene();
353  osg::ref_ptr<osg::Camera> GetViewCamera();
358  osg::ref_ptr<osg::Camera> GetViewCamera2();
363  osg::ref_ptr<osg::Camera> GetViewDepthCamera();
364 
365  static osgCloudyDay::LightShaft* m_lightshaft;
370  static osg::ref_ptr<osg::Camera> GetLightshaftCamera();
371 
375  void CreateAtmosphere();
379  void CreateTerrain();
380 
384  void CreateNight();
388  void CreateForest();
392  void CreateGrass();
396  void CreateRain();
401  void Create2DClouds(std::vector<osgCloudyDay::Cloud2DState> cloud2dstates);
408  void CreatePostProcessing(std::vector<std::string> luds, int hdr_mapping, bool use_avglum);
413  void CreateClouds(osgCloudyDay::CloudState*& clouds);
414 
419  osg::ref_ptr<osg::Camera> GetLuminanceCalculation();
424  osg::ref_ptr<osg::Camera> GetBlurShadowMapCamera();
425 
430  bool UseBloom();
435  bool UseStar();
436 
437  protected:
438  LightShaft2* m_lightshaft2;
439 
441 
442  osgCloudyDay::CloudCreator* m_cloudcreator;
443  ObjectModel** m_object;
444  osgCloudyDay::Atmosphere* m_atmopshere;
445  osgCloudyDay::MyTerrain* m_terrain;
446 
447  osg::ref_ptr<osg::Camera> camera_lightshaft;
448  osg::ref_ptr<osg::Group> lightView_scene;
449 
450  osg::ref_ptr<osg::Group> scene;
451  osg::ref_ptr<osg::Group> cube_scene;
452  osg::ref_ptr<osg::Group> hud;
453 
454  ReflectionCamera** m_reflection;
455  int num_reflections;
456 
457 
458  osg::ref_ptr<osg::Texture2D> tex_ortho;
459 
460  Ground* m_ground;
461  Scene* my_scene;
462  osg::ref_ptr<osg::Group> scene2;
463 
464  osgCloudyDay::HUD* m_hud;
465  osgCloudyDay::PostProcess* m_postprocess;
466  osgCloudyDay::Blur* m_blur;
467  osgCloudyDay::Blur* m_blur2;
469 
470  osgCloudyDay::TerrainConfig* m_terrainconfig;
471 
472  osgCloudyDay::Rain* m_rain;
473  Night* m_night;
474  Forest* m_forrest;
475  Grass* m_grass;
476  bool m_add_forest;
477  bool m_add_grass;
478  bool m_add_rain;
479  bool m_bloom;
480  bool m_star;
481 
482  osg::ref_ptr<osg::Geode> debug_geode;
483  osg::ref_ptr<osg::Geode> debug_geode2;
484  osg::ref_ptr<osg::Geode> debug_geode3;
485 
486  osgCloudyDay::Create2DCloud* m_create2dcloud;
487 
488  osgCloudyDay::Fog* m_fog;
489  osgCloudyDay::BlurShadow* blurShadow;
490  };
491 }