blob: 4df97ec4f502593f44518e067b0c58c4bf2db26b [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
Brian8bef18f2008-04-01 14:56:10 -060030static GLint Win = -1;
31
jtgafb833d1999-08-19 00:55:39 +000032static GLuint CylinderObj = 0;
33static GLboolean Animate = GL_TRUE;
34
35static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
Brian Paul92eddb02005-01-09 17:37:50 +000036static GLfloat DXrot = 50.0, DYrot = 125.0;
jtgafb833d1999-08-19 00:55:39 +000037
Brian Paul4f664982000-09-29 23:09:39 +000038/* performance info */
39static GLint T0 = 0;
40static GLint Frames = 0;
41
jtgafb833d1999-08-19 00:55:39 +000042
43static void Idle( void )
44{
Brian Paul92eddb02005-01-09 17:37:50 +000045 static double t0 = -1.;
46 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
47 if (t0 < 0.0)
48 t0 = t;
49 dt = t - t0;
50 t0 = t;
51
jtgafb833d1999-08-19 00:55:39 +000052 if (Animate) {
Brian Paul92eddb02005-01-09 17:37:50 +000053 Xrot += DXrot * dt;
54 Yrot += DYrot * dt;
jtgafb833d1999-08-19 00:55:39 +000055 glutPostRedisplay();
56 }
57}
58
59
60static void Display( void )
61{
62 glClear( GL_COLOR_BUFFER_BIT );
63
64 glPushMatrix();
65 glRotatef(Xrot, 1.0, 0.0, 0.0);
66 glRotatef(Yrot, 0.0, 1.0, 0.0);
67 glRotatef(Zrot, 0.0, 0.0, 1.0);
68 glScalef(5.0, 5.0, 5.0);
69 glCallList(CylinderObj);
70
71 glPopMatrix();
72
73 glutSwapBuffers();
Brian Paul4f664982000-09-29 23:09:39 +000074
75 if (Animate) {
76 GLint t = glutGet(GLUT_ELAPSED_TIME);
77 Frames++;
78 if (t - T0 >= 5000) {
79 GLfloat seconds = (t - T0) / 1000.0;
80 GLfloat fps = Frames / seconds;
81 printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
Keith Whitwelle6479c62009-02-24 12:02:24 +000082 fflush(stdout);
Brian Paul4f664982000-09-29 23:09:39 +000083 T0 = t;
84 Frames = 0;
85 }
86 }
jtgafb833d1999-08-19 00:55:39 +000087}
88
89
90static void Reshape( int width, int height )
91{
92 glViewport( 0, 0, width, height );
93 glMatrixMode( GL_PROJECTION );
94 glLoadIdentity();
95 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
96 glMatrixMode( GL_MODELVIEW );
97 glLoadIdentity();
98 glTranslatef( 0.0, 0.0, -70.0 );
99}
100
101
102static void SetMode(GLuint m)
103{
104 /* disable everything */
105 glDisable(GL_LIGHTING);
106 glDisable(GL_TEXTURE_2D);
107 glDisable(GL_TEXTURE_GEN_S);
108 glDisable(GL_TEXTURE_GEN_T);
109
110 /* enable what's needed */
111 if (m==LIT) {
112 glEnable(GL_LIGHTING);
113 }
114 else if (m==TEXTURED) {
115 glEnable(GL_TEXTURE_2D);
116 }
117 else if (m==REFLECT) {
118 glEnable(GL_TEXTURE_2D);
119 glEnable(GL_TEXTURE_GEN_S);
120 glEnable(GL_TEXTURE_GEN_T);
121 }
122}
123
124
125static void ModeMenu(int entry)
126{
127 if (entry==ANIMATE) {
128 Animate = !Animate;
Brian Paul0b126502003-08-28 03:07:49 +0000129 if (Animate)
130 glutIdleFunc(Idle);
131 else
132 glutIdleFunc(NULL);
jtgafb833d1999-08-19 00:55:39 +0000133 }
134 else if (entry==POINT_FILTER) {
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
137 }
138 else if (entry==LINEAR_FILTER) {
139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
141 }
142 else if (entry==QUIT) {
143 exit(0);
144 }
145 else {
146 SetMode(entry);
147 }
148 glutPostRedisplay();
149}
150
151
152static void Key( unsigned char key, int x, int y )
153{
154 (void) x;
155 (void) y;
156 switch (key) {
Brian Paul0b126502003-08-28 03:07:49 +0000157 case ' ':
158 Animate = !Animate;
159 if (Animate)
160 glutIdleFunc(Idle);
161 else
162 glutIdleFunc(NULL);
163 break;
jtgafb833d1999-08-19 00:55:39 +0000164 case 27:
Brian8bef18f2008-04-01 14:56:10 -0600165 glutDestroyWindow(Win);
jtgafb833d1999-08-19 00:55:39 +0000166 exit(0);
167 break;
168 }
169 glutPostRedisplay();
170}
171
172
173static void SpecialKey( int key, int x, int y )
174{
175 float step = 3.0;
176 (void) x;
177 (void) y;
178
179 switch (key) {
180 case GLUT_KEY_UP:
181 Xrot += step;
182 break;
183 case GLUT_KEY_DOWN:
184 Xrot -= step;
185 break;
186 case GLUT_KEY_LEFT:
187 Yrot += step;
188 break;
189 case GLUT_KEY_RIGHT:
190 Yrot -= step;
191 break;
192 }
193 glutPostRedisplay();
194}
195
196
Brian Paulac126091999-10-21 16:39:06 +0000197static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000198{
199 GLUquadricObj *q = gluNewQuadric();
200 CylinderObj = glGenLists(1);
201 glNewList(CylinderObj, GL_COMPILE);
202
203 glTranslatef(0.0, 0.0, -1.0);
204
205 /* cylinder */
206 gluQuadricNormals(q, GL_SMOOTH);
207 gluQuadricTexture(q, GL_TRUE);
208 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
209
210 /* end cap */
211 glTranslatef(0.0, 0.0, 2.0);
212 gluDisk(q, 0.0, 0.6, 24, 1);
213
214 /* other end cap */
215 glTranslatef(0.0, 0.0, -2.0);
216 gluQuadricOrientation(q, GLU_INSIDE);
217 gluDisk(q, 0.0, 0.6, 24, 1);
218
219 glEndList();
220 gluDeleteQuadric(q);
221
222 /* lighting */
223 glEnable(GL_LIGHTING);
224 {
225 GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
226 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
227 GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
228 glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
229 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
230 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
231 glEnable(GL_LIGHT0);
232 }
233
234 /* fitering = nearest, initially */
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
237
238 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
239 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
240
241 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
242 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
243
244 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
245 printf("Error: couldn't load texture image\n");
246 exit(1);
247 }
248
249 glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
250
251 SetMode(LIT);
Brian Paulac126091999-10-21 16:39:06 +0000252
253 if (argc > 1 && strcmp(argv[1], "-info")==0) {
254 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
255 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
256 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
257 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
258 }
jtgafb833d1999-08-19 00:55:39 +0000259}
260
261
262int main( int argc, char *argv[] )
263{
jtgafb833d1999-08-19 00:55:39 +0000264 glutInitWindowSize( 400, 400 );
Brian Paul263f4322009-12-18 08:12:55 -0700265 glutInit( &argc, argv );
jtgafb833d1999-08-19 00:55:39 +0000266 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
267
Brian8bef18f2008-04-01 14:56:10 -0600268 Win = glutCreateWindow(argv[0] );
jtgafb833d1999-08-19 00:55:39 +0000269
Brian Paulac126091999-10-21 16:39:06 +0000270 Init(argc, argv);
jtgafb833d1999-08-19 00:55:39 +0000271
272 glutReshapeFunc( Reshape );
273 glutKeyboardFunc( Key );
274 glutSpecialFunc( SpecialKey );
275 glutDisplayFunc( Display );
276 glutIdleFunc( Idle );
277
278 glutCreateMenu(ModeMenu);
279 glutAddMenuEntry("Lit", LIT);
280 glutAddMenuEntry("Textured", TEXTURED);
281 glutAddMenuEntry("Reflect", REFLECT);
282 glutAddMenuEntry("Point Filtered", POINT_FILTER);
283 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
284 glutAddMenuEntry("Toggle Animation", ANIMATE);
285 glutAddMenuEntry("Quit", QUIT);
286 glutAttachMenu(GLUT_RIGHT_BUTTON);
287
288 glutMainLoop();
289 return 0;
290}