CloudyDay
 All Classes Functions Variables Enumerations
BlurShadow.h
1 #pragma once
2 #include "CloudScene.h"
3 #include <osg/Depth>
4 
5 namespace osgCloudyDay
6 {
10  class BlurShadow
11  {
12  public:
16  BlurShadow();
20  ~BlurShadow(void);
21 
25  void Initialize();
30  void CreateGeometry(int rendering);
31 
35  virtual void CreateCamera();
36 
41  osg::ref_ptr<osg::Camera> GetCamera();
42 
43  protected:
44  osg::ref_ptr<osg::Program> sh_blur_linear_vert1;
45  osg::ref_ptr<osg::Program> sh_blur_linear_hori1;
46  osg::ref_ptr<osg::Uniform> uniform_blur_texsize2;
47  osg::ref_ptr<osg::Uniform> uniform_blur_texsize1;
48 
49  osg::ref_ptr<osg::Camera> cam_hud;
50  osg::ref_ptr<osg::StateSet> nodessP;
51  osg::ref_ptr<osg::Program> planeProg;
52 
53  int rendering;
54  };
55 
56 
57  class BlurShadowGeometry : public osg::Geometry
58  {
59  public:
60  BlurShadowGeometry( osg::ref_ptr<osg::Program> prog_vert1, osg::ref_ptr<osg::Uniform> _uniform_blur_texsize, int mode)
61  {
62  sh_blur_linear_vert1 = (prog_vert1);
63  uniform_blur_texsize = _uniform_blur_texsize;
64  m_mode = mode;
65  }
66 
67  ~BlurShadowGeometry(void)
68  {
69 
70  }
71 
72  void drawImplementation(osg::RenderInfo& renderInfo) const
73  {
74  if (_internalOptimizedGeometry.valid())
75  {
76  _internalOptimizedGeometry->drawImplementation(renderInfo);
77  return;
78  }
79 
80  osg::State& state = *renderInfo.getState();
81 
82  bool checkForGLErrors = state.getCheckForGLErrors()==osg::State::ONCE_PER_ATTRIBUTE;
83  if (checkForGLErrors) state.checkGLErrors("start of Geometry::drawImplementation()");
84 
85  bool useFastPath = areFastPathsUsed();
86  // useFastPath = false;
87 
88  bool usingVertexBufferObjects = _useVertexBufferObjects && state.isVertexBufferObjectSupported();
89  bool handleVertexAttributes = !_vertexAttribList.empty();
90 
91  osg::ArrayDispatchers& arrayDispatchers = state.getArrayDispatchers();
92 
93  arrayDispatchers.reset();
94  arrayDispatchers.setUseVertexAttribAlias(useFastPath && state.getUseVertexAttributeAliasing());
95  arrayDispatchers.setUseGLBeginEndAdapter(!useFastPath);
96 
97  arrayDispatchers.activateNormalArray(_normalData.binding, _normalData.array.get(), _normalData.indices.get());
98  arrayDispatchers.activateColorArray(_colorData.binding, _colorData.array.get(), _colorData.indices.get());
99  arrayDispatchers.activateSecondaryColorArray(_secondaryColorData.binding, _secondaryColorData.array.get(), _secondaryColorData.indices.get());
100  arrayDispatchers.activateFogCoordArray(_fogCoordData.binding, _fogCoordData.array.get(), _fogCoordData.indices.get());
101 
102  if (handleVertexAttributes)
103  {
104  for(unsigned int unit=0;unit<_vertexAttribList.size();++unit)
105  {
106  arrayDispatchers.activateVertexAttribArray(_vertexAttribList[unit].binding, unit, _vertexAttribList[unit].array.get(), _vertexAttribList[unit].indices.get());
107  }
108  }
109 
110  // dispatch any attributes that are bound overall
111  arrayDispatchers.dispatch(BIND_OVERALL,0);
112 
113  state.lazyDisablingOfVertexAttributes();
114 
115  if (useFastPath)
116  {
117  // set up arrays
118  if( _vertexData.array.valid() )
119  state.setVertexPointer(_vertexData.array.get());
120 
121  if (_normalData.binding==BIND_PER_VERTEX && _normalData.array.valid())
122  state.setNormalPointer(_normalData.array.get());
123 
124  if (_colorData.binding==BIND_PER_VERTEX && _colorData.array.valid())
125  state.setColorPointer(_colorData.array.get());
126 
127  if (_secondaryColorData.binding==BIND_PER_VERTEX && _secondaryColorData.array.valid())
128  state.setSecondaryColorPointer(_secondaryColorData.array.get());
129 
130  if (_fogCoordData.binding==BIND_PER_VERTEX && _fogCoordData.array.valid())
131  state.setFogCoordPointer(_fogCoordData.array.get());
132 
133  for(unsigned int unit=0;unit<_texCoordList.size();++unit)
134  {
135  const osg::Array* array = _texCoordList[unit].array.get();
136  if (array) state.setTexCoordPointer(unit,array);
137  }
138 
139  if( handleVertexAttributes )
140  {
141  for(unsigned int index = 0; index < _vertexAttribList.size(); ++index )
142  {
143  const osg::Array* array = _vertexAttribList[index].array.get();
144  const AttributeBinding ab = _vertexAttribList[index].binding;
145  if( ab == BIND_PER_VERTEX && array )
146  {
147  state.setVertexAttribPointer( index, array, _vertexAttribList[index].normalize );
148  }
149  }
150  }
151  }
152  else
153  {
154  for(unsigned int unit=0;unit<_texCoordList.size();++unit)
155  {
156  arrayDispatchers.activateTexCoordArray(BIND_PER_VERTEX, unit, _texCoordList[unit].array.get(), _texCoordList[unit].indices.get());
157  }
158 
159  arrayDispatchers.activateVertexArray(BIND_PER_VERTEX, _vertexData.array.get(), _vertexData.indices.get());
160  }
161 
162  state.applyDisablingOfVertexAttributes();
163 
164  bool bindPerPrimitiveSetActive = arrayDispatchers.active(BIND_PER_PRIMITIVE_SET);
165  bool bindPerPrimitiveActive = arrayDispatchers.active(BIND_PER_PRIMITIVE);
166 
167  if (checkForGLErrors) state.checkGLErrors("Geometry::drawImplementation() after vertex arrays setup.");
168 
169 
170  osg::GL2Extensions* extension = osg::GL2Extensions::Get(0,true);
171 
172  glClearColor(0.f, 0.1f, 0.f, 1.f);
174  //
175  // draw the primitives themselves.
176  //
177 
178  if(m_mode == 0)
179  {
180  glViewport(0,0,1024, 1024);
181  Scene::fbo_vert_linear_blur2->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
182  glClear( GL_COLOR_BUFFER_BIT);
183  }
184  else
185  {
186  Scene::fbo_hori_linear_blur2->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
187  glClear( GL_COLOR_BUFFER_BIT);
188  }
189 
190  for(unsigned int primitiveSetNum=0; primitiveSetNum!= _primitives.size() ; ++primitiveSetNum)
191  {
192  // dispatch any attributes that are bound per primitive
193  if (bindPerPrimitiveSetActive)
194  arrayDispatchers.dispatch(BIND_PER_PRIMITIVE_SET, primitiveSetNum);
195 
196  const osg::PrimitiveSet* primitiveset = _primitives[primitiveSetNum].get();
197 
198  glDisable(GL_BLEND);
199  glDisable(GL_DEPTH_TEST);
200  glDisable(GL_CULL_FACE);
201 
202  osg::State& state = *renderInfo.getState();
203 
204  osg::Depth* depth2 = new osg::Depth(osg::Depth::ALWAYS, 0.0, 1.0, false); //MIT DEPTH => FEHLER
205  depth2->apply(state);
206 
207  //std::cerr << std::endl << " BlurShadowGeometry" << std::endl << std::endl;
208  if(m_mode == 0)
209  {
210  glViewport(0.f, 0.f, 1024, 1024);
211  sh_blur_linear_vert1->apply(state);
212  state.applyTextureAttribute(0, Scene::GetShadowTexture());
213  Scene::fbo_vert_linear_blur2->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
214  uniform_blur_texsize->set(1024.f);
215  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
216  primitiveset->draw(state, usingVertexBufferObjects);
217  }
218  else
219  {
220  glViewport(0.f, 0.f, 1024, 1024);
221  sh_blur_linear_vert1->apply(state);
222  state.applyTextureAttribute(0, Scene::GetBlurTexture());
223  Scene::fbo_hori_linear_blur2->apply(state, osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER);
224  uniform_blur_texsize->set(1024.f);
225  uniform_blur_texsize->apply(extension, state.getUniformLocation("texsize"));
226  primitiveset->draw(state, usingVertexBufferObjects);
227  }
228 
229  glEnable(GL_DEPTH_TEST);
230  }
231 
233 
234  // unbind the VBO's if any are used.
235  state.unbindVertexBufferObject();
236  state.unbindElementBufferObject();
237 
238  if (checkForGLErrors) state.checkGLErrors("end of Geometry::drawImplementation().");
239  }
240 
241  osg::ref_ptr<osg::Program> sh_blur_linear_vert1;
242  osg::ref_ptr<osg::Uniform> uniform_blur_texsize;
243  int m_mode;
244  };
245 
246  class UniformBlurShadowTexsize : public osg::Uniform::Callback
247  {
248  public:
250  void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ){ uniform->set( (float)(osgCloudyDay::Scene::GetWidth()));}
251  };
252 
253  class BlurCullCallback : public osg::NodeCallback
254  {
255  public:
256  BlurCullCallback(void) : iteration(0)
257  {
258 
259  }
260 
261  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
262  {
263  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
264  osg::ref_ptr<osg::Geode> geometry = dynamic_cast<osg::Geode*> (node);
265 
266 
267  if(geometry && cv && false)
268  {
269  /*//Hack for Shader
270  switch(iteration)
271  {
272  case 1: geometry->getOrCreateStateSet()->setAttribute(HUD::sh_blur_linear_vert1);
273  std::cerr <<"sh_blur_linear_vert1" << std::endl;
274  iteration = 0;
275  break;
276  case 0: geometry->getOrCreateStateSet()->setAttribute(HUD::sh_blur_linear_hori1);
277  std::cerr <<"sh_blur_linear_hori1" << std::endl;
278  iteration = 1;
279  break;
280  }*/
281  }
282  traverse(node, nv);
283 
284  }
285 
286  int iteration;
287  };
288 }