Jump to content
  • Advertisement
Sign in to follow this  
Ahmedg

OpenGL FBO and reading with glReadPixels

This topic is 3328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I want to render a scene larger that the screen size, if I got it correctly (source: http://www.songho.ca/opengl/gl_fbo.html), I should use FBO because the default framebuffer called window-system-provided will be controlled and I won't be able to render a scene larger than the screen resolution. I'm sort of confused, I can't get the code work, here is part of the code: myProjection.initOpenGL(inputImage->width , inputImage->height); // initOpenGL is for glutInitWindowSize(), glViewport(), and gluPerspective() //(If my image width and height are larger than my screen resolution, the program without //FBO crash) glGenFramebuffersEXT(1, &fboId); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId); glGenRenderbuffersEXT(1, &rboId); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rboId); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, windowWidth, windowHeight); // glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); // attach a texture to FBO color attachement point // glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureId, 0); // attach a renderbuffer to depth attachment point glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, rboId); // I the beginning of the drawScene() I add glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId); // then I draw the scene, which contains a loaded 3D model with texture // at the end of the drawing scene, I add // back to normal window-system-provided framebuffer glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // unbind // and after that, I want to read the render scene to OpenCV void Projection::OpenGL2OpenCV(IplImage * imgO) {// source: http://webeng.cs.ait.ac.th/cvwiki/gltocv:main int width = imgO->width; int height = imgO->height; . . . IplImage *img = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3); glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, imageData); . . . } I can't find where is the problem, it's not working. I don't want to use the render image as texture, I just want to read it after the rendering, so is it enough to use a renderbuffer even than my scene has a texture? I also check for errors in FBO generation, I render my scene twice, the first time I get an error "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT", and the second rendering is fine, it pass the test GL_FRAMEBUFFER_COMPLETE_EXT. Many thanks for any help :) Bye

Share this post


Link to post
Share on other sites
Advertisement
I think you need to tell it where to draw to. Try putting these two lines after your call to glFramebufferTexture2DEXT:

glDrawBuffer(GL_FRONT);
glReadBuffer(GL_FRONT);

I'm not too sure about the GL_FRONT param - I use FBO's just for a depth render and use GL_NONE...

Share this post


Link to post
Share on other sites
You have to set the corresponding attachment you want to read when calling glReadPixels to extract the color data from an FBO such as:

glDrawBuffer (GL_COLOR_ATTACHMENT0_EXT);


Also make sure to call glWindowPos AFTER binding the FBO. Obviously the FBO needs to be bound while reading its color attachment pixels.

Note that you can also read pixels from the texture itself using glGetTexImage instead of glReadPixels. You already have the FBO texture and can just keep the FBO unbound and then bind its texture as GL texture and read its content.

Finally, find out why you get an FBO setup error and fix this before proceeding any further.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!