132001f49Smrg/* 232001f49Smrg * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc. 332001f49Smrg * 432001f49Smrg * Permission to use, copy, modify, distribute, and sell this software and 532001f49Smrg * its documentation for any purpose is hereby granted without fee, provided 632001f49Smrg * that (i) the above copyright notices and this permission notice appear in 732001f49Smrg * all copies of the software and related documentation, and (ii) the name of 832001f49Smrg * Silicon Graphics may not be used in any advertising or 932001f49Smrg * publicity relating to the software without the specific, prior written 1032001f49Smrg * permission of Silicon Graphics. 1132001f49Smrg * 1232001f49Smrg * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF 1332001f49Smrg * ANY KIND, 1432001f49Smrg * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 1532001f49Smrg * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 1632001f49Smrg * 1732001f49Smrg * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR 1832001f49Smrg * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 1932001f49Smrg * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 2032001f49Smrg * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 2132001f49Smrg * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 2232001f49Smrg * OF THIS SOFTWARE. 2332001f49Smrg */ 2432001f49Smrg 2532001f49Smrg#include <stdio.h> 2632001f49Smrg#include <string.h> 2732001f49Smrg#include <math.h> 2832001f49Smrg#include <stdlib.h> 2932001f49Smrg#include "glut_wrap.h" 3032001f49Smrg 3132001f49Smrg 3232001f49Smrg#include "loadppm.c" 3332001f49Smrg 3432001f49SmrgGLenum doubleBuffer; 3532001f49Smrg 3632001f49Smrgchar *texFileName = 0; 3732001f49SmrgPPMImage *image; 3832001f49Smrg 3932001f49Smrgfloat *minFilter, *magFilter, *sWrapMode, *tWrapMode; 4032001f49Smrgfloat decal[] = {GL_DECAL}; 4132001f49Smrgfloat modulate[] = {GL_MODULATE}; 4232001f49Smrgfloat repeat[] = {GL_REPEAT}; 4332001f49Smrgfloat clamp[] = {GL_CLAMP}; 4432001f49Smrgfloat nr[] = {GL_NEAREST}; 4532001f49Smrgfloat ln[] = {GL_LINEAR}; 4632001f49Smrgfloat nr_mipmap_nr[] = {GL_NEAREST_MIPMAP_NEAREST}; 4732001f49Smrgfloat nr_mipmap_ln[] = {GL_NEAREST_MIPMAP_LINEAR}; 4832001f49Smrgfloat ln_mipmap_nr[] = {GL_LINEAR_MIPMAP_NEAREST}; 4932001f49Smrgfloat ln_mipmap_ln[] = {GL_LINEAR_MIPMAP_LINEAR}; 5032001f49SmrgGLint sphereMap[] = {GL_SPHERE_MAP}; 5132001f49Smrg 5232001f49SmrgGLenum doSphere = GL_FALSE; 5332001f49Smrgfloat xRotation = 0.0, yRotation = 0.0, zTranslate = -3.125; 5432001f49Smrg 5532001f49SmrgGLint cube; 5632001f49Smrgfloat c[6][4][3] = { 5732001f49Smrg { 5832001f49Smrg { 5932001f49Smrg 1.0, 1.0, -1.0 6032001f49Smrg }, 6132001f49Smrg { 6232001f49Smrg -1.0, 1.0, -1.0 6332001f49Smrg }, 6432001f49Smrg { 6532001f49Smrg -1.0, -1.0, -1.0 6632001f49Smrg }, 6732001f49Smrg { 6832001f49Smrg 1.0, -1.0, -1.0 6932001f49Smrg } 7032001f49Smrg }, 7132001f49Smrg { 7232001f49Smrg { 7332001f49Smrg 1.0, 1.0, 1.0 7432001f49Smrg }, 7532001f49Smrg { 7632001f49Smrg 1.0, 1.0, -1.0 7732001f49Smrg }, 7832001f49Smrg { 7932001f49Smrg 1.0, -1.0, -1.0 8032001f49Smrg }, 8132001f49Smrg { 8232001f49Smrg 1.0, -1.0, 1.0 8332001f49Smrg } 8432001f49Smrg }, 8532001f49Smrg { 8632001f49Smrg { 8732001f49Smrg -1.0, 1.0, 1.0 8832001f49Smrg }, 8932001f49Smrg { 9032001f49Smrg 1.0, 1.0, 1.0 9132001f49Smrg }, 9232001f49Smrg { 9332001f49Smrg 1.0, -1.0, 1.0 9432001f49Smrg }, 9532001f49Smrg { 9632001f49Smrg -1.0, -1.0, 1.0 9732001f49Smrg } 9832001f49Smrg }, 9932001f49Smrg { 10032001f49Smrg { 10132001f49Smrg -1.0, 1.0, -1.0 10232001f49Smrg }, 10332001f49Smrg { 10432001f49Smrg -1.0, 1.0, 1.0 10532001f49Smrg }, 10632001f49Smrg { 10732001f49Smrg -1.0, -1.0, 1.0 10832001f49Smrg }, 10932001f49Smrg { 11032001f49Smrg -1.0, -1.0, -1.0 11132001f49Smrg } 11232001f49Smrg }, 11332001f49Smrg { 11432001f49Smrg { 11532001f49Smrg -1.0, 1.0, 1.0 11632001f49Smrg }, 11732001f49Smrg { 11832001f49Smrg -1.0, 1.0, -1.0 11932001f49Smrg }, 12032001f49Smrg { 12132001f49Smrg 1.0, 1.0, -1.0 12232001f49Smrg }, 12332001f49Smrg { 12432001f49Smrg 1.0, 1.0, 1.0 12532001f49Smrg } 12632001f49Smrg }, 12732001f49Smrg { 12832001f49Smrg { 12932001f49Smrg -1.0, -1.0, -1.0 13032001f49Smrg }, 13132001f49Smrg { 13232001f49Smrg -1.0, -1.0, 1.0 13332001f49Smrg }, 13432001f49Smrg { 13532001f49Smrg 1.0, -1.0, 1.0 13632001f49Smrg }, 13732001f49Smrg { 13832001f49Smrg 1.0, -1.0, -1.0 13932001f49Smrg } 14032001f49Smrg } 14132001f49Smrg}; 14232001f49Smrgstatic float n[6][3] = { 14332001f49Smrg { 14432001f49Smrg 0.0, 0.0, -1.0 14532001f49Smrg }, 14632001f49Smrg { 14732001f49Smrg 1.0, 0.0, 0.0 14832001f49Smrg }, 14932001f49Smrg { 15032001f49Smrg 0.0, 0.0, 1.0 15132001f49Smrg }, 15232001f49Smrg { 15332001f49Smrg -1.0, 0.0, 0.0 15432001f49Smrg }, 15532001f49Smrg { 15632001f49Smrg 0.0, 1.0, 0.0 15732001f49Smrg }, 15832001f49Smrg { 15932001f49Smrg 0.0, -1.0, 0.0 16032001f49Smrg } 16132001f49Smrg}; 16232001f49Smrgstatic float t[6][4][2] = { 16332001f49Smrg { 16432001f49Smrg { 16532001f49Smrg 1.1, 1.1 16632001f49Smrg }, 16732001f49Smrg { 16832001f49Smrg -0.1, 1.1 16932001f49Smrg }, 17032001f49Smrg { 17132001f49Smrg -0.1, -0.1 17232001f49Smrg }, 17332001f49Smrg { 17432001f49Smrg 1.1, -0.1 17532001f49Smrg } 17632001f49Smrg }, 17732001f49Smrg { 17832001f49Smrg { 17932001f49Smrg 1.1, 1.1 18032001f49Smrg }, 18132001f49Smrg { 18232001f49Smrg -0.1, 1.1 18332001f49Smrg }, 18432001f49Smrg { 18532001f49Smrg -0.1, -0.1 18632001f49Smrg }, 18732001f49Smrg { 18832001f49Smrg 1.1, -0.1 18932001f49Smrg } 19032001f49Smrg }, 19132001f49Smrg { 19232001f49Smrg { 19332001f49Smrg -0.1, 1.1 19432001f49Smrg }, 19532001f49Smrg { 19632001f49Smrg 1.1, 1.1 19732001f49Smrg }, 19832001f49Smrg { 19932001f49Smrg 1.1, -0.1 20032001f49Smrg }, 20132001f49Smrg { 20232001f49Smrg -0.1, -0.1 20332001f49Smrg } 20432001f49Smrg }, 20532001f49Smrg { 20632001f49Smrg { 20732001f49Smrg 1.1, 1.1 20832001f49Smrg }, 20932001f49Smrg { 21032001f49Smrg -0.1, 1.1 21132001f49Smrg }, 21232001f49Smrg { 21332001f49Smrg -0.1, -0.1 21432001f49Smrg }, 21532001f49Smrg { 21632001f49Smrg 1.1, -0.1 21732001f49Smrg } 21832001f49Smrg }, 21932001f49Smrg { 22032001f49Smrg { 22132001f49Smrg 1.1, 1.1 22232001f49Smrg }, 22332001f49Smrg { 22432001f49Smrg -0.1, 1.1 22532001f49Smrg }, 22632001f49Smrg { 22732001f49Smrg -0.1, -0.1 22832001f49Smrg }, 22932001f49Smrg { 23032001f49Smrg 1.1, -0.1 23132001f49Smrg } 23232001f49Smrg }, 23332001f49Smrg { 23432001f49Smrg { 23532001f49Smrg 1.1, 1.1 23632001f49Smrg }, 23732001f49Smrg { 23832001f49Smrg -0.1, 1.1 23932001f49Smrg }, 24032001f49Smrg { 24132001f49Smrg -0.1, -0.1 24232001f49Smrg }, 24332001f49Smrg { 24432001f49Smrg 1.1, -0.1 24532001f49Smrg } 24632001f49Smrg }, 24732001f49Smrg}; 24832001f49Smrg 24932001f49Smrgstatic void BuildCube(void) 25032001f49Smrg{ 25132001f49Smrg GLint i; 25232001f49Smrg 25332001f49Smrg glNewList(cube, GL_COMPILE); 25432001f49Smrg for (i = 0; i < 6; i++) { 25532001f49Smrg glBegin(GL_POLYGON); 25632001f49Smrg glNormal3fv(n[i]); glTexCoord2fv(t[i][0]); glVertex3fv(c[i][0]); 25732001f49Smrg glNormal3fv(n[i]); glTexCoord2fv(t[i][1]); glVertex3fv(c[i][1]); 25832001f49Smrg glNormal3fv(n[i]); glTexCoord2fv(t[i][2]); glVertex3fv(c[i][2]); 25932001f49Smrg glNormal3fv(n[i]); glTexCoord2fv(t[i][3]); glVertex3fv(c[i][3]); 26032001f49Smrg glEnd(); 26132001f49Smrg } 26232001f49Smrg glEndList(); 26332001f49Smrg} 26432001f49Smrg 26532001f49Smrgstatic void BuildLists(void) 26632001f49Smrg{ 26732001f49Smrg 26832001f49Smrg cube = glGenLists(1); 26932001f49Smrg BuildCube(); 27032001f49Smrg} 27132001f49Smrg 27232001f49Smrgstatic void Init(void) 27332001f49Smrg{ 27432001f49Smrg 27532001f49Smrg glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 27632001f49Smrg gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY, 27732001f49Smrg GL_RGB, GL_UNSIGNED_BYTE, image->data); 27832001f49Smrg glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal); 27932001f49Smrg glEnable(GL_TEXTURE_2D); 28032001f49Smrg 28132001f49Smrg glFrontFace(GL_CCW); 28232001f49Smrg glCullFace(GL_FRONT); 28332001f49Smrg glEnable(GL_CULL_FACE); 28432001f49Smrg 28532001f49Smrg BuildLists(); 28632001f49Smrg 28732001f49Smrg glClearColor(0.0, 0.0, 0.0, 0.0); 28832001f49Smrg 28932001f49Smrg magFilter = nr; 29032001f49Smrg minFilter = nr; 29132001f49Smrg sWrapMode = repeat; 29232001f49Smrg tWrapMode = repeat; 29332001f49Smrg} 29432001f49Smrg 29532001f49Smrgstatic void Reshape(int width, int height) 29632001f49Smrg{ 29732001f49Smrg 29832001f49Smrg glViewport(0, 0, (GLint)width, (GLint)height); 29932001f49Smrg 30032001f49Smrg glMatrixMode(GL_PROJECTION); 30132001f49Smrg glLoadIdentity(); 30232001f49Smrg gluPerspective(145.0, 1.0, 0.01, 1000); 30332001f49Smrg glMatrixMode(GL_MODELVIEW); 30432001f49Smrg} 30532001f49Smrg 30632001f49Smrgstatic void Key2(int key, int x, int y) 30732001f49Smrg{ 30832001f49Smrg 30932001f49Smrg switch (key) { 31032001f49Smrg case GLUT_KEY_LEFT: 31132001f49Smrg yRotation -= 0.5; 31232001f49Smrg break; 31332001f49Smrg case GLUT_KEY_RIGHT: 31432001f49Smrg yRotation += 0.5; 31532001f49Smrg break; 31632001f49Smrg case GLUT_KEY_UP: 31732001f49Smrg xRotation -= 0.5; 31832001f49Smrg break; 31932001f49Smrg case GLUT_KEY_DOWN: 32032001f49Smrg xRotation += 0.5; 32132001f49Smrg break; 32232001f49Smrg default: 32332001f49Smrg return; 32432001f49Smrg } 32532001f49Smrg 32632001f49Smrg glutPostRedisplay(); 32732001f49Smrg} 32832001f49Smrg 32932001f49Smrgstatic void Key(unsigned char key, int x, int y) 33032001f49Smrg{ 33132001f49Smrg 33232001f49Smrg switch (key) { 33332001f49Smrg case 27: 33432001f49Smrg exit(1); 33532001f49Smrg 33632001f49Smrg case 'T': 33732001f49Smrg zTranslate += 0.25; 33832001f49Smrg break; 33932001f49Smrg case 't': 34032001f49Smrg zTranslate -= 0.25; 34132001f49Smrg break; 34232001f49Smrg 34332001f49Smrg case 's': 34432001f49Smrg doSphere = !doSphere; 34532001f49Smrg if (doSphere) { 34632001f49Smrg glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap); 34732001f49Smrg glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap); 34832001f49Smrg glEnable(GL_TEXTURE_GEN_S); 34932001f49Smrg glEnable(GL_TEXTURE_GEN_T); 35032001f49Smrg } else { 35132001f49Smrg glDisable(GL_TEXTURE_GEN_S); 35232001f49Smrg glDisable(GL_TEXTURE_GEN_T); 35332001f49Smrg } 35432001f49Smrg break; 35532001f49Smrg 35632001f49Smrg case '0': 35732001f49Smrg magFilter = nr; 35832001f49Smrg break; 35932001f49Smrg case '1': 36032001f49Smrg magFilter = ln; 36132001f49Smrg break; 36232001f49Smrg case '2': 36332001f49Smrg minFilter = nr; 36432001f49Smrg break; 36532001f49Smrg case '3': 36632001f49Smrg minFilter = ln; 36732001f49Smrg break; 36832001f49Smrg case '4': 36932001f49Smrg minFilter = nr_mipmap_nr; 37032001f49Smrg break; 37132001f49Smrg case '5': 37232001f49Smrg minFilter = nr_mipmap_ln; 37332001f49Smrg break; 37432001f49Smrg case '6': 37532001f49Smrg minFilter = ln_mipmap_nr; 37632001f49Smrg break; 37732001f49Smrg case '7': 37832001f49Smrg minFilter = ln_mipmap_ln; 37932001f49Smrg break; 38032001f49Smrg default: 38132001f49Smrg return; 38232001f49Smrg } 38332001f49Smrg 38432001f49Smrg glutPostRedisplay(); 38532001f49Smrg} 38632001f49Smrg 38732001f49Smrgstatic void Draw(void) 38832001f49Smrg{ 38932001f49Smrg 39032001f49Smrg glClear(GL_COLOR_BUFFER_BIT); 39132001f49Smrg 39232001f49Smrg glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode); 39332001f49Smrg glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode); 39432001f49Smrg glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); 39532001f49Smrg glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); 39632001f49Smrg 39732001f49Smrg glPushMatrix(); 39832001f49Smrg 39932001f49Smrg glTranslatef(0.0, 0.0, zTranslate); 40032001f49Smrg glRotatef(xRotation, 1, 0, 0); 40132001f49Smrg glRotatef(yRotation, 0, 1, 0); 40232001f49Smrg glCallList(cube); 40332001f49Smrg 40432001f49Smrg glPopMatrix(); 40532001f49Smrg 40632001f49Smrg glFlush(); 40732001f49Smrg 40832001f49Smrg if (doubleBuffer) { 40932001f49Smrg glutSwapBuffers(); 41032001f49Smrg } 41132001f49Smrg} 41232001f49Smrg 41332001f49Smrgstatic GLenum Args(int argc, char **argv) 41432001f49Smrg{ 41532001f49Smrg GLint i; 41632001f49Smrg 41732001f49Smrg doubleBuffer = GL_FALSE; 41832001f49Smrg 41932001f49Smrg for (i = 1; i < argc; i++) { 42032001f49Smrg if (strcmp(argv[i], "-sb") == 0) { 42132001f49Smrg doubleBuffer = GL_FALSE; 42232001f49Smrg } else if (strcmp(argv[i], "-db") == 0) { 42332001f49Smrg doubleBuffer = GL_TRUE; 42432001f49Smrg } else if (strcmp(argv[i], "-f") == 0) { 42532001f49Smrg if (i+1 >= argc || argv[i+1][0] == '-') { 42632001f49Smrg printf("-f (No file name).\n"); 42732001f49Smrg return GL_FALSE; 42832001f49Smrg } else { 42932001f49Smrg texFileName = argv[++i]; 43032001f49Smrg } 43132001f49Smrg } else { 43232001f49Smrg printf("%s (Bad option).\n", argv[i]); 43332001f49Smrg return GL_FALSE; 43432001f49Smrg } 43532001f49Smrg } 43632001f49Smrg return GL_TRUE; 43732001f49Smrg} 43832001f49Smrg 43932001f49Smrgint main(int argc, char **argv) 44032001f49Smrg{ 44132001f49Smrg GLenum type; 44232001f49Smrg 44332001f49Smrg glutInit(&argc, argv); 44432001f49Smrg 44532001f49Smrg if (Args(argc, argv) == GL_FALSE) { 44632001f49Smrg exit(1); 44732001f49Smrg } 44832001f49Smrg 44932001f49Smrg if (texFileName == 0) { 45032001f49Smrg printf("No image file.\n"); 45132001f49Smrg exit(1); 45232001f49Smrg } 45332001f49Smrg 45432001f49Smrg image = LoadPPM(texFileName); 45532001f49Smrg 45632001f49Smrg glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300); 45732001f49Smrg 45832001f49Smrg type = GLUT_RGB; 45932001f49Smrg type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; 46032001f49Smrg glutInitDisplayMode(type); 46132001f49Smrg 46232001f49Smrg if (glutCreateWindow("Texture Test") == GL_FALSE) { 46332001f49Smrg exit(1); 46432001f49Smrg } 46532001f49Smrg 46632001f49Smrg Init(); 46732001f49Smrg 46832001f49Smrg glutReshapeFunc(Reshape); 46932001f49Smrg glutKeyboardFunc(Key); 47032001f49Smrg glutSpecialFunc(Key2); 47132001f49Smrg glutDisplayFunc(Draw); 47232001f49Smrg glutMainLoop(); 47332001f49Smrg return 0; 47432001f49Smrg} 475