texgen.c revision 32001f49
1/*
2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 * Permission to use, copy, modify, and distribute this software for
5 * any purpose and without fee is hereby granted, provided that the above
6 * copyright notice appear in all copies and that both the copyright notice
7 * and this permission notice appear in supporting documentation, and that
8 * the name of Silicon Graphics, Inc. not be used in advertising
9 * or publicity pertaining to distribution of the software without specific,
10 * written prior permission.
11 *
12 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
13 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
14 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
15 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
16 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
17 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
18 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
19 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
20 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
21 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
23 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 * US Government Users Restricted Rights
26 * Use, duplication, or disclosure by the Government is subject to
27 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
28 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
29 * clause at DFARS 252.227-7013 and/or in similar or successor
30 * clauses in the FAR or the DOD or NASA FAR Supplement.
31 * Unpublished-- rights reserved under the copyright laws of the
32 * United States.  Contractor/manufacturer is Silicon Graphics,
33 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
34 *
35 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
36 */
37
38/*  texgen.c
39 *  This program draws a texture mapped teapot with
40 *  automatically generated texture coordinates.  The
41 *  texture is rendered as stripes on the teapot.
42 *  Initially, the object is drawn with texture coordinates
43 *  based upon the object coordinates of the vertex
44 *  and distance from the plane x = 0.  Pressing the 'e'
45 *  key changes the coordinate generation to eye coordinates
46 *  of the vertex.  Pressing the 'o' key switches it back
47 *  to the object coordinates.  Pressing the 's' key
48 *  changes the plane to a slanted one (x + y + z = 0).
49 *  Pressing the 'x' key switches it back to x = 0.
50 */
51
52#include "glut_wrap.h"
53#include <stdlib.h>
54#include <stdio.h>
55
56#define	stripeImageWidth 32
57GLubyte stripeImage[4*stripeImageWidth];
58
59#ifdef GL_VERSION_1_1
60static GLuint texName;
61#endif
62
63static void makeStripeImage(void)
64{
65   int j;
66
67   for (j = 0; j < stripeImageWidth; j++) {
68      stripeImage[4*j] = (GLubyte) ((j<=4) ? 255 : 0);
69      stripeImage[4*j+1] = (GLubyte) ((j>4) ? 255 : 0);
70      stripeImage[4*j+2] = (GLubyte) 0;
71      stripeImage[4*j+3] = (GLubyte) 255;
72   }
73}
74
75/*  planes for texture coordinate generation  */
76static GLfloat xequalzero[] = {1.0, 0.0, 0.0, 0.0};
77static GLfloat slanted[] = {1.0, 1.0, 1.0, 0.0};
78static GLfloat *currentCoeff;
79static GLenum currentPlane;
80static GLint currentGenMode;
81
82static void init(void)
83{
84   glClearColor (0.0, 0.0, 0.0, 0.0);
85   glEnable(GL_DEPTH_TEST);
86   glShadeModel(GL_SMOOTH);
87
88   makeStripeImage();
89   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
90
91#ifdef GL_VERSION_1_1
92   glGenTextures(1, &texName);
93   glBindTexture(GL_TEXTURE_1D, texName);
94#endif
95   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
96   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
97   glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
98#ifdef GL_VERSION_1_1
99   glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, stripeImageWidth, 0,
100                GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
101#else
102   glTexImage1D(GL_TEXTURE_1D, 0, 4, stripeImageWidth, 0,
103                GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
104#endif
105
106   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
107   currentCoeff = xequalzero;
108   currentGenMode = GL_OBJECT_LINEAR;
109   currentPlane = GL_OBJECT_PLANE;
110   glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
111   glTexGenfv(GL_S, currentPlane, currentCoeff);
112
113   glEnable(GL_TEXTURE_GEN_S);
114   glEnable(GL_TEXTURE_1D);
115   glEnable(GL_CULL_FACE);
116   glEnable(GL_LIGHTING);
117   glEnable(GL_LIGHT0);
118   glEnable(GL_AUTO_NORMAL);
119   glEnable(GL_NORMALIZE);
120   glFrontFace(GL_CW);
121   glCullFace(GL_BACK);
122   glMaterialf (GL_FRONT, GL_SHININESS, 64.0);
123}
124
125static void display(void)
126{
127   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
128
129   glPushMatrix ();
130   glRotatef(45.0, 0.0, 0.0, 1.0);
131#ifdef GL_VERSION_1_1
132   glBindTexture(GL_TEXTURE_1D, texName);
133#endif
134   glutSolidTeapot(2.0);
135   glPopMatrix ();
136   glFlush();
137}
138
139static void reshape(int w, int h)
140{
141   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
142   glMatrixMode(GL_PROJECTION);
143   glLoadIdentity();
144   if (w <= h)
145      glOrtho (-3.5, 3.5, -3.5*(GLfloat)h/(GLfloat)w,
146               3.5*(GLfloat)h/(GLfloat)w, -3.5, 3.5);
147   else
148      glOrtho (-3.5*(GLfloat)w/(GLfloat)h,
149               3.5*(GLfloat)w/(GLfloat)h, -3.5, 3.5, -3.5, 3.5);
150   glMatrixMode(GL_MODELVIEW);
151   glLoadIdentity();
152}
153
154/* ARGSUSED1 */
155static void keyboard (unsigned char key, int x, int y)
156{
157   switch (key) {
158      case 'e':
159      case 'E':
160         currentGenMode = GL_EYE_LINEAR;
161         currentPlane = GL_EYE_PLANE;
162         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
163         glTexGenfv(GL_S, currentPlane, currentCoeff);
164         glutPostRedisplay();
165         break;
166      case 'o':
167      case 'O':
168         currentGenMode = GL_OBJECT_LINEAR;
169         currentPlane = GL_OBJECT_PLANE;
170         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
171         glTexGenfv(GL_S, currentPlane, currentCoeff);
172         glutPostRedisplay();
173         break;
174      case 's':
175      case 'S':
176         currentCoeff = slanted;
177         glTexGenfv(GL_S, currentPlane, currentCoeff);
178         glutPostRedisplay();
179         break;
180      case 'x':
181      case 'X':
182         currentCoeff = xequalzero;
183         glTexGenfv(GL_S, currentPlane, currentCoeff);
184         glutPostRedisplay();
185         break;
186      case 27:
187         exit(0);
188         break;
189      default:
190         break;
191   }
192}
193
194int main(int argc, char** argv)
195{
196   glutInit(&argc, argv);
197   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
198   glutInitWindowSize(256, 256);
199   glutInitWindowPosition(100, 100);
200   glutCreateWindow (argv[0]);
201   init ();
202   glutDisplayFunc(display);
203   glutReshapeFunc(reshape);
204   glutKeyboardFunc(keyboard);
205   glutMainLoop();
206   return 0;
207}
208