#define GL_GLEXT_PROTOTYPES #include #include #include #include #include #include #define TEX_W 256 #define TEX_H 256 #define CHECK_GLERROR() \ { \ GLenum err = glGetError(); \ while (err != GL_NO_ERROR) \ { \ fprintf(stderr, "GL Error %#x, %s, line %d\n", \ err, __FILE__, __LINE__); \ err = glGetError(); \ } \ } void init(void) { GLuint tex, fbo; GLenum status; /* Generate a destination texture */ glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, TEX_W, TEX_H, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL); CHECK_GLERROR(); /* Prepare a fbo with the texture as color attachment */ glGenFramebuffersEXT(1, &fbo); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0); glViewport(0, 0, TEX_W, TEX_H); CHECK_GLERROR(); status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); if (status != GL_FRAMEBUFFER_COMPLETE_EXT) fprintf(stderr, "FBO status %#x\n", status); glClearColor(0.0, 0.0, 1.0, 0); glClear(GL_COLOR_BUFFER_BIT); CHECK_GLERROR(); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glFrontFace(GL_CCW); CHECK_GLERROR(); glColor4f(0.0, 1.0, 0.0, 1.0); glBegin(GL_TRIANGLES); glVertex3f(-1.0, -1.0, -1.0); glVertex3f(-1.0, 1.0, -1.0); glVertex3f( 1.0, 1.0, 1.0); glEnd(); glColor4f(1.0, 0.0, 0.0, 1.0); glBegin(GL_TRIANGLES); glVertex3f( 1.0, -1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0); glEnd(); CHECK_GLERROR(); glDisable(GL_CULL_FACE); CHECK_GLERROR(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); glBindTexture(GL_TEXTURE_2D, tex); glEnable(GL_TEXTURE_2D); glClearColor(1.0, 1.0, 1.0, 1.0); glViewport(0, 0, 200, 200); glColor4f(1.0, 1.0, 1.0, 1.0); CHECK_GLERROR(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -0.5); glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -0.5); glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -0.5); glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -0.5); glEnd(); glFlush(); glutSwapBuffers(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_ACCUM); glutInitWindowSize(200, 200); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutIdleFunc(display); glutMainLoop(); return 0; }