Sign in to follow this  
aboteler

OpenGL Basic OpenGL - Render to FBO, Readback Texture Help

Recommended Posts

I am new to OpenGL and have just started trying to work with it. My goal in this program is to render a square to an offscreen buffer using a single color with float precision and then readback the data. The compilation is successful. However when I execute the program I get an GL error of illegal operation when I try to attach the texture to the frame buffer. I have not been able to figure out why I get this error here. Plus, the next error is an invalid enumeration when I try to readback the pixels from the texture. Any help will be greatly appreciated, Aaron fyi: source code #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdio.h> #include <stdlib.h> #include <assert.h> #define ASSERT assert #include <GL/glew.h> #include <GL/glut.h> typedef unsigned int glutConfig_t; const int X_DIMENSION = 16; const int Y_DIMENSION = 16; const glutConfig_t theGlutDisplayMode = GLUT_DOUBLE | // Double Buffering GLUT_RGB; // Color Model const GLenum theTextureFormat = GL_TEXTURE_RECTANGLE_ARB; static GLuint theFrameBuffer; static GLuint theFrameBufferTexture; static float theImage[X_DIMENSION * Y_DIMENSION]; static void CheckOpenGLStatus(char* strFile, int line) ... #define CHECK_OPENGL CheckOpenGLStatus(__FILE__,__LINE__) static void CheckFrameBufferStatus(char* strFile, int line) ... #define CHECK_FRAME_BUFFER CheckFrameBufferStatus(__FILE__,__LINE__) int main(int argc, char *argv[]) { glutInit( &argc, argv ); glutInitDisplayMode( theGlutDisplayMode ); glutInitWindowSize( X_DIMENSION, Y_DIMENSION ); glutCreateWindow( "Simple Example" ); glewInit(); glDisable( GL_DITHER ); // Dither is on by default but not needed CHECK_OPENGL; glDisable( GL_DEPTH_TEST ); CHECK_OPENGL; glGenFramebuffersEXT(1,&theFrameBuffer); CHECK_OPENGL; glGenTextures(1,&theFrameBufferTexture); CHECK_OPENGL; glBindTexture(theTextureFormat,theFrameBufferTexture); // bind texture CHECK_OPENGL; // turn off filtering wrap modes glTexParameteri(theTextureFormat, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(theTextureFormat, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(theTextureFormat, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(theTextureFormat, GL_TEXTURE_WRAP_T, GL_CLAMP); CHECK_OPENGL; // define texture parameters glTexImage2D(theTextureFormat,0,GL_FLOAT_R32_NV, X_DIMENSION,Y_DIMENSION, 0,GL_LUMINANCE,GL_FLOAT,NULL); CHECK_OPENGL; // attach texture to frame buffer color 0 (RED) glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, theTextureFormat, theFrameBufferTexture, 0); CHECK_OPENGL; // reports Illegal operation CHECK_FRAME_BUFFER; // setup projection glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, X_DIMENSION, 0.0, Y_DIMENSION, -5, 50); CHECK_OPENGL; // render glClear(GL_COLOR_BUFFER_BIT); glColor3f(5.0, 0.0, 0.0); { glBegin(GL_POLYGON); glVertex3f(2,2,0.0); glVertex3f(8,2,0.0); glVertex3f(8,8,0.0); glVertex3f(2,8,0.0); glEnd(); } glFlush(); CHECK_OPENGL; // Swap buffers (display the rendered image). glutSwapBuffers(); CHECK_OPENGL; // transfer texture from FrameBuffer to local memory glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); glReadPixels(0,0,X_DIMENSION,Y_DIMENSION, GL_FLOAT_R32_NV,GL_FLOAT,theImage); CHECK_OPENGL; // report illegal operation CHECK_FRAME_BUFFER; { unsigned xx, yy, lineOffset; printf("theImage\n\n"); for(yy = 0; yy < Y_DIMENSION; ++yy) { lineOffset = yy * X_DIMENSION; for(xx = 0; xx < X_DIMENSION; ++xx) { printf("%2.2f ",theImage[lineOffset + xx]); // prints all zeros }; printf("\n"); }; printf("\n"); } return EXIT_SUCCESS; }

Share this post


Link to post
Share on other sites
Look to the FBO spec for the cases glFramebufferTexture2DEXT will generate an invalid operation error.
Quote:
From the FBO spec
INVALID_OPERATION is generated if the current value of FRAMEBUFFER_BINDING_EXT is zero when FramebufferTexture{1D|2D|3D}EXT is called.
I don't see you binding the framebuffer anywhere.

When posting source code, use the [code][/code] tags for short snippets and [source][/source] tags for longer pieces. See the Forum FAQ for more info.

And welcome to GDNet! [smile]

Share this post


Link to post
Share on other sites
Thanks for the info and link on the framebuffer. I decided to take a step back in the code and try only render to an offscreen buffer and readback. I have it working except on the readback the values are clamped.

Thanks,

Aaron

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this