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/*  texture3d.c
4432001f49Smrg *  This program demonstrates using a three-dimensional texture.
4532001f49Smrg *  It creates a 3D texture and then renders two rectangles
4632001f49Smrg *  with different texture coordinates to obtain different
4732001f49Smrg *  "slices" of the 3D texture.
4832001f49Smrg */
4932001f49Smrg#include <GL/glew.h>
5032001f49Smrg#include "glut_wrap.h"
5132001f49Smrg#include <stdlib.h>
5232001f49Smrg#include <stdio.h>
5332001f49Smrg
5432001f49Smrg#ifdef GL_VERSION_1_2
5532001f49Smrg#define	iWidth 16
5632001f49Smrg#define	iHeight 16
5732001f49Smrg#define iDepth 16
5832001f49Smrg
5932001f49Smrgstatic GLubyte image[iDepth][iHeight][iWidth][3];
6032001f49Smrgstatic GLuint texName;
6132001f49Smrg
6232001f49Smrg/*  Create a 16x16x16x3 array with different color values in
6332001f49Smrg *  each array element [r, g, b].  Values range from 0 to 255.
6432001f49Smrg */
6532001f49Smrg
6632001f49Smrgstatic void makeImage(void)
6732001f49Smrg{
6832001f49Smrg   int s, t, r;
6932001f49Smrg
7032001f49Smrg   for (s = 0; s < 16; s++)
7132001f49Smrg      for (t = 0; t < 16; t++)
7232001f49Smrg         for (r = 0; r < 16; r++) {
7332001f49Smrg            image[r][t][s][0] = (GLubyte) (s * 17);
7432001f49Smrg            image[r][t][s][1] = (GLubyte) (t * 17);
7532001f49Smrg            image[r][t][s][2] = (GLubyte) (r * 17);
7632001f49Smrg         }
7732001f49Smrg}
7832001f49Smrg
7932001f49Smrgstatic void init(void)
8032001f49Smrg{
8132001f49Smrg   glClearColor (0.0, 0.0, 0.0, 0.0);
8232001f49Smrg   glShadeModel(GL_FLAT);
8332001f49Smrg   glEnable(GL_DEPTH_TEST);
8432001f49Smrg
8532001f49Smrg   makeImage();
8632001f49Smrg   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
8732001f49Smrg
8832001f49Smrg   glGenTextures(1, &texName);
8932001f49Smrg   glBindTexture(GL_TEXTURE_3D, texName);
9032001f49Smrg   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
9132001f49Smrg   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
9232001f49Smrg   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
9332001f49Smrg   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER,
9432001f49Smrg                   GL_NEAREST);
9532001f49Smrg   glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER,
9632001f49Smrg                   GL_NEAREST);
9732001f49Smrg   glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, iWidth, iHeight,
9832001f49Smrg                iDepth, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
9932001f49Smrg   glEnable(GL_TEXTURE_3D);
10032001f49Smrg}
10132001f49Smrg
10232001f49Smrgstatic void display(void)
10332001f49Smrg{
10432001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
10532001f49Smrg   glBegin(GL_QUADS);
10632001f49Smrg   glTexCoord3f(0.0, 0.0, 0.0); glVertex3f(-2.25, -1.0, 0.0);
10732001f49Smrg   glTexCoord3f(0.0, 1.0, 0.0); glVertex3f(-2.25, 1.0, 0.0);
10832001f49Smrg   glTexCoord3f(1.0, 1.0, 1.0); glVertex3f(-0.25, 1.0, 0.0);
10932001f49Smrg   glTexCoord3f(1.0, 0.0, 1.0); glVertex3f(-0.25, -1.0, 0.0);
11032001f49Smrg
11132001f49Smrg   glTexCoord3f(0.0, 0.0, 1.0); glVertex3f(0.25, -1.0, 0.0);
11232001f49Smrg   glTexCoord3f(0.0, 1.0, 1.0); glVertex3f(0.25, 1.0, 0.0);
11332001f49Smrg   glTexCoord3f(1.0, 1.0, 0.0); glVertex3f(2.25, 1.0, 0.0);
11432001f49Smrg   glTexCoord3f(1.0, 0.0, 0.0); glVertex3f(2.25, -1.0, 0.0);
11532001f49Smrg   glEnd();
11632001f49Smrg   glFlush();
11732001f49Smrg}
11832001f49Smrg
11932001f49Smrgstatic void reshape(int w, int h)
12032001f49Smrg{
12132001f49Smrg   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
12232001f49Smrg   glMatrixMode(GL_PROJECTION);
12332001f49Smrg   glLoadIdentity();
12432001f49Smrg   gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
12532001f49Smrg   glMatrixMode(GL_MODELVIEW);
12632001f49Smrg   glLoadIdentity();
12732001f49Smrg   glTranslatef(0.0, 0.0, -4.0);
12832001f49Smrg}
12932001f49Smrg
13032001f49Smrgstatic void keyboard(unsigned char key, int x, int y)
13132001f49Smrg{
13232001f49Smrg   switch (key) {
13332001f49Smrg      case 27:
13432001f49Smrg         exit(0);
13532001f49Smrg         break;
13632001f49Smrg   }
13732001f49Smrg}
13832001f49Smrg
13932001f49Smrgint main(int argc, char** argv)
14032001f49Smrg{
14132001f49Smrg   glutInit(&argc, argv);
14232001f49Smrg   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
14332001f49Smrg   glutInitWindowSize(250, 250);
14432001f49Smrg   glutInitWindowPosition(100, 100);
14532001f49Smrg   glutCreateWindow(argv[0]);
14632001f49Smrg   glewInit();
14732001f49Smrg   init();
14832001f49Smrg   glutReshapeFunc(reshape);
14932001f49Smrg   glutDisplayFunc(display);
15032001f49Smrg   glutKeyboardFunc (keyboard);
15132001f49Smrg   glutMainLoop();
15232001f49Smrg   return 0;
15332001f49Smrg}
15432001f49Smrg#else
15532001f49Smrgint main(int argc, char** argv)
15632001f49Smrg{
15732001f49Smrg    fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0 or 1.1.\n");
15832001f49Smrg    fprintf (stderr, "If your implementation of OpenGL has the right extensions,\n");
15932001f49Smrg    fprintf (stderr, "you may be able to modify this program to make it run.\n");
16032001f49Smrg    return 0;
16132001f49Smrg}
16232001f49Smrg#endif
16332001f49Smrg
164