Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
ssao.hpp
Go to the documentation of this file.
1 
23 #ifndef __SSAO__
24 #define __SSAO__
25 
26 #include <tucano/effect.hpp>
27 #include <tucano/camera.hpp>
28 #include <tucano/framebuffer.hpp>
29 #include <tucano/utils/ppmIO.hpp>
30 
31 namespace Tucano
32 {
33 namespace Effects
34 {
35 
41 class SSAO: public Tucano::Effect {
42 
43 protected:
44 
47 
49  float radius;
50 
53 
56 
59 
62 
65 
67  bool apply_blur = true;
68 
70  bool displayAmbientPass = false;
71 
73  int blurRange = 3;
74 
76  int depthTextureID = 0;
77 
79  int normalTextureID = 1;
80 
82  int colorTextureID = 2;
83 
85  int ssaoTextureID = 3;
86 
88  int blurTextureID = 4;
89 
91  int intensity = 1;
92 
94  float global_scale = 1.0;
95 
96 
97 public:
98 
104  SSAO (float rad = 1.0)
105  {
106  radius = rad;
107  }
108 
114  virtual void initialize (void)
115  {
117 
118  // @TODO put appropiate image path for random.ppm
119  Tucano::ImageImporter::loadPPMImage("../samples/images/random.ppm", &noise_texture);
120  noise_texture.setTexParameters( GL_REPEAT, GL_REPEAT, GL_NEAREST, GL_NEAREST );
121 
122  #ifdef TUCANODEBUG
123  Tucano::Misc::errorCheckFunc(__FILE__, __LINE__);
124  #endif
125 
126  quad.createQuad();
127  }
128 
135  void createViewSpaceBuffer (Tucano::Mesh& mesh, const Tucano::Camera& camera_trackball, const Tucano::Camera& light_trackball)
136  {
137 
138  // Bind buffer to store coord, normal and color information
139  fbo.clearAttachments();
140  fbo.bindRenderBuffers(depthTextureID, normalTextureID, colorTextureID);
141 
142  deferred_shader.bind();
143  deferred_shader.setUniform("projectionMatrix", camera_trackball.getProjectionMatrix());
144  deferred_shader.setUniform("modelMatrix",mesh.getModelMatrix());
145  deferred_shader.setUniform("viewMatrix", camera_trackball.getViewMatrix());
146  deferred_shader.setUniform("lightViewMatrix", light_trackball.getViewMatrix());
147  deferred_shader.setUniform("has_color", mesh.hasAttribute("in_Color"));
148 
149  mesh.setAttributeLocation(deferred_shader);
150  mesh.render();
151 
152  deferred_shader.unbind();
153  fbo.unbind();
154  fbo.clearDepth();
155 
156  }
157 
161  void computeSSAO (const Tucano::Camera& camera)
162  {
163 
164  fbo.bindRenderBuffer(ssaoTextureID);
165 
166  ssao_shader.bind();
167 
168  ssao_shader.setUniform("coordsTexture", fbo.bindAttachment(depthTextureID));
169  ssao_shader.setUniform("normalTexture", fbo.bindAttachment(normalTextureID));
170  ssao_shader.setUniform("noise_texture", noise_texture.bind());
171  ssao_shader.setUniform("viewport", (GLfloat)fbo.getWidth(), (GLfloat)fbo.getHeight());
172  ssao_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
173  ssao_shader.setUniform("radius", radius);
174  ssao_shader.setUniform("global_scale", global_scale);
175  ssao_shader.setUniform("intensity", (GLfloat)intensity);
176 
177  quad.setAttributeLocation(ssao_shader);
178  quad.render();
179 
180  ssao_shader.unbind();
181  noise_texture.unbind();
182  fbo.unbind();
183  fbo.clearDepth();
184  }
185 
186 
191  void applySSAO (const Tucano::Camera& light_trackball)
192  {
193  ssao_final_shader.bind();
194 
195  ssao_final_shader.setUniform("lightViewMatrix", light_trackball.getViewMatrix());
196 
197  ssao_final_shader.setUniform("coordsTexture", fbo.bindAttachment(depthTextureID));
198  ssao_final_shader.setUniform("normalTexture", fbo.bindAttachment(normalTextureID));
199  ssao_final_shader.setUniform("colorTexture", fbo.bindAttachment(colorTextureID));
200  ssao_final_shader.setUniform("ssaoTexture", fbo.bindAttachment(ssaoTextureID));
201  ssao_final_shader.setUniform("blurRange", blurRange);
202 
203  quad.setAttributeLocation(ssao_final_shader);
204  quad.render();
205 
206  ssao_final_shader.unbind();
207  fbo.unbind();
208  }
209 
223  virtual void render(Tucano::Mesh& mesh, const Tucano::Camera& camera_trackball, const Tucano::Camera& light_trackball)
224  {
225  Eigen::Vector4f viewport = camera_trackball.getViewport();
226  Eigen::Vector2i viewport_size = camera_trackball.getViewportSize();
227 
228  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
229 
230  // check if viewport was modified, if so, regenerate fbo
231  if (fbo.getWidth() != viewport_size[0] || fbo.getHeight() != viewport_size[1])
232  {
233  fbo.create(viewport_size[0], viewport_size[1], 5);
234  }
235 
236  glEnable(GL_DEPTH_TEST);
237  glClearColor(1.0, 1.0, 1.0, 0.0);
238  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
239 
240  // first pass
241  createViewSpaceBuffer (mesh, camera_trackball, light_trackball);
242 
243  // second pass
244  computeSSAO(camera_trackball);
245 
246  // final pass, blur SSAO and join with original render
247  applySSAO(light_trackball);
248  }
249 
254  int getIntensity (void)
255  {
256  return intensity;
257  }
258 
263  void setIntensity (int value)
264  {
265  intensity = value;
266  }
267 
272  int getRadius (void)
273  {
274  return radius;
275  }
276 
277 
282  void setRadius (int value)
283  {
284  radius = value;
285  }
286 
291  void setScale (int value)
292  {
293  global_scale = value;
294  }
295 
296 
300  void setBlurRange (int value)
301  {
302  blurRange = value;
303  }
304 
308  void toggleAmbientPass (void)
309  {
310  displayAmbientPass = !displayAmbientPass;
311  }
312 
313 
314 private:
315 
319  void initializeShaders (void)
320  {
321  loadShader(ssao_shader, "ssao");
322  loadShader(deferred_shader, "viewspacebuffer");
323  loadShader(ssao_final_shader, "ssaofinal");
324  }
325 
326 };
327 }
328 }
329 
330 #endif
void createQuad(void)
Sets the mesh as Unit Quad.
Definition: mesh.hpp:841
Definition: ssao.hpp:41
virtual void initialize(void)
Initializes the SSAO effects,.
Definition: ssao.hpp:114
int getHeight(void)
Returns the height of the FBO.
Definition: framebuffer.hpp:893
Tucano::Framebuffer fbo
Framebuffer to store coord/normal buffer.
Definition: ssao.hpp:52
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
int getRadius(void)
Get radius value.
Definition: ssao.hpp:272
virtual void render(Tucano::Mesh &mesh, const Tucano::Camera &camera_trackball, const Tucano::Camera &light_trackball)
Renders the mesh with the desired effect.
Definition: ssao.hpp:223
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
void clearDepth(void)
Clears the FBO depthbuffer.
Definition: framebuffer.hpp:493
bool apply_blur
Flag indicating wether blur shall be applied or not.
Definition: ssao.hpp:67
static bool loadPPMImage(string filename, Tucano::Texture *tex) __attribute__((unused))
Loads a texture from a PPM file. The texture receives data in the range [0,1] to create a FLOAT textu...
Definition: ppmIO.hpp:46
void clearAttachments(Eigen::Vector4f clear_color=Eigen::Vector4f::Zero())
Clears all attachments with a given color.
Definition: framebuffer.hpp:448
int intensity
Global intensity value.
Definition: ssao.hpp:91
void bind(int texture_unit)
Binds the texture to a given unit. Note that if there is another texture already binded to this unit...
Definition: texture.hpp:285
Tucano::Shader ssao_final_shader
Join original render with SSAO (blur it first)
Definition: ssao.hpp:61
void setTexParameters(GLenum wraps=GL_CLAMP, GLenum wrapt=GL_CLAMP, GLenum magfilter=GL_NEAREST, GLenum minfilter=GL_NEAREST)
Deletes the texture.
Definition: texture.hpp:221
Tucano::Mesh quad
A quad mesh for framebuffer rendering.
Definition: ssao.hpp:64
A Shader object represents one GLSL program.
Definition: shader.hpp:45
void toggleAmbientPass(void)
Toggles the displayAmbientPass flag.
Definition: ssao.hpp:308
Tucano::Shader ssao_shader
The per pixel AO computation shader.
Definition: ssao.hpp:55
void errorCheckFunc(std::string file, int line, std::string message="")
GL error check method.
Definition: misc.hpp:53
virtual void bindRenderBuffer(GLuint attachID)
Bind framebuffer object and set render buffer to given attachment.
Definition: framebuffer.hpp:258
Tucano::Texture noise_texture
Noise texture.
Definition: ssao.hpp:46
int getIntensity(void)
Get intensity value.
Definition: ssao.hpp:254
bool displayAmbientPass
Flag indicating if the mesh should be rendered only with ambient occlusion pass or with full illumina...
Definition: ssao.hpp:70
bool hasAttribute(const string &name) const
Returns wether an attribute exists or not.
Definition: mesh.hpp:503
void setScale(int value)
Set global scale value.
Definition: ssao.hpp:291
virtual void bindRenderBuffers(GLuint attachID0, GLuint attachID1)
Bind framebuffer object and set render buffer to 2 given attachments.
Definition: framebuffer.hpp:269
void setIntensity(int value)
Set intensity value.
Definition: ssao.hpp:263
void unbind(void)
Unbinds fbo and all texture units in use.
Definition: framebuffer.hpp:408
int ssaoTextureID
The ID of the color attachment holding the SSAO result.
Definition: ssao.hpp:85
int blurRange
Number of neighbour pixels used in blurring. The blur will be applied to a blurRange x blurRange squa...
Definition: ssao.hpp:73
SSAO(float rad=1.0)
Default constructor.
Definition: ssao.hpp:104
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
int normalTextureID
The ID defining the color attachment to which the normal texture is bound in the framebuffer.
Definition: ssao.hpp:79
int colorTextureID
The ID defining the color attachment to which the color texture is bound in the framebuffer.
Definition: ssao.hpp:82
A wrapper class for creating and using FBOs.
Definition: framebuffer.hpp:44
virtual Shader * loadShader(string shader_name)
Loads a shader by filename, initializes it, and inserts in shaders list.
Definition: effect.hpp:73
void create(int w, int h, int num_attachs=1, int nsamples=1)
Creates the framebuffer with specified parameters.
Definition: framebuffer.hpp:146
void computeSSAO(const Tucano::Camera &camera)
Compute the Ambient Occlusion factor for each pixel.
Definition: ssao.hpp:161
void setBlurRange(int value)
Definition: ssao.hpp:300
virtual void render(void)
Render the mesh triangles. The method binds the buffers, calls the method to render triangles...
Definition: mesh.hpp:756
Eigen::Affine3f getModelMatrix(void) const
Returns the model matrix.
Definition: model.hpp:103
An OpenGL texture. It can be a simple texture or an FBO texture.
Definition: texture.hpp:41
int getWidth(void)
Returns the width of the FBO.
Definition: framebuffer.hpp:884
void bind(void)
Enables the shader program for usage.
Definition: shader.hpp:1176
void setRadius(int value)
Set radius value.
Definition: ssao.hpp:282
void applySSAO(const Tucano::Camera &light_trackball)
Blur SSAO result and mix with original render.
Definition: ssao.hpp:191
Tucano::Shader deferred_shader
Save coord, normal and color to FBO.
Definition: ssao.hpp:58
A common Mesh, usually containing triagles or points.
Definition: mesh.hpp:194
Defines an abstract camera with a projection and view matrices.
Definition: camera.hpp:37
void setAttributeLocation(Shader *shader)
Automatically sets the attribute locations for a given Shader.
Definition: mesh.hpp:541
int depthTextureID
The ID defining the color attachment to which the depth texture is bound in the framebuffer.
Definition: ssao.hpp:76
Eigen::Vector4f getViewport(void) const
Returns the viewport coordinates.
Definition: camera.hpp:246
int blurTextureID
The ID defining the color attachment to which the blur texture is bound in the framebuffer.
Definition: ssao.hpp:88
void createViewSpaceBuffer(Tucano::Mesh &mesh, const Tucano::Camera &camera_trackball, const Tucano::Camera &light_trackball)
First pass of the SSAO, writes coords, normals and colors to a buffer.
Definition: ssao.hpp:135
The Effect class is a holder for Shaders. It is completely optional, but is contains a few methods fo...
Definition: effect.hpp:43
Eigen::Vector2i getViewportSize(void) const
Returns the dimensions of the viewport.
Definition: camera.hpp:256
void initializeShaders(void)
Creates and loads all shaders.
Definition: ssao.hpp:319
float global_scale
Global scale.
Definition: ssao.hpp:94
void bindAttachment(int attachment, int texture_unit)
Binds a texture attachment to a given texture unit.
Definition: framebuffer.hpp:510
void unbind(void)
Unbinds this texture and frees the texture unit.
Definition: texture.hpp:367
float radius
Kernel radius. If the distance between a sample point and the point for which the occlusion is being ...
Definition: ssao.hpp:49
void setUniform(GLint location, GLint a, GLint b, GLint c, GLint d)
Sets an uniform integer 4D vector (ivec4) given a location and the vector values. ...
Definition: shader.hpp:1258