13 static int Width = 400, Height = 400;
14 static GLuint MyFB, MyRB;
17 #define CheckError() assert(glGetError() == 0)
21 static void Init(void)
23 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
24 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
25 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
28 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
29 printf("GL_EXT_framebuffer_object not found!\n");
34 glGenFramebuffersEXT(1, &MyFB);
35 glGenRenderbuffersEXT(1, &MyRB);
38 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
40 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, MyRB);
42 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
43 GL_RENDERBUFFER_EXT, MyRB);
45 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
47 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
50 glClearColor(0.0, 0.0, 1.0, 0.0);
56 Reshape( int width, int height )
58 glViewport( 0, 0, width, height );
59 glMatrixMode( GL_PROJECTION );
61 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
62 glMatrixMode( GL_MODELVIEW );
66 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
70 static void Key(unsigned char key, int x, int y)
85 static void Draw( void )
88 /* draw to user framebuffer */
89 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
90 glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
91 glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
93 glClearColor(0.5, 0.5, 1.0, 0.0);
94 glClear(GL_COLOR_BUFFER_BIT);
97 glBegin(GL_TRIANGLES);
99 glVertex3f( 0.9, -0.9, -30.0);
101 glVertex3f( 0.9, 0.9, -30.0);
103 glVertex3f(-0.9, 0.0, -30.0);
108 GLubyte *buffer = malloc(Width * Height * 4);
110 /* read from user framebuffer */
111 glReadPixels(0, 0, Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
115 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
116 /* Try to clear the window, but will overwrite:
118 glClearColor(0.8, 0.8, 0, 0.0);
119 glClear(GL_COLOR_BUFFER_BIT);
121 glWindowPos2iARB(30, 30);
122 glDrawPixels(Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
128 /* Bind normal framebuffer */
129 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
132 glBegin(GL_TRIANGLES);
134 glVertex3f( 0.5, -0.5, -30.0);
136 glVertex3f( 0.5, 0.5, -30.0);
138 glVertex3f(-0.5, 0.0, -30.0);
151 static GLenum Args(int argc, char **argv)
155 doubleBuffer = GL_FALSE;
157 for (i = 1; i < argc; i++) {
158 if (strcmp(argv[i], "-sb") == 0) {
159 doubleBuffer = GL_FALSE;
160 } else if (strcmp(argv[i], "-db") == 0) {
161 doubleBuffer = GL_TRUE;
163 fprintf(stderr, "%s (Bad option).\n", argv[i]);
173 main( int argc, char *argv[] )
177 glutInit(&argc, argv);
179 if (Args(argc, argv) == GL_FALSE) {
183 glutInitWindowPosition(100, 0); glutInitWindowSize( Width, Height );
186 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
187 glutInitDisplayMode(type);
189 if (glutCreateWindow(argv[0]) == GL_FALSE) {
197 glutReshapeFunc(Reshape);
198 glutKeyboardFunc(Key);
199 glutDisplayFunc(Draw);