Merge commit 'origin/gallium-0.1'
[mesa.git] / progs / glsl / multitex.c
1 /**
2 * Test multi-texturing with GL shading language.
3 *
4 * Copyright (C) 2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25
26 #include <assert.h>
27 #include <math.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "GL/glut.h"
32 #include "readtex.h"
33 #include "extfuncs.h"
34 #include "shaderutil.h"
35
36 static const char *Demo = "multitex";
37
38 static const char *VertFile = "multitex.vert";
39 static const char *FragFile = "multitex.frag";
40
41 static const char *TexFiles[2] =
42 {
43 "../images/tile.rgb",
44 "../images/tree2.rgba"
45 };
46
47
48 static GLuint Program;
49
50 static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0;
51 static GLfloat EyeDist = 10;
52 static GLboolean Anim = GL_TRUE;
53 static GLboolean UseArrays = GL_TRUE;
54
55 static GLint VertCoord_attr = -1, TexCoord0_attr = -1, TexCoord1_attr = -1;
56
57
58 /* value[0] = tex unit */
59 static struct uniform_info Uniforms[] = {
60 { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
61 { "tex2", 1, GL_INT, { 1, 0, 0, 0 }, -1 },
62 END_OF_UNIFORMS
63 };
64
65
66 static const GLfloat Tex0Coords[4][2] = {
67 { 0.0, 0.0 }, { 2.0, 0.0 }, { 2.0, 2.0 }, { 0.0, 2.0 }
68 };
69
70 static const GLfloat Tex1Coords[4][2] = {
71 { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 }
72 };
73
74 static const GLfloat VertCoords[4][2] = {
75 { -3.0, -3.0 }, { 3.0, -3.0 }, { 3.0, 3.0 }, { -3.0, 3.0 }
76 };
77
78
79 static void
80 DrawPolygonArray(void)
81 {
82 if (VertCoord_attr >= 0) {
83 glVertexAttribPointer_func(VertCoord_attr, 2, GL_FLOAT, GL_FALSE,
84 0, VertCoords);
85 glEnableVertexAttribArray_func(VertCoord_attr);
86 }
87 else {
88 glVertexPointer(2, GL_FLOAT, 0, VertCoords);
89 glEnable(GL_VERTEX_ARRAY);
90 }
91
92 glVertexAttribPointer_func(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE,
93 0, Tex0Coords);
94 glEnableVertexAttribArray_func(TexCoord0_attr);
95
96 glVertexAttribPointer_func(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE,
97 0, Tex1Coords);
98 glEnableVertexAttribArray_func(TexCoord1_attr);
99
100 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
101 }
102
103
104 static void
105 DrawPolygonVert(void)
106 {
107 GLuint i;
108
109 glBegin(GL_TRIANGLE_FAN);
110
111 for (i = 0; i < 4; i++) {
112 glVertexAttrib2fv_func(TexCoord0_attr, Tex0Coords[i]);
113 glVertexAttrib2fv_func(TexCoord1_attr, Tex1Coords[i]);
114
115 if (VertCoord_attr >= 0)
116 glVertexAttrib2fv_func(VertCoord_attr, VertCoords[i]);
117 else
118 glVertex2fv(VertCoords[i]);
119 }
120
121 glEnd();
122 }
123
124
125 static void
126 draw(void)
127 {
128 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
129
130 glPushMatrix(); /* modelview matrix */
131 glTranslatef(0.0, 0.0, -EyeDist);
132 glRotatef(Zrot, 0, 0, 1);
133 glRotatef(Yrot, 0, 1, 0);
134 glRotatef(Xrot, 1, 0, 0);
135
136 if (UseArrays)
137 DrawPolygonArray();
138 else
139 DrawPolygonVert();
140
141 glPopMatrix();
142
143 glutSwapBuffers();
144 }
145
146
147 static void
148 idle(void)
149 {
150 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
151 Yrot = t;
152 glutPostRedisplay();
153 }
154
155
156 static void
157 key(unsigned char k, int x, int y)
158 {
159 (void) x;
160 (void) y;
161 switch (k) {
162 case 'a':
163 UseArrays = !UseArrays;
164 printf("Arrays: %d\n", UseArrays);
165 break;
166 case ' ':
167 Anim = !Anim;
168 if (Anim)
169 glutIdleFunc(idle);
170 else
171 glutIdleFunc(NULL);
172 break;
173 case 'z':
174 EyeDist -= 0.5;
175 if (EyeDist < 3.0)
176 EyeDist = 3.0;
177 break;
178 case 'Z':
179 EyeDist += 0.5;
180 if (EyeDist > 90.0)
181 EyeDist = 90;
182 break;
183 case 27:
184 exit(0);
185 }
186 glutPostRedisplay();
187 }
188
189
190 static void
191 specialkey(int key, int x, int y)
192 {
193 GLfloat step = 2.0;
194 (void) x;
195 (void) y;
196 switch (key) {
197 case GLUT_KEY_UP:
198 Xrot += step;
199 break;
200 case GLUT_KEY_DOWN:
201 Xrot -= step;
202 break;
203 case GLUT_KEY_LEFT:
204 Yrot -= step;
205 break;
206 case GLUT_KEY_RIGHT:
207 Yrot += step;
208 break;
209 }
210 glutPostRedisplay();
211 }
212
213
214 /* new window size or exposure */
215 static void
216 Reshape(int width, int height)
217 {
218 GLfloat ar = (float) width / (float) height;
219 glViewport(0, 0, (GLint)width, (GLint)height);
220 glMatrixMode(GL_PROJECTION);
221 glLoadIdentity();
222 glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
223 glMatrixMode(GL_MODELVIEW);
224 glLoadIdentity();
225 }
226
227
228 static void
229 InitTextures(void)
230 {
231 GLenum filter = GL_LINEAR;
232 int i;
233
234 for (i = 0; i < 2; i++) {
235 GLint imgWidth, imgHeight;
236 GLenum imgFormat;
237 GLubyte *image = NULL;
238
239 image = LoadRGBImage(TexFiles[i], &imgWidth, &imgHeight, &imgFormat);
240 if (!image) {
241 printf("Couldn't read %s\n", TexFiles[i]);
242 exit(0);
243 }
244
245 glActiveTexture(GL_TEXTURE0 + i);
246 glBindTexture(GL_TEXTURE_2D, 42 + i);
247 gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imgWidth, imgHeight,
248 imgFormat, GL_UNSIGNED_BYTE, image);
249 free(image);
250
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
253 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
255 }
256 }
257
258
259 static GLuint
260 CreateProgram(const char *vertProgFile, const char *fragProgFile,
261 struct uniform_info *uniforms)
262 {
263 GLuint fragShader, vertShader, program;
264
265 vertShader = CompileShaderFile(GL_VERTEX_SHADER, vertProgFile);
266 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, fragProgFile);
267 assert(vertShader);
268 program = LinkShaders(vertShader, fragShader);
269
270 glUseProgram_func(program);
271
272 InitUniforms(program, uniforms);
273
274 TexCoord0_attr = glGetAttribLocation_func(program, "TexCoord0");
275 TexCoord1_attr = glGetAttribLocation_func(program, "TexCoord1");
276 VertCoord_attr = glGetAttribLocation_func(program, "VertCoord");
277 printf("TexCoord0_attr = %d\n", TexCoord0_attr);
278 printf("TexCoord1_attr = %d\n", TexCoord1_attr);
279 printf("VertCoord_attr = %d\n", VertCoord_attr);
280
281 return program;
282 }
283
284
285 static void
286 InitPrograms(void)
287 {
288 Program = CreateProgram(VertFile, FragFile, Uniforms);
289 }
290
291
292 static void
293 InitGL(void)
294 {
295 const char *version = (const char *) glGetString(GL_VERSION);
296
297 if (version[0] != '2' || version[1] != '.') {
298 printf("Warning: this program expects OpenGL 2.0\n");
299 /*exit(1);*/
300 }
301 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
302
303 GetExtensionFuncs();
304
305 InitTextures();
306 InitPrograms();
307
308 glEnable(GL_DEPTH_TEST);
309
310 glClearColor(.6, .6, .9, 0);
311 glColor3f(1.0, 1.0, 1.0);
312 }
313
314
315 int
316 main(int argc, char *argv[])
317 {
318 glutInit(&argc, argv);
319 glutInitWindowSize(500, 400);
320 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
321 glutCreateWindow(Demo);
322 glutReshapeFunc(Reshape);
323 glutKeyboardFunc(key);
324 glutSpecialFunc(specialkey);
325 glutDisplayFunc(draw);
326 if (Anim)
327 glutIdleFunc(idle);
328 InitGL();
329 glutMainLoop();
330 return 0;
331 }