132001f49Smrg/* Test GL_TEXTURE_BASE_LEVEL and GL_TEXTURE_MAX_LEVEL
232001f49Smrg * Brian Paul
332001f49Smrg * 10 May 2006
432001f49Smrg */
532001f49Smrg
632001f49Smrg
732001f49Smrg/* Copyright (c) Mark J. Kilgard, 1994. */
832001f49Smrg
932001f49Smrg/*
1032001f49Smrg * (c) Copyright 1993, Silicon Graphics, Inc.
1132001f49Smrg * ALL RIGHTS RESERVED
1232001f49Smrg * Permission to use, copy, modify, and distribute this software for
1332001f49Smrg * any purpose and without fee is hereby granted, provided that the above
1432001f49Smrg * copyright notice appear in all copies and that both the copyright notice
1532001f49Smrg * and this permission notice appear in supporting documentation, and that
1632001f49Smrg * the name of Silicon Graphics, Inc. not be used in advertising
1732001f49Smrg * or publicity pertaining to distribution of the software without specific,
1832001f49Smrg * written prior permission.
1932001f49Smrg *
2032001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
2132001f49Smrg * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
2232001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
2332001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
2432001f49Smrg * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
2532001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
2632001f49Smrg * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
2732001f49Smrg * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
2832001f49Smrg * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
2932001f49Smrg * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
3032001f49Smrg * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
3132001f49Smrg * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
3232001f49Smrg *
3332001f49Smrg * US Government Users Restricted Rights
3432001f49Smrg * Use, duplication, or disclosure by the Government is subject to
3532001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
3632001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software
3732001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor
3832001f49Smrg * clauses in the FAR or the DOD or NASA FAR Supplement.
3932001f49Smrg * Unpublished-- rights reserved under the copyright laws of the
4032001f49Smrg * United States.  Contractor/manufacturer is Silicon Graphics,
4132001f49Smrg * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
4232001f49Smrg *
4332001f49Smrg * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
4432001f49Smrg */
4532001f49Smrg
4632001f49Smrg#include <stdlib.h>
4732001f49Smrg#include <stdio.h>
4832001f49Smrg#include <GL/glew.h>
4932001f49Smrg#include "glut_wrap.h"
5032001f49Smrg
5132001f49Smrg
5232001f49Smrgstatic GLfloat LodBias = 6.0;   /* make smallest miplevel visible */
5332001f49Smrgstatic GLuint texImage;
5432001f49Smrg
5532001f49Smrg#define WIDTH 2
5632001f49Smrg#define HEIGHT 2
5732001f49Smrg
5832001f49Smrgstatic void
5932001f49SmrgInitValues(void)
6032001f49Smrg{
6132001f49Smrg   LodBias = 6.0;               /* make smallest miplevel visible */
6232001f49Smrg}
6332001f49Smrg
6432001f49Smrg
6532001f49Smrgstatic void MakeImage(void)
6632001f49Smrg{
6732001f49Smrg   const GLubyte color0[4] = { 0xff, 0x80, 0x20, 0xff };
6832001f49Smrg   const GLubyte color1[4] = { 0x10, 0x20, 0x40, 0xff };
6932001f49Smrg
7032001f49Smrg   GLubyte img[WIDTH*HEIGHT*3];
7132001f49Smrg   int i, j;
7232001f49Smrg   for (i = 0; i < HEIGHT; i++) {
7332001f49Smrg      for (j = 0; j < WIDTH; j++) {
7432001f49Smrg         int k = (i * WIDTH + j) * 3;
7532001f49Smrg         int p = ((i+j)%2);
7632001f49Smrg         if (p == 0) {
7732001f49Smrg            img[k + 0] = color0[0];
7832001f49Smrg            img[k + 1] = color0[1];
7932001f49Smrg            img[k + 2] = color0[2];
8032001f49Smrg         }
8132001f49Smrg         else {
8232001f49Smrg            img[k + 0] = color1[0];
8332001f49Smrg            img[k + 1] = color1[1];
8432001f49Smrg            img[k + 2] = color1[2];
8532001f49Smrg         }
8632001f49Smrg      }
8732001f49Smrg   }
8832001f49Smrg
8932001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
9032001f49Smrg   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0,
9132001f49Smrg                GL_RGB, GL_UNSIGNED_BYTE, img);
9232001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_FALSE);
9332001f49Smrg}
9432001f49Smrg
9532001f49Smrg
9632001f49Smrg
9732001f49Smrgstatic void myinit(void)
9832001f49Smrg{
9932001f49Smrg   InitValues();
10032001f49Smrg
10132001f49Smrg   glShadeModel(GL_FLAT);
10232001f49Smrg
10332001f49Smrg   glTranslatef(0.0, 0.0, -3.6);
10432001f49Smrg
10532001f49Smrg   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
10632001f49Smrg   glGenTextures(1, &texImage);
10732001f49Smrg   glBindTexture(GL_TEXTURE_2D, texImage);
10832001f49Smrg   MakeImage();
10932001f49Smrg
11032001f49Smrg   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
11132001f49Smrg   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
11232001f49Smrg   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
11332001f49Smrg   glEnable(GL_TEXTURE_2D);
11432001f49Smrg
11532001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
11632001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
11732001f49Smrg
11832001f49Smrg   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, -1);
11932001f49Smrg   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 1);
12032001f49Smrg
12132001f49Smrg   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12232001f49Smrg   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
12332001f49Smrg                   GL_NEAREST_MIPMAP_NEAREST);
12432001f49Smrg}
12532001f49Smrg
12632001f49Smrg
12732001f49Smrg
12832001f49Smrgstatic void display(void)
12932001f49Smrg{
13032001f49Smrg   GLfloat tcm = 1.0;
13132001f49Smrg   glBindTexture(GL_TEXTURE_2D, texImage);
13232001f49Smrg
13332001f49Smrg   printf("Bias=%.2g\n", LodBias);
13432001f49Smrg   fflush(stdout);
13532001f49Smrg
13632001f49Smrg   glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, LodBias);
13732001f49Smrg
13832001f49Smrg   glClear(GL_COLOR_BUFFER_BIT);
13932001f49Smrg   glBegin(GL_QUADS);
14032001f49Smrg   glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
14132001f49Smrg   glTexCoord2f(0.0, tcm); glVertex3f(-2.0, 1.0, 0.0);
14232001f49Smrg   glTexCoord2f(tcm * 3000.0, tcm); glVertex3f(3000.0, 1.0, -6000.0);
14332001f49Smrg   glTexCoord2f(tcm * 3000.0, 0.0); glVertex3f(3000.0, -1.0, -6000.0);
14432001f49Smrg   glEnd();
14532001f49Smrg   glFlush();
14632001f49Smrg}
14732001f49Smrg
14832001f49Smrgstatic void myReshape(int w, int h)
14932001f49Smrg{
15032001f49Smrg   glViewport(0, 0, w, h);
15132001f49Smrg   glMatrixMode(GL_PROJECTION);
15232001f49Smrg   glLoadIdentity();
15332001f49Smrg   gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30000.0);
15432001f49Smrg   glMatrixMode(GL_MODELVIEW);
15532001f49Smrg   glLoadIdentity();
15632001f49Smrg}
15732001f49Smrg
15832001f49Smrgstatic void
15932001f49Smrgkey(unsigned char k, int x, int y)
16032001f49Smrg{
16132001f49Smrg  (void) x;
16232001f49Smrg  (void) y;
16332001f49Smrg  switch (k) {
16432001f49Smrg  case 'l':
16532001f49Smrg     LodBias -= 0.25;
16632001f49Smrg     break;
16732001f49Smrg  case 'L':
16832001f49Smrg     LodBias += 0.25;
16932001f49Smrg     break;
17032001f49Smrg  case ' ':
17132001f49Smrg     InitValues();
17232001f49Smrg     break;
17332001f49Smrg  case 27:  /* Escape */
17432001f49Smrg    exit(0);
17532001f49Smrg    break;
17632001f49Smrg  default:
17732001f49Smrg    return;
17832001f49Smrg  }
17932001f49Smrg  glutPostRedisplay();
18032001f49Smrg}
18132001f49Smrg
18232001f49Smrg
18332001f49Smrgstatic void usage(void)
18432001f49Smrg{
18532001f49Smrg   printf("usage:\n");
18632001f49Smrg   printf("  l/L    decrease/increase GL_TEXTURE_LOD_BIAS\n");
18732001f49Smrg   printf("  SPACE  reset values\n");
18832001f49Smrg}
18932001f49Smrg
19032001f49Smrg
19132001f49Smrgint main(int argc, char** argv)
19232001f49Smrg{
19332001f49Smrg    glutInit(&argc, argv);
19432001f49Smrg    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB );
19532001f49Smrg    glutInitWindowSize (600, 600);
19632001f49Smrg    glutCreateWindow (argv[0]);
19732001f49Smrg    glewInit();
19832001f49Smrg    myinit();
19932001f49Smrg    glutReshapeFunc (myReshape);
20032001f49Smrg    glutDisplayFunc(display);
20132001f49Smrg    glutKeyboardFunc(key);
20232001f49Smrg    usage();
20332001f49Smrg    glutMainLoop();
20432001f49Smrg    return 0;             /* ANSI C requires main to return int. */
20532001f49Smrg}
206