Merge branch '7.8'
[mesa.git] / progs / redbook / cubemap.c
1 /*
2 * Copyright (c) 1993-2003, Silicon Graphics, Inc.
3 * All Rights Reserved
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose and without fee is hereby granted, provided that the above
7 * copyright notice appear in all copies and that both the copyright
8 * notice and this permission notice appear in supporting documentation,
9 * and that the name of Silicon Graphics, Inc. not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission.
12 *
13 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
14 * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
16 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
17 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
18 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
20 * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD
21 * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF
22 * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE
24 * OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * US Government Users Restricted Rights
27 * Use, duplication, or disclosure by the Government is subject to
28 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
29 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
30 * clause at DFARS 252.227-7013 and/or in similar or successor clauses
31 * in the FAR or the DOD or NASA FAR Supplement. Unpublished - rights
32 * reserved under the copyright laws of the United States.
33 *
34 * Contractor/manufacturer is:
35 * Silicon Graphics, Inc.
36 * 1500 Crittenden Lane
37 * Mountain View, CA 94043
38 * United State of America
39 *
40 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
41 */
42
43 /* cubemap.c
44 *
45 * This program demonstrates cube map textures.
46 * Six different colored checker board textures are
47 * created and applied to a lit sphere.
48 *
49 * Pressing the 'f' and 'b' keys translate the viewer
50 * forward and backward.
51 */
52
53 #include <GL/glut.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56
57 #define imageSize 4
58 static GLubyte image1[imageSize][imageSize][4];
59 static GLubyte image2[imageSize][imageSize][4];
60 static GLubyte image3[imageSize][imageSize][4];
61 static GLubyte image4[imageSize][imageSize][4];
62 static GLubyte image5[imageSize][imageSize][4];
63 static GLubyte image6[imageSize][imageSize][4];
64
65 static GLdouble ztrans = 0.0;
66
67 static void makeImages(void)
68 {
69 int i, j, c;
70
71 for (i = 0; i < imageSize; i++) {
72 for (j = 0; j < imageSize; j++) {
73 c = ( ((i & 0x1) == 0) ^ ((j & 0x1) == 0) ) * 255;
74 image1[i][j][0] = (GLubyte) c;
75 image1[i][j][1] = (GLubyte) c;
76 image1[i][j][2] = (GLubyte) c;
77 image1[i][j][3] = (GLubyte) 255;
78
79 image2[i][j][0] = (GLubyte) c;
80 image2[i][j][1] = (GLubyte) c;
81 image2[i][j][2] = (GLubyte) 0;
82 image2[i][j][3] = (GLubyte) 255;
83
84 image3[i][j][0] = (GLubyte) c;
85 image3[i][j][1] = (GLubyte) 0;
86 image3[i][j][2] = (GLubyte) c;
87 image3[i][j][3] = (GLubyte) 255;
88
89 image4[i][j][0] = (GLubyte) 0;
90 image4[i][j][1] = (GLubyte) c;
91 image4[i][j][2] = (GLubyte) c;
92 image4[i][j][3] = (GLubyte) 255;
93
94 image5[i][j][0] = (GLubyte) 255;
95 image5[i][j][1] = (GLubyte) c;
96 image5[i][j][2] = (GLubyte) c;
97 image5[i][j][3] = (GLubyte) 255;
98
99 image6[i][j][0] = (GLubyte) c;
100 image6[i][j][1] = (GLubyte) c;
101 image6[i][j][2] = (GLubyte) 255;
102 image6[i][j][3] = (GLubyte) 255;
103 }
104 }
105 }
106
107 static void init(void)
108 {
109 GLfloat diffuse[4] = {1.0, 1.0, 1.0, 1.0};
110
111 glClearColor (0.0, 0.0, 0.0, 0.0);
112 glEnable(GL_DEPTH_TEST);
113 glShadeModel(GL_SMOOTH);
114
115 makeImages();
116 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
117 glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT);
118 glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT);
119 glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_R, GL_REPEAT);
120 glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
121 glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
122 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT, 0, GL_RGBA, imageSize,
123 imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1);
124 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT, 0, GL_RGBA, imageSize,
125 imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image4);
126 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT, 0, GL_RGBA, imageSize,
127 imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image2);
128 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT, 0, GL_RGBA, imageSize,
129 imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image5);
130 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT, 0, GL_RGBA, imageSize,
131 imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image3);
132 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT, 0, GL_RGBA, imageSize,
133 imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image6);
134 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_EXT);
135 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_EXT);
136 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_EXT);
137 glEnable(GL_TEXTURE_GEN_S);
138 glEnable(GL_TEXTURE_GEN_T);
139 glEnable(GL_TEXTURE_GEN_R);
140
141 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
142
143 glEnable(GL_TEXTURE_CUBE_MAP_EXT);
144 glEnable(GL_LIGHTING);
145 glEnable(GL_LIGHT0);
146 glEnable(GL_AUTO_NORMAL);
147 glEnable(GL_NORMALIZE);
148 glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);
149 }
150
151 static void display(void)
152 {
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
154
155 glPushMatrix ();
156 glTranslatef (0.0, 0.0, ztrans);
157 glutSolidSphere (5.0, 20, 10);
158 glPopMatrix ();
159 glutSwapBuffers();
160 }
161
162 static void reshape(int w, int h)
163 {
164 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
165 glMatrixMode(GL_PROJECTION);
166 glLoadIdentity();
167 gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 300.0);
168 glMatrixMode(GL_MODELVIEW);
169 glLoadIdentity();
170 glTranslatef(0.0, 0.0, -20.0);
171 }
172
173 static void keyboard (unsigned char key, int x, int y)
174 {
175 switch (key) {
176 case 'f':
177 ztrans = ztrans - 0.2;
178 glutPostRedisplay();
179 break;
180 case 'b':
181 ztrans = ztrans + 0.2;
182 glutPostRedisplay();
183 break;
184 case 27:
185 exit(0);
186 break;
187 default:
188 break;
189 }
190 }
191
192 int main(int argc, char** argv)
193 {
194 glutInit(&argc, argv);
195 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
196 glutInitWindowSize(400, 400);
197 glutInitWindowPosition(100, 100);
198 glutCreateWindow (argv[0]);
199 init ();
200 glutDisplayFunc(display);
201 glutReshapeFunc(reshape);
202 glutKeyboardFunc(keyboard);
203 glutMainLoop();
204 return 0;
205 }