Jump to content
  • Advertisement
Sign in to follow this  
Yours3!f

OpenGL fbo not working

This topic is 2531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I tried to render to a texture and render it later as a full-screen quad. However the screen remains black. I checked the fbo, no errors it is complete, I checked the shaders, they're working, I checked the render texture on another object, and it contains the scene data, and I could display it. I checked the full-screen quad that I draw the texture onto, and I can draw it too, but to be sure I disabled backface culling while drawing the fs quad. I get no opengl error, I can pass all the stuff to the shaders, I get no error while loading them too. I tried immediate mode drawing too, but it didn't help. The scene is drawn properly when not using RTTs.

Here's the loading code:

//load in the shaders
objs::get()->shader_loader.load_shader_control_file ( "../shaders/deferred/fs_quad.sc", &the_render.fs_quad );
//load in the full-screen quad from an obj file
objs::get()->obj.load_obj_file ( "../resources/fs_quad.obj", &the_render.quad, &the_render.fs_quad );

//create the texture to render to
the_render.fbo_tex.create();
the_render.fbo_tex.bind();
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA16F, objs::get()->conf.SCREEN_WIDTH, objs::get()->conf.SCREEN_HEIGHT, 0, GL_RGBA, GL_FLOAT, 0 );
the_render.fbo_tex.set_dimensions ( objs::get()->conf.SCREEN_WIDTH, objs::get()->conf.SCREEN_HEIGHT );

//create the render buffer that contains the depth data
the_render.rbuf_depth.create();
the_render.rbuf_depth.bind();
the_render.rbuf_depth.set_storage_format ( GL_DEPTH_COMPONENT32, objs::get()->conf.SCREEN_WIDTH, objs::get()->conf.SCREEN_HEIGHT );

//create fbo that will contain the color data
the_render.fbo.create();
the_render.fbo.bind();
the_render.fbo.set_as_draw_buffer ( GL_COLOR_ATTACHMENT0 );

//attach render buffer to the frame buffer
the_render.rbuf_depth.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, &the_render.fbo );
//attach the texture to the frame buffer
the_render.fbo_tex.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, &the_render.fbo );

//unbind the fbo
the_render.fbo.unbind();


here's the render code:

//set up camera...

//bind fbo
fbo.bind();
clear_screen();
//draw stuff....
fbo.unbind();

//set orhographic mode, disable depth testing, bind the fs quad shader
event::get()->get_resize()->set_orthographic ( objs::get()->conf.SCREEN_WIDTH, objs::get()->conf.SCREEN_HEIGHT, -1.0f, 1.0f );
glDisable ( GL_DEPTH_TEST );
fs_quad.bind();

//pass the matrices
fs_quad.pass_m4x4 ( objs::get()->ppl.get_projection_matrix(), "m4_p" );
fs_quad.pass_m4x4 ( objs::get()->ppl.get_model_view_matrix(), "m4_mv" );

//bind the texture and set up texture parameters
fbo_tex.bind();
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
//pass the texture to the shader
fs_quad.pass_int ( 0, "texture0" );

//draw full-screen quad
quad.render();

//unbind shader, enable depth test, set perspective mode again
fs_quad.unbind();
glEnable ( GL_DEPTH_TEST );
event::get()->get_resize()->set_perspective ();

objs::get()->check_for_opengl_error();
swap_buffer();


vertex shader:

#version 410

//projection, modelview matrices
uniform mat4 m4_p, m4_mv;

//the vertex position
in vec4 v4_vertex;
//the texture coordinates
in vec2 v2_texture;

smooth out vec2 v2_texture_coords;

void main()
{
v2_texture_coords = v2_texture;
mat4 m4_mvp = m4_p * m4_mv;
gl_Position = m4_mvp * v4_vertex;
}


pixel shader code:

#version 410

uniform sampler2D texture0;

smooth in vec2 v2_texture_coords;

out vec4 v4_frag_color;

void main()
{
v4_frag_color = texture(texture0, v2_texture_coords.st);
}


and the obj file:

v 1.000000 -1.000000 0.000000
v 1.000000 1.000000 0.000000
v -1.000000 1.000000 0.000000
v -1.000000 -1.000000 0.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
f 3/3/ 4/2/ 1/1/
f 2/4/ 3/3/ 1/1/


