CloudyDay
 All Classes Functions Variables Enumerations
HUD.h
1 #pragma once
2 #include "CloudScene.h"
3 #include <osg/Depth>
4 
5 namespace osgCloudyDay
6 {
13  class HUD
14  {
15  public:
20  HUD(osg::ref_ptr<osg::Texture> debug_tex);
24  HUD(void);
28  ~HUD(void);
29 
33  static void Initialize();
34 
38  void CreateGeometry();
39 
44  virtual void SetupUniform(osg::ref_ptr<osg::StateSet> states);
49  virtual void SetupTextures(osg::ref_ptr<osg::StateSet> states);
50 
54  virtual void CreateCamera();
55 
60  void SetSceneTexture(osg::ref_ptr<osg::Texture2D> tex);
65  void SetGodrayTexture(osg::ref_ptr<osg::Texture2D> tex);
70  void SetCloudTexture(osg::ref_ptr<osg::Texture2D> tex);
71 
76  osg::ref_ptr<osg::Camera> GetCamera();
77 
78  protected:
79  osg::ref_ptr<osg::Texture2D> m_scenetex;
80  osg::ref_ptr<osg::Texture2D> m_goodraytex;
81  osg::ref_ptr<osg::Texture2D> m_cloudtex;
82 
83  osg::ref_ptr<osg::Program> sh_blur_linear;
84  osg::ref_ptr<osg::Uniform> uniform_blur_texsize;
85 
86  osg::ref_ptr<osg::Uniform> uniform_avg_texsize;
87  osg::ref_ptr<osg::Program> sh_lum;
88 
89  osg::ref_ptr<osg::Camera> cam_hud;
90  osg::ref_ptr<osg::StateSet> nodessP;
91  osg::ref_ptr<osg::Program> planeProg;
92  osg::ref_ptr<osg::Geode> plane;
93 
94  int renderorder;
95 
96  osg::ref_ptr<osg::Texture> m_debug_tex;
97 
98  //CALCULATION OF LUMMINANCE
99  static osg::ref_ptr<osg::Texture2D> fbo_lum0_texture;
100  static osg::ref_ptr<osg::Texture2D> fbo_lum1_texture;
101  static osg::ref_ptr<osg::Texture2D> fbo_lum2_texture;
102  static osg::ref_ptr<osg::Texture2D> fbo_lum3_texture;
103  static osg::ref_ptr<osg::Texture2D> fbo_lum4_texture;
104  static osg::FrameBufferObject* fbo_Lum0;
105  static osg::FrameBufferObject* fbo_Lum1;
106  static osg::FrameBufferObject* fbo_Lum2;
107  static osg::FrameBufferObject* fbo_Lum3;
108  static osg::FrameBufferObject* fbo_Lum4;
109 
110 
111  //POST PROCESSING: STAR EFFECT AND GLARE EFFECT
112 
113  static osg::ref_ptr<osg::Texture2D> fbo_blurV1_texture;
114  static osg::ref_ptr<osg::Texture2D> fbo_blurV2_texture;
115  static osg::ref_ptr<osg::Texture2D> fbo_blurV3_texture;
116  static osg::ref_ptr<osg::Texture2D> fbo_blurV4_texture;
117 
118  static osg::ref_ptr<osg::Texture2D> fbo_blurH1_texture;
119  static osg::ref_ptr<osg::Texture2D> fbo_blurH2_texture;
120  static osg::ref_ptr<osg::Texture2D> fbo_blurH3_texture;
121  static osg::ref_ptr<osg::Texture2D> fbo_blurH4_texture;
122 
123  static osg::ref_ptr<osg::Texture2D> fbo_star1_texture;
124  static osg::ref_ptr<osg::Texture2D> fbo_star2_texture;
125  static osg::ref_ptr<osg::Texture2D> fbo_star3_texture;
126  static osg::ref_ptr<osg::Texture2D> fbo_star4_texture;
127 
128  static osg::FrameBufferObject* fbo_V1_blur;
129  static osg::FrameBufferObject* fbo_H1_blur;
130  static osg::FrameBufferObject* fbo_V2_blur;
131  static osg::FrameBufferObject* fbo_H2_blur;
132  static osg::FrameBufferObject* fbo_V3_blur;
133  static osg::FrameBufferObject* fbo_H3_blur;
134  static osg::FrameBufferObject* fbo_V4_blur;
135  static osg::FrameBufferObject* fbo_H4_blur;
136 
137  static osg::ref_ptr<osg::Texture2D> fbo_glare_texture;
138  };
139 
143  class Luminance : public osg::Geometry
144  {
145  public:
161  Luminance( osg::ref_ptr<osg::Program> lum_prog,
162  osg::ref_ptr<osg::Texture2D> _fbo_scene_texture,
163  osg::ref_ptr<osg::FrameBufferObject> _fbo_Lum0,
164  osg::ref_ptr<osg::FrameBufferObject> _fbo_Lum1,
165  osg::ref_ptr<osg::FrameBufferObject> _fbo_Lum2,
166  osg::ref_ptr<osg::FrameBufferObject> _fbo_Lum3,
167  osg::ref_ptr<osg::FrameBufferObject> _fbo_Lum4,
168  osg::ref_ptr<osg::Texture2D> _fbo_lum0,
169  osg::ref_ptr<osg::Texture2D> _fbo_lum1,
170  osg::ref_ptr<osg::Texture2D> _fbo_lum2,
171  osg::ref_ptr<osg::Texture2D> _fbo_lum3,
172  osg::ref_ptr<osg::Texture2D> _fbo_lum4)
173  {
174  sh_blur_linear_vert1 = (prog_vert1);
175 
176  fbo_scene_texture = _fbo_scene_texture;
177 
178  fbo_Lum0 = _fbo_Lum0;
179  fbo_Lum1 = _fbo_Lum1;
180  fbo_Lum2 = _fbo_Lum2;
181  fbo_Lum3 = _fbo_Lum3,
182  fbo_Lum4 = _fbo_Lum4;
183  fbo_lum0_texture = _fbo_lum0;
184  fbo_lum1_texture = _fbo_lum1;
185  fbo_lum2_texture = _fbo_lum2;
186  fbo_lum3_texture = _fbo_lum3;
187  fbo_lum4_texture = _fbo_lum4;
188  }
193  {
194 
195  }
196 
200  void drawImplementation(osg::RenderInfo& renderInfo) const
201  {
202  if (_internalOptimizedGeometry.valid())
203  {
204  _internalOptimizedGeometry->drawImplementation(renderInfo);
205  return;
206  }
207 
208  osg::State& state = *renderInfo.getState();
209 
210  bool checkForGLErrors = state.getCheckForGLErrors()==osg::State::ONCE_PER_ATTRIBUTE;
211  if (checkForGLErrors) state.checkGLErrors("start of Geometry::drawImplementation()");
212 
213  bool useFastPath = areFastPathsUsed();
214  // useFastPath = false;
215 
216  bool usingVertexBufferObjects = _useVertexBufferObjects && state.isVertexBufferObjectSupported();
217  bool handleVertexAttributes = !_vertexAttribList.empty();
218 
219  osg::ArrayDispatchers& arrayDispatchers = state.getArrayDispatchers();
220 
221  arrayDispatchers.reset();
222  arrayDispatchers.setUseVertexAttribAlias(useFastPath && state.getUseVertexAttributeAliasing());
223  arrayDispatchers.setUseGLBeginEndAdapter(!useFastPath);
224 
225  arrayDispatchers.activateNormalArray(_normalData.binding, _normalData.array.get(), _normalData.indices.get());
226  arrayDispatchers.activateColorArray(_colorData.binding, _colorData.array.get(), _colorData.indices.get());
227  arrayDispatchers.activateSecondaryColorArray(_secondaryColorData.binding, _secondaryColorData.array.get(), _secondaryColorData.indices.get());
228  arrayDispatchers.activateFogCoordArray(_fogCoordData.binding, _fogCoordData.array.get(), _fogCoordData.indices.get());
229 
230  if (handleVertexAttributes)
231  {
232  for(unsigned int unit=0;unit<_vertexAttribList.size();++unit)
233  {
234  arrayDispatchers.activateVertexAttribArray(_vertexAttribList[unit].binding, unit, _vertexAttribList[unit].array.get(), _vertexAttribList[unit].indices.get());
235  }
236  }
237 
238  // dispatch any attributes that are bound overall
239  arrayDispatchers.dispatch(BIND_OVERALL,0);
240 
241  state.lazyDisablingOfVertexAttributes();
242 
243  if (useFastPath)
244  {
245  // set up arrays
246  if( _vertexData.array.valid() )
247  state.setVertexPointer(_vertexData.array.get());
248 
249  if (_normalData.binding==BIND_PER_VERTEX && _normalData.array.valid())
250  state.setNormalPointer(_normalData.array.get());
251 
252  if (_colorData.binding==BIND_PER_VERTEX && _colorData.array.valid())
253  state.setColorPointer(_colorData.array.get());
254 
255  if (_secondaryColorData.binding==BIND_PER_VERTEX && _secondaryColorData.array.valid())
256  state.setSecondaryColorPointer(_secondaryColorData.array.get());
257 
258  if (_fogCoordData.binding==BIND_PER_VERTEX && _fogCoordData.array.valid())
259  state.setFogCoordPointer(_fogCoordData.array.get());
260 
261  for(unsigned int unit=0;unit<_texCoordList.size();++unit)
262  {
263  const osg::Array* array = _texCoordList[unit].array.get();
264  if (array) state.setTexCoordPointer(unit,array);
265  }
266 
267  if( handleVertexAttributes )
268  {
269  for(unsigned int index = 0; index < _vertexAttribList.size(); ++index )
270  {
271  const osg::Array* array = _vertexAttribList[index].array.get();
272  const AttributeBinding ab = _vertexAttribList[index].binding;
273  if( ab == BIND_PER_VERTEX && array )
274  {
275  state.setVertexAttribPointer( index, array, _vertexAttribList[index].normalize );
276  }
277  }
278  }
279  }
280  else
281  {
282  for(unsigned int unit=0;unit<_texCoordList.size();++unit)
283  {
284  arrayDispatchers.activateTexCoordArray(BIND_PER_VERTEX, unit, _texCoordList[unit].array.get(), _texCoordList[unit].indices.get());
285  }
286 
287  arrayDispatchers.activateVertexArray(BIND_PER_VERTEX, _vertexData.array.get(), _vertexData.indices.get());
288  }
289 
290  state.applyDisablingOfVertexAttributes();
291 
292  bool bindPerPrimitiveSetActive = arrayDispatchers.active(BIND_PER_PRIMITIVE_SET);
293  bool bindPerPrimitiveActive = arrayDispatchers.active(BIND_PER_PRIMITIVE);
294 
295  if (checkForGLErrors) state.checkGLErrors("Geometry::drawImplementation() after vertex arrays setup.");
296 
297 
298  osg::GL2Extensions* extension = osg::GL2Extensions::Get(0,true);
299 
300  glClearColor(0.f, 0.f, 0.f, 0.f);
302  //
303  // draw the primitives themselves.
304  //
305 
306 
307  fbo_Lum0->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
308  glViewport(0,0,256, 256);
309  glClear( GL_COLOR_BUFFER_BIT);
310 
311  fbo_Lum1->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
312  glViewport(0,0,64, 64);
313  glClear( GL_COLOR_BUFFER_BIT);
314 
315  fbo_Lum2->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
316  glViewport(0,0,16, 16);
317  glClear( GL_COLOR_BUFFER_BIT);
318 
319  fbo_Lum3->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
320  glViewport(0,0,4, 4);
321  glClear( GL_COLOR_BUFFER_BIT);
322 
323  fbo_Lum4->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
324  glViewport(0,0,1, 1);
325  glClear( GL_COLOR_BUFFER_BIT);
326 
327 
328  // std::cerr << "HUD - CALCULATE LUMIANCNE" << std::endl;
329 
331 
332  for(unsigned int primitiveSetNum=0; primitiveSetNum!= _primitives.size() ; ++primitiveSetNum)
333  {
334  // dispatch any attributes that are bound per primitive
335  if (bindPerPrimitiveSetActive)
336  arrayDispatchers.dispatch(BIND_PER_PRIMITIVE_SET, primitiveSetNum);
337 
338  const osg::PrimitiveSet* primitiveset = _primitives[primitiveSetNum].get();
339 
340  glDisable(GL_BLEND);
341  glDisable(GL_DEPTH_TEST);
342  glDisable(GL_CULL_FACE);
343 
344  osg::State& state = *renderInfo.getState();
345 
346  osg::Depth* depth2 = new osg::Depth(osg::Depth::ALWAYS, 0.0, 1.0, false); //MIT DEPTH => FEHLER
347  depth2->apply(state);
348 
349 
350  glViewport(0, 0, 256, 256);
351  sh_blur_linear_vert1->apply(state);
352  state.applyTextureAttribute(0, fbo_scene_texture);
353  fbo_Lum0->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
354  uniform_avg_texsize->set(256.f);
355  uniform_avg_texsize->apply(extension, state.getUniformLocation("texsize"));
356  primitiveset->draw(state, usingVertexBufferObjects);
357 
358  glViewport(0, 0, 64, 64);
359  sh_blur_linear_vert1->apply(state);
360  state.applyTextureAttribute(0, fbo_lum0_texture);
361  fbo_Lum1->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
362  uniform_avg_texsize->set(64.f);
363  uniform_avg_texsize->apply(extension, state.getUniformLocation("texsize"));
364  primitiveset->draw(state, usingVertexBufferObjects);
365 
366  glViewport(0, 0, 16, 16);
367  sh_blur_linear_vert1->apply(state);
368  state.applyTextureAttribute(0, fbo_lum1_texture);
369  fbo_Lum2->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
370  uniform_avg_texsize->set(16.f);
371  uniform_avg_texsize->apply(extension, state.getUniformLocation("texsize"));
372  primitiveset->draw(state, usingVertexBufferObjects);
373 
374  glViewport(0, 0, 4, 4);
375  sh_blur_linear_vert1->apply(state);
376  state.applyTextureAttribute(0, fbo_lum2_texture);
377  fbo_Lum3->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
378  uniform_avg_texsize->set(4.f);
379  uniform_avg_texsize->apply(extension, state.getUniformLocation("texsize"));
380  primitiveset->draw(state, usingVertexBufferObjects);
381 
382  glViewport(0, 0, 1, 1);
383  sh_blur_linear_vert1->apply(state);
384  state.applyTextureAttribute(0, fbo_lum3_texture);
385  fbo_Lum4->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
386  uniform_avg_texsize->set(1.f);
387  uniform_avg_texsize->apply(extension, state.getUniformLocation("texsize"));
388  primitiveset->draw(state, usingVertexBufferObjects);
389 
391  glEnable(GL_DEPTH_TEST);
392  }
393 
394  // unbind the VBO's if any are used.
395  state.unbindVertexBufferObject();
396  state.unbindElementBufferObject();
397 
398  if (checkForGLErrors) state.checkGLErrors("end of Geometry::drawImplementation().");
399  }
400 
401  public:
402  osg::ref_ptr<osg::Uniform> uniform_avg_texsize;
403 
404  protected:
405  osg::ref_ptr<osg::Program> sh_blur_linear_vert1;
406  osg::ref_ptr<osg::FrameBufferObject> fbo_Lum0;
407  osg::ref_ptr<osg::FrameBufferObject> fbo_Lum1;
408  osg::ref_ptr<osg::FrameBufferObject> fbo_Lum2;
409  osg::ref_ptr<osg::FrameBufferObject> fbo_Lum3;
410  osg::ref_ptr<osg::FrameBufferObject> fbo_Lum4;
411  osg::ref_ptr<osg::Texture2D> fbo_lum0_texture;
412  osg::ref_ptr<osg::Texture2D> fbo_lum1_texture;
413  osg::ref_ptr<osg::Texture2D> fbo_lum2_texture;
414  osg::ref_ptr<osg::Texture2D> fbo_lum3_texture;
415  osg::ref_ptr<osg::Texture2D> fbo_lum4_texture;
416 
417 
418  osg::ref_ptr<osg::Texture2D> fbo_scene_texture;
419  };
420 
424  class BlurGeometry : public osg::Geometry
425  {
426  public:
427  BlurGeometry( osg::ref_ptr<osg::Program> prog_vert1, int mode,
428  osg::ref_ptr<osg::Texture2D> _fbo_glare_texture,
429  osg::ref_ptr<osg::Uniform> _uniform_blur_texsize,
430 
431  osg::ref_ptr<osg::FrameBufferObject> _fbo_V1_blur, osg::ref_ptr<osg::FrameBufferObject> _fbo_H1_blur,
432  osg::ref_ptr<osg::FrameBufferObject> _fbo_V2_blur, osg::ref_ptr<osg::FrameBufferObject> _fbo_H2_blur,
433  osg::ref_ptr<osg::FrameBufferObject> _fbo_V3_blur, osg::ref_ptr<osg::FrameBufferObject> _fbo_H3_blur,
434  osg::ref_ptr<osg::FrameBufferObject> _fbo_V4_blur, osg::ref_ptr<osg::FrameBufferObject> _fbo_H4_blur,
435 
436  osg::ref_ptr<osg::Texture2D> _fbo_blurV1_texture, osg::ref_ptr<osg::Texture2D> _fbo_blurH1_texture,
437  osg::ref_ptr<osg::Texture2D> _fbo_blurV2_texture, osg::ref_ptr<osg::Texture2D> _fbo_blurH2_texture,
438  osg::ref_ptr<osg::Texture2D> _fbo_blurV3_texture, osg::ref_ptr<osg::Texture2D> _fbo_blurH3_texture,
439  osg::ref_ptr<osg::Texture2D> _fbo_blurV4_texture, osg::ref_ptr<osg::Texture2D> _fbo_blurH4_texture
440  )
441  {
442  sh_blur_linear_vert1 = (prog_vert1);
443  m_mode = mode;
444 
445  fbo_glare_texture = _fbo_glare_texture;
446  uniform_blur_texsize = _uniform_blur_texsize;
447 
448  fbo_V1_blur = _fbo_V1_blur; fbo_H1_blur = _fbo_H1_blur;
449  fbo_V2_blur = _fbo_V2_blur; fbo_H2_blur = _fbo_H2_blur;
450  fbo_V3_blur = _fbo_V3_blur; fbo_H3_blur = _fbo_H3_blur;
451  fbo_V4_blur = _fbo_V4_blur; fbo_H4_blur = _fbo_H4_blur;
452 
453  fbo_blurV1_texture = _fbo_blurV1_texture; fbo_blurH4_texture = _fbo_blurH1_texture;
454  fbo_blurV2_texture = _fbo_blurV2_texture; fbo_blurH4_texture = _fbo_blurH2_texture;
455  fbo_blurV3_texture = _fbo_blurV3_texture; fbo_blurH4_texture = _fbo_blurH3_texture;
456  fbo_blurV4_texture = _fbo_blurV4_texture; fbo_blurH4_texture = _fbo_blurH4_texture;
457  }
462  {
463 
464  }
465 
469  void drawImplementation(osg::RenderInfo& renderInfo) const
470  {
471  if (_internalOptimizedGeometry.valid())
472  {
473  _internalOptimizedGeometry->drawImplementation(renderInfo);
474  return;
475  }
476 
477  osg::State& state = *renderInfo.getState();
478 
479  bool checkForGLErrors = state.getCheckForGLErrors()==osg::State::ONCE_PER_ATTRIBUTE;
480  if (checkForGLErrors) state.checkGLErrors("start of Geometry::drawImplementation()");
481 
482  bool useFastPath = areFastPathsUsed();
483  // useFastPath = false;
484 
485  bool usingVertexBufferObjects = _useVertexBufferObjects && state.isVertexBufferObjectSupported();
486  bool handleVertexAttributes = !_vertexAttribList.empty();
487 
488  osg::ArrayDispatchers& arrayDispatchers = state.getArrayDispatchers();
489 
490  arrayDispatchers.reset();
491  arrayDispatchers.setUseVertexAttribAlias(useFastPath && state.getUseVertexAttributeAliasing());
492  arrayDispatchers.setUseGLBeginEndAdapter(!useFastPath);
493 
494  arrayDispatchers.activateNormalArray(_normalData.binding, _normalData.array.get(), _normalData.indices.get());
495  arrayDispatchers.activateColorArray(_colorData.binding, _colorData.array.get(), _colorData.indices.get());
496  arrayDispatchers.activateSecondaryColorArray(_secondaryColorData.binding, _secondaryColorData.array.get(), _secondaryColorData.indices.get());
497  arrayDispatchers.activateFogCoordArray(_fogCoordData.binding, _fogCoordData.array.get(), _fogCoordData.indices.get());
498 
499  if (handleVertexAttributes)
500  {
501  for(unsigned int unit=0;unit<_vertexAttribList.size();++unit)
502  {
503  arrayDispatchers.activateVertexAttribArray(_vertexAttribList[unit].binding, unit, _vertexAttribList[unit].array.get(), _vertexAttribList[unit].indices.get());
504  }
505  }
506 
507  // dispatch any attributes that are bound overall
508  arrayDispatchers.dispatch(BIND_OVERALL,0);
509 
510  state.lazyDisablingOfVertexAttributes();
511 
512  if (useFastPath)
513  {
514  // set up arrays
515  if( _vertexData.array.valid() )
516  state.setVertexPointer(_vertexData.array.get());
517 
518  if (_normalData.binding==BIND_PER_VERTEX && _normalData.array.valid())
519  state.setNormalPointer(_normalData.array.get());
520 
521  if (_colorData.binding==BIND_PER_VERTEX && _colorData.array.valid())
522  state.setColorPointer(_colorData.array.get());
523 
524  if (_secondaryColorData.binding==BIND_PER_VERTEX && _secondaryColorData.array.valid())
525  state.setSecondaryColorPointer(_secondaryColorData.array.get());
526 
527  if (_fogCoordData.binding==BIND_PER_VERTEX && _fogCoordData.array.valid())
528  state.setFogCoordPointer(_fogCoordData.array.get());
529 
530  for(unsigned int unit=0;unit<_texCoordList.size();++unit)
531  {
532  const osg::Array* array = _texCoordList[unit].array.get();
533  if (array) state.setTexCoordPointer(unit,array);
534  }
535 
536  if( handleVertexAttributes )
537  {
538  for(unsigned int index = 0; index < _vertexAttribList.size(); ++index )
539  {
540  const osg::Array* array = _vertexAttribList[index].array.get();
541  const AttributeBinding ab = _vertexAttribList[index].binding;
542  if( ab == BIND_PER_VERTEX && array )
543  {
544  state.setVertexAttribPointer( index, array, _vertexAttribList[index].normalize );
545  }
546  }
547  }
548  }
549  else
550  {
551  for(unsigned int unit=0;unit<_texCoordList.size();++unit)
552  {
553  arrayDispatchers.activateTexCoordArray(BIND_PER_VERTEX, unit, _texCoordList[unit].array.get(), _texCoordList[unit].indices.get());
554  }
555 
556  arrayDispatchers.activateVertexArray(BIND_PER_VERTEX, _vertexData.array.get(), _vertexData.indices.get());
557  }
558 
559  state.applyDisablingOfVertexAttributes();
560 
561  bool bindPerPrimitiveSetActive = arrayDispatchers.active(BIND_PER_PRIMITIVE_SET);
562  bool bindPerPrimitiveActive = arrayDispatchers.active(BIND_PER_PRIMITIVE);
563 
564  if (checkForGLErrors) state.checkGLErrors("Geometry::drawImplementation() after vertex arrays setup.");
565 
566 
567  osg::GL2Extensions* extension = osg::GL2Extensions::Get(0,true);
568 
569  glClearColor(0.f, 0.1f, 0.f, 1.f);
571  //
572  // draw the primitives themselves.
573  //
574 
575  if(m_mode == 0)
576  {
577  fbo_V1_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
579  glClear( GL_COLOR_BUFFER_BIT);
580 
581  fbo_H1_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
582  glClear( GL_COLOR_BUFFER_BIT);
583 
584 
585  fbo_V2_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
587  glClear( GL_COLOR_BUFFER_BIT);
588 
589  fbo_H2_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
590  glClear( GL_COLOR_BUFFER_BIT);
591 
592 
593  fbo_V3_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
595  glClear( GL_COLOR_BUFFER_BIT);
596 
597  fbo_H3_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
598  glClear( GL_COLOR_BUFFER_BIT);
599 
600 
601  fbo_V4_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
603  glClear( GL_COLOR_BUFFER_BIT);
604 
605  fbo_H4_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
606  glClear( GL_COLOR_BUFFER_BIT);
607  }
608 
609 
611 
612  for(unsigned int primitiveSetNum=0; primitiveSetNum!= _primitives.size() ; ++primitiveSetNum)
613  {
614  // dispatch any attributes that are bound per primitive
615  if (bindPerPrimitiveSetActive)
616  arrayDispatchers.dispatch(BIND_PER_PRIMITIVE_SET, primitiveSetNum);
617 
618  const osg::PrimitiveSet* primitiveset = _primitives[primitiveSetNum].get();
619 
620  glDisable(GL_BLEND);
621  glDisable(GL_DEPTH_TEST);
622  glDisable(GL_CULL_FACE);
623 
624  osg::State& state = *renderInfo.getState();
625 
626  osg::Depth* depth2 = new osg::Depth(osg::Depth::ALWAYS, 0.0, 1.0, false); //MIT DEPTH => FEHLER
627  depth2->apply(state);
628 
629  if(m_mode == 0)
630  {
632  sh_blur_linear_vert1->apply(state);
633  state.applyTextureAttribute(0, fbo_glare_texture);
634  fbo_V1_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
635  uniform_blur_texsize->set((float)(osgCloudyDay::Scene::GetWidth()));
636  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
637  primitiveset->draw(state, usingVertexBufferObjects);
638 
639  glViewport(0.f, 0.f, osgCloudyDay::Scene::GetWidth()/2, osgCloudyDay::Scene::GetHeight()/2);
640  sh_blur_linear_vert1->apply(state);
641  state.applyTextureAttribute(0, fbo_glare_texture);
642  fbo_V2_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
643  uniform_blur_texsize->set((float)(osgCloudyDay::Scene::GetWidth())/2.f);
644  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
645  primitiveset->draw(state, usingVertexBufferObjects);
646 
647  glViewport(0.f, 0.f, osgCloudyDay::Scene::GetWidth()/4, osgCloudyDay::Scene::GetHeight()/4);
648  sh_blur_linear_vert1->apply(state);
649  state.applyTextureAttribute(0, fbo_glare_texture);
650  fbo_V3_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
651  uniform_blur_texsize->set((float)(osgCloudyDay::Scene::GetWidth())/4.f);
652  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
653  primitiveset->draw(state, usingVertexBufferObjects);
654 
655  glViewport(0.f, 0.f, osgCloudyDay::Scene::GetWidth()/8, osgCloudyDay::Scene::GetHeight()/8);
656  sh_blur_linear_vert1->apply(state);
657  state.applyTextureAttribute(0, fbo_glare_texture);
658  fbo_V4_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
659  uniform_blur_texsize->set((float)(osgCloudyDay::Scene::GetWidth())/8.f);
660  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
661  primitiveset->draw(state, usingVertexBufferObjects);
662  }
663  else
664  {
666  sh_blur_linear_vert1->apply(state);
667  state.applyTextureAttribute(0, fbo_blurV1_texture);
668  state.applyTextureAttribute(1, fbo_glare_texture);
669  fbo_H1_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
670  uniform_blur_texsize->set((float)(osgCloudyDay::Scene::GetHeight()));
671  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
672  primitiveset->draw(state, usingVertexBufferObjects);
673 
674  glViewport(0.f, 0.f, osgCloudyDay::Scene::GetWidth()/2, osgCloudyDay::Scene::GetHeight()/2);
675  sh_blur_linear_vert1->apply(state);
676  state.applyTextureAttribute(0, fbo_blurV2_texture);
677  state.applyTextureAttribute(1, fbo_glare_texture);
678  fbo_H2_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
679  uniform_blur_texsize->set((float)(osgCloudyDay::Scene::GetHeight())/2.f);
680  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
681  primitiveset->draw(state, usingVertexBufferObjects);
682 
683  glViewport(0.f, 0.f, osgCloudyDay::Scene::GetWidth()/4, osgCloudyDay::Scene::GetHeight()/4);
684  sh_blur_linear_vert1->apply(state);
685  state.applyTextureAttribute(0, fbo_blurV3_texture);
686  state.applyTextureAttribute(1, fbo_glare_texture);
687  fbo_H3_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
688  uniform_blur_texsize->set((float)(osgCloudyDay::Scene::GetHeight())/4.f);
689  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
690  primitiveset->draw(state, usingVertexBufferObjects);
691 
692  glViewport(0.f, 0.f, osgCloudyDay::Scene::GetWidth()/8, osgCloudyDay::Scene::GetHeight()/8);
693  sh_blur_linear_vert1->apply(state);
694  state.applyTextureAttribute(0, fbo_blurV4_texture);
695  state.applyTextureAttribute(1, fbo_glare_texture);
696  fbo_H4_blur->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
697  uniform_blur_texsize->set((float)(osgCloudyDay::Scene::GetHeight())/8.f);
698  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
699  primitiveset->draw(state, usingVertexBufferObjects);
700  }
701 
703  glEnable(GL_DEPTH_TEST);
704  }
705 
706  // unbind the VBO's if any are used.
707  state.unbindVertexBufferObject();
708  state.unbindElementBufferObject();
709 
710  if (checkForGLErrors) state.checkGLErrors("end of Geometry::drawImplementation().");
711  }
712 
713  int m_mode;
714 
715  private:
716  osg::ref_ptr<osg::Program> sh_blur_linear_vert1;
717 
718  osg::ref_ptr<osg::FrameBufferObject> fbo_V1_blur; osg::ref_ptr<osg::FrameBufferObject> fbo_H1_blur;
719  osg::ref_ptr<osg::FrameBufferObject> fbo_V2_blur; osg::ref_ptr<osg::FrameBufferObject> fbo_H2_blur;
720  osg::ref_ptr<osg::FrameBufferObject> fbo_V3_blur; osg::ref_ptr<osg::FrameBufferObject> fbo_H3_blur;
721  osg::ref_ptr<osg::FrameBufferObject> fbo_V4_blur; osg::ref_ptr<osg::FrameBufferObject> fbo_H4_blur;
722 
723  osg::ref_ptr<osg::Texture2D> fbo_blurV1_texture; osg::ref_ptr<osg::Texture2D> fbo_blurH1_texture;
724  osg::ref_ptr<osg::Texture2D> fbo_blurV2_texture; osg::ref_ptr<osg::Texture2D> fbo_blurH2_texture;
725  osg::ref_ptr<osg::Texture2D> fbo_blurV3_texture; osg::ref_ptr<osg::Texture2D> fbo_blurH3_texture;
726  osg::ref_ptr<osg::Texture2D> fbo_blurV4_texture; osg::ref_ptr<osg::Texture2D> fbo_blurH4_texture;
727 
728  osg::ref_ptr<osg::Texture2D> fbo_glare_texture;
729  osg::ref_ptr<osg::Uniform> uniform_blur_texsize;
730  };
731 
735  class FBODrawCallback : public osg::Drawable::DrawCallback
736  {
737  public:
743  FBODrawCallback(osg::Texture* texture, osg::Texture* texture2)
744  : osg::Drawable::DrawCallback()
745  , _texture(texture)
746  , _texture2(texture2)
747  {}
752  {}
753 
757  void drawImplementation(osg::RenderInfo& ri,const osg::Drawable* dr) const
758  {
759  //std::cout << "draw HUD" << std::endl;
760  //osg::FBOExtensions::instance(0,true)->__glewGenerateMipmap(GL_TEXTURE_2D);
761 
762  osg::FBOExtensions* fbo_ext = osg::FBOExtensions::instance(ri.getContextID(),true);
763  ri.getState()->setActiveTextureUnit(0);
764  ri.getState()->applyTextureAttribute(0, _texture);
765  //_texture->setUseHardwareMipMapGeneration(true);
766  _texture->getTextureObject(ri.getContextID())->bind();
767  //fbo_ext->__glewGenerateMipmap(_texture->getTextureTarget());
768 
769  ri.getState()->setActiveTextureUnit(1);
770  ri.getState()->applyTextureAttribute(1, _texture2);
771  //_texture2->setUseHardwareMipMapGeneration(true);
772  _texture2->getTextureObject(ri.getContextID())->bind();
773  //fbo_ext->__glewGenerateMipmap(_texture2->getTextureTarget());
774 
775  dr->drawImplementation(ri);
776 
777  }
778  osg::Texture* _texture;
779  osg::Texture* _texture2;
780  };
781 
785  class ViewerCallbackHUD : public osg::NodeCallback
786  {
787  public:
792  {
793 
794  }
799 
803  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
804  {
805  //std::cerr <<"POSTPROCESS" << std::endl;
806  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
807  osg::ref_ptr<osg::Geode> geometry = dynamic_cast<osg::Geode*> (node);
808 
809  if(geometry && cv)
810  {
811  osg::RefMatrixd* m = cv->getModelViewMatrix();
812  osg::Matrixd viewMatrix = Scene::GetViewMatrix_View();
813  osg::Matrixd invViewMatrix = osg::Matrixd::inverse(Scene::GetViewMatrix_View());
814  osg::Matrixd worldmat = invViewMatrix * (*m);
815  osg::Matrixd invWorldMatrix = osg::Matrixd::inverse(worldmat);
816 
817  geometry->getOrCreateStateSet()->getUniform("ProjectionMatrix")->set(Scene::GetProjectionMatrix_View());
818  geometry->getOrCreateStateSet()->getUniform("ModelViewMatrix")->set(viewMatrix*worldmat);
819  geometry->getOrCreateStateSet()->getUniform("ViewMatrix")->set(viewMatrix);
820  if(Scene::m_skydome != 0)
821  {
822  osg::Vec3 lightpos = Scene::m_skydome->GetLightPosition();
823  geometry->getOrCreateStateSet()->getUniform("un_lightPos")->set(lightpos);
824  }
825 
826  }
827  traverse(node, nv);
828 
829  }
830  };
831 
832  class UniformBlurTexsize : public osg::Uniform::Callback
833  {
834  public:
843 
847  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( (float)(osgCloudyDay::Scene::GetWidth()));}
848  };
849 }