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