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