132001f49Smrg/*
232001f49Smrg * Copyright (c) 1993-1997, Silicon Graphics, Inc.
332001f49Smrg * ALL RIGHTS RESERVED
432001f49Smrg * Permission to use, copy, modify, and distribute this software for
532001f49Smrg * any purpose and without fee is hereby granted, provided that the above
632001f49Smrg * copyright notice appear in all copies and that both the copyright notice
732001f49Smrg * and this permission notice appear in supporting documentation, and that
832001f49Smrg * the name of Silicon Graphics, Inc. not be used in advertising
932001f49Smrg * or publicity pertaining to distribution of the software without specific,
1032001f49Smrg * written prior permission.
1132001f49Smrg *
1232001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
1332001f49Smrg * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
1432001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
1532001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
1632001f49Smrg * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
1732001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
1832001f49Smrg * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
1932001f49Smrg * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
2032001f49Smrg * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
2132001f49Smrg * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
2232001f49Smrg * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
2332001f49Smrg * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
2432001f49Smrg *
2532001f49Smrg * US Government Users Restricted Rights
2632001f49Smrg * Use, duplication, or disclosure by the Government is subject to
2732001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
2832001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software
2932001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor
3032001f49Smrg * clauses in the FAR or the DOD or NASA FAR Supplement.
3132001f49Smrg * Unpublished-- rights reserved under the copyright laws of the
3232001f49Smrg * United States.  Contractor/manufacturer is Silicon Graphics,
3332001f49Smrg * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
3432001f49Smrg *
3532001f49Smrg * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
3632001f49Smrg */
3732001f49Smrg
3832001f49Smrg/*  texsub.c
3932001f49Smrg *  This program texture maps a checkerboard image onto
4032001f49Smrg *  two rectangles.  This program clamps the texture, if
4132001f49Smrg *  the texture coordinates fall outside 0.0 and 1.0.
4232001f49Smrg *  If the s key is pressed, a texture subimage is used to
4332001f49Smrg *  alter the original texture.  If the r key is pressed,
4432001f49Smrg *  the original texture is restored.
4532001f49Smrg */
4632001f49Smrg#include "glut_wrap.h"
4732001f49Smrg#include <stdlib.h>
4832001f49Smrg#include <stdio.h>
4932001f49Smrg
5032001f49Smrg#ifdef GL_VERSION_1_1
5132001f49Smrg/*  Create checkerboard textures  */
5232001f49Smrg#define checkImageWidth 64
5332001f49Smrg#define checkImageHeight 64
5432001f49Smrg#define subImageWidth 16
5532001f49Smrg#define subImageHeight 16
5632001f49Smrgstatic GLubyte checkImage[checkImageHeight][checkImageWidth][4];
5732001f49Smrgstatic GLubyte subImage[subImageHeight][subImageWidth][4];
5832001f49Smrg
5932001f49Smrgstatic GLuint texName;
6032001f49Smrg
6132001f49Smrgstatic void makeCheckImages(void)
6232001f49Smrg{
6332001f49Smrg   int i, j, c;
6432001f49Smrg
6532001f49Smrg   for (i = 0; i < checkImageHeight; i++) {
6632001f49Smrg      for (j = 0; j < checkImageWidth; j++) {
6732001f49Smrg         c = ((((i&0x8)==0)^((j&0x8)==0)))*255;
6832001f49Smrg         checkImage[i][j][0] = (GLubyte) c;
6932001f49Smrg         checkImage[i][j][1] = (GLubyte) c;
7032001f49Smrg         checkImage[i][j][2] = (GLubyte) c;
7132001f49Smrg         checkImage[i][j][3] = (GLubyte) 255;
7232001f49Smrg      }
7332001f49Smrg   }
7432001f49Smrg   for (i = 0; i < subImageHeight; i++) {
7532001f49Smrg      for (j = 0; j < subImageWidth; j++) {
7632001f49Smrg         c = ((((i&0x4)==0)^((j&0x4)==0)))*255;
7732001f49Smrg         subImage[i][j][0] = (GLubyte) c;
7832001f49Smrg         subImage[i][j][1] = (GLubyte) 0;
7932001f49Smrg         subImage[i][j][2] = (GLubyte) 0;
8032001f49Smrg         subImage[i][j][3] = (GLubyte) 255;
8132001f49Smrg      }
8232001f49Smrg   }
8332001f49Smrg}
8432001f49Smrg
8532001f49Smrgstatic void init(void)
8632001f49Smrg{
8732001f49Smrg   glClearColor (0.0, 0.0, 0.0, 0.0);
8832001f49Smrg   glShadeModel(GL_FLAT);
8932001f49Smrg   glEnable(GL_DEPTH_TEST);
9032001f49Smrg
9132001f49Smrg   makeCheckImages();
9232001f49Smrg   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
9332001f49Smrg
9432001f49Smrg   glGenTextures(1, &texName);
9532001f49Smrg   glBindTexture(GL_TEXTURE_2D, texName);
9632001f49Smrg
9732001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
9832001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
9932001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
10032001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
10132001f49Smrg   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight,
10232001f49Smrg                0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
10332001f49Smrg}
10432001f49Smrg
10532001f49Smrgstatic void display(void)
10632001f49Smrg{
10732001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
10832001f49Smrg   glEnable(GL_TEXTURE_2D);
10932001f49Smrg   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
11032001f49Smrg   glBindTexture(GL_TEXTURE_2D, texName);
11132001f49Smrg   glBegin(GL_QUADS);
11232001f49Smrg   glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
11332001f49Smrg   glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
11432001f49Smrg   glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
11532001f49Smrg   glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
11632001f49Smrg
11732001f49Smrg   glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
11832001f49Smrg   glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
11932001f49Smrg   glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
12032001f49Smrg   glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
12132001f49Smrg   glEnd();
12232001f49Smrg   glFlush();
12332001f49Smrg   glDisable(GL_TEXTURE_2D);
12432001f49Smrg}
12532001f49Smrg
12632001f49Smrgstatic void reshape(int w, int h)
12732001f49Smrg{
12832001f49Smrg   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
12932001f49Smrg   glMatrixMode(GL_PROJECTION);
13032001f49Smrg   glLoadIdentity();
13132001f49Smrg   gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
13232001f49Smrg   glMatrixMode(GL_MODELVIEW);
13332001f49Smrg   glLoadIdentity();
13432001f49Smrg   glTranslatef(0.0, 0.0, -3.6);
13532001f49Smrg}
13632001f49Smrg
13732001f49Smrg/* ARGSUSED1 */
13832001f49Smrgstatic void keyboard (unsigned char key, int x, int y)
13932001f49Smrg{
14032001f49Smrg   switch (key) {
14132001f49Smrg      case 's':
14232001f49Smrg      case 'S':
14332001f49Smrg         glBindTexture(GL_TEXTURE_2D, texName);
14432001f49Smrg         glTexSubImage2D(GL_TEXTURE_2D, 0, 12, 44, subImageWidth,
14532001f49Smrg                         subImageHeight, GL_RGBA,
14632001f49Smrg                         GL_UNSIGNED_BYTE, subImage);
14732001f49Smrg         glutPostRedisplay();
14832001f49Smrg         break;
14932001f49Smrg      case 'r':
15032001f49Smrg      case 'R':
15132001f49Smrg         glBindTexture(GL_TEXTURE_2D, texName);
15232001f49Smrg         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
15332001f49Smrg                      checkImageHeight, 0, GL_RGBA,
15432001f49Smrg                      GL_UNSIGNED_BYTE, checkImage);
15532001f49Smrg         glutPostRedisplay();
15632001f49Smrg         break;
15732001f49Smrg      case 27:
15832001f49Smrg         exit(0);
15932001f49Smrg         break;
16032001f49Smrg      default:
16132001f49Smrg         break;
16232001f49Smrg   }
16332001f49Smrg}
16432001f49Smrg
16532001f49Smrgint main(int argc, char** argv)
16632001f49Smrg{
16732001f49Smrg   glutInit(&argc, argv);
16832001f49Smrg   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
16932001f49Smrg   glutInitWindowSize(250, 250);
17032001f49Smrg   glutInitWindowPosition(100, 100);
17132001f49Smrg   glutCreateWindow(argv[0]);
17232001f49Smrg   init();
17332001f49Smrg   glutDisplayFunc(display);
17432001f49Smrg   glutReshapeFunc(reshape);
17532001f49Smrg   glutKeyboardFunc(keyboard);
17632001f49Smrg   glutMainLoop();
17732001f49Smrg   return 0;
17832001f49Smrg}
17932001f49Smrg#else
18032001f49Smrgint main(int argc, char** argv)
18132001f49Smrg{
18232001f49Smrg    fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
18332001f49Smrg    fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
18432001f49Smrg    fprintf (stderr, "you may be able to modify this program to make it run.\n");
18532001f49Smrg    return 0;
18632001f49Smrg}
18732001f49Smrg#endif
188