blob: c04d5004e37572f6ee2355ef0b220db2f442765e [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001
2/*
3 * Textured cylinder demo: lighting, texturing, reflection mapping.
Brian Paulac126091999-10-21 16:39:06 +00004 *
5 * Command line options:
6 * -info print GL implementation information
7 *
8 *
jtgafb833d1999-08-19 00:55:39 +00009 * Brian Paul May 1997 This program is in the public domain.
10 */
11
jtgafb833d1999-08-19 00:55:39 +000012#include <stdio.h>
13#include <stdlib.h>
14#include <math.h>
Brian Paul92eddb02005-01-09 17:37:50 +000015#include <string.h>
jtgafb833d1999-08-19 00:55:39 +000016#include <GL/glut.h>
17
Brian Paul92eddb02005-01-09 17:37:50 +000018#include "readtex.h"
jtgafb833d1999-08-19 00:55:39 +000019
20#define TEXTURE_FILE "../images/reflect.rgb"
21
22#define LIT 1
23#define TEXTURED 2
24#define REFLECT 3
25#define ANIMATE 10
26#define POINT_FILTER 20
27#define LINEAR_FILTER 21
28#define QUIT 100
29
30static GLuint CylinderObj = 0;
31static GLboolean Animate = GL_TRUE;
32
33static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
Brian Paul92eddb02005-01-09 17:37:50 +000034static GLfloat DXrot = 50.0, DYrot = 125.0;
jtgafb833d1999-08-19 00:55:39 +000035
Brian Paul4f664982000-09-29 23:09:39 +000036/* performance info */
37static GLint T0 = 0;
38static GLint Frames = 0;
39
jtgafb833d1999-08-19 00:55:39 +000040
41static void Idle( void )
42{
Brian Paul92eddb02005-01-09 17:37:50 +000043 static double t0 = -1.;
44 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
45 if (t0 < 0.0)
46 t0 = t;
47 dt = t - t0;
48 t0 = t;
49
jtgafb833d1999-08-19 00:55:39 +000050 if (Animate) {
Brian Paul92eddb02005-01-09 17:37:50 +000051 Xrot += DXrot * dt;
52 Yrot += DYrot * dt;
jtgafb833d1999-08-19 00:55:39 +000053 glutPostRedisplay();
54 }
55}
56
57
58static void Display( void )
59{
60 glClear( GL_COLOR_BUFFER_BIT );
61
62 glPushMatrix();
63 glRotatef(Xrot, 1.0, 0.0, 0.0);
64 glRotatef(Yrot, 0.0, 1.0, 0.0);
65 glRotatef(Zrot, 0.0, 0.0, 1.0);
66 glScalef(5.0, 5.0, 5.0);
67 glCallList(CylinderObj);
68
69 glPopMatrix();
70
71 glutSwapBuffers();
Brian Paul4f664982000-09-29 23:09:39 +000072
73 if (Animate) {
74 GLint t = glutGet(GLUT_ELAPSED_TIME);
75 Frames++;
76 if (t - T0 >= 5000) {
77 GLfloat seconds = (t - T0) / 1000.0;
78 GLfloat fps = Frames / seconds;
79 printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
80 T0 = t;
81 Frames = 0;
82 }
83 }
jtgafb833d1999-08-19 00:55:39 +000084}
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;
Brian Paul0b126502003-08-28 03:07:49 +0000126 if (Animate)
127 glutIdleFunc(Idle);
128 else
129 glutIdleFunc(NULL);
jtgafb833d1999-08-19 00:55:39 +0000130 }
131 else if (entry==POINT_FILTER) {
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
134 }
135 else if (entry==LINEAR_FILTER) {
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
138 }
139 else if (entry==QUIT) {
140 exit(0);
141 }
142 else {
143 SetMode(entry);
144 }
145 glutPostRedisplay();
146}
147
148
149static void Key( unsigned char key, int x, int y )
150{
151 (void) x;
152 (void) y;
153 switch (key) {
Brian Paul0b126502003-08-28 03:07:49 +0000154 case ' ':
155 Animate = !Animate;
156 if (Animate)
157 glutIdleFunc(Idle);
158 else
159 glutIdleFunc(NULL);
160 break;
jtgafb833d1999-08-19 00:55:39 +0000161 case 27:
162 exit(0);
163 break;
164 }
165 glutPostRedisplay();
166}
167
168
169static void SpecialKey( int key, int x, int y )
170{
171 float step = 3.0;
172 (void) x;
173 (void) y;
174
175 switch (key) {
176 case GLUT_KEY_UP:
177 Xrot += step;
178 break;
179 case GLUT_KEY_DOWN:
180 Xrot -= step;
181 break;
182 case GLUT_KEY_LEFT:
183 Yrot += step;
184 break;
185 case GLUT_KEY_RIGHT:
186 Yrot -= step;
187 break;
188 }
189 glutPostRedisplay();
190}
191
192
Brian Paulac126091999-10-21 16:39:06 +0000193static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000194{
195 GLUquadricObj *q = gluNewQuadric();
196 CylinderObj = glGenLists(1);
197 glNewList(CylinderObj, GL_COMPILE);
198
199 glTranslatef(0.0, 0.0, -1.0);
200
201 /* cylinder */
202 gluQuadricNormals(q, GL_SMOOTH);
203 gluQuadricTexture(q, GL_TRUE);
204 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
205
206 /* end cap */
207 glTranslatef(0.0, 0.0, 2.0);
208 gluDisk(q, 0.0, 0.6, 24, 1);
209
210 /* other end cap */
211 glTranslatef(0.0, 0.0, -2.0);
212 gluQuadricOrientation(q, GLU_INSIDE);
213 gluDisk(q, 0.0, 0.6, 24, 1);
214
215 glEndList();
216 gluDeleteQuadric(q);
217
218 /* lighting */
219 glEnable(GL_LIGHTING);
220 {
221 GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
222 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
223 GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
224 glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
225 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
226 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
227 glEnable(GL_LIGHT0);
228 }
229
230 /* fitering = nearest, initially */
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
233
234 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
235 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
236
237 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
238 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
239
240 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
241 printf("Error: couldn't load texture image\n");
242 exit(1);
243 }
244
245 glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
246
247 SetMode(LIT);
Brian Paulac126091999-10-21 16:39:06 +0000248
249 if (argc > 1 && strcmp(argv[1], "-info")==0) {
250 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
251 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
252 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
253 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
254 }
jtgafb833d1999-08-19 00:55:39 +0000255}
256
257
258int main( int argc, char *argv[] )
259{
260 glutInit( &argc, argv );
261 glutInitWindowSize( 400, 400 );
Brian Paul8afa9e52001-03-27 17:35:26 +0000262 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000263
264 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
265
266 glutCreateWindow(argv[0] );
267
Brian Paulac126091999-10-21 16:39:06 +0000268 Init(argc, argv);
jtgafb833d1999-08-19 00:55:39 +0000269
270 glutReshapeFunc( Reshape );
271 glutKeyboardFunc( Key );
272 glutSpecialFunc( SpecialKey );
273 glutDisplayFunc( Display );
274 glutIdleFunc( Idle );
275
276 glutCreateMenu(ModeMenu);
277 glutAddMenuEntry("Lit", LIT);
278 glutAddMenuEntry("Textured", TEXTURED);
279 glutAddMenuEntry("Reflect", REFLECT);
280 glutAddMenuEntry("Point Filtered", POINT_FILTER);
281 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
282 glutAddMenuEntry("Toggle Animation", ANIMATE);
283 glutAddMenuEntry("Quit", QUIT);
284 glutAttachMenu(GLUT_RIGHT_BUTTON);
285
286 glutMainLoop();
287 return 0;
288}