gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / trivial / clear-fbo.c
blob6435c901adcea4b05b0203242bdab855881ad90b
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #include <string.h>
8 #include <GL/glew.h>
9 #include <GL/glut.h>
13 static int Width = 512, Height = 512;
14 static GLuint MyFB, MyRB;
17 #define CheckError() assert(glGetError() == 0)
19 GLenum doubleBuffer;
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));
26 fflush(stderr);
29 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
30 printf("GL_EXT_framebuffer_object not found!\n");
31 exit(0);
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);
55 static void
56 Reshape( int width, int height )
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 );
64 Width = width;
65 Height = height;
66 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
69 static void Key(unsigned char key, int x, int y)
72 switch (key) {
73 case 27:
74 exit(1);
75 default:
76 break;
79 glutPostRedisplay();
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);
94 CheckError();
96 glClearColor(0.5, 0.5, 1.0, 0.0);
97 glClear(GL_COLOR_BUFFER_BIT);
98 CheckError();
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();
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);
116 CheckError();
118 /* draw to window */
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);
130 free(buffer);
133 /* Bind normal framebuffer */
134 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
135 glViewport(0, 0, Width, Height);
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();
148 if (doubleBuffer)
149 glutSwapBuffers();
150 else
151 glFinish();
153 CheckError();
157 static GLenum Args(int argc, char **argv)
159 GLint i;
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;
168 } else {
169 fprintf(stderr, "%s (Bad option).\n", argv[i]);
170 return GL_FALSE;
173 return GL_TRUE;
179 main( int argc, char *argv[] )
181 GLenum type;
183 glutInit(&argc, argv);
185 if (Args(argc, argv) == GL_FALSE) {
186 exit(1);
189 glutInitWindowPosition(100, 0); glutInitWindowSize( Width, Height );
191 type = GLUT_RGB;
192 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
193 glutInitDisplayMode(type);
195 if (glutCreateWindow(argv[0]) == GL_FALSE) {
196 exit(1);
199 glewInit();
201 Init();
203 glutReshapeFunc(Reshape);
204 glutKeyboardFunc(Key);
205 glutDisplayFunc(Draw);
206 glutMainLoop();
207 return 0;