Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
shadowmap.hpp
Go to the documentation of this file.
1 
23 #ifndef __SHADOWMAP__
24 #define __SHADOWMAP__
25 
26 #include <tucano/tucano.hpp>
27 
28 namespace Tucano
29 {
30 namespace Effects
31 {
32 
37 class ShadowMap: public Tucano::Effect {
38 
39 protected:
42 
45 
48 
51 
54 
56  Eigen::Vector2i viewport = Eigen::Vector2i(1024, 1024);
57 
58  int depth_tex_id = 0;
59 
60  int num_samples = 4;
61 
63  bool recreate_fbo = true;
64 
65 public:
66 
71  ShadowMap (void)
72  {
73  quad.createQuad();
74  }
75 
80  virtual void initialize (void)
81  {
82  loadShader(shadowbuffer_shader, "shadowbuffer");
83  loadShader(drawbuffer_shader, "rendershadowmap");
84  }
85 
86  void clearShadowBuffer (void)
87  {
88  fbo.clearAttachments();
89  }
90 
98  {
99  if (num_samples == 1)
100  return &fbo;
101  return &aa_fbo;
102  }
103 
109  {
110  return &fbo;
111  }
112 
117  void setNumSamples (int n)
118  {
119  num_samples = n;
120  recreate_fbo = true;
121  }
122 
127  void setBufferSize (const Eigen::Vector2i & size)
128  {
129  viewport = size;
130  recreate_fbo = true;
131  }
132 
136  void createBuffers (void)
137  {
138  aa_fbo.create(viewport[0], viewport[1], 1, 1); // single sampling
139  fbo.create(viewport[0], viewport[1], 1, num_samples); // multi sampling (actually used for generatin map)
140  fbo.getTexture(0)->setTexParameters(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR);
141 
142  // if using sampler2DShadow in shaders, but usually we do the check manually
143  fbo.getTexture(0)->bind();
144  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
145  fbo.getTexture(0)->unbind();
146  }
147 
148  void renderBuffer (const Tucano::Camera& camera)
149  {
150  Eigen::Vector4f viewport = camera.getViewport();
151  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
152  drawbuffer_shader.bind();
153  drawbuffer_shader.setUniform("shadowmap", fbo.bindAttachment(0));
154  quad.setAttributeLocation(drawbuffer_shader);
155  quad.render();
156  drawbuffer_shader.unbind();
157  fbo.unbindAttachments();
158 
159  }
160 
167  void render (Tucano::Mesh& mesh, const Tucano::Camera& camera, const Tucano::Camera & light)
168  {
169  if (recreate_fbo)
170  {
171  createBuffers();
172  recreate_fbo = false;
173  }
174 
175  glEnable(GL_DEPTH_TEST);
176 
177  glViewport(0, 0, viewport[0], viewport[1]);
178 
179  // Bind buffer to store coord
180  fbo.bindRenderBuffer(depth_tex_id);
181 
182  shadowbuffer_shader.bind();
183  shadowbuffer_shader.setUniform("projectionMatrix", light.getProjectionMatrix());
184  shadowbuffer_shader.setUniform("modelMatrix",mesh.getModelMatrix());
185  shadowbuffer_shader.setUniform("viewMatrix", light.getViewMatrix().inverse());
186 
187  mesh.setAttributeLocation(shadowbuffer_shader);
188  mesh.render();
189 
190  shadowbuffer_shader.unbind();
191  fbo.unbind();
192 
193  if (num_samples > 1) // if multisampling prepare Anti Aliased single sampled buffer
194  fbo.blitTo(aa_fbo);
195  }
196 
197 };
198 }
199 }
200 #endif
Texture * getTexture(int tex_id)
Returns a pointer to a texture (attachment).
Definition: framebuffer.hpp:206
Tucano::Shader shadowbuffer_shader
Creates the shadowmap depth buffer by rendering from light pos.
Definition: shadowmap.hpp:47
void createQuad(void)
Sets the mesh as Unit Quad.
Definition: mesh.hpp:841
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
Tucano::Shader drawbuffer_shader
Shader to render shadow map.
Definition: shadowmap.hpp:50
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
void clearAttachments(Eigen::Vector4f clear_color=Eigen::Vector4f::Zero())
Clears all attachments with a given color.
Definition: framebuffer.hpp:448
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
ShadowMap(void)
Default constructor.
Definition: shadowmap.hpp:71
void renderBuffer(const Tucano::Camera &camera)
Definition: shadowmap.hpp:148
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
int num_samples
Definition: shadowmap.hpp:60
Definition: shadowmap.hpp:37
A Shader object represents one GLSL program.
Definition: shader.hpp:45
virtual void initialize(void)
Initializes the ShadowMap effects,.
Definition: shadowmap.hpp:80
virtual void bindRenderBuffer(GLuint attachID)
Bind framebuffer object and set render buffer to given attachment.
Definition: framebuffer.hpp:258
Tucano::Framebuffer fbo
Framebuffer to store the shadow depth map, this is using multisampling.
Definition: shadowmap.hpp:41
void createBuffers(void)
Creates FBOs, or recreates when size changes.
Definition: shadowmap.hpp:136
void setBufferSize(const Eigen::Vector2i &size)
Set the shadow buffer size.
Definition: shadowmap.hpp:127
Eigen::Vector2i viewport
Shadowbuffer size.
Definition: shadowmap.hpp:56
int depth_tex_id
Definition: shadowmap.hpp:58
Tucano::Mesh quad
Quad for rendering shadow map, mostly for debug.
Definition: shadowmap.hpp:53
void unbind(void)
Unbinds fbo and all texture units in use.
Definition: framebuffer.hpp:408
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
Tucano::Framebuffer * getShadowMap(void)
Returns a pointer to the shadow map with single sampling Note that if multisapling is used (num sampl...
Definition: shadowmap.hpp:97
void blitTo(Framebuffer &copyfbo, int source_attach=0, int dest_attach=0)
Copy fbo attachment to another fbo with blit operation.
Definition: framebuffer.hpp:421
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
virtual void render(void)
Render the mesh triangles. The method binds the buffers, calls the method to render triangles...
Definition: mesh.hpp:756
bool recreate_fbo
Flag to recreate buffer during next render.
Definition: shadowmap.hpp:63
void clearShadowBuffer(void)
Definition: shadowmap.hpp:86
void unbindAttachments(void)
Unbinds all texture attachments.
Definition: framebuffer.hpp:528
Eigen::Affine3f getModelMatrix(void) const
Returns the model matrix.
Definition: model.hpp:103
void bind(void)
Enables the shader program for usage.
Definition: shader.hpp:1176
void render(Tucano::Mesh &mesh, const Tucano::Camera &camera, const Tucano::Camera &light)
Composes the shadow depth buffer.
Definition: shadowmap.hpp:167
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
void setNumSamples(int n)
Set the number of samples for multisampling.
Definition: shadowmap.hpp:117
Eigen::Vector4f getViewport(void) const
Returns the viewport coordinates.
Definition: camera.hpp:246
The Effect class is a holder for Shaders. It is completely optional, but is contains a few methods fo...
Definition: effect.hpp:43
Tucano::Framebuffer * getShadowMapMultisample(void)
Returns a pointer to the multisampled shadow map.
Definition: shadowmap.hpp:108
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
Tucano::Framebuffer aa_fbo
A auxiliary fbo to blit multisampled fbo.
Definition: shadowmap.hpp:44
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