13 static int Width
= 512, Height
= 512;
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
));
29 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
30 printf("GL_EXT_framebuffer_object not found!\n");
35 glGenFramebuffersEXT(1, &MyFB
);
36 glGenRenderbuffersEXT(1, &MyRB
);
39 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
41 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT
, MyRB
);
43 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT1_EXT
,
44 GL_RENDERBUFFER_EXT
, MyRB
);
46 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGB
, Width
, Height
);
48 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 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
);
69 static void Key(unsigned char key
, int x
, int y
)
84 static void Draw( void )
87 /* draw to user framebuffer */
88 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
89 glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT
);
90 glReadBuffer(GL_COLOR_ATTACHMENT1_EXT
);
93 glViewport(0, 0, Width
, Height
);
96 glClearColor(0.5, 0.5, 1.0, 0.0);
97 glClear(GL_COLOR_BUFFER_BIT
);
101 glBegin(GL_TRIANGLES
);
103 glVertex3f( 0.9, -0.9, -30.0);
105 glVertex3f( 0.9, 0.9, -30.0);
107 glVertex3f(-0.9, 0.0, -30.0);
112 GLubyte
*buffer
= malloc(Width
* Height
* 4);
114 /* read from user framebuffer */
115 glReadPixels(0, 0, Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
119 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
120 glViewport(0, 0, Width
, Height
);
122 /* Try to clear the window, but will overwrite:
124 glClearColor(0.8, 0.8, 0, 0.0);
125 glClear(GL_COLOR_BUFFER_BIT
);
127 glWindowPos2iARB(30, 30);
128 glDrawPixels(Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
133 /* Bind normal framebuffer */
134 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
135 glViewport(0, 0, Width
, Height
);
138 glBegin(GL_TRIANGLES
);
140 glVertex3f( 0.5, -0.5, -30.0);
142 glVertex3f( 0.5, 0.5, -30.0);
144 glVertex3f(-0.5, 0.0, -30.0);
157 static GLenum
Args(int argc
, char **argv
)
161 doubleBuffer
= GL_FALSE
;
163 for (i
= 1; i
< argc
; i
++) {
164 if (strcmp(argv
[i
], "-sb") == 0) {
165 doubleBuffer
= GL_FALSE
;
166 } else if (strcmp(argv
[i
], "-db") == 0) {
167 doubleBuffer
= GL_TRUE
;
169 fprintf(stderr
, "%s (Bad option).\n", argv
[i
]);
179 main( int argc
, char *argv
[] )
183 glutInit(&argc
, argv
);
185 if (Args(argc
, argv
) == GL_FALSE
) {
189 glutInitWindowPosition(100, 0); glutInitWindowSize( Width
, Height
);
192 type
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
193 glutInitDisplayMode(type
);
195 if (glutCreateWindow(argv
[0]) == GL_FALSE
) {
203 glutReshapeFunc(Reshape
);
204 glutKeyboardFunc(Key
);
205 glutDisplayFunc(Draw
);