Brian Paul | 4978953 | 2008-05-20 11:01:17 -0600 | [diff] [blame] | 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 | |
Brian Paul | c0dd912 | 2008-08-16 09:36:46 -0600 | [diff] [blame] | 38 | static const char *VertFile = "multitex.vert"; |
| 39 | static const char *FragFile = "multitex.frag"; |
Brian Paul | 4978953 | 2008-05-20 11:01:17 -0600 | [diff] [blame] | 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 = -90.0, Yrot = .0, Zrot = 0.0; |
| 51 | static GLfloat EyeDist = 10; |
| 52 | static GLboolean Anim = GL_TRUE; |
| 53 | |
| 54 | |
| 55 | /* value[0] = tex unit */ |
| 56 | static struct uniform_info Uniforms[] = { |
| 57 | { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 }, |
| 58 | { "tex2", 1, GL_INT, { 1, 0, 0, 0 }, -1 }, |
| 59 | END_OF_UNIFORMS |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | static void |
| 64 | DrawPolygon(GLfloat size) |
| 65 | { |
| 66 | glPushMatrix(); |
| 67 | glRotatef(90, 1, 0, 0); |
| 68 | glNormal3f(0, 0, 1); |
| 69 | glBegin(GL_POLYGON); |
| 70 | |
| 71 | glMultiTexCoord2f(GL_TEXTURE0, 0, 0); |
| 72 | glMultiTexCoord2f(GL_TEXTURE1, 0, 0); |
| 73 | glVertex2f(-size, -size); |
| 74 | |
| 75 | glMultiTexCoord2f(GL_TEXTURE0, 2, 0); |
| 76 | glMultiTexCoord2f(GL_TEXTURE1, 1, 0); |
| 77 | glVertex2f( size, -size); |
| 78 | |
| 79 | glMultiTexCoord2f(GL_TEXTURE0, 2, 2); |
| 80 | glMultiTexCoord2f(GL_TEXTURE1, 1, 1); |
| 81 | glVertex2f( size, size); |
| 82 | |
| 83 | glMultiTexCoord2f(GL_TEXTURE0, 0, 2); |
| 84 | glMultiTexCoord2f(GL_TEXTURE1, 0, 1); |
| 85 | glVertex2f(-size, size); |
| 86 | |
| 87 | glEnd(); |
| 88 | glPopMatrix(); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | static void |
| 93 | draw(void) |
| 94 | { |
| 95 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 96 | |
| 97 | glPushMatrix(); /* modelview matrix */ |
| 98 | glTranslatef(0.0, 0.0, -EyeDist); |
| 99 | glRotatef(Zrot, 0, 0, 1); |
| 100 | glRotatef(Yrot, 0, 1, 0); |
| 101 | glRotatef(Xrot, 1, 0, 0); |
| 102 | |
| 103 | DrawPolygon(3.0); |
| 104 | |
| 105 | glPopMatrix(); |
| 106 | |
| 107 | glutSwapBuffers(); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static void |
| 112 | idle(void) |
| 113 | { |
| 114 | GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME); |
| 115 | Yrot = t; |
| 116 | glutPostRedisplay(); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | static void |
| 121 | key(unsigned char k, int x, int y) |
| 122 | { |
| 123 | (void) x; |
| 124 | (void) y; |
| 125 | switch (k) { |
| 126 | case ' ': |
| 127 | case 'a': |
| 128 | Anim = !Anim; |
| 129 | if (Anim) |
| 130 | glutIdleFunc(idle); |
| 131 | else |
| 132 | glutIdleFunc(NULL); |
| 133 | break; |
| 134 | case 'z': |
| 135 | EyeDist -= 0.5; |
| 136 | if (EyeDist < 3.0) |
| 137 | EyeDist = 3.0; |
| 138 | break; |
| 139 | case 'Z': |
| 140 | EyeDist += 0.5; |
| 141 | if (EyeDist > 90.0) |
| 142 | EyeDist = 90; |
| 143 | break; |
| 144 | case 27: |
| 145 | exit(0); |
| 146 | } |
| 147 | glutPostRedisplay(); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | static void |
| 152 | specialkey(int key, int x, int y) |
| 153 | { |
| 154 | GLfloat step = 2.0; |
| 155 | (void) x; |
| 156 | (void) y; |
| 157 | switch (key) { |
| 158 | case GLUT_KEY_UP: |
| 159 | Xrot += step; |
| 160 | break; |
| 161 | case GLUT_KEY_DOWN: |
| 162 | Xrot -= step; |
| 163 | break; |
| 164 | case GLUT_KEY_LEFT: |
| 165 | Yrot -= step; |
| 166 | break; |
| 167 | case GLUT_KEY_RIGHT: |
| 168 | Yrot += step; |
| 169 | break; |
| 170 | } |
| 171 | glutPostRedisplay(); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /* new window size or exposure */ |
| 176 | static void |
| 177 | Reshape(int width, int height) |
| 178 | { |
| 179 | GLfloat ar = (float) width / (float) height; |
| 180 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 181 | glMatrixMode(GL_PROJECTION); |
| 182 | glLoadIdentity(); |
| 183 | glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0); |
| 184 | glMatrixMode(GL_MODELVIEW); |
| 185 | glLoadIdentity(); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | static void |
| 190 | InitTextures(void) |
| 191 | { |
| 192 | GLenum filter = GL_LINEAR; |
| 193 | int i; |
| 194 | |
| 195 | for (i = 0; i < 2; i++) { |
| 196 | GLint imgWidth, imgHeight; |
| 197 | GLenum imgFormat; |
| 198 | GLubyte *image = NULL; |
| 199 | |
| 200 | image = LoadRGBImage(TexFiles[i], &imgWidth, &imgHeight, &imgFormat); |
| 201 | if (!image) { |
| 202 | printf("Couldn't read %s\n", TexFiles[i]); |
| 203 | exit(0); |
| 204 | } |
| 205 | |
| 206 | glActiveTexture(GL_TEXTURE0 + i); |
| 207 | glBindTexture(GL_TEXTURE_2D, 42 + i); |
| 208 | gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imgWidth, imgHeight, |
| 209 | imgFormat, GL_UNSIGNED_BYTE, image); |
| 210 | free(image); |
| 211 | |
| 212 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 213 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 214 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); |
| 215 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
| 220 | static GLuint |
| 221 | CreateProgram(const char *vertProgFile, const char *fragProgFile, |
| 222 | struct uniform_info *uniforms) |
| 223 | { |
| 224 | GLuint fragShader, vertShader, program; |
| 225 | |
| 226 | vertShader = CompileShaderFile(GL_VERTEX_SHADER, vertProgFile); |
| 227 | fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, fragProgFile); |
| 228 | assert(vertShader); |
| 229 | program = LinkShaders(vertShader, fragShader); |
| 230 | |
| 231 | glUseProgram_func(program); |
| 232 | |
| 233 | InitUniforms(program, uniforms); |
| 234 | |
| 235 | return program; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | static void |
| 240 | InitPrograms(void) |
| 241 | { |
| 242 | Program = CreateProgram(VertFile, FragFile, Uniforms); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | static void |
| 247 | InitGL(void) |
| 248 | { |
| 249 | const char *version = (const char *) glGetString(GL_VERSION); |
| 250 | |
| 251 | if (version[0] != '2' || version[1] != '.') { |
| 252 | printf("Warning: this program expects OpenGL 2.0\n"); |
| 253 | /*exit(1);*/ |
| 254 | } |
| 255 | printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); |
| 256 | |
| 257 | GetExtensionFuncs(); |
| 258 | |
| 259 | InitTextures(); |
| 260 | InitPrograms(); |
| 261 | |
| 262 | glEnable(GL_DEPTH_TEST); |
| 263 | |
| 264 | glClearColor(.6, .6, .9, 0); |
| 265 | glColor3f(1.0, 1.0, 1.0); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | int |
| 270 | main(int argc, char *argv[]) |
| 271 | { |
| 272 | glutInit(&argc, argv); |
| 273 | glutInitWindowSize(500, 400); |
| 274 | glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); |
| 275 | glutCreateWindow(Demo); |
| 276 | glutReshapeFunc(Reshape); |
| 277 | glutKeyboardFunc(key); |
| 278 | glutSpecialFunc(specialkey); |
| 279 | glutDisplayFunc(draw); |
| 280 | if (Anim) |
| 281 | glutIdleFunc(idle); |
| 282 | InitGL(); |
| 283 | glutMainLoop(); |
| 284 | return 0; |
| 285 | } |