cubemap.c revision 32001f49
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/glew.h>
54#include "glut_wrap.h"
55#include <stdlib.h>
56#include <stdio.h>
57
58#define	imageSize 4
59static GLubyte image1[imageSize][imageSize][4];
60static GLubyte image2[imageSize][imageSize][4];
61static GLubyte image3[imageSize][imageSize][4];
62static GLubyte image4[imageSize][imageSize][4];
63static GLubyte image5[imageSize][imageSize][4];
64static GLubyte image6[imageSize][imageSize][4];
65
66static GLdouble ztrans = 0.0;
67
68static void makeImages(void)
69{
70   int i, j, c;
71
72   for (i = 0; i < imageSize; i++) {
73      for (j = 0; j < imageSize; j++) {
74         c = ( ((i & 0x1) == 0) ^ ((j & 0x1) == 0) ) * 255;
75         image1[i][j][0] = (GLubyte) c;
76         image1[i][j][1] = (GLubyte) c;
77         image1[i][j][2] = (GLubyte) c;
78         image1[i][j][3] = (GLubyte) 255;
79
80         image2[i][j][0] = (GLubyte) c;
81         image2[i][j][1] = (GLubyte) c;
82         image2[i][j][2] = (GLubyte) 0;
83         image2[i][j][3] = (GLubyte) 255;
84
85         image3[i][j][0] = (GLubyte) c;
86         image3[i][j][1] = (GLubyte) 0;
87         image3[i][j][2] = (GLubyte) c;
88         image3[i][j][3] = (GLubyte) 255;
89
90         image4[i][j][0] = (GLubyte) 0;
91         image4[i][j][1] = (GLubyte) c;
92         image4[i][j][2] = (GLubyte) c;
93         image4[i][j][3] = (GLubyte) 255;
94
95         image5[i][j][0] = (GLubyte) 255;
96         image5[i][j][1] = (GLubyte) c;
97         image5[i][j][2] = (GLubyte) c;
98         image5[i][j][3] = (GLubyte) 255;
99
100         image6[i][j][0] = (GLubyte) c;
101         image6[i][j][1] = (GLubyte) c;
102         image6[i][j][2] = (GLubyte) 255;
103         image6[i][j][3] = (GLubyte) 255;
104      }
105   }
106}
107
108static void init(void)
109{
110   GLfloat diffuse[4] = {1.0, 1.0, 1.0, 1.0};
111
112   glClearColor (0.0, 0.0, 0.0, 0.0);
113   glEnable(GL_DEPTH_TEST);
114   glShadeModel(GL_SMOOTH);
115
116   makeImages();
117   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
118   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT);
119   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT);
120   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_R, GL_REPEAT);
121   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
122   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
123   glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT, 0, GL_RGBA, imageSize,
124                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1);
125   glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT, 0, GL_RGBA, imageSize,
126                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image4);
127   glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT, 0, GL_RGBA, imageSize,
128                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image2);
129   glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT, 0, GL_RGBA, imageSize,
130                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image5);
131   glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT, 0, GL_RGBA, imageSize,
132                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image3);
133   glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT, 0, GL_RGBA, imageSize,
134                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image6);
135   glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_EXT);
136   glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_EXT);
137   glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_EXT);
138   glEnable(GL_TEXTURE_GEN_S);
139   glEnable(GL_TEXTURE_GEN_T);
140   glEnable(GL_TEXTURE_GEN_R);
141
142   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
143
144   glEnable(GL_TEXTURE_CUBE_MAP_EXT);
145   glEnable(GL_LIGHTING);
146   glEnable(GL_LIGHT0);
147   glEnable(GL_AUTO_NORMAL);
148   glEnable(GL_NORMALIZE);
149   glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);
150}
151
152static void display(void)
153{
154   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155
156   glPushMatrix ();
157   glTranslatef (0.0, 0.0, ztrans);
158   glutSolidSphere (5.0, 20, 10);
159   glPopMatrix ();
160   glutSwapBuffers();
161}
162
163static void reshape(int w, int h)
164{
165   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
166   glMatrixMode(GL_PROJECTION);
167   glLoadIdentity();
168   gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 300.0);
169   glMatrixMode(GL_MODELVIEW);
170   glLoadIdentity();
171   glTranslatef(0.0, 0.0, -20.0);
172}
173
174static void keyboard (unsigned char key, int x, int y)
175{
176   switch (key) {
177      case 'f':
178         ztrans = ztrans - 0.2;
179         glutPostRedisplay();
180         break;
181      case 'b':
182         ztrans = ztrans + 0.2;
183         glutPostRedisplay();
184         break;
185      case 27:
186         exit(0);
187         break;
188      default:
189         break;
190   }
191}
192
193int main(int argc, char** argv)
194{
195   glutInit(&argc, argv);
196   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
197   glutInitWindowSize(400, 400);
198   glutInitWindowPosition(100, 100);
199   glutCreateWindow (argv[0]);
200   init ();
201   glutDisplayFunc(display);
202   glutReshapeFunc(reshape);
203   glutKeyboardFunc(keyboard);
204   glutMainLoop();
205   return 0;
206}
207