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