Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
camerabuffer.hpp
Go to the documentation of this file.
1 
23 #ifndef __CAMERABUFFER__
24 #define __CAMERABUFFER__
25 
26 #include <tucano/tucano.hpp>
27 
28 namespace Tucano
29 {
30 namespace Effects
31 {
32 
39 
40 protected:
43 
46 
49 
52 
55 
57  int num_samples = 1;
58 
60  int num_attachs = 1;
61 
62 
63 public:
64 
69  CameraBuffer (void)
70  {
71  quad.createQuad();
72  }
73 
78  void setNumAttachs (int nt)
79  {
80  num_attachs = nt;
81  }
82 
83 
88  virtual void initialize (void)
89  {
90  loadShader(camerabuffer_shader, "cameracoords");
91  loadShader(drawbuffer_shader, "renderbuffer");
92  }
93 
94  void clearBuffer (void)
95  {
96  fbo.clearAttachments();
97  }
98 
106  {
107  if (num_samples == 1)
108  return &fbo;
109  return &aa_fbo;
110  }
111 
117  {
118  return &fbo;
119  }
120 
125  void setNumSamples (int n)
126  {
127  num_samples = n;
128  }
129 
134  void renderBuffer (const Tucano::Camera& camera, int attach = 0)
135  {
136  Eigen::Vector4f viewport = camera.getViewport();
137  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
138  drawbuffer_shader.bind();
139  if (num_samples == 1)
140  drawbuffer_shader.setUniform("tex", fbo.bindAttachment(attach));
141  else
142  drawbuffer_shader.setUniform("tex", aa_fbo.bindAttachment(attach));
143  drawbuffer_shader.setUniform("use_aa_filter", 0);
144  quad.setAttributeLocation(drawbuffer_shader);
145  quad.render();
146  drawbuffer_shader.unbind();
147  fbo.unbindAttachments();
148 
149  }
150 
156  void render (Tucano::Mesh& mesh, const Tucano::Camera& camera, int target = 0, const Eigen::Vector4f& buffer_viewport = Eigen::Vector4f(0,0,0,0))
157  {
158  glEnable(GL_DEPTH_TEST);
159 
160  Eigen::Vector4f viewport = buffer_viewport;
161  Eigen::Vector2i viewport_size;
162  viewport_size << buffer_viewport[2] - buffer_viewport[0], buffer_viewport[3] - buffer_viewport[1];
163 
164  if (viewport.norm() == 0.0)
165  {
166  viewport = camera.getViewport();
167  viewport_size = camera.getViewportSize();
168  }
169 
170  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
171 
172  // check if viewport was modified, if so, regenerate fbo
173  if (fbo.getWidth() != viewport_size[0] || fbo.getHeight() != viewport_size[1])
174  {
175  aa_fbo.create(viewport_size[0], viewport_size[1], num_attachs, 1); // single sampling
176  fbo.create(viewport_size[0], viewport_size[1], num_attachs, num_samples); // multi sampling (actually used for generatin map)
177  for (int i = 0; i < num_attachs; ++i)
178  {
179  fbo.getTexture(i)->setTexParameters(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR);
180 
181  // if using sampler2DShadow in shaders the following parameter must be set, but usually we do the check manually
182  fbo.getTexture(i)->bind();
183  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
184  fbo.getTexture(i)->unbind();
185  }
186  }
187 
188  // Bind buffer to store coord
189  fbo.bindRenderBuffer(target);
190 
191  camerabuffer_shader.bind();
192  camerabuffer_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
193  camerabuffer_shader.setUniform("modelMatrix", mesh.getModelMatrix());
194  camerabuffer_shader.setUniform("viewMatrix", camera.getViewMatrix());
195 
196  mesh.setAttributeLocation(camerabuffer_shader);
197  mesh.render();
198 
199  camerabuffer_shader.unbind();
200  fbo.unbind();
201 
202  if (num_samples > 1) // if multisampling prepare Anti Aliased single sampled buffer
203  {
204  for (int i = 0; i < num_attachs; ++i)
205  fbo.blitTo(aa_fbo, i, i);
206  }
207  }
208 
209 };
210 }
211 }
212 #endif
void setNumSamples(int n)
Set the number of samples for multisampling.
Definition: camerabuffer.hpp:125
CameraBuffer(void)
Default constructor.
Definition: camerabuffer.hpp:69
Texture * getTexture(int tex_id)
Returns a pointer to a texture (attachment).
Definition: framebuffer.hpp:206
void createQuad(void)
Sets the mesh as Unit Quad.
Definition: mesh.hpp:841
int getHeight(void)
Returns the height of the FBO.
Definition: framebuffer.hpp:893
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
Tucano::Framebuffer aa_fbo
A auxiliary fbo to blit multisampled fbo, this is without multisampling.
Definition: camerabuffer.hpp:45
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
Tucano::Shader drawbuffer_shader
Shader to render the camera coordinates buffer.
Definition: camerabuffer.hpp:51
Definition: camerabuffer.hpp:38
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
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::Framebuffer * getFbo(void)
Returns a pointer to the camera coordinates map with single sampling Note that if multisapling is use...
Definition: camerabuffer.hpp:105
Tucano::Shader camerabuffer_shader
Creates the camera buffer by rendering from given camera.
Definition: camerabuffer.hpp:48
A Shader object represents one GLSL program.
Definition: shader.hpp:45
virtual void initialize(void)
Initializes the CameraBuffer effects,.
Definition: camerabuffer.hpp:88
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 camera coordiantes map, this is using multisampling.
Definition: camerabuffer.hpp:42
void unbind(void)
Unbinds fbo and all texture units in use.
Definition: framebuffer.hpp:408
void render(Tucano::Mesh &mesh, const Tucano::Camera &camera, int target=0, const Eigen::Vector4f &buffer_viewport=Eigen::Vector4f(0, 0, 0, 0))
Creates the camera coordinates buffer.
Definition: camerabuffer.hpp:156
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
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
int num_attachs
Numer of render targets.
Definition: camerabuffer.hpp:60
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
void unbindAttachments(void)
Unbinds all texture attachments.
Definition: framebuffer.hpp:528
void setNumAttachs(int nt)
Set number of render targets.
Definition: camerabuffer.hpp:78
Eigen::Affine3f getModelMatrix(void) const
Returns the model matrix.
Definition: model.hpp:103
Tucano::Framebuffer * getFboMultisample(void)
Returns a pointer to the multisampled camera coords map.
Definition: camerabuffer.hpp:116
Tucano::Mesh quad
Quad for rendering buffer, mostly for debug.
Definition: camerabuffer.hpp:54
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
int num_samples
Number of multisamples, if 1, no multisamping.
Definition: camerabuffer.hpp:57
void clearBuffer(void)
Definition: camerabuffer.hpp:94
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
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
Eigen::Vector2i getViewportSize(void) const
Returns the dimensions of the viewport.
Definition: camera.hpp:256
void renderBuffer(const Tucano::Camera &camera, int attach=0)
Renders the buffers, usually useful for visual debugging.
Definition: camerabuffer.hpp:134
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
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