15 glGenFramebuffers(1, &m_fbo);
18 m_draw_buffers= std::vector<GLenum>(8, GL_NONE);
19 m_color_textures= std::vector<GLuint>(8, 0);
22 m_clear_colors= std::vector< std::array<unsigned, 4> >(8);
25 m_color_units= std::vector<int>(8, -1);
35 glDeleteTextures(8, m_color_textures.data());
36 glDeleteTextures(1, &m_depth_texture);
37 glDeleteFramebuffers(1, &m_fbo);
51 void Framebuffer::bind(
const GLuint program,
const bool color,
const bool depth,
const bool position,
const bool texcoord,
const bool normal,
const bool material_id )
62 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
71 char label[2048]= { 0 };
75 glGetObjectLabel(GL_PROGRAM, program,
sizeof(tmp),
nullptr, tmp);
76 sprintf(
label,
"program( %u '%s' )", program, tmp);
79 sprintf(
label,
"program( %u )", program);
85 glGetIntegerv(GL_CURRENT_PROGRAM, (GLint *) ¤t);
86 if(current != program)
87 printf(
"[oops] %s: not active... undefined draw !!\n",
label);
94 glGetProgramInterfaceiv(program, GL_PROGRAM_OUTPUT, GL_ACTIVE_RESOURCES, &outputs);
97 for(
int i= 0; i < outputs; i++)
100 GLenum props[]= { GL_LOCATION };
101 glGetProgramResourceiv(program, GL_PROGRAM_OUTPUT, i, 1, props, 1,
nullptr, &location);
102 glGetProgramResourceName(program, GL_PROGRAM_OUTPUT, i,
sizeof(name),
nullptr, name);
107 printf(
"[oops] color output '%s' in %s not stored...\n", name,
label);
109 else if(location == 1)
112 printf(
"[oops] position output '%s' in %s not stored...\n", name,
label);
114 else if(location == 2)
117 printf(
"[oops] texcoord output '%s' in %s not stored...\n", name,
label);
119 else if(location == 3)
122 printf(
"[oops] normal output '%s' in %s not stored...\n", name,
label);
124 else if(location == 4)
127 printf(
"[oops] material output '%s' in %s not stored...\n", name,
label);
137 if(m_depth_texture == 0)
140 assert(m_depth_texture > 0);
141 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_depth_texture, 0);
145 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 0, 0);
149 bool draw_buffers=
false;
152 if(m_color_textures[0] == 0)
155 assert(m_color_textures[0] > 0);
156 if(m_draw_buffers[0] == GL_NONE)
158 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_color_textures[0], 0);
159 m_draw_buffers[0]= GL_COLOR_ATTACHMENT0;
165 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 0, 0);
166 m_draw_buffers[0]= GL_NONE;
172 if(m_color_textures[1] == 0)
175 assert(m_color_textures[1] > 0);
176 if(m_draw_buffers[1] == GL_NONE)
178 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, m_color_textures[1], 0);
179 m_draw_buffers[1]= GL_COLOR_ATTACHMENT1;
185 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, 0, 0);
186 m_draw_buffers[1]= GL_NONE;
192 if(m_color_textures[2] == 0)
195 assert(m_color_textures[2] > 0);
196 if(m_draw_buffers[2] == GL_NONE)
198 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, m_color_textures[2], 0);
199 m_draw_buffers[2]= GL_COLOR_ATTACHMENT2;
205 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, 0, 0);
206 m_draw_buffers[2]= GL_NONE;
212 if(m_color_textures[3] == 0)
215 assert(m_color_textures[3] > 0);
216 if(m_draw_buffers[3] == GL_NONE)
218 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, m_color_textures[3], 0);
219 m_draw_buffers[3]= GL_COLOR_ATTACHMENT3;
225 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, 0, 0);
226 m_draw_buffers[3]= GL_NONE;
232 if(m_color_textures[4] == 0)
235 assert(m_color_textures[4] > 0);
236 if(m_draw_buffers[4] == GL_NONE)
238 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, m_color_textures[4], 0);
239 m_draw_buffers[4]= GL_COLOR_ATTACHMENT4;
245 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, 0, 0);
246 m_draw_buffers[4]= GL_NONE;
251 glDrawBuffers(8, m_draw_buffers.data());
258 glViewport(0, 0, m_width, m_height);
262 glClearBufferfv(GL_DEPTH, 0, &m_clear_depth);
264 glClearBufferfv(GL_COLOR, 0, (
const GLfloat *) &m_clear_colors[0]);
266 glClearBufferfv(GL_COLOR, 1, (
const GLfloat *) &m_clear_colors[1]);
268 glClearBufferfv(GL_COLOR, 2, (
const GLfloat *) &m_clear_colors[2]);
270 glClearBufferfv(GL_COLOR, 3, (
const GLfloat *) &m_clear_colors[3]);
272 glClearBufferuiv(GL_COLOR, 4, (
const GLuint *) &m_clear_colors[4]);
278 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
287 for(
int i= 0; i < 8; i++)
288 if(m_color_units[i] != -1)
290 glActiveTexture(GL_TEXTURE0 + m_color_units[i]);
291 glBindTexture(GL_TEXTURE_2D, 0);
293 m_color_units[i]= -1;
296 if(m_depth_unit != -1)
298 glActiveTexture(GL_TEXTURE0 + m_depth_unit);
299 glBindTexture(GL_TEXTURE_2D, 0);
310 printf(
"[error] uninitialized framebuffer...\n");
312 m_clear_depth=
value;
318 printf(
"[error] uninitialized framebuffer...\n");
321 memcpy(&m_clear_colors[0], values,
sizeof(values));
327 printf(
"[error] uninitialized framebuffer...\n");
330 memcpy(&m_clear_colors[1], values,
sizeof(values));
336 printf(
"[error] uninitialized framebuffer...\n");
339 memcpy(&m_clear_colors[2], values,
sizeof(values));
345 printf(
"[error] uninitialized framebuffer...\n");
348 memcpy(&m_clear_colors[3], values,
sizeof(values));
354 printf(
"[error] uninitialized framebuffer...\n");
356 unsigned values[4]= {
value, 0, 0, 0 };
357 memcpy(&m_clear_colors[4], values,
sizeof(values));
361 bool Framebuffer::status( )
363 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
364 GLenum code= glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
366 if(code == GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER)
367 printf(
"[error] framebuffer: incomplete draw buffer... can't draw!\n");
368 else if(code == GL_FRAMEBUFFER_UNSUPPORTED)
369 printf(
"[error] framebuffer: unsupported format... can't draw!\n");
371 return (code == GL_FRAMEBUFFER_COMPLETE);
375 void Framebuffer::use_depth_texture(
const GLuint program,
const char *uniform,
const int unit,
const GLuint sampler )
381 void Framebuffer::use_color_texture(
const GLuint program,
const char *uniform,
const int unit,
const GLuint sampler )
383 m_color_units[0]= unit;
387 void Framebuffer::use_position_texture(
const GLuint program,
const char *uniform,
const int unit,
const GLuint sampler )
389 m_color_units[1]= unit;
393 void Framebuffer::use_texcoord_texture(
const GLuint program,
const char *uniform,
const int unit,
const GLuint sampler )
395 m_color_units[2]= unit;
399 void Framebuffer::use_normal_texture(
const GLuint program,
const char *uniform,
const int unit,
const GLuint sampler )
401 m_color_units[3]= unit;
405 void Framebuffer::use_material_texture(
const GLuint program,
const char *uniform,
const int unit,
const GLuint sampler )
407 m_color_units[4]= unit;
413 void Framebuffer::blit_depth(
const int dstX0,
const int dstY0,
const int dstX1,
const int dstY1 )
415 glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo);
417 glBlitFramebuffer(0, 0, m_width, m_height, dstX0, dstY0, dstX1, dstY1, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
419 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
422 void Framebuffer::blit_color(
const int dstX0,
const int dstY0,
const int dstX1,
const int dstY1 )
424 glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo);
425 glReadBuffer(GL_COLOR_ATTACHMENT0);
427 glBlitFramebuffer(0, 0, m_width, m_height, dstX0, dstY0, dstX1, dstY1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
429 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
432 void Framebuffer::blit_position(
const int dstX0,
const int dstY0,
const int dstX1,
const int dstY1 )
434 glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo);
435 glReadBuffer(GL_COLOR_ATTACHMENT1);
437 glBlitFramebuffer(0, 0, m_width, m_height, dstX0, dstY0, dstX1, dstY1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
439 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
442 void Framebuffer::blit_texcoord(
const int dstX0,
const int dstY0,
const int dstX1,
const int dstY1 )
444 glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo);
445 glReadBuffer(GL_COLOR_ATTACHMENT2);
447 glBlitFramebuffer(0, 0, m_width, m_height, dstX0, dstY0, dstX1, dstY1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
449 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
452 void Framebuffer::blit_normal(
const int dstX0,
const int dstY0,
const int dstX1,
const int dstY1 )
454 glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo);
455 glReadBuffer(GL_COLOR_ATTACHMENT3);
457 glBlitFramebuffer(0, 0, m_width, m_height, dstX0, dstY0, dstX1, dstY1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
459 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
void label(Widgets &w, const char *format,...)
cree un texte. meme fonctionnement que printf().
bool value(Widgets &w, const char *label, int &value, const int value_min, const int value_max, const int value_step)
valeur editable par increment.
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
GLuint make_vec2_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
GLuint make_vec3_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
GLuint make_vec4_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
void program_use_texture(const GLuint program, const char *uniform, const int unit, const GLuint texture, const GLuint sampler)
configure le pipeline et le shader program pour utiliser une texture, et des parametres de filtrage,...
GLuint make_uint_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
GLuint make_depth_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
representation d'une couleur (rgba) transparente ou opaque.
void release()
destruction.
int height() const
renvoie la hauteur du framebuffer.
void clear_texcoord(const vec2 &value)
texcoord par defaut.
void clear_position(const Point &value)
position par defaut.
void clear_material(const unsigned value)
indice de matiere par defaut.
int width() const
renvoie la largeur du framebuffer.
void unbind(const int width, const int height)
desactive le framebuffer, selection du framebuffer par defaut associe a la fenetre.
GLuint create(const int width, const int height)
creation du framebuffer
void clear_color(const Color &value)
couleur par defaut.
void clear_depth(const float value)
profondeur par defaut.
void unbind_textures()
nettoyage, desactive les textures utilisees par les autres shaders, cf use_color_texture(),...
void bind(const GLuint program, const bool store_color, const bool store_depth, const bool store_position, const bool store_texcoord, const bool store_normal, const bool store_material)
selection du framebuffer, stocker les sorties du fragment shader. les textures sont initialisees avec...
void clear_normal(const Vector &value)
normale par defaut.
representation d'un point 3d.
representation d'un vecteur 3d.
vecteur generique, utilitaire.