Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / progs / trivial / tri-fbo-tex-mip.c
1 /* Framebuffer object test */
2
3
4 #include <GL/glew.h>
5 #include <GL/glut.h>
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <math.h>
11
12 /* For debug */
13
14
15 static int Win = 0;
16 static int Width = 512, Height = 512;
17
18 static GLenum TexTarget = GL_TEXTURE_2D;
19 static int TexWidth = 512, TexHeight = 512;
20
21 static GLuint MyFB;
22 static GLuint TexObj;
23 static GLboolean Anim = GL_FALSE;
24 static GLfloat Rot = 0.0;
25 static GLuint TextureLevel = 4; /* which texture level to render to */
26 static GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */
27
28
29 static void
30 CheckError(int line)
31 {
32 GLenum err = glGetError();
33 if (err) {
34 printf("GL Error 0x%x at line %d\n", (int) err, line);
35 }
36 }
37
38
39 static void
40 Idle(void)
41 {
42 Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
43 glutPostRedisplay();
44 }
45
46
47 static void
48 RenderTexture(void)
49 {
50 GLenum status;
51
52 glMatrixMode(GL_PROJECTION);
53 glLoadIdentity();
54 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
55 glMatrixMode(GL_MODELVIEW);
56 glLoadIdentity();
57 glTranslatef(0.0, 0.0, -15.0);
58
59 if (1) {
60 /* draw to texture image */
61 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
62
63 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
64 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
65 printf("Framebuffer incomplete!!!\n");
66 }
67
68 glViewport(0, 0,
69 TexWidth / (1 << TextureLevel),
70 TexHeight / (1 << TextureLevel));
71 glClearColor(0.5, 0.5, 1.0, 0.0);
72 glClear(GL_COLOR_BUFFER_BIT);
73
74 CheckError(__LINE__);
75
76 glBegin(GL_POLYGON);
77 glColor3f(1, 0, 0);
78 glVertex2f(-1, -1);
79 glColor3f(0, 1, 0);
80 glVertex2f(1, -1);
81 glColor3f(0, 0, 1);
82 glVertex2f(0, 1);
83 glEnd();
84
85 /* Bind normal framebuffer */
86 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
87 }
88 else {
89 }
90
91 CheckError(__LINE__);
92 }
93
94
95
96 static void
97 Display(void)
98 {
99 float ar = (float) Width / (float) Height;
100
101 RenderTexture();
102
103 /* draw textured quad in the window */
104 glMatrixMode(GL_PROJECTION);
105 glLoadIdentity();
106 glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
107 glMatrixMode(GL_MODELVIEW);
108 glLoadIdentity();
109 glTranslatef(0.0, 0.0, -7.0);
110
111 glViewport(0, 0, Width, Height);
112
113 glClearColor(0.25, 0.25, 0.25, 0);
114 glClear(GL_COLOR_BUFFER_BIT);
115
116 glPushMatrix();
117 glRotatef(Rot, 0, 1, 0);
118 glEnable(TexTarget);
119 glBindTexture(TexTarget, TexObj);
120
121 {
122 glBegin(GL_POLYGON);
123 glColor3f(0.25, 0.25, 0.25);
124 glTexCoord2f(0, 0);
125 glVertex2f(-1, -1);
126 glTexCoord2f(1, 0);
127 glVertex2f(1, -1);
128 glColor3f(1.0, 1.0, 1.0);
129 glTexCoord2f(1, 1);
130 glVertex2f(1, 1);
131 glTexCoord2f(0, 1);
132 glVertex2f(-1, 1);
133 glEnd();
134 }
135
136 glPopMatrix();
137 glDisable(TexTarget);
138
139 glutSwapBuffers();
140 CheckError(__LINE__);
141 }
142
143
144 static void
145 Reshape(int width, int height)
146 {
147 glViewport(0, 0, width, height);
148 Width = width;
149 Height = height;
150 }
151
152
153 static void
154 CleanUp(void)
155 {
156 glDeleteFramebuffersEXT(1, &MyFB);
157
158 glDeleteTextures(1, &TexObj);
159
160 glutDestroyWindow(Win);
161
162 exit(0);
163 }
164
165
166 static void
167 Key(unsigned char key, int x, int y)
168 {
169 (void) x;
170 (void) y;
171 switch (key) {
172 case 'a':
173 Anim = !Anim;
174 if (Anim)
175 glutIdleFunc(Idle);
176 else
177 glutIdleFunc(NULL);
178 break;
179 case 's':
180 Rot += 2.0;
181 break;
182 case 27:
183 CleanUp();
184 break;
185 }
186 glutPostRedisplay();
187 }
188
189
190 static void
191 Init(int argc, char *argv[])
192 {
193 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
194 printf("GL_EXT_framebuffer_object not found!\n");
195 exit(0);
196 }
197
198 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
199
200
201 /* Make texture object/image */
202 glGenTextures(1, &TexObj);
203 glBindTexture(TexTarget, TexObj);
204 glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
205 glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
206 glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel);
207 glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel);
208
209 glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
210 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
211 glTexImage2D(TexTarget, TextureLevel, TexIntFormat,
212 TexWidth / (1 << TextureLevel), TexHeight / (1 << TextureLevel), 0,
213 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
214
215
216 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
217
218
219
220
221 /* gen framebuffer id, delete it, do some assertions, just for testing */
222 glGenFramebuffersEXT(1, &MyFB);
223 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
224 assert(glIsFramebufferEXT(MyFB));
225
226
227 CheckError(__LINE__);
228
229 /* Render color to texture */
230 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
231 TexTarget, TexObj, TextureLevel);
232
233
234
235 CheckError(__LINE__);
236
237 /* bind regular framebuffer */
238 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
239
240
241 }
242
243
244 int
245 main(int argc, char *argv[])
246 {
247 glutInit(&argc, argv);
248 glutInitWindowPosition(0, 0);
249 glutInitWindowSize(Width, Height);
250 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
251 Win = glutCreateWindow(argv[0]);
252 glewInit();
253 glutReshapeFunc(Reshape);
254 glutKeyboardFunc(Key);
255 glutDisplayFunc(Display);
256 if (Anim)
257 glutIdleFunc(Idle);
258 Init(argc, argv);
259 glutMainLoop();
260 return 0;
261 }