blob: 56e7ea1457a4bc874e5594e1d43aa346c258849b [file] [log] [blame]
Brian Paul02e8a032000-06-27 17:04:43 +00001/* $Id: cubemap.c,v 1.3 2000/06/27 17:04:43 brianp Exp $ */
Brian Pauld04d2092000-05-30 01:18:29 +00002
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>
Brian Paul02e8a032000-06-27 17:04:43 +000040#include <stdio.h>
Brian Pauld04d2092000-05-30 01:18:29 +000041#include <stdlib.h>
42#include <string.h>
43#include "GL/glut.h"
44
45static GLfloat Xrot = 0, Yrot = 0;
46
47
48static void draw( void )
49{
50 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
51
52 glMatrixMode(GL_TEXTURE);
53 glLoadIdentity();
54 glRotatef(Xrot, 1, 0, 0);
55 glRotatef(Yrot, 0, 1, 0);
56 glutSolidSphere(2.0, 20, 20);
57 glMatrixMode(GL_MODELVIEW);
58
59 glutSwapBuffers();
60}
61
62
63static void idle(void)
64{
65 Yrot += 5.0;
66 glutPostRedisplay();
67}
68
69
70static void set_mode(GLuint mode)
71{
72 if (mode == 0) {
73 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
74 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
75 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
76 printf("GL_REFLECTION_MAP_ARB mode\n");
77 }
78 else if (mode == 1) {
79 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
80 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
81 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_ARB);
82 printf("GL_NORMAL_MAP_ARB mode\n");
83 }
84 glEnable(GL_TEXTURE_GEN_S);
85 glEnable(GL_TEXTURE_GEN_T);
86 glEnable(GL_TEXTURE_GEN_R);
87}
88
89
90static void key(unsigned char k, int x, int y)
91{
92 static GLboolean anim = GL_TRUE;
93 static GLuint mode = 0;
94 (void) x;
95 (void) y;
96 switch (k) {
97 case ' ':
98 anim = !anim;
99 if (anim)
100 glutIdleFunc(idle);
101 else
102 glutIdleFunc(NULL);
103 break;
104 case 'm':
105 mode = !mode;
106 set_mode(mode);
107 break;
108 case 27:
109 exit(0);
110 }
111 glutPostRedisplay();
112}
113
114
115static void specialkey(int key, int x, int y)
116{
117 GLfloat step = 10;
118 (void) x;
119 (void) y;
120 switch (key) {
121 case GLUT_KEY_UP:
122 Xrot -= step;
123 break;
124 case GLUT_KEY_DOWN:
125 Xrot += step;
126 break;
127 case GLUT_KEY_LEFT:
128 Yrot -= step;
129 break;
130 case GLUT_KEY_RIGHT:
131 Yrot += step;
132 break;
133 }
134 glutPostRedisplay();
135}
136
137
138/* new window size or exposure */
139static void reshape(int width, int height)
140{
141 glViewport(0, 0, (GLint)width, (GLint)height);
142 glMatrixMode(GL_PROJECTION);
143 glLoadIdentity();
144 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
145 glMatrixMode(GL_MODELVIEW);
146 glLoadIdentity();
147 glTranslatef( 0.0, 0.0, -8.0 );
148}
149
150
151static void init( void )
152{
153#define CUBE_TEX_SIZE 64
154 GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3];
155 static const GLubyte colors[6][3] = {
156 { 255, 0, 0 },
157 { 0, 255, 255 },
158 { 0, 255, 0 },
159 { 255, 0, 255 },
160 { 0, 0, 255 },
161 { 255, 255, 0 }
162 };
163 static const GLenum targets[6] = {
164 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
165 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
166 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
167 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
168 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
169 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
170 };
171
172 GLint i, j, f;
173
174 /* check for extension */
175 {
176 char *exten = (char *) glGetString(GL_EXTENSIONS);
177 if (!strstr(exten, "GL_ARB_texture_cube_map")) {
178 printf("Sorry, this demo requires GL_ARB_texture_cube_map\n");
179 exit(0);
180 }
181 }
182
183
184 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
185
186 /* make colored checkerboard cube faces */
187 for (f = 0; f < 6; f++) {
188 for (i = 0; i < CUBE_TEX_SIZE; i++) {
189 for (j = 0; j < CUBE_TEX_SIZE; j++) {
190 if ((i/4 + j/4) & 1) {
191 image[i][j][0] = colors[f][0];
192 image[i][j][1] = colors[f][1];
193 image[i][j][2] = colors[f][2];
194 }
195 else {
196 image[i][j][0] = 255;
197 image[i][j][1] = 255;
198 image[i][j][2] = 255;
199 }
200 }
201 }
202
203 glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
204 GL_RGB, GL_UNSIGNED_BYTE, image);
205 }
206
Brian Pauld27e2562000-06-13 18:45:54 +0000207#if 1
Brian Pauld04d2092000-05-30 01:18:29 +0000208 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
209 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Brian Pauld27e2562000-06-13 18:45:54 +0000210#else
211 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
212 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
213#endif
214 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
215 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Brian Pauld04d2092000-05-30 01:18:29 +0000216
217 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
218
219 glClearColor(.3, .3, .3, 0);
220 glColor3f( 1.0, 1.0, 1.0 );
221
222 set_mode(0);
223}
224
225
226static void usage(void)
227{
228 printf("keys:\n");
229 printf(" SPACE - toggle animation\n");
230 printf(" CURSOR KEYS - rotation\n");
231 printf(" m - toggle texgen reflection mode\n");
232}
233
234
235int main( int argc, char *argv[] )
236{
237 glutInitWindowPosition(0, 0);
238 glutInitWindowSize(300, 300);
239 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
240 glutCreateWindow("Texture Cube Maping");
241 init();
242 glutReshapeFunc( reshape );
243 glutKeyboardFunc( key );
244 glutSpecialFunc( specialkey );
245 glutIdleFunc( idle );
246 glutDisplayFunc( draw );
247 usage();
248 glutMainLoop();
249 return 0;
250}