#include #include #include #include #include #define WINDSIZEX 100 #define WINDSIZEY 100 #define TEXSIZE 2 void init (void) { int i; GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 }; glClearColor (0.0, 0.0, 0.0, 1.0); glColor4fv (white); glDisable (GL_DITHER); // Check if GL_EXT_framebuffer_object is supported if (!strstr (glGetString (GL_EXTENSIONS), "GL_EXT_framebuffer_object")) { printf ("GL_EXT_framebuffer_object is not supported\n"); exit(0); } else { printf ("GL_EXT_framebuffer_object is supported\n"); } } #define CHECK_FRAMEBUFFER_STATUS() \ { \ GLenum status; \ status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); \ switch(status) { \ case GL_FRAMEBUFFER_COMPLETE_EXT: \ printf("GL_FRAMEBUFFER_COMPLETE_EXT\n"); \ break; \ case GL_FRAMEBUFFER_UNSUPPORTED_EXT: \ printf("GL_FRAMEBUFFER_UNSUPPORTED_EXT\n"); \ /* choose different formats */ \ break; \ case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: \ printf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT\n"); \ break; \ case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: \ printf("GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT\n"); \ break; \ case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: \ printf("GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT\n"); \ break; \ case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: \ printf("%d:GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT\n", __LINE__);\ break; \ case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: \ printf("GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT\n");\ break; \ default: \ /* programming error; will fail on all hardware */ \ printf("programming error\n"); \ break; \ } \ } static void test (void) { GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 }; GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 }; GLuint fbs[1]; GLuint textures[1]; GLint t[TEXSIZE * TEXSIZE * 3]; int i, j; glClearColor (0.0, 0.0, 0.0, 1.0); glClear (GL_COLOR_BUFFER_BIT); glGenFramebuffersEXT (1, fbs); glGenTextures (1, textures); glBindTexture (GL_TEXTURE_1D, textures[0]); glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage1D (GL_TEXTURE_1D, 0, GL_RGB, TEXSIZE, 0, GL_RGB, GL_INT, NULL); printf ("glTexImage1D: %s\n", gluErrorString (glGetError ())); glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, fbs[0]); glFramebufferTexture1DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_1D, textures[0], 0); CHECK_FRAMEBUFFER_STATUS (); // Render to the texture glBindTexture (GL_TEXTURE_1D, 0); glDisable (GL_TEXTURE_1D); glColor4fv (red); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glBegin (GL_LINE); glVertex2f (0, 0); glVertex2f (TEXSIZE, 0); glEnd (); // Render to the window glEnable (GL_TEXTURE_1D); glBindTexture (GL_TEXTURE_1D, textures[0]); glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0); printf ("glBindFramebufferEXT: %s\n", gluErrorString (glGetError ())); glBindTexture (GL_TEXTURE_2D, textures[0]); glEnable (GL_TEXTURE_1D); glColor4fv (white); glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glBegin (GL_POLYGON); glTexCoord2f (0.0, 0.0); glVertex2f (0, 0); glTexCoord2f (1.0, 0.0); glVertex2f (TEXSIZE, 0); glTexCoord2f (1.0, 1.0); glVertex2f (TEXSIZE, TEXSIZE); glTexCoord2f (0.0, 1.0); glVertex2f (0, TEXSIZE); glEnd (); GLfloat buf[WINDSIZEX * WINDSIZEY * 3]; printf ("read out pixels:\n"); glReadPixels (0, 0, WINDSIZEX, WINDSIZEY, GL_RGB, GL_FLOAT, buf); for (j = 0; j < WINDSIZEY; j++) { for (i = 0; i < WINDSIZEY; i++) { if (buf[(j * WINDSIZEX + i) * 3] != 0 || buf[(j * WINDSIZEX + i) * 3 + 1] != 0 || buf[(j * WINDSIZEX + i) * 3 + 2] != 0) { printf ("(%d, %d) = [%f, %f, %f]\n", i, j, buf[(j * WINDSIZEX + i) * 3], buf[(j * WINDSIZEX + i) * 3 + 1], buf[(j * WINDSIZEX + i) * 3 + 2]); } } } glFlush (); glDeleteTextures(1, textures); glDeleteFramebuffersEXT(1, fbs); } void display (void) { glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (0, WINDSIZEX, 0, WINDSIZEX); test (); } int main (int argc, char **argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize (WINDSIZEX, WINDSIZEY); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc (display); glutMainLoop (); return 0; }