progs/glsl: compile with scons and glew
[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/glew.h>
32 #include "GL/glut.h"
33 #include "readtex.h"
34 #include "extfuncs.h"
35 #include "shaderutil.h"
36
37 static const char *Demo = "multitex";
38
39 static const char *VertFile = "multitex.vert";
40 static const char *FragFile = "multitex.frag";
41
42 static const char *TexFiles[2] =
43 {
44 "../images/tile.rgb",
45 "../images/tree2.rgba"
46 };
47
48
49 static GLuint Program;
50
51 static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0;
52 static GLfloat EyeDist = 10;
53 static GLboolean Anim = GL_TRUE;
54 static GLboolean UseArrays = GL_TRUE;
55
56 static GLint VertCoord_attr = -1, TexCoord0_attr = -1, TexCoord1_attr = -1;
57
58
59 /* value[0] = tex unit */
60 static struct uniform_info Uniforms[] = {
61 { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
62 { "tex2", 1, GL_INT, { 1, 0, 0, 0 }, -1 },
63 END_OF_UNIFORMS
64 };
65
66
67 static const GLfloat Tex0Coords[4][2] = {
68 { 0.0, 0.0 }, { 2.0, 0.0 }, { 2.0, 2.0 }, { 0.0, 2.0 }
69 };
70
71 static const GLfloat Tex1Coords[4][2] = {
72 { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 }
73 };
74
75 static const GLfloat VertCoords[4][2] = {
76 { -3.0, -3.0 }, { 3.0, -3.0 }, { 3.0, 3.0 }, { -3.0, 3.0 }
77 };
78
79
80 static void
81 DrawPolygonArray(void)
82 {
83 if (VertCoord_attr >= 0) {
84 glVertexAttribPointer_func(VertCoord_attr, 2, GL_FLOAT, GL_FALSE,
85 0, VertCoords);
86 glEnableVertexAttribArray_func(VertCoord_attr);
87 }
88 else {
89 glVertexPointer(2, GL_FLOAT, 0, VertCoords);
90 glEnable(GL_VERTEX_ARRAY);
91 }
92
93 glVertexAttribPointer_func(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE,
94 0, Tex0Coords);
95 glEnableVertexAttribArray_func(TexCoord0_attr);
96
97 glVertexAttribPointer_func(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE,
98 0, Tex1Coords);
99 glEnableVertexAttribArray_func(TexCoord1_attr);
100
101 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
102 }
103
104
105 static void
106 DrawPolygonVert(void)
107 {
108 GLuint i;
109
110 glBegin(GL_TRIANGLE_FAN);
111
112 for (i = 0; i < 4; i++) {
113 glVertexAttrib2fv_func(TexCoord0_attr, Tex0Coords[i]);
114 glVertexAttrib2fv_func(TexCoord1_attr, Tex1Coords[i]);
115
116 if (VertCoord_attr >= 0)
117 glVertexAttrib2fv_func(VertCoord_attr, VertCoords[i]);
118 else
119 glVertex2fv(VertCoords[i]);
120 }
121
122 glEnd();
123 }
124
125
126 static void
127 draw(void)
128 {
129 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
130
131 glPushMatrix(); /* modelview matrix */
132 glTranslatef(0.0, 0.0, -EyeDist);
133 glRotatef(Zrot, 0, 0, 1);
134 glRotatef(Yrot, 0, 1, 0);
135 glRotatef(Xrot, 1, 0, 0);
136
137 if (UseArrays)
138 DrawPolygonArray();
139 else
140 DrawPolygonVert();
141
142 glPopMatrix();
143
144 glutSwapBuffers();
145 }
146
147
148 static void
149 idle(void)
150 {
151 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
152 Yrot = t;
153 glutPostRedisplay();
154 }
155
156
157 static void
158 key(unsigned char k, int x, int y)
159 {
160 (void) x;
161 (void) y;
162 switch (k) {
163 case 'a':
164 UseArrays = !UseArrays;
165 printf("Arrays: %d\n", UseArrays);
166 break;
167 case ' ':
168 Anim = !Anim;
169 if (Anim)
170 glutIdleFunc(idle);
171 else
172 glutIdleFunc(NULL);
173 break;
174 case 'z':
175 EyeDist -= 0.5;
176 if (EyeDist < 3.0)
177 EyeDist = 3.0;
178 break;
179 case 'Z':
180 EyeDist += 0.5;
181 if (EyeDist > 90.0)
182 EyeDist = 90;
183 break;
184 case 27:
185 exit(0);
186 }
187 glutPostRedisplay();
188 }
189
190
191 static void
192 specialkey(int key, int x, int y)
193 {
194 GLfloat step = 2.0;
195 (void) x;
196 (void) y;
197 switch (key) {
198 case GLUT_KEY_UP:
199 Xrot += step;
200 break;
201 case GLUT_KEY_DOWN:
202 Xrot -= step;
203 break;
204 case GLUT_KEY_LEFT:
205 Yrot -= step;
206 break;
207 case GLUT_KEY_RIGHT:
208 Yrot += step;
209 break;
210 }
211 glutPostRedisplay();
212 }
213
214
215 /* new window size or exposure */
216 static void
217 Reshape(int width, int height)
218 {
219 GLfloat ar = (float) width / (float) height;
220 glViewport(0, 0, (GLint)width, (GLint)height);
221 glMatrixMode(GL_PROJECTION);
222 glLoadIdentity();
223 glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
224 glMatrixMode(GL_MODELVIEW);
225 glLoadIdentity();
226 }
227
228
229 static void
230 InitTextures(void)
231 {
232 GLenum filter = GL_LINEAR;
233 int i;
234
235 for (i = 0; i < 2; i++) {
236 GLint imgWidth, imgHeight;
237 GLenum imgFormat;
238 GLubyte *image = NULL;
239
240 image = LoadRGBImage(TexFiles[i], &imgWidth, &imgHeight, &imgFormat);
241 if (!image) {
242 printf("Couldn't read %s\n", TexFiles[i]);
243 exit(0);
244 }
245
246 glActiveTexture(GL_TEXTURE0 + i);
247 glBindTexture(GL_TEXTURE_2D, 42 + i);
248 gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imgWidth, imgHeight,
249 imgFormat, GL_UNSIGNED_BYTE, image);
250 free(image);
251
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
253 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
255 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
256 }
257 }
258
259
260 static GLuint
261 CreateProgram(const char *vertProgFile, const char *fragProgFile,
262 struct uniform_info *uniforms)
263 {
264 GLuint fragShader, vertShader, program;
265
266 vertShader = CompileShaderFile(GL_VERTEX_SHADER, vertProgFile);
267 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, fragProgFile);
268 assert(vertShader);
269 program = LinkShaders(vertShader, fragShader);
270
271 glUseProgram_func(program);
272
273 InitUniforms(program, uniforms);
274
275 VertCoord_attr = glGetAttribLocation_func(program, "VertCoord");
276 if (VertCoord_attr > 0) {
277 /* We want the VertCoord attrib to have position zero so that
278 * the call to glVertexAttrib(0, xyz) triggers vertex processing.
279 * Otherwise, if TexCoord0 or TexCoord1 gets position 0 we'd have
280 * to set that attribute last (which is a PITA to manage).
281 */
282 glBindAttribLocation_func(program, 0, "VertCoord");
283 /* re-link */
284 glLinkProgram_func(program);
285 /* VertCoord_attr should be zero now */
286 VertCoord_attr = glGetAttribLocation_func(program, "VertCoord");
287 assert(VertCoord_attr == 0);
288 }
289
290 TexCoord0_attr = glGetAttribLocation_func(program, "TexCoord0");
291 TexCoord1_attr = glGetAttribLocation_func(program, "TexCoord1");
292
293 printf("TexCoord0_attr = %d\n", TexCoord0_attr);
294 printf("TexCoord1_attr = %d\n", TexCoord1_attr);
295 printf("VertCoord_attr = %d\n", VertCoord_attr);
296
297 return program;
298 }
299
300
301 static void
302 InitPrograms(void)
303 {
304 Program = CreateProgram(VertFile, FragFile, Uniforms);
305 }
306
307
308 static void
309 InitGL(void)
310 {
311 const char *version = (const char *) glGetString(GL_VERSION);
312
313 if (version[0] != '2' || version[1] != '.') {
314 printf("Warning: this program expects OpenGL 2.0\n");
315 /*exit(1);*/
316 }
317 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
318
319 GetExtensionFuncs();
320
321 InitTextures();
322 InitPrograms();
323
324 glEnable(GL_DEPTH_TEST);
325
326 glClearColor(.6, .6, .9, 0);
327 glColor3f(1.0, 1.0, 1.0);
328 }
329
330
331 int
332 main(int argc, char *argv[])
333 {
334 glutInit(&argc, argv);
335 glutInitWindowSize(500, 400);
336 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
337 glutCreateWindow(Demo);
338 glewInit();
339 glutReshapeFunc(Reshape);
340 glutKeyboardFunc(key);
341 glutSpecialFunc(specialkey);
342 glutDisplayFunc(draw);
343 if (Anim)
344 glutIdleFunc(idle);
345 InitGL();
346 glutMainLoop();
347 return 0;
348 }