Merge branch '7.8'
[mesa.git] / progs / redbook / texture3d.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 /* texture3d.c
44 * This program demonstrates using a three-dimensional texture.
45 * It creates a 3D texture and then renders two rectangles
46 * with different texture coordinates to obtain different
47 * "slices" of the 3D texture.
48 */
49 #include <GL/glew.h>
50 #include <GL/glut.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53
54 #ifdef GL_VERSION_1_2
55 #define iWidth 16
56 #define iHeight 16
57 #define iDepth 16
58
59 static GLubyte image[iDepth][iHeight][iWidth][3];
60 static GLuint texName;
61
62 /* Create a 16x16x16x3 array with different color values in
63 * each array element [r, g, b]. Values range from 0 to 255.
64 */
65
66 static void makeImage(void)
67 {
68 int s, t, r;
69
70 for (s = 0; s < 16; s++)
71 for (t = 0; t < 16; t++)
72 for (r = 0; r < 16; r++) {
73 image[r][t][s][0] = (GLubyte) (s * 17);
74 image[r][t][s][1] = (GLubyte) (t * 17);
75 image[r][t][s][2] = (GLubyte) (r * 17);
76 }
77 }
78
79 static void init(void)
80 {
81 glClearColor (0.0, 0.0, 0.0, 0.0);
82 glShadeModel(GL_FLAT);
83 glEnable(GL_DEPTH_TEST);
84
85 makeImage();
86 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
87
88 glGenTextures(1, &texName);
89 glBindTexture(GL_TEXTURE_3D, texName);
90 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
91 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
92 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
93 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER,
94 GL_NEAREST);
95 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER,
96 GL_NEAREST);
97 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, iWidth, iHeight,
98 iDepth, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
99 glEnable(GL_TEXTURE_3D);
100 }
101
102 static void display(void)
103 {
104 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105 glBegin(GL_QUADS);
106 glTexCoord3f(0.0, 0.0, 0.0); glVertex3f(-2.25, -1.0, 0.0);
107 glTexCoord3f(0.0, 1.0, 0.0); glVertex3f(-2.25, 1.0, 0.0);
108 glTexCoord3f(1.0, 1.0, 1.0); glVertex3f(-0.25, 1.0, 0.0);
109 glTexCoord3f(1.0, 0.0, 1.0); glVertex3f(-0.25, -1.0, 0.0);
110
111 glTexCoord3f(0.0, 0.0, 1.0); glVertex3f(0.25, -1.0, 0.0);
112 glTexCoord3f(0.0, 1.0, 1.0); glVertex3f(0.25, 1.0, 0.0);
113 glTexCoord3f(1.0, 1.0, 0.0); glVertex3f(2.25, 1.0, 0.0);
114 glTexCoord3f(1.0, 0.0, 0.0); glVertex3f(2.25, -1.0, 0.0);
115 glEnd();
116 glFlush();
117 }
118
119 static void reshape(int w, int h)
120 {
121 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
122 glMatrixMode(GL_PROJECTION);
123 glLoadIdentity();
124 gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
125 glMatrixMode(GL_MODELVIEW);
126 glLoadIdentity();
127 glTranslatef(0.0, 0.0, -4.0);
128 }
129
130 static void keyboard(unsigned char key, int x, int y)
131 {
132 switch (key) {
133 case 27:
134 exit(0);
135 break;
136 }
137 }
138
139 int main(int argc, char** argv)
140 {
141 glutInit(&argc, argv);
142 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
143 glutInitWindowSize(250, 250);
144 glutInitWindowPosition(100, 100);
145 glutCreateWindow(argv[0]);
146 glewInit();
147 init();
148 glutReshapeFunc(reshape);
149 glutDisplayFunc(display);
150 glutKeyboardFunc (keyboard);
151 glutMainLoop();
152 return 0;
153 }
154 #else
155 int main(int argc, char** argv)
156 {
157 fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0 or 1.1.\n");
158 fprintf (stderr, "If your implementation of OpenGL has the right extensions,\n");
159 fprintf (stderr, "you may be able to modify this program to make it run.\n");
160 return 0;
161 }
162 #endif
163