Brian Paul | d04d209 | 2000-05-30 01:18:29 +0000 | [diff] [blame^] | 1 | /* $Id: cubemap.c,v 1.1 2000/05/30 01:18:29 brianp Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * GL_ARB_texture_cube_map demo |
| 5 | * |
| 6 | * Brian Paul |
| 7 | * May 2000 |
| 8 | * |
| 9 | * |
| 10 | * Copyright (C) 2000 Brian Paul All Rights Reserved. |
| 11 | * |
| 12 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 13 | * copy of this software and associated documentation files (the "Software"), |
| 14 | * to deal in the Software without restriction, including without limitation |
| 15 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 16 | * and/or sell copies of the Software, and to permit persons to whom the |
| 17 | * Software is furnished to do so, subject to the following conditions: |
| 18 | * |
| 19 | * The above copyright notice and this permission notice shall be included |
| 20 | * in all copies or substantial portions of the Software. |
| 21 | * |
| 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 23 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 25 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 26 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 27 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * This is a pretty minimalistic demo for now. Eventually, use some |
| 33 | * interesting cube map textures and 3D objects. |
| 34 | * For now, we use 6 checkerboard "walls" and a sphere (good for |
| 35 | * verification purposes). |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | #include <math.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include "GL/glut.h" |
| 43 | |
| 44 | static GLfloat Xrot = 0, Yrot = 0; |
| 45 | |
| 46 | |
| 47 | static void draw( void ) |
| 48 | { |
| 49 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 50 | |
| 51 | glMatrixMode(GL_TEXTURE); |
| 52 | glLoadIdentity(); |
| 53 | glRotatef(Xrot, 1, 0, 0); |
| 54 | glRotatef(Yrot, 0, 1, 0); |
| 55 | glutSolidSphere(2.0, 20, 20); |
| 56 | glMatrixMode(GL_MODELVIEW); |
| 57 | |
| 58 | glutSwapBuffers(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | static void idle(void) |
| 63 | { |
| 64 | Yrot += 5.0; |
| 65 | glutPostRedisplay(); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static void set_mode(GLuint mode) |
| 70 | { |
| 71 | if (mode == 0) { |
| 72 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB); |
| 73 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB); |
| 74 | glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB); |
| 75 | printf("GL_REFLECTION_MAP_ARB mode\n"); |
| 76 | } |
| 77 | else if (mode == 1) { |
| 78 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB); |
| 79 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB); |
| 80 | glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB); |
| 81 | printf("GL_NORMAL_MAP_ARB mode\n"); |
| 82 | } |
| 83 | glEnable(GL_TEXTURE_GEN_S); |
| 84 | glEnable(GL_TEXTURE_GEN_T); |
| 85 | glEnable(GL_TEXTURE_GEN_R); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | static void key(unsigned char k, int x, int y) |
| 90 | { |
| 91 | static GLboolean anim = GL_TRUE; |
| 92 | static GLuint mode = 0; |
| 93 | (void) x; |
| 94 | (void) y; |
| 95 | switch (k) { |
| 96 | case ' ': |
| 97 | anim = !anim; |
| 98 | if (anim) |
| 99 | glutIdleFunc(idle); |
| 100 | else |
| 101 | glutIdleFunc(NULL); |
| 102 | break; |
| 103 | case 'm': |
| 104 | mode = !mode; |
| 105 | set_mode(mode); |
| 106 | break; |
| 107 | case 27: |
| 108 | exit(0); |
| 109 | } |
| 110 | glutPostRedisplay(); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static void specialkey(int key, int x, int y) |
| 115 | { |
| 116 | GLfloat step = 10; |
| 117 | (void) x; |
| 118 | (void) y; |
| 119 | switch (key) { |
| 120 | case GLUT_KEY_UP: |
| 121 | Xrot -= step; |
| 122 | break; |
| 123 | case GLUT_KEY_DOWN: |
| 124 | Xrot += step; |
| 125 | break; |
| 126 | case GLUT_KEY_LEFT: |
| 127 | Yrot -= step; |
| 128 | break; |
| 129 | case GLUT_KEY_RIGHT: |
| 130 | Yrot += step; |
| 131 | break; |
| 132 | } |
| 133 | glutPostRedisplay(); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /* new window size or exposure */ |
| 138 | static void reshape(int width, int height) |
| 139 | { |
| 140 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 141 | glMatrixMode(GL_PROJECTION); |
| 142 | glLoadIdentity(); |
| 143 | glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 ); |
| 144 | glMatrixMode(GL_MODELVIEW); |
| 145 | glLoadIdentity(); |
| 146 | glTranslatef( 0.0, 0.0, -8.0 ); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | static void init( void ) |
| 151 | { |
| 152 | #define CUBE_TEX_SIZE 64 |
| 153 | GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3]; |
| 154 | static const GLubyte colors[6][3] = { |
| 155 | { 255, 0, 0 }, |
| 156 | { 0, 255, 255 }, |
| 157 | { 0, 255, 0 }, |
| 158 | { 255, 0, 255 }, |
| 159 | { 0, 0, 255 }, |
| 160 | { 255, 255, 0 } |
| 161 | }; |
| 162 | static const GLenum targets[6] = { |
| 163 | GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, |
| 164 | GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, |
| 165 | GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, |
| 166 | GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, |
| 167 | GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, |
| 168 | GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB |
| 169 | }; |
| 170 | |
| 171 | GLint i, j, f; |
| 172 | |
| 173 | /* check for extension */ |
| 174 | { |
| 175 | char *exten = (char *) glGetString(GL_EXTENSIONS); |
| 176 | if (!strstr(exten, "GL_ARB_texture_cube_map")) { |
| 177 | printf("Sorry, this demo requires GL_ARB_texture_cube_map\n"); |
| 178 | exit(0); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 184 | |
| 185 | /* make colored checkerboard cube faces */ |
| 186 | for (f = 0; f < 6; f++) { |
| 187 | for (i = 0; i < CUBE_TEX_SIZE; i++) { |
| 188 | for (j = 0; j < CUBE_TEX_SIZE; j++) { |
| 189 | if ((i/4 + j/4) & 1) { |
| 190 | image[i][j][0] = colors[f][0]; |
| 191 | image[i][j][1] = colors[f][1]; |
| 192 | image[i][j][2] = colors[f][2]; |
| 193 | } |
| 194 | else { |
| 195 | image[i][j][0] = 255; |
| 196 | image[i][j][1] = 255; |
| 197 | image[i][j][2] = 255; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0, |
| 203 | GL_RGB, GL_UNSIGNED_BYTE, image); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 208 | glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 209 | */ |
| 210 | glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 211 | glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 212 | |
| 213 | glEnable(GL_TEXTURE_CUBE_MAP_ARB); |
| 214 | |
| 215 | glClearColor(.3, .3, .3, 0); |
| 216 | glColor3f( 1.0, 1.0, 1.0 ); |
| 217 | |
| 218 | set_mode(0); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | static void usage(void) |
| 223 | { |
| 224 | printf("keys:\n"); |
| 225 | printf(" SPACE - toggle animation\n"); |
| 226 | printf(" CURSOR KEYS - rotation\n"); |
| 227 | printf(" m - toggle texgen reflection mode\n"); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | int main( int argc, char *argv[] ) |
| 232 | { |
| 233 | glutInitWindowPosition(0, 0); |
| 234 | glutInitWindowSize(300, 300); |
| 235 | glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE ); |
| 236 | glutCreateWindow("Texture Cube Maping"); |
| 237 | init(); |
| 238 | glutReshapeFunc( reshape ); |
| 239 | glutKeyboardFunc( key ); |
| 240 | glutSpecialFunc( specialkey ); |
| 241 | glutIdleFunc( idle ); |
| 242 | glutDisplayFunc( draw ); |
| 243 | usage(); |
| 244 | glutMainLoop(); |
| 245 | return 0; |
| 246 | } |