What am I missing?

Best regards,
Yours3!f

Share this post


Link to post
Share on other sites
Advertisement

[quote name='Yours3!f' timestamp='1313066471' post='4847647']

//pass the texture to the shader
fs_quad.pass_int ( 0, "texture0" );



Any reason why this is 0?
[/quote]

as far as I know if you bind a texture, then there is no need to pass anything but a zero as the variable to the shader (to the sampler2D).

Share this post


Link to post
Share on other sites
Hidden

Hi,

I tried to render to a texture and render it later as a full-screen quad. However the screen remains black. I checked the fbo, no errors it is complete, I checked the shaders, they're working, I checked the render texture on another object, and it contains the scene data, and I could display it. I checked the full-screen quad that I draw the texture onto, and I can draw it too, but to be sure I disabled backface culling while drawing the fs quad. I get no opengl error, I can pass all the stuff to the shaders, I get no error while loading them too. I tried immediate mode drawing too, but it didn't help. The scene is drawn properly when not using RTTs.

Here's the loading code:

//load in the shaders
objs::get()->shader_loader.load_shader_control_file ( "../shaders/deferred/fs_quad.sc", &the_render.fs_quad );
//load in the full-screen quad from an obj file
objs::get()->obj.load_obj_file ( "../resources/fs_quad.obj", &the_render.quad, &the_render.fs_quad );

//create the texture to render to
the_render.fbo_tex.create();
the_render.fbo_tex.bind();
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA16F, objs::get()->conf.SCREEN_WIDTH, objs::get()->conf.SCREEN_HEIGHT, 0, GL_RGBA, GL_FLOAT, 0 );
the_render.fbo_tex.set_dimensions ( objs::get()->conf.SCREEN_WIDTH, objs::get()->conf.SCREEN_HEIGHT );

//create the render buffer that contains the depth data
the_render.rbuf_depth.create();
the_render.rbuf_depth.bind();
the_render.rbuf_depth.set_storage_format ( GL_DEPTH_COMPONENT32, objs::get()->conf.SCREEN_WIDTH, objs::get()->conf.SCREEN_HEIGHT );

//create fbo that will contain the color data
the_render.fbo.create();
the_render.fbo.bind();
the_render.fbo.set_as_draw_buffer ( GL_COLOR_ATTACHMENT0 );

//attach render buffer to the frame buffer
the_render.rbuf_depth.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, &the_render.fbo );
//attach the texture to the frame buffer
the_render.fbo_tex.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, &the_render.fbo );

//unbind the fbo
the_render.fbo.unbind();


here's the render code:

//set up camera...

//bind fbo
fbo.bind();
//clear screen...
//draw stuff....
fbo.unbind();

//set orhographic mode, disable depth testing, bind the fs quad shader
event::get()->get_resize()->set_orthographic ( objs::get()->conf.SCREEN_WIDTH, objs::get()->conf.SCREEN_HEIGHT, -1.0f, 1.0f );
glDisable ( GL_DEPTH_TEST );
fs_quad.bind();

//pass the matrices
fs_quad.pass_m4x4 ( objs::get()->ppl.get_projection_matrix(), "m4_p" );
fs_quad.pass_m4x4 ( objs::get()->ppl.get_model_view_matrix(), "m4_mv" );

//bind the texture and set up texture parameters
fbo_tex.bind();
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
//pass the texture to the shader
fs_quad.pass_int ( 0, "texture0" );

//draw full-screen quad
quad.render();

//unbind shader, enable depth test, set perspective mode again
fs_quad.unbind();
glEnable ( GL_DEPTH_TEST );
event::get()->get_resize()->set_perspective ();

objs::get()->check_for_opengl_error();
swap_buffer();


vertex shader:

#version 330

//projection, modelview matrices
uniform mat4 m4_p, m4_mv;

//the vertex position
in vec4 v4_vertex;
//the texture coordinates
in vec2 v2_texture;

smooth out vec2 v2_texture_coords;

void main()
{
v2_texture_coords = v2_texture;
mat4 m4_mvp = m4_p * m4_mv;
gl_Position = m4_mvp * v4_vertex;
}


