Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [OpenGL] Framebuffer Objects

This thread is locked; no one can reply to it. rss feed Print
[OpenGL] Framebuffer Objects
Archon
Member #4,195
January 2004
avatar

Here is the code that I currently have for making a framebuffer object, and a texture to write to:

1 const int TEXTURE_WIDTH = 1024;
2 const int TEXTURE_HEIGHT = 768;
3 const int RENDERBUFFER_WIDTH = 800;
4 const int RENDERBUFFER_HEIGHT = 600;
5
6 //Create the framebuffer object
7 glGenFramebuffersEXT(1, &framebuffer);
8
9 //Create the renderbuffer
10 glGenRenderbuffersEXT(1, &renderbuffer);
11 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, RENDERBUFFER_WIDTH, RENDERBUFFER_HEIGHT);
12
13 //Attach the texture to the renderbuffer
14 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, renderbuffer);
15
16 //Create the texture
17 glGenTextures(1, &texture);
18 glPushAttrib(GL_CURRENT_BIT);
19 glBindTexture(GL_TEXTURE_2D, texture);
20 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
21 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
22 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
23 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
24 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
25 glPopAttrib();
26
27 //Attach the texture to the framebuffer
28 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, texture, GL_TEXTURE_2D, 0);
29
30 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
31 printf("status = %i\n", status);
32 printf("GL_FRAMEBUFFER_COMPLETE_EXT = %i\n", GL_FRAMEBUFFER_COMPLETE_EXT);

Output:

status = 36053
GL_FRAMEBUFFER_COMPLETE_EXT = 36053

Additionally, I have these in the same class:

1 public GLuint GetTexture()
2 {
3 return texture;
4 }
5 
6 public void Bind()
7 {
8 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
9 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer);
10 }
11 
12 public void Unbind()
13 {
14 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
15 }

And I also have this (note that removing the Bind and Unbind calls would make the scene appear)

        lighting.Bind();
         world.Render();
        lighting.Unbind();

        glPushAttrib(GL_CURRENT_BIT);
        glBindTexture(GL_TEXTURE_2D, lighting.GetTexture());
        glBegin(GL_QUADS);
         glTexCoord2i(0,0); glVertex2f(0,0);
         glTexCoord2i(1,0); glVertex2f(14,0);
         glTexCoord2i(1,1); glVertex2f(14,10);
         glTexCoord2i(0,1); glVertex2f(0,10);
        glEnd();
        glPopAttrib();

I call it "lighting" because it relates to how I want pixel-based lighting/blending.

It doesn't display anything. The quad is actually black (but so is the background)

1. What I am doing wrong?
2. Do I need the renderbuffer?
3. Does the renderbuffer have to be the same size as the texture?

Go to: