132001f49Smrg/*
232001f49Smrg * Copyright (c) 1993-2003, Silicon Graphics, Inc.
332001f49Smrg * All Rights Reserved
432001f49Smrg *
532001f49Smrg * Permission to use, copy, modify, and distribute this software for any
632001f49Smrg * purpose and without fee is hereby granted, provided that the above
732001f49Smrg * copyright notice appear in all copies and that both the copyright
832001f49Smrg * notice and this permission notice appear in supporting documentation,
932001f49Smrg * and that the name of Silicon Graphics, Inc. not be used in
1032001f49Smrg * advertising or publicity pertaining to distribution of the software
1132001f49Smrg * without specific, written prior permission.
1232001f49Smrg *
1332001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
1432001f49Smrg * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
1532001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
1632001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
1732001f49Smrg * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
1832001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
1932001f49Smrg * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
2032001f49Smrg * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD
2132001f49Smrg * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF
2232001f49Smrg * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF
2332001f49Smrg * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE
2432001f49Smrg * OR PERFORMANCE OF THIS SOFTWARE.
2532001f49Smrg *
2632001f49Smrg * US Government Users Restricted Rights
2732001f49Smrg * Use, duplication, or disclosure by the Government is subject to
2832001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
2932001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software
3032001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor clauses
3132001f49Smrg * in the FAR or the DOD or NASA FAR Supplement.  Unpublished - rights
3232001f49Smrg * reserved under the copyright laws of the United States.
3332001f49Smrg *
3432001f49Smrg * Contractor/manufacturer is:
3532001f49Smrg *	Silicon Graphics, Inc.
3632001f49Smrg *	1500 Crittenden Lane
3732001f49Smrg *	Mountain View, CA  94043
3832001f49Smrg *	United State of America
3932001f49Smrg *
4032001f49Smrg * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
4132001f49Smrg */
4232001f49Smrg
4332001f49Smrg/*  cubemap.c
4432001f49Smrg *
4532001f49Smrg *  This program demonstrates cube map textures.
4632001f49Smrg *  Six different colored checker board textures are
4732001f49Smrg *  created and applied to a lit sphere.
4832001f49Smrg *
4932001f49Smrg *  Pressing the 'f' and 'b' keys translate the viewer
5032001f49Smrg *  forward and backward.
5132001f49Smrg */
5232001f49Smrg
5332001f49Smrg#include <GL/glew.h>
5432001f49Smrg#include "glut_wrap.h"
5532001f49Smrg#include <stdlib.h>
5632001f49Smrg#include <stdio.h>
5732001f49Smrg
5832001f49Smrg#define	imageSize 4
5932001f49Smrgstatic GLubyte image1[imageSize][imageSize][4];
6032001f49Smrgstatic GLubyte image2[imageSize][imageSize][4];
6132001f49Smrgstatic GLubyte image3[imageSize][imageSize][4];
6232001f49Smrgstatic GLubyte image4[imageSize][imageSize][4];
6332001f49Smrgstatic GLubyte image5[imageSize][imageSize][4];
6432001f49Smrgstatic GLubyte image6[imageSize][imageSize][4];
6532001f49Smrg
6632001f49Smrgstatic GLdouble ztrans = 0.0;
6732001f49Smrg
6832001f49Smrgstatic void makeImages(void)
6932001f49Smrg{
7032001f49Smrg   int i, j, c;
7132001f49Smrg
7232001f49Smrg   for (i = 0; i < imageSize; i++) {
7332001f49Smrg      for (j = 0; j < imageSize; j++) {
7432001f49Smrg         c = ( ((i & 0x1) == 0) ^ ((j & 0x1) == 0) ) * 255;
7532001f49Smrg         image1[i][j][0] = (GLubyte) c;
7632001f49Smrg         image1[i][j][1] = (GLubyte) c;
7732001f49Smrg         image1[i][j][2] = (GLubyte) c;
7832001f49Smrg         image1[i][j][3] = (GLubyte) 255;
7932001f49Smrg
8032001f49Smrg         image2[i][j][0] = (GLubyte) c;
8132001f49Smrg         image2[i][j][1] = (GLubyte) c;
8232001f49Smrg         image2[i][j][2] = (GLubyte) 0;
8332001f49Smrg         image2[i][j][3] = (GLubyte) 255;
8432001f49Smrg
8532001f49Smrg         image3[i][j][0] = (GLubyte) c;
8632001f49Smrg         image3[i][j][1] = (GLubyte) 0;
8732001f49Smrg         image3[i][j][2] = (GLubyte) c;
8832001f49Smrg         image3[i][j][3] = (GLubyte) 255;
8932001f49Smrg
9032001f49Smrg         image4[i][j][0] = (GLubyte) 0;
9132001f49Smrg         image4[i][j][1] = (GLubyte) c;
9232001f49Smrg         image4[i][j][2] = (GLubyte) c;
9332001f49Smrg         image4[i][j][3] = (GLubyte) 255;
9432001f49Smrg
9532001f49Smrg         image5[i][j][0] = (GLubyte) 255;
9632001f49Smrg         image5[i][j][1] = (GLubyte) c;
9732001f49Smrg         image5[i][j][2] = (GLubyte) c;
9832001f49Smrg         image5[i][j][3] = (GLubyte) 255;
9932001f49Smrg
10032001f49Smrg         image6[i][j][0] = (GLubyte) c;
10132001f49Smrg         image6[i][j][1] = (GLubyte) c;
10232001f49Smrg         image6[i][j][2] = (GLubyte) 255;
10332001f49Smrg         image6[i][j][3] = (GLubyte) 255;
10432001f49Smrg      }
10532001f49Smrg   }
10632001f49Smrg}
10732001f49Smrg
10832001f49Smrgstatic void init(void)
10932001f49Smrg{
11032001f49Smrg   GLfloat diffuse[4] = {1.0, 1.0, 1.0, 1.0};
11132001f49Smrg
11232001f49Smrg   glClearColor (0.0, 0.0, 0.0, 0.0);
11332001f49Smrg   glEnable(GL_DEPTH_TEST);
11432001f49Smrg   glShadeModel(GL_SMOOTH);
11532001f49Smrg
11632001f49Smrg   makeImages();
11732001f49Smrg   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
11832001f49Smrg   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT);
11932001f49Smrg   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT);
12032001f49Smrg   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_R, GL_REPEAT);
12132001f49Smrg   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12232001f49Smrg   glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12332001f49Smrg   glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT, 0, GL_RGBA, imageSize,
12432001f49Smrg                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1);
12532001f49Smrg   glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT, 0, GL_RGBA, imageSize,
12632001f49Smrg                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image4);
12732001f49Smrg   glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT, 0, GL_RGBA, imageSize,
12832001f49Smrg                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image2);
12932001f49Smrg   glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT, 0, GL_RGBA, imageSize,
13032001f49Smrg                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image5);
13132001f49Smrg   glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT, 0, GL_RGBA, imageSize,
13232001f49Smrg                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image3);
13332001f49Smrg   glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT, 0, GL_RGBA, imageSize,
13432001f49Smrg                imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, image6);
13532001f49Smrg   glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_EXT);
13632001f49Smrg   glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_EXT);
13732001f49Smrg   glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP_EXT);
13832001f49Smrg   glEnable(GL_TEXTURE_GEN_S);
13932001f49Smrg   glEnable(GL_TEXTURE_GEN_T);
14032001f49Smrg   glEnable(GL_TEXTURE_GEN_R);
14132001f49Smrg
14232001f49Smrg   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
14332001f49Smrg
14432001f49Smrg   glEnable(GL_TEXTURE_CUBE_MAP_EXT);
14532001f49Smrg   glEnable(GL_LIGHTING);
14632001f49Smrg   glEnable(GL_LIGHT0);
14732001f49Smrg   glEnable(GL_AUTO_NORMAL);
14832001f49Smrg   glEnable(GL_NORMALIZE);
14932001f49Smrg   glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);
15032001f49Smrg}
15132001f49Smrg
15232001f49Smrgstatic void display(void)
15332001f49Smrg{
15432001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
15532001f49Smrg
15632001f49Smrg   glPushMatrix ();
15732001f49Smrg   glTranslatef (0.0, 0.0, ztrans);
15832001f49Smrg   glutSolidSphere (5.0, 20, 10);
15932001f49Smrg   glPopMatrix ();
16032001f49Smrg   glutSwapBuffers();
16132001f49Smrg}
16232001f49Smrg
16332001f49Smrgstatic void reshape(int w, int h)
16432001f49Smrg{
16532001f49Smrg   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
16632001f49Smrg   glMatrixMode(GL_PROJECTION);
16732001f49Smrg   glLoadIdentity();
16832001f49Smrg   gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 300.0);
16932001f49Smrg   glMatrixMode(GL_MODELVIEW);
17032001f49Smrg   glLoadIdentity();
17132001f49Smrg   glTranslatef(0.0, 0.0, -20.0);
17232001f49Smrg}
17332001f49Smrg
17432001f49Smrgstatic void keyboard (unsigned char key, int x, int y)
17532001f49Smrg{
17632001f49Smrg   switch (key) {
17732001f49Smrg      case 'f':
17832001f49Smrg         ztrans = ztrans - 0.2;
17932001f49Smrg         glutPostRedisplay();
18032001f49Smrg         break;
18132001f49Smrg      case 'b':
18232001f49Smrg         ztrans = ztrans + 0.2;
18332001f49Smrg         glutPostRedisplay();
18432001f49Smrg         break;
18532001f49Smrg      case 27:
18632001f49Smrg         exit(0);
18732001f49Smrg         break;
18832001f49Smrg      default:
18932001f49Smrg         break;
19032001f49Smrg   }
19132001f49Smrg}
19232001f49Smrg
19332001f49Smrgint main(int argc, char** argv)
19432001f49Smrg{
19532001f49Smrg   glutInit(&argc, argv);
19632001f49Smrg   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
19732001f49Smrg   glutInitWindowSize(400, 400);
19832001f49Smrg   glutInitWindowPosition(100, 100);
19932001f49Smrg   glutCreateWindow (argv[0]);
20032001f49Smrg   init ();
20132001f49Smrg   glutDisplayFunc(display);
20232001f49Smrg   glutReshapeFunc(reshape);
20332001f49Smrg   glutKeyboardFunc(keyboard);
20432001f49Smrg   glutMainLoop();
20532001f49Smrg   return 0;
20632001f49Smrg}
207