PDA

View Full Version : render image in FBO



sungsoo
04-01-2009, 08:06 AM
Hi,
I face a problem.

What I intend in this sobel function is like that;

1. using glDrawBuffer the result of sobel filter is drawn in 0th color
buffer.
2. read the color buffer using glReadBuffer and glReadPixels
3. Display on the screen

However, it doesn't give correct result (in fact, there are no change)
and even an error called "bad_alloc at memory location" occur after
glReadBuffer or glReadPixels function.

Here is part of my code;


//initialize
//1. create a context
Context = cgCreateContext();
//2. Define fragment profile
fragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
//3. create and compile a program
fragmentProgram = cgCreateProgramFromFile(Context, CG_SOURCE, "fragment3x3Mask.cg", fragmentProfile, NULL, NULL);
//4. Load a program
cgGLLoadProgram(fragmentProgram);
//5. get the handle to a parameter
matrixMask = cgGetNamedParameter(fragmentProgram, "mask");
checkForCgError();

//apply sobel program
float Gx[]={-1, 0, 1, -2, 0, 2, -1, 0, 1};

//1. display on the frame buffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
//2. apply the effect to the 0th texture (set render destination)
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBindTexture(GL_TEXTURE_RECTANGLE_NV, DisplayTexture);
glEnable(GL_TEXTURE_RECTANGLE_NV);
//3. Bind fragment program: ready to use
cgGLBindProgram(fragmentProgram);
//4. give the argument to the fragment program (Gx)
cgGLSetMatrixParameterfc(matrixMask, Gx);
//5. enable fragment program
cgGLEnableProfile(fragmentProfile);
checkForCgError();
//7. render geometry
glBegin(GL_QUADS);
glTexCoord2f( 0, 0 ); glVertex3f(-0.5, -0.5, 0.0f);
glTexCoord2f( 0, imgy); glVertex3f(-0.5, 0.5, 0.0f);
glTexCoord2f(imgx, imgy); glVertex3f( 0.5, 0.5, 0.0f);
glTexCoord2f(imgx, 0 ); glVertex3f( 0.5, -0.5, 0.0f);
glEnd();

cgGLDisableProfile(fragmentProfile);
//GPU texture -> CPU arrays
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glReadPixels(0, 0, imgx, imgy, GL_RGB, GL_UNSIGNED_BYTE, displayTexture);
//CPU array -> GPU texture
glBindTexture(GL_TEXTURE_RECTANGLE_NV, DisplayTexture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, imgx, imgy, GL_RGB, GL_UNSIGNED_BYTE, displayTexture);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);


And also when I initialize FBO, I create three color buffers in the FBO
like follwing;


glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_RECTANGLE_NV, TempTexture, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
GL_TEXTURE_RECTANGLE_NV, DisplayTexture, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT,
GL_TEXTURE_RECTANGLE_NV, BackupTexture, 0);


and also before executing this code, I read an image (ppm file) and stored in DisplyTexture. (imgx and imgy indicate width and height of the image, respectively.).

Pliease give me any comments or suggestion to solve this problem.

Really Appreciate to your helps in advance.

p.s. the Cg program work properly when I didn't use FBO.

scratt
04-01-2009, 09:33 AM
Personally I found this article very helpful when first using FBOs...

http://www.songho.ca/opengl/gl_fbo.html

I am not sure of your implementation specifics, but you really need to check with your FBO to see if it is setup properly before you try and use it..

// check FBO status
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status != GL_FRAMEBUFFER_COMPLETE_EXT)
fboUsed = false;

Also you need to check what you can and cannot attach to an FBO on your specific platform. You are using a lot of colour attachments, and from the looks of it NPOT textures. Either of those may be a problem....