Skip to main content
GameDev.net gamedev.net
🔒 Locked

FBO texture is empty / black

Started by Decrius Nov 20, 2011 at 8:43 PM 5 replies 5.7k views
Original Post
Decrius
Decrius
I've been trying but nothing seemed to work out, it's got to be an easy fix, but I don't see it.

Draw code:
void draw()
{
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, scene_fbo);

glViewport (0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, cast(float) width / height, nearPlane, farPlane);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, cast(float) width / height, nearPlane, farPlane);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, scene_texture);
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0);
glVertex3d(-2.0, -2.0, -2.0);

glTexCoord2d(texture_scale, 0.0);
glVertex3d(2.0, -2.0, -2.0);

glTexCoord2d(texture_scale, texture_scale);
glVertex3d(2.0, 2.0, -2.0);

glTexCoord2d(0.0, texture_scale);
glVertex3d(-2.0, 2.0, -2.0);
glEnd();

SDL_GL_SwapBuffers();
}


Init code (both functions are called prior to draw):
GLuint scene_texture, scene_rbo, scene_fbo;

void init_opengl()
{
glViewport (0, 0, width, height);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0);

glShadeModel(GL_SMOOTH);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}

void init_fbo()
{
glGenTextures(1, &scene_texture);
glBindTexture(GL_TEXTURE_2D, scene_texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automatic mipmap
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
glBindTexture(GL_TEXTURE_2D, 0);

// create a renderbuffer object to store depth info
glGenRenderbuffersEXT(1, &scene_rbo);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, scene_rbo);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

// create a framebuffer object
glGenFramebuffersEXT(1, &scene_fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, scene_fbo);

// attach the texture to FBO color attachment point
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, scene_texture, 0);

// attach the renderbuffer to depth attachment point
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, scene_rbo);

// check FBO status
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
{
writeln("FBO error");
}

// switch back to window-system-provided framebuffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}


The FBO is complete, no error message. glGetError() returns 0.

The FBO is black, but it should be green. If I remove the framebuffer bindings (and the code afterwards) it indeed shows green. But somehow the scene_texture remains empty.
Drawing something else but scene_texture (other texture) works, so scene_texture is simply black.

I hate dumping code like this, but I really don't see the problem...

Thanks!
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Yours3!f
Yours3!f
why do you unbind your RBO? don't you need it?

why do you set the viewport and perspective matrix settings when rendering to the FBO? Shouldn't you set it before any rendering?

are you sure you want to use mipmapping? + you don't generate any mipmaps...
glGenerateMipmap(GL_TEXTURE_2D); perhaps

can't think of any other stupid mistake I'd make
Decrius
Decrius
The unsetting of the RBO is indeed not necessary, but neither a problem.
The viewport settings were indeed redundant but neither causing the problem.

It appeared to be the mipmapping, silly enough the tutorial mentions it: http://www.songho.ca/opengl/gl_fbo.html but I didn't add it.
I uncommented the mipmapping setting line when generating the texture, so I thought it was without mipmapping.

I don't need mipmapping, why does the FBO require it?

Thanks.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
V-man
V-man

The unsetting of the RBO is indeed not necessary, but neither a problem.
The viewport settings were indeed redundant but neither causing the problem.

It appeared to be the mipmapping, silly enough the tutorial mentions it: http://www.songho.ca...ngl/gl_fbo.html but I didn't add it.
I uncommented the mipmapping setting line when generating the texture, so I thought it was without mipmapping.

I don't need mipmapping, why does the FBO require it?

Thanks.


Then set glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Yours3!f
Yours3!f

[quote name='Decrius' timestamp='1321826502' post='4885985']
The unsetting of the RBO is indeed not necessary, but neither a problem.
The viewport settings were indeed redundant but neither causing the problem.

It appeared to be the mipmapping, silly enough the tutorial mentions it: http://www.songho.ca...ngl/gl_fbo.html but I didn't add it.
I uncommented the mipmapping setting line when generating the texture, so I thought it was without mipmapping.

I don't need mipmapping, why does the FBO require it?

Thanks.


Then set glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
[/quote]

yep
but also try if adding mipmap generation (glGenerateMipmap(GL_TEXTURE_2D);) works, just in case you'd need it.
V-man
V-man
glGenerateMipmap is for GL 3.
He is using the old EXT functions for the FBO code, so you would need/use glGenerateMipmapEXT.

http://www.opengl.org/wiki/Framebuffer_Object_Examples

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.