PDA

View Full Version : FrameBuffer Object use


alopesajr
06-13-05, 08:09 AM
Hi,

I'm trying to use the FBO Extension in Linux (using the latest NVidia driver) but it doesnt work as it should. I tried to draw 2 triangles and recover the image as a texture, but the texture I got is all black....

That is what I'm doing:


/// ************************************************** *********************
//// ************************************************** *********************
void draw() {

glBegin(GL_TRIANGLE_STRIP);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3i(wTexture, hTexture/2, 1);

glColor4f(0.0, 1.0, 0.0, 1.0);
glVertex3i(wTexture/2, hTexture, 1);

glColor4f(0.0, 0.0, 1.0, 1.0);
glVertex3i(wTexture/2, 0, 1);

glColor4f(1.0, 1.0, 1.0, 1.0);
glVertex3i(0, hTexture/2, 1);
glEnd();

glBegin(GL_TRIANGLE_STRIP);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3i(3*wTexture/4, hTexture/4, 1);

glColor4f(0.0, 1.0, 0.0, 1.0);
glVertex3i(3*wTexture/4, 3*hTexture/4, 1);

glColor4f(0.0, 0.0, 1.0, 1.0);
glVertex3i(wTexture/4, hTexture/4, 1);

glColor4f(1.0, 1.0, 1.0, 1.0);
glVertex3i(wTexture/4, 3*hTexture/4, 1);
glEnd();
}

/// ************************************************** *********************
/// ************************************************** *********************
void stopFBO() {

if(indFB)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

/// ************************************************** *********************
/// ************************************************** *********************

void keyboard(unsigned char key, int x, int y) {

switch (key) {

case 'F' :
case 'f' : initializeFBO (wTexture, hTexture);
renderToTextureFBO();
draw();
stopFBO();
renderFBO = true;
break;
....
glutPostRedisplay();
}

/// ************************************************** *********************
/// ************************************************** *********************
bool initializeFBO(int width, int height) {

// Ensure we have the necessary OpenGL FBO extensions.
if (glewGetExtension("GL_EXT_framebuffer_object") != GL_TRUE) {
cout << "Driver does not support FBO extension" << endl;
return false;
}
else
cout << "Driver does support FBO extension !!!" << endl;

hFBO = height;
wFBO = width;

if(wFBO <= 0 || hFBO <= 0) {
cout << "Width and height of FBO must be positive" << endl;
return false;
}
else
cout << "FBO size: " << int(wFBO) << " , " << int(hFBO) << ">" << endl;

glGenFramebuffersEXT(1, &indFB);

if(indFB)
cout << "GenFBO sucessifull !!! (" << indFB << ")" << endl;
else {
cout << "GenFBO failed !!" << endl;
return false;
}

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, indFB);

glGenRenderbuffersEXT(1, &indRB);

glBindRenderbufferEXT ( GL_RENDERBUFFER_EXT, indRB);

glRenderbufferStorageEXT ( GL_RENDERBUFFER_EXT,
GL_RGBA,
wFBO,
hFBO);

glFramebufferRenderbufferEXT ( GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT,
indRB);

bool result = checkFrameBufferStatus();

return result;
}

/// ************************************************** *********************
/// ************************************************** *********************

bool renderToTextureFBO() {

cout << "Starting RenderToTextureFBO...." << endl;

glBindTexture( GL_TEXTURE_2D, tex);
// glActiveTexture(GL_TEXTURE1);

glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D,
tex,
0);


// attach renderbufferto framebufferdepth buffer

glFramebufferRenderbufferEXT ( GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT,
indRB);

bool result = checkFrameBufferStatus();

return result;
}

/// ************************************************** *********************
/// ************************************************** *********************

void display(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glViewport(0, 0, wTexture, hTexture);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, wTexture, 0, hTexture, -10, 10);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

drawAxis();

glEnable(GL_TEXTURE_2D);

if (renderFBO)
glBindTexture(GL_TEXTURE_2D, tex);

glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f( 1.0, 0.0);
glVertex3i(wTexture, 0, 0);

glTexCoord2f( 1.0, 1.0);
glVertex3i(wTexture, hTexture, 0);

glTexCoord2f(0.0, 0.0);
glVertex3i(0, 0, 0);

glTexCoord2f(0.0, 1.0);
glVertex3i(0, hTexture, 0);
glEnd();

glDisable(GL_TEXTURE_2D);

if (!renderFBO)
draw();

glutSwapBuffers();
}

/// ************************************************** *********************
/// ************************************************** *********************

Does anyone have a demo code that works ? I downloaded some demos in this forum but all use the SXMLEngine which is not portable.

Thanks a lot for any help !!

Best Regards,

yooyo
06-13-05, 06:18 PM
Better ask this on OpenGL programming forums (http://www.opengl.org/discussion_boards/cgi_directory/ultimatebb.cgi?category=2).
Here is my setup code:

glGenFramebuffersEXT(1, &fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);

// create color texture & attach to FBO
glGenTextures(1, &color_tex);
glBindTexture(GL_TEXTURE_2D, color_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, TEXX, TEXY, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glGenerateMipmapEXT(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, color_tex, 0);

// create depth texture & attach to FBO
glGenTextures(1, &depth_tex);
glBindTexture(GL_TEXTURE_2D, depth_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, TEXX, TEXY, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depth_tex, 0);
{
GLenum status;
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE_EXT:
break;
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
Log.AddLine(__FILE__, "GL_FRAMEBUFFER_UNSUPPORTED_EXT");
break;
default:
Log.AddLine(__FILE__, "ERROR %04x", status);
return false;
}
}

Absolution
06-13-05, 06:45 PM
gametutorials.com

CaptNKILL
06-13-05, 08:58 PM
Yes, looks good. I especially like this:
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, indFB);

glGenRenderbuffersEXT(1, &indRB);

glBindRenderbufferEXT ( GL_RENDERBUFFER_EXT, indRB);

glRenderbufferStorageEXT ( GL_RENDERBUFFER_EXT,
GL_RGBA,
wFBO,
hFBO);
:alien:

alopesajr
06-14-05, 12:11 PM
Thank all for the sugestions. It seams that all works now. I found a simple demo code in :

http://openvidia.sourceforge.net/simpleFBO.c

I'm a little suprised this code didn't define any RenderBuffer, so I assume my mistake could be this definition :-\

Well, thank you ALL again for the help !!

Best Regards.