gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / trivial / tri-fbo-tex-mip.c
blob2e8fb74a00bb575319a2abf00af404c189d1f73d
1 /* Framebuffer object test */
4 #include <GL/glew.h>
5 #include <GL/glut.h>
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 /* For debug */
13 static int Win = 0;
14 static int Width = 512, Height = 512;
16 static GLenum TexTarget = GL_TEXTURE_2D;
17 static int TexWidth = 512, TexHeight = 512;
19 static GLuint MyFB;
20 static GLuint TexObj;
21 static GLboolean Anim = GL_FALSE;
22 static GLfloat Rot = 0.0;
23 static GLuint TextureLevel = 4; /* which texture level to render to */
24 static GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */
27 static void
28 CheckError(int line)
30 GLenum err = glGetError();
31 if (err) {
32 printf("GL Error 0x%x at line %d\n", (int) err, line);
37 static void
38 Idle(void)
40 Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
41 glutPostRedisplay();
45 static void
46 RenderTexture(void)
48 GLenum status;
50 glMatrixMode(GL_PROJECTION);
51 glLoadIdentity();
52 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
53 glMatrixMode(GL_MODELVIEW);
54 glLoadIdentity();
55 glTranslatef(0.0, 0.0, -15.0);
57 if (1) {
58 /* draw to texture image */
59 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
61 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
62 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
63 printf("Framebuffer incomplete!!!\n");
66 glViewport(0, 0,
67 TexWidth / (1 << TextureLevel),
68 TexHeight / (1 << TextureLevel));
69 glClearColor(0.5, 0.5, 1.0, 0.0);
70 glClear(GL_COLOR_BUFFER_BIT);
72 CheckError(__LINE__);
74 glBegin(GL_POLYGON);
75 glColor3f(1, 0, 0);
76 glVertex2f(-1, -1);
77 glColor3f(0, 1, 0);
78 glVertex2f(1, -1);
79 glColor3f(0, 0, 1);
80 glVertex2f(0, 1);
81 glEnd();
83 /* Bind normal framebuffer */
84 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
86 else {
89 CheckError(__LINE__);
94 static void
95 Display(void)
97 float ar = (float) Width / (float) Height;
99 RenderTexture();
101 /* draw textured quad in the window */
102 glMatrixMode(GL_PROJECTION);
103 glLoadIdentity();
104 glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
105 glMatrixMode(GL_MODELVIEW);
106 glLoadIdentity();
107 glTranslatef(0.0, 0.0, -7.0);
109 glViewport(0, 0, Width, Height);
111 glClearColor(0.25, 0.25, 0.25, 0);
112 glClear(GL_COLOR_BUFFER_BIT);
114 glPushMatrix();
115 glRotatef(Rot, 0, 1, 0);
116 glEnable(TexTarget);
117 glBindTexture(TexTarget, TexObj);
120 glBegin(GL_POLYGON);
121 glColor3f(0.25, 0.25, 0.25);
122 glTexCoord2f(0, 0);
123 glVertex2f(-1, -1);
124 glTexCoord2f(1, 0);
125 glVertex2f(1, -1);
126 glColor3f(1.0, 1.0, 1.0);
127 glTexCoord2f(1, 1);
128 glVertex2f(1, 1);
129 glTexCoord2f(0, 1);
130 glVertex2f(-1, 1);
131 glEnd();
134 glPopMatrix();
135 glDisable(TexTarget);
137 glutSwapBuffers();
138 CheckError(__LINE__);
142 static void
143 Reshape(int width, int height)
145 glViewport(0, 0, width, height);
146 Width = width;
147 Height = height;
151 static void
152 CleanUp(void)
154 glDeleteFramebuffersEXT(1, &MyFB);
156 glDeleteTextures(1, &TexObj);
158 glutDestroyWindow(Win);
160 exit(0);
164 static void
165 Key(unsigned char key, int x, int y)
167 (void) x;
168 (void) y;
169 switch (key) {
170 case 'a':
171 Anim = !Anim;
172 if (Anim)
173 glutIdleFunc(Idle);
174 else
175 glutIdleFunc(NULL);
176 break;
177 case 's':
178 Rot += 2.0;
179 break;
180 case 27:
181 CleanUp();
182 break;
184 glutPostRedisplay();
188 static void
189 Init(int argc, char *argv[])
191 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
192 printf("GL_EXT_framebuffer_object not found!\n");
193 exit(0);
196 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
199 /* Make texture object/image */
200 glGenTextures(1, &TexObj);
201 glBindTexture(TexTarget, TexObj);
202 glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
203 glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
204 glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel);
205 glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel);
207 glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
208 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
209 glTexImage2D(TexTarget, TextureLevel, TexIntFormat,
210 TexWidth / (1 << TextureLevel), TexHeight / (1 << TextureLevel), 0,
211 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
214 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
219 /* gen framebuffer id, delete it, do some assertions, just for testing */
220 glGenFramebuffersEXT(1, &MyFB);
221 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
222 assert(glIsFramebufferEXT(MyFB));
225 CheckError(__LINE__);
227 /* Render color to texture */
228 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
229 TexTarget, TexObj, TextureLevel);
233 CheckError(__LINE__);
235 /* bind regular framebuffer */
236 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
243 main(int argc, char *argv[])
245 glutInit(&argc, argv);
246 glutInitWindowPosition(0, 0);
247 glutInitWindowSize(Width, Height);
248 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
249 Win = glutCreateWindow(argv[0]);
250 glewInit();
251 glutReshapeFunc(Reshape);
252 glutKeyboardFunc(Key);
253 glutDisplayFunc(Display);
254 if (Anim)
255 glutIdleFunc(Idle);
256 Init(argc, argv);
257 glutMainLoop();
258 return 0;