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
));
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);
51 glClearColor(0.0, 0.0, 1.0, 0.0);
57 Reshape( int width
, int height
)
59 glViewport( 0, 0, width
, height
);
60 glMatrixMode( GL_PROJECTION
);
62 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
63 glMatrixMode( GL_MODELVIEW
);
67 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGB
, Width
, Height
);
71 static void Key(unsigned char key
, int x
, int y
)
86 static void Draw( void )
89 /* draw to user framebuffer */
90 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
91 glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT
);
92 glReadBuffer(GL_COLOR_ATTACHMENT1_EXT
);
94 glClearColor(0.5, 0.5, 1.0, 0.0);
95 glClear(GL_COLOR_BUFFER_BIT
);
98 glBegin(GL_TRIANGLES
);
100 glVertex3f( 0.9, -0.9, -30.0);
102 glVertex3f( 0.9, 0.9, -30.0);
104 glVertex3f(-0.9, 0.0, -30.0);
109 GLubyte
*buffer
= malloc(Width
* Height
* 4);
111 /* read from user framebuffer */
112 glReadPixels(0, 0, Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
116 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
117 /* Try to clear the window, but will overwrite:
119 glClearColor(0.8, 0.8, 0, 0.0);
120 glClear(GL_COLOR_BUFFER_BIT
);
122 glWindowPos2iARB(30, 30);
123 glDrawPixels(Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
129 /* Bind normal framebuffer */
130 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
133 glBegin(GL_TRIANGLES
);
135 glVertex3f( 0.5, -0.5, -30.0);
137 glVertex3f( 0.5, 0.5, -30.0);
139 glVertex3f(-0.5, 0.0, -30.0);
152 static GLenum
Args(int argc
, char **argv
)
156 doubleBuffer
= GL_FALSE
;
158 for (i
= 1; i
< argc
; i
++) {
159 if (strcmp(argv
[i
], "-sb") == 0) {
160 doubleBuffer
= GL_FALSE
;
161 } else if (strcmp(argv
[i
], "-db") == 0) {
162 doubleBuffer
= GL_TRUE
;
164 fprintf(stderr
, "%s (Bad option).\n", argv
[i
]);
174 main( int argc
, char *argv
[] )
178 glutInit(&argc
, argv
);
180 if (Args(argc
, argv
) == GL_FALSE
) {
184 glutInitWindowPosition(100, 0); glutInitWindowSize( Width
, Height
);
187 type
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
188 glutInitDisplayMode(type
);
190 if (glutCreateWindow(argv
[0]) == GL_FALSE
) {
198 glutReshapeFunc(Reshape
);
199 glutKeyboardFunc(Key
);
200 glutDisplayFunc(Draw
);