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

problem using gldrawbuffer function in FBO

Started by sung Apr 1, 2009 at 9:36 AM 0 replies 1.4k views
Original Post
sung
sung
Hi, Again 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.
Digitalfragment
Digitalfragment
glReadPixels(0, 0, imgx, imgy, GL_RGB, GL_UNSIGNED_BYTE, displayTexture);

1) is displayTexture a valid pointer?
2) is displayTexture a valid pointer pointing to an allocation at least imgx*imgy*3 bytes in size?



3) Why are you reading from GPU to main memory only to upload the data to the GPU again? The idea of using Framebuffers is that you draw to GPU memory directly into the texture you want it in.

Topic Locked

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

Sign in to reply to this topic.