pixel shader code:

#version 330

uniform sampler2D texture0;

smooth in vec2 v2_texture_coords;

out vec4 v4_frag_color;

void main()
{
v4_frag_color = texture(texture0, v2_texture_coords.st);
}


and the obj file:

v 1.000000 -1.000000 0.000000
v 1.000000 1.000000 0.000000
v -1.000000 1.000000 0.000000
v -1.000000 -1.000000 0.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
f 3/3/ 4/2/ 1/1/
f 2/4/ 3/3/ 1/1/


What am I missing?

Best regards,
Yours3!f

Share this post


Link to post

as far as I know if you bind a texture, then there is no need to pass anything but a zero as the variable to the shader (to the sampler2D).


Nope. Binding a texture is only required for those stages of fixed pipeline that operate on it (setting image, filters, etc.). sampler2D should contain actual texture ID. You pass a 'default' textureID (0) to shader, that's why all you see is a black quad :)

Share this post


Link to post
Share on other sites

[quote name='Yours3!f' timestamp='1313068976' post='4847666']
as far as I know if you bind a texture, then there is no need to pass anything but a zero as the variable to the shader (to the sampler2D).


Nope. Binding a texture is only required for those stages of fixed pipeline that operate on it (setting image, filters, etc.). sampler2D should contain actual texture ID. You pass a 'default' textureID (0) to shader, that's why all you see is a black quad :)
[/quote]

but I do it everywhere else, and it works... anyways I tried it:

fs_quad.pass_int ( *fbo_tex.get_id(), "texture0" ); //the returned id is the GLuint of the texture

//draw full-screen quad
quad.render();


but at the quad.render() function it crashes. this is the last call from my application, and there are some from the gpu driver :) (fglrx_dri.so)

Share this post


Link to post
Share on other sites

[quote name='Yours3!f' timestamp='1313068976' post='4847666']
as far as I know if you bind a texture, then there is no need to pass anything but a zero as the variable to the shader (to the sampler2D).


Nope. Binding a texture is only required for those stages of fixed pipeline that operate on it (setting image, filters, etc.). sampler2D should contain actual texture ID. You pass a 'default' textureID (0) to shader, that's why all you see is a black quad :)
[/quote]

Nope, sampler2D should contain the texture unit unit number. It starts from 0 and goes to GL_MAX_whatever (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS or GL_MAX_TEXTURE_IMAGE_UNITS)
http://www.opengl.org/wiki/GLSL_:_common_mistakes#Binding_A_Texture

Share this post


Link to post
Share on other sites

[quote name='capricorn' timestamp='1313069182' post='4847668']
[quote name='Yours3!f' timestamp='1313068976' post='4847666']
as far as I know if you bind a texture, then there is no need to pass anything but a zero as the variable to the shader (to the sampler2D).


Nope. Binding a texture is only required for those stages of fixed pipeline that operate on it (setting image, filters, etc.). sampler2D should contain actual texture ID. You pass a 'default' textureID (0) to shader, that's why all you see is a black quad :)
[/quote]

Nope, sampler2D should contain the texture unit unit number. It starts from 0 and goes to GL_MAX_whatever (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS or GL_MAX_TEXTURE_IMAGE_UNITS)
http://www.opengl.or...nding_A_Texture
[/quote]

ok but that is for multi-texturing right? and since there is only one texture and I don't use multi-texturing anywhere else it should work, shouldn't it?

anyways the driver should've given me an opengl error, not crashing :blink:

Share this post


Link to post
Share on other sites

Nope, sampler2D should contain the texture unit unit number.


Ugh, what I was smoking? blink.gif Should go slap myself in the head a couple times. Apologies, Yours3!f.

Share this post


Link to post
Share on other sites

[quote name='V-man' timestamp='1313070921' post='4847678']
Nope, sampler2D should contain the texture unit unit number.


Ugh, what I was smoking? blink.gif Should go slap myself in the head a couple times. Apologies, Yours3!f.
[/quote]

no problem :) you've just discovered a bug in the linux ATI drivers as it's not giving an error when using a GLint > max
actually the texture id was 707, so I tried to use my video card excessively lol

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!