blob: 0358d2975ccd0bd38fe995492cc2beec1b6e4d16 [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001/* $Id: texcyl.c,v 1.1 1999/08/19 00:55:40 jtg Exp $ */
2
3/*
4 * Textured cylinder demo: lighting, texturing, reflection mapping.
5 * Brian Paul May 1997 This program is in the public domain.
6 */
7
8/*
9 * $Log: texcyl.c,v $
10 * Revision 1.1 1999/08/19 00:55:40 jtg
11 * Initial revision
12 *
13 * Revision 3.3 1999/03/28 18:24:37 brianp
14 * minor clean-up
15 *
16 * Revision 3.2 1998/11/05 04:34:04 brianp
17 * moved image files to ../images/ directory
18 *
19 * Revision 3.1 1998/06/23 03:16:51 brianp
20 * added Point/Linear sampling menu items
21 *
22 * Revision 3.0 1998/02/14 18:42:29 brianp
23 * initial rev
24 *
25 */
26
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <math.h>
31#include <GL/glut.h>
32
33#include "../util/readtex.c" /* I know, this is a hack. */
34
35#define TEXTURE_FILE "../images/reflect.rgb"
36
37#define LIT 1
38#define TEXTURED 2
39#define REFLECT 3
40#define ANIMATE 10
41#define POINT_FILTER 20
42#define LINEAR_FILTER 21
43#define QUIT 100
44
45static GLuint CylinderObj = 0;
46static GLboolean Animate = GL_TRUE;
47
48static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
49static GLfloat DXrot = 1.0, DYrot = 2.5;
50
51
52static void Idle( void )
53{
54 if (Animate) {
55 Xrot += DXrot;
56 Yrot += DYrot;
57 glutPostRedisplay();
58 }
59}
60
61
62static void Display( void )
63{
64 glClear( GL_COLOR_BUFFER_BIT );
65
66 glPushMatrix();
67 glRotatef(Xrot, 1.0, 0.0, 0.0);
68 glRotatef(Yrot, 0.0, 1.0, 0.0);
69 glRotatef(Zrot, 0.0, 0.0, 1.0);
70 glScalef(5.0, 5.0, 5.0);
71 glCallList(CylinderObj);
72
73 glPopMatrix();
74
75 glutSwapBuffers();
76}
77
78
79static void Reshape( int width, int height )
80{
81 glViewport( 0, 0, width, height );
82 glMatrixMode( GL_PROJECTION );
83 glLoadIdentity();
84 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
85 glMatrixMode( GL_MODELVIEW );
86 glLoadIdentity();
87 glTranslatef( 0.0, 0.0, -70.0 );
88}
89
90
91static void SetMode(GLuint m)
92{
93 /* disable everything */
94 glDisable(GL_LIGHTING);
95 glDisable(GL_TEXTURE_2D);
96 glDisable(GL_TEXTURE_GEN_S);
97 glDisable(GL_TEXTURE_GEN_T);
98
99 /* enable what's needed */
100 if (m==LIT) {
101 glEnable(GL_LIGHTING);
102 }
103 else if (m==TEXTURED) {
104 glEnable(GL_TEXTURE_2D);
105 }
106 else if (m==REFLECT) {
107 glEnable(GL_TEXTURE_2D);
108 glEnable(GL_TEXTURE_GEN_S);
109 glEnable(GL_TEXTURE_GEN_T);
110 }
111}
112
113
114static void ModeMenu(int entry)
115{
116 if (entry==ANIMATE) {
117 Animate = !Animate;
118 }
119 else if (entry==POINT_FILTER) {
120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
122 }
123 else if (entry==LINEAR_FILTER) {
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
126 }
127 else if (entry==QUIT) {
128 exit(0);
129 }
130 else {
131 SetMode(entry);
132 }
133 glutPostRedisplay();
134}
135
136
137static void Key( unsigned char key, int x, int y )
138{
139 (void) x;
140 (void) y;
141 switch (key) {
142 case 27:
143 exit(0);
144 break;
145 }
146 glutPostRedisplay();
147}
148
149
150static void SpecialKey( int key, int x, int y )
151{
152 float step = 3.0;
153 (void) x;
154 (void) y;
155
156 switch (key) {
157 case GLUT_KEY_UP:
158 Xrot += step;
159 break;
160 case GLUT_KEY_DOWN:
161 Xrot -= step;
162 break;
163 case GLUT_KEY_LEFT:
164 Yrot += step;
165 break;
166 case GLUT_KEY_RIGHT:
167 Yrot -= step;
168 break;
169 }
170 glutPostRedisplay();
171}
172
173
174static void Init( void )
175{
176 GLUquadricObj *q = gluNewQuadric();
177 CylinderObj = glGenLists(1);
178 glNewList(CylinderObj, GL_COMPILE);
179
180 glTranslatef(0.0, 0.0, -1.0);
181
182 /* cylinder */
183 gluQuadricNormals(q, GL_SMOOTH);
184 gluQuadricTexture(q, GL_TRUE);
185 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
186
187 /* end cap */
188 glTranslatef(0.0, 0.0, 2.0);
189 gluDisk(q, 0.0, 0.6, 24, 1);
190
191 /* other end cap */
192 glTranslatef(0.0, 0.0, -2.0);
193 gluQuadricOrientation(q, GLU_INSIDE);
194 gluDisk(q, 0.0, 0.6, 24, 1);
195
196 glEndList();
197 gluDeleteQuadric(q);
198
199 /* lighting */
200 glEnable(GL_LIGHTING);
201 {
202 GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
203 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
204 GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
205 glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
206 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
207 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
208 glEnable(GL_LIGHT0);
209 }
210
211 /* fitering = nearest, initially */
212 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
213 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
214
215 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
216 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
217
218 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
219 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
220
221 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
222 printf("Error: couldn't load texture image\n");
223 exit(1);
224 }
225
226 glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
227
228 SetMode(LIT);
229}
230
231
232int main( int argc, char *argv[] )
233{
234 glutInit( &argc, argv );
235 glutInitWindowSize( 400, 400 );
236
237 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
238
239 glutCreateWindow(argv[0] );
240
241 Init();
242
243 glutReshapeFunc( Reshape );
244 glutKeyboardFunc( Key );
245 glutSpecialFunc( SpecialKey );
246 glutDisplayFunc( Display );
247 glutIdleFunc( Idle );
248
249 glutCreateMenu(ModeMenu);
250 glutAddMenuEntry("Lit", LIT);
251 glutAddMenuEntry("Textured", TEXTURED);
252 glutAddMenuEntry("Reflect", REFLECT);
253 glutAddMenuEntry("Point Filtered", POINT_FILTER);
254 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
255 glutAddMenuEntry("Toggle Animation", ANIMATE);
256 glutAddMenuEntry("Quit", QUIT);
257 glutAttachMenu(GLUT_RIGHT_BUTTON);
258
259 glutMainLoop();
260 return 0;
261}