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 <stdio.h>
49#include <GL/glew.h>
50#include "glut_wrap.h"
51
52GLubyte mipmapImage32[40][46][3];
53GLubyte mipmapImage16[20][23][3];
54GLubyte mipmapImage8[10][11][3];
55GLubyte mipmapImage4[5][5][3];
56GLubyte mipmapImage2[2][2][3];
57GLubyte mipmapImage1[1][1][3];
58
59static void makeImages(void)
60{
61    int i, j;
62
63    for (i = 0; i < 40; i++) {
64	for (j = 0; j < 46; j++) {
65	    mipmapImage32[i][j][0] = 255;
66	    mipmapImage32[i][j][1] = 255;
67	    mipmapImage32[i][j][2] = 0;
68	}
69    }
70    for (i = 0; i < 20; i++) {
71	for (j = 0; j < 23; j++) {
72	    mipmapImage16[i][j][0] = 255;
73	    mipmapImage16[i][j][1] = 0;
74	    mipmapImage16[i][j][2] = 255;
75	}
76    }
77    for (i = 0; i < 10; i++) {
78	for (j = 0; j < 11; j++) {
79	    mipmapImage8[i][j][0] = 255;
80	    mipmapImage8[i][j][1] = 0;
81	    mipmapImage8[i][j][2] = 0;
82	}
83    }
84    for (i = 0; i < 5; i++) {
85	for (j = 0; j < 5; j++) {
86	    mipmapImage4[i][j][0] = 0;
87	    mipmapImage4[i][j][1] = 255;
88	    mipmapImage4[i][j][2] = 0;
89	}
90    }
91    for (i = 0; i < 2; i++) {
92	for (j = 0; j < 2; j++) {
93	    mipmapImage2[i][j][0] = 0;
94	    mipmapImage2[i][j][1] = 0;
95	    mipmapImage2[i][j][2] = 255;
96	}
97    }
98    mipmapImage1[0][0][0] = 255;
99    mipmapImage1[0][0][1] = 255;
100    mipmapImage1[0][0][2] = 255;
101}
102
103static void myinit(void)
104{
105    if (!glutExtensionSupported("GL_ARB_texture_non_power_of_two")) {
106       printf("Sorry, this program requires GL_ARB_texture_non_power_of_two\n");
107       exit(1);
108    }
109
110    glEnable(GL_DEPTH_TEST);
111    glDepthFunc(GL_LESS);
112    glShadeModel(GL_FLAT);
113
114    glTranslatef(0.0, 0.0, -3.6);
115    makeImages();
116    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
117    glTexImage2D(GL_TEXTURE_2D, 0, 3, 40, 46, 0,
118		 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage32[0][0][0]);
119    glTexImage2D(GL_TEXTURE_2D, 1, 3, 20, 23, 0,
120		 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage16[0][0][0]);
121    glTexImage2D(GL_TEXTURE_2D, 2, 3, 10, 11, 0,
122		 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage8[0][0][0]);
123    glTexImage2D(GL_TEXTURE_2D, 3, 3, 5, 5, 0,
124		 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage4[0][0][0]);
125    glTexImage2D(GL_TEXTURE_2D, 4, 3, 2, 2, 0,
126		 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage2[0][0][0]);
127    glTexImage2D(GL_TEXTURE_2D, 5, 3, 1, 1, 0,
128		 GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage1[0][0][0]);
129    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
130    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
131    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
132    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
133	GL_NEAREST_MIPMAP_NEAREST);
134    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
135    glEnable(GL_TEXTURE_2D);
136}
137
138static void display(void)
139{
140    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
141    glBegin(GL_QUADS);
142    glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
143    glTexCoord2f(0.0, 8.0); glVertex3f(-2.0, 1.0, 0.0);
144    glTexCoord2f(8.0, 8.0); glVertex3f(2000.0, 1.0, -6000.0);
145    glTexCoord2f(8.0, 0.0); glVertex3f(2000.0, -1.0, -6000.0);
146    glEnd();
147    glFlush();
148}
149
150static void myReshape(int w, int h)
151{
152    glViewport(0, 0, w, h);
153    glMatrixMode(GL_PROJECTION);
154    glLoadIdentity();
155    gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30000.0);
156    glMatrixMode(GL_MODELVIEW);
157    glLoadIdentity();
158}
159
160static void
161key(unsigned char k, int x, int y)
162{
163  switch (k) {
164  case 27:  /* Escape */
165    exit(0);
166    break;
167  default:
168    return;
169  }
170  glutPostRedisplay();
171}
172
173int main(int argc, char** argv)
174{
175    glutInit(&argc, argv);
176    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
177    glutInitWindowSize (500, 500);
178    glutCreateWindow (argv[0]);
179    glewInit();
180    myinit();
181    glutReshapeFunc (myReshape);
182    glutDisplayFunc(display);
183    glutKeyboardFunc(key);
184    glutMainLoop();
185    return 0;             /* ANSI C requires main to return int. */
186}
187