Merge commit 'origin/master' into gallium-0.2
[mesa.git] / progs / redbook / mipmap.c
1
2 /* Copyright (c) Mark J. Kilgard, 1994. */
3
4 /*
5 * (c) Copyright 1993, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * US Government Users Restricted Rights
29 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States. Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40 /* mipmap.c
41 * This program demonstrates using mipmaps for texture maps.
42 * To overtly show the effect of mipmaps, each mipmap reduction
43 * level has a solidly colored, contrasting texture image.
44 * Thus, the quadrilateral which is drawn is drawn with several
45 * different colors.
46 */
47 #include <stdlib.h>
48 #include <GL/glut.h>
49
50 GLubyte mipmapImage32[32][32][3];
51 GLubyte mipmapImage16[16][16][3];
52 GLubyte mipmapImage8[8][8][3];
53 GLubyte mipmapImage4[4][4][3];
54 GLubyte mipmapImage2[2][2][3];
55 GLubyte mipmapImage1[1][1][3];
56
57 void makeImages(void)
58 {
59 int i, j;
60
61 for (i = 0; i < 32; i++) {
62 for (j = 0; j < 32; j++) {
63 mipmapImage32[i][j][0] = 255;
64 mipmapImage32[i][j][1] = 255;
65 mipmapImage32[i][j][2] = 0;
66 }
67 }
68 for (i = 0; i < 16; i++) {
69 for (j = 0; j < 16; j++) {
70 mipmapImage16[i][j][0] = 255;
71 mipmapImage16[i][j][1] = 0;
72 mipmapImage16[i][j][2] = 255;
73 }
74 }
75 for (i = 0; i < 8; i++) {
76 for (j = 0; j < 8; j++) {
77 mipmapImage8[i][j][0] = 255;
78 mipmapImage8[i][j][1] = 0;
79 mipmapImage8[i][j][2] = 0;
80 }
81 }
82 for (i = 0; i < 4; i++) {
83 for (j = 0; j < 4; j++) {
84 mipmapImage4[i][j][0] = 0;
85 mipmapImage4[i][j][1] = 255;
86 mipmapImage4[i][j][2] = 0;
87 }
88 }
89 for (i = 0; i < 2; i++) {
90 for (j = 0; j < 2; j++) {
91 mipmapImage2[i][j][0] = 0;
92 mipmapImage2[i][j][1] = 0;
93 mipmapImage2[i][j][2] = 255;
94 }
95 }
96 mipmapImage1[0][0][0] = 255;
97 mipmapImage1[0][0][1] = 255;
98 mipmapImage1[0][0][2] = 255;
99 }
100
101 void myinit(void)
102 {
103 glEnable(GL_DEPTH_TEST);
104 glDepthFunc(GL_LESS);
105 glShadeModel(GL_FLAT);
106
107 glTranslatef(0.0, 0.0, -3.6);
108 makeImages();
109 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
110 glTexImage2D(GL_TEXTURE_2D, 0, 3, 32, 32, 0,
111 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage32[0][0][0]);
112 glTexImage2D(GL_TEXTURE_2D, 1, 3, 16, 16, 0,
113 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage16[0][0][0]);
114 glTexImage2D(GL_TEXTURE_2D, 2, 3, 8, 8, 0,
115 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage8[0][0][0]);
116 glTexImage2D(GL_TEXTURE_2D, 3, 3, 4, 4, 0,
117 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage4[0][0][0]);
118 glTexImage2D(GL_TEXTURE_2D, 4, 3, 2, 2, 0,
119 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage2[0][0][0]);
120 glTexImage2D(GL_TEXTURE_2D, 5, 3, 1, 1, 0,
121 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage1[0][0][0]);
122 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
123 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
124 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
125 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
126 GL_NEAREST_MIPMAP_NEAREST);
127 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
128 glEnable(GL_TEXTURE_2D);
129 }
130
131 void display(void)
132 {
133 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
134 glBegin(GL_QUADS);
135 glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
136 glTexCoord2f(0.0, 8.0); glVertex3f(-2.0, 1.0, 0.0);
137 glTexCoord2f(8.0, 8.0); glVertex3f(2000.0, 1.0, -6000.0);
138 glTexCoord2f(8.0, 0.0); glVertex3f(2000.0, -1.0, -6000.0);
139 glEnd();
140 glFlush();
141 }
142
143 void myReshape(int w, int h)
144 {
145 glViewport(0, 0, w, h);
146 glMatrixMode(GL_PROJECTION);
147 glLoadIdentity();
148 gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30000.0);
149 glMatrixMode(GL_MODELVIEW);
150 glLoadIdentity();
151 }
152
153 static void
154 key(unsigned char k, int x, int y)
155 {
156 switch (k) {
157 case 27: /* Escape */
158 exit(0);
159 break;
160 default:
161 return;
162 }
163 glutPostRedisplay();
164 }
165
166 int main(int argc, char** argv)
167 {
168 glutInit(&argc, argv);
169 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
170 glutInitWindowSize (500, 500);
171 glutCreateWindow (argv[0]);
172 myinit();
173 glutReshapeFunc (myReshape);
174 glutDisplayFunc(display);
175 glutKeyboardFunc(key);
176 glutMainLoop();
177 return 0; /* ANSI C requires main to return int. */
178 }