Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 1 | /* Test GL_TEXTURE_BASE_LEVEL and GL_TEXTURE_MAX_LEVEL |
| 2 | * Brian Paul |
| 3 | * 10 May 2006 |
| 4 | */ |
| 5 | |
| 6 | |
| 7 | /* Copyright (c) Mark J. Kilgard, 1994. */ |
| 8 | |
| 9 | /* |
| 10 | * (c) Copyright 1993, Silicon Graphics, Inc. |
| 11 | * ALL RIGHTS RESERVED |
| 12 | * Permission to use, copy, modify, and distribute this software for |
| 13 | * any purpose and without fee is hereby granted, provided that the above |
| 14 | * copyright notice appear in all copies and that both the copyright notice |
| 15 | * and this permission notice appear in supporting documentation, and that |
| 16 | * the name of Silicon Graphics, Inc. not be used in advertising |
| 17 | * or publicity pertaining to distribution of the software without specific, |
| 18 | * written prior permission. |
| 19 | * |
| 20 | * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" |
| 21 | * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, |
| 22 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON |
| 24 | * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, |
| 25 | * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY |
| 26 | * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, |
| 27 | * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF |
| 28 | * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN |
| 29 | * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON |
| 30 | * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE |
| 31 | * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. |
| 32 | * |
| 33 | * US Government Users Restricted Rights |
| 34 | * Use, duplication, or disclosure by the Government is subject to |
| 35 | * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph |
| 36 | * (c)(1)(ii) of the Rights in Technical Data and Computer Software |
| 37 | * clause at DFARS 252.227-7013 and/or in similar or successor |
| 38 | * clauses in the FAR or the DOD or NASA FAR Supplement. |
| 39 | * Unpublished-- rights reserved under the copyright laws of the |
| 40 | * United States. Contractor/manufacturer is Silicon Graphics, |
| 41 | * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. |
| 42 | * |
| 43 | * OpenGL(TM) is a trademark of Silicon Graphics, Inc. |
| 44 | */ |
| 45 | /* mipmap.c |
| 46 | * This program demonstrates using mipmaps for texture maps. |
| 47 | * To overtly show the effect of mipmaps, each mipmap reduction |
| 48 | * level has a solidly colored, contrasting texture image. |
| 49 | * Thus, the quadrilateral which is drawn is drawn with several |
| 50 | * different colors. |
| 51 | */ |
| 52 | #include <stdlib.h> |
| 53 | #include <stdio.h> |
| 54 | #include <GL/glut.h> |
| 55 | |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 56 | static GLint BaseLevel = 0, MaxLevel = 8; |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 57 | static GLfloat LodBias = 0.0; |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 58 | static GLboolean NearestFilter = GL_TRUE; |
| 59 | |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 60 | |
| 61 | static void MakeImage(int level, int width, int height, const GLubyte color[4]) |
| 62 | { |
| 63 | const int makeStripes = 0; |
| 64 | GLubyte img[256*256*3]; |
| 65 | int i, j; |
| 66 | for (i = 0; i < height; i++) { |
| 67 | for (j = 0; j < width; j++) { |
| 68 | int k = (i * width + j) * 3; |
| 69 | int p = (i/8) & makeStripes; |
| 70 | if (p == 0) { |
| 71 | img[k + 0] = color[0]; |
| 72 | img[k + 1] = color[1]; |
| 73 | img[k + 2] = color[2]; |
| 74 | } |
| 75 | else { |
| 76 | img[k + 0] = 0; |
| 77 | img[k + 1] = 0; |
| 78 | img[k + 2] = 0; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, width, height, 0, |
| 84 | GL_RGB, GL_UNSIGNED_BYTE, img); |
| 85 | } |
| 86 | |
| 87 | |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 88 | static void makeImages(void) |
| 89 | { |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 90 | static const GLubyte colors[8][3] = { |
| 91 | {128, 128, 128 }, |
| 92 | { 0, 255, 255 }, |
| 93 | { 255, 255, 0 }, |
| 94 | { 255, 0, 255 }, |
| 95 | { 255, 0, 0 }, |
| 96 | { 0, 255, 0 }, |
| 97 | { 0, 0, 255 }, |
| 98 | { 255, 255, 255 } |
| 99 | }; |
| 100 | int i, sz = 128; |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 101 | |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 102 | for (i = 0; i < 8; i++) { |
| 103 | MakeImage(i, sz, sz, colors[i]); |
| 104 | sz /= 2; |
| 105 | } |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void myinit(void) |
| 109 | { |
| 110 | glEnable(GL_DEPTH_TEST); |
| 111 | glDepthFunc(GL_LESS); |
| 112 | glShadeModel(GL_FLAT); |
| 113 | |
| 114 | glTranslatef(0.0, 0.0, -3.6); |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 115 | |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 116 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 117 | makeImages(); |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 118 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 119 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 120 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); |
| 121 | glEnable(GL_TEXTURE_2D); |
| 122 | } |
| 123 | |
| 124 | static void display(void) |
| 125 | { |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 126 | GLfloat tcm = 4.0; |
| 127 | printf("GL_TEXTURE_BASE_LEVEL = %d GL_TEXTURE_MAX_LEVEL = %d Bias = %.2g filter = %s\n", |
| 128 | BaseLevel, MaxLevel, LodBias, |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 129 | NearestFilter ? "LINEAR" : "NEAREST"); |
| 130 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, BaseLevel); |
| 131 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, MaxLevel); |
| 132 | |
| 133 | if (NearestFilter) { |
| 134 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 135 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 136 | GL_NEAREST_MIPMAP_NEAREST); |
| 137 | } |
| 138 | else { |
| 139 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 140 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 141 | GL_LINEAR_MIPMAP_LINEAR); |
| 142 | } |
| 143 | |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 144 | glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, LodBias); |
| 145 | |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 146 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 147 | glBegin(GL_QUADS); |
| 148 | glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0); |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 149 | glTexCoord2f(0.0, tcm); glVertex3f(-2.0, 1.0, 0.0); |
| 150 | glTexCoord2f(tcm, tcm); glVertex3f(3000.0, 1.0, -6000.0); |
| 151 | glTexCoord2f(tcm, 0.0); glVertex3f(3000.0, -1.0, -6000.0); |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 152 | glEnd(); |
| 153 | glFlush(); |
| 154 | } |
| 155 | |
| 156 | static void myReshape(int w, int h) |
| 157 | { |
| 158 | glViewport(0, 0, w, h); |
| 159 | glMatrixMode(GL_PROJECTION); |
| 160 | glLoadIdentity(); |
| 161 | gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30000.0); |
| 162 | glMatrixMode(GL_MODELVIEW); |
| 163 | glLoadIdentity(); |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | key(unsigned char k, int x, int y) |
| 168 | { |
| 169 | (void) x; |
| 170 | (void) y; |
| 171 | switch (k) { |
| 172 | case 'b': |
| 173 | BaseLevel--; |
| 174 | if (BaseLevel < 0) |
| 175 | BaseLevel = 0; |
| 176 | break; |
| 177 | case 'B': |
| 178 | BaseLevel++; |
| 179 | if (BaseLevel > 10) |
| 180 | BaseLevel = 10; |
| 181 | break; |
| 182 | case 'm': |
| 183 | MaxLevel--; |
| 184 | if (MaxLevel < 0) |
| 185 | MaxLevel = 0; |
| 186 | break; |
| 187 | case 'M': |
| 188 | MaxLevel++; |
| 189 | if (MaxLevel > 10) |
| 190 | MaxLevel = 10; |
| 191 | break; |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 192 | case 'l': |
| 193 | LodBias -= 0.02; |
| 194 | break; |
| 195 | case 'L': |
| 196 | LodBias += 0.02; |
| 197 | break; |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 198 | case 'f': |
| 199 | NearestFilter = !NearestFilter; |
| 200 | break; |
| 201 | case 27: /* Escape */ |
| 202 | exit(0); |
| 203 | break; |
| 204 | default: |
| 205 | return; |
| 206 | } |
| 207 | glutPostRedisplay(); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | static void usage(void) |
| 212 | { |
| 213 | printf("usage:\n"); |
| 214 | printf(" b/B decrease/increase GL_TEXTURE_BASE_LEVEL\n"); |
| 215 | printf(" m/M decrease/increase GL_TEXTURE_MAX_LEVEL\n"); |
| 216 | printf(" f toggle nearest/linear filtering\n"); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | int main(int argc, char** argv) |
| 221 | { |
| 222 | glutInit(&argc, argv); |
| 223 | glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); |
Brian Paul | fa489dd | 2006-05-11 01:15:48 +0000 | [diff] [blame^] | 224 | glutInitWindowSize (600, 600); |
Brian Paul | bb2dd38 | 2006-05-10 22:37:56 +0000 | [diff] [blame] | 225 | glutCreateWindow (argv[0]); |
| 226 | myinit(); |
| 227 | glutReshapeFunc (myReshape); |
| 228 | glutDisplayFunc(display); |
| 229 | glutKeyboardFunc(key); |
| 230 | usage(); |
| 231 | glutMainLoop(); |
| 232 | return 0; /* ANSI C requires main to return int. */ |
| 233 | } |