8 #define GL_GLEXT_PROTOTYPES
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
));
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);
55 Reshape( int width
, int height
)
57 glViewport( 0, 0, width
, height
);
58 glMatrixMode( GL_PROJECTION
);
60 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
61 glMatrixMode( GL_MODELVIEW
);
65 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT
, GL_RGB
, Width
, Height
);
68 static void Key(unsigned char key
, int x
, int y
)
83 static void Draw( void )
86 /* draw to user framebuffer */
87 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
88 glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT
);
89 glReadBuffer(GL_COLOR_ATTACHMENT1_EXT
);
92 glViewport(0, 0, Width
, Height
);
95 glClearColor(0.5, 0.5, 1.0, 0.0);
96 glClear(GL_COLOR_BUFFER_BIT
);
100 glBegin(GL_TRIANGLES
);
102 glVertex3f( 0.9, -0.9, -30.0);
104 glVertex3f( 0.9, 0.9, -30.0);
106 glVertex3f(-0.9, 0.0, -30.0);
111 GLubyte
*buffer
= malloc(Width
* Height
* 4);
113 /* read from user framebuffer */
114 glReadPixels(0, 0, Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
118 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
119 glViewport(0, 0, Width
, Height
);
121 /* Try to clear the window, but will overwrite:
123 glClearColor(0.8, 0.8, 0, 0.0);
124 glClear(GL_COLOR_BUFFER_BIT
);
126 glWindowPos2iARB(30, 30);
127 glDrawPixels(Width
-60, Height
-60, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
);
132 /* Bind normal framebuffer */
133 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
134 glViewport(0, 0, Width
, Height
);
137 glBegin(GL_TRIANGLES
);
139 glVertex3f( 0.5, -0.5, -30.0);
141 glVertex3f( 0.5, 0.5, -30.0);
143 glVertex3f(-0.5, 0.0, -30.0);
156 static GLenum
Args(int argc
, char **argv
)
160 doubleBuffer
= GL_FALSE
;
162 for (i
= 1; i
< argc
; i
++) {
163 if (strcmp(argv
[i
], "-sb") == 0) {
164 doubleBuffer
= GL_FALSE
;
165 } else if (strcmp(argv
[i
], "-db") == 0) {
166 doubleBuffer
= GL_TRUE
;
168 fprintf(stderr
, "%s (Bad option).\n", argv
[i
]);
178 main( int argc
, char *argv
[] )
182 glutInit(&argc
, argv
);
184 if (Args(argc
, argv
) == GL_FALSE
) {
188 glutInitWindowPosition(100, 0); glutInitWindowSize( Width
, Height
);
191 type
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
192 glutInitDisplayMode(type
);
194 if (glutCreateWindow(argv
[0]) == GL_FALSE
) {
200 glutReshapeFunc(Reshape
);
201 glutKeyboardFunc(Key
);
202 glutDisplayFunc(Draw
);