Merge branch 'mesa_7_7_branch'
[mesa.git] / progs / trivial / clear-fbo.c
1
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #include <string.h>
7
8 #include <GL/glew.h>
9 #include <GL/glut.h>
10
11
12
13 static int Width = 512, Height = 512;
14 static GLuint MyFB, MyRB;
15
16
17 #define CheckError() assert(glGetError() == 0)
18
19 GLenum doubleBuffer;
20
21 static void Init(void)
22 {
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));
26 fflush(stderr);
27
28
29 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
30 printf("GL_EXT_framebuffer_object not found!\n");
31 exit(0);
32 }
33
34
35 glGenFramebuffersEXT(1, &MyFB);
36 glGenRenderbuffersEXT(1, &MyRB);
37
38 {
39 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
40
41 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, MyRB);
42
43 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
44 GL_RENDERBUFFER_EXT, MyRB);
45
46 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
47
48 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
49 }
50
51 }
52
53
54
55 static void
56 Reshape( int width, int height )
57 {
58 glViewport( 0, 0, width, height );
59 glMatrixMode( GL_PROJECTION );
60 glLoadIdentity();
61 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
62 glMatrixMode( GL_MODELVIEW );
63
64 Width = width;
65 Height = height;
66 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
67 }
68
69 static void Key(unsigned char key, int x, int y)
70 {
71
72 switch (key) {
73 case 27:
74 exit(1);
75 default:
76 break;
77 }
78
79 glutPostRedisplay();
80 }
81
82
83
84 static void Draw( void )
85 {
86
87 /* draw to user framebuffer */
88 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
89 glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
90 glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
91
92
93 glViewport(0, 0, Width, Height);
94 CheckError();
95
96 glClearColor(0.5, 0.5, 1.0, 0.0);
97 glClear(GL_COLOR_BUFFER_BIT);
98 CheckError();
99
100 if (0) {
101 glBegin(GL_TRIANGLES);
102 glColor3f(0,0,.7);
103 glVertex3f( 0.9, -0.9, -30.0);
104 glColor3f(.8,0,0);
105 glVertex3f( 0.9, 0.9, -30.0);
106 glColor3f(0,.9,0);
107 glVertex3f(-0.9, 0.0, -30.0);
108 glEnd();
109 }
110
111 {
112 GLubyte *buffer = malloc(Width * Height * 4);
113
114 /* read from user framebuffer */
115 glReadPixels(0, 0, Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
116 CheckError();
117
118 /* draw to window */
119 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
120 glViewport(0, 0, Width, Height);
121
122 /* Try to clear the window, but will overwrite:
123 */
124 glClearColor(0.8, 0.8, 0, 0.0);
125 glClear(GL_COLOR_BUFFER_BIT);
126
127 glWindowPos2iARB(30, 30);
128 glDrawPixels(Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
129
130 free(buffer);
131 }
132
133 /* Bind normal framebuffer */
134 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
135 glViewport(0, 0, Width, Height);
136
137 if (0) {
138 glBegin(GL_TRIANGLES);
139 glColor3f(0,.7,0);
140 glVertex3f( 0.5, -0.5, -30.0);
141 glColor3f(0,0,.8);
142 glVertex3f( 0.5, 0.5, -30.0);
143 glColor3f(.9,0,0);
144 glVertex3f(-0.5, 0.0, -30.0);
145 glEnd();
146 }
147
148 if (doubleBuffer)
149 glutSwapBuffers();
150 else
151 glFinish();
152
153 CheckError();
154 }
155
156
157 static GLenum Args(int argc, char **argv)
158 {
159 GLint i;
160
161 doubleBuffer = GL_FALSE;
162
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;
168 } else {
169 fprintf(stderr, "%s (Bad option).\n", argv[i]);
170 return GL_FALSE;
171 }
172 }
173 return GL_TRUE;
174 }
175
176
177
178 int
179 main( int argc, char *argv[] )
180 {
181 GLenum type;
182
183 glutInit(&argc, argv);
184
185 if (Args(argc, argv) == GL_FALSE) {
186 exit(1);
187 }
188
189 glutInitWindowPosition(100, 0); glutInitWindowSize( Width, Height );
190
191 type = GLUT_RGB;
192 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
193 glutInitDisplayMode(type);
194
195 if (glutCreateWindow(argv[0]) == GL_FALSE) {
196 exit(1);
197 }
198
199 glewInit();
200
201 Init();
202
203 glutReshapeFunc(Reshape);
204 glutKeyboardFunc(Key);
205 glutDisplayFunc(Draw);
206 glutMainLoop();
207 return 0;
208 }