blob: 52f7da0af205b264d17c28bfceb112589f3580b9 [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>
15#include <GL/glut.h>
16
pescod1ff1f62000-12-24 22:53:54 +000017#include "readtex.c" /* I know, this is a hack. */
jtgafb833d1999-08-19 00:55:39 +000018
19#define TEXTURE_FILE "../images/reflect.rgb"
20
21#define LIT 1
22#define TEXTURED 2
23#define REFLECT 3
24#define ANIMATE 10
25#define POINT_FILTER 20
26#define LINEAR_FILTER 21
27#define QUIT 100
28
29static GLuint CylinderObj = 0;
30static GLboolean Animate = GL_TRUE;
31
32static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
33static GLfloat DXrot = 1.0, DYrot = 2.5;
34
Brian Paul4f664982000-09-29 23:09:39 +000035/* performance info */
36static GLint T0 = 0;
37static GLint Frames = 0;
38
jtgafb833d1999-08-19 00:55:39 +000039
40static void Idle( void )
41{
42 if (Animate) {
43 Xrot += DXrot;
44 Yrot += DYrot;
45 glutPostRedisplay();
46 }
47}
48
49
50static void Display( void )
51{
52 glClear( GL_COLOR_BUFFER_BIT );
53
54 glPushMatrix();
55 glRotatef(Xrot, 1.0, 0.0, 0.0);
56 glRotatef(Yrot, 0.0, 1.0, 0.0);
57 glRotatef(Zrot, 0.0, 0.0, 1.0);
58 glScalef(5.0, 5.0, 5.0);
59 glCallList(CylinderObj);
60
61 glPopMatrix();
62
63 glutSwapBuffers();
Brian Paul4f664982000-09-29 23:09:39 +000064
65 if (Animate) {
66 GLint t = glutGet(GLUT_ELAPSED_TIME);
67 Frames++;
68 if (t - T0 >= 5000) {
69 GLfloat seconds = (t - T0) / 1000.0;
70 GLfloat fps = Frames / seconds;
71 printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
72 T0 = t;
73 Frames = 0;
74 }
75 }
jtgafb833d1999-08-19 00:55:39 +000076}
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;
Brian Paul0b126502003-08-28 03:07:49 +0000118 if (Animate)
119 glutIdleFunc(Idle);
120 else
121 glutIdleFunc(NULL);
jtgafb833d1999-08-19 00:55:39 +0000122 }
123 else if (entry==POINT_FILTER) {
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
126 }
127 else if (entry==LINEAR_FILTER) {
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
129 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
130 }
131 else if (entry==QUIT) {
132 exit(0);
133 }
134 else {
135 SetMode(entry);
136 }
137 glutPostRedisplay();
138}
139
140
141static void Key( unsigned char key, int x, int y )
142{
143 (void) x;
144 (void) y;
145 switch (key) {
Brian Paul0b126502003-08-28 03:07:49 +0000146 case ' ':
147 Animate = !Animate;
148 if (Animate)
149 glutIdleFunc(Idle);
150 else
151 glutIdleFunc(NULL);
152 break;
jtgafb833d1999-08-19 00:55:39 +0000153 case 27:
154 exit(0);
155 break;
156 }
157 glutPostRedisplay();
158}
159
160
161static void SpecialKey( int key, int x, int y )
162{
163 float step = 3.0;
164 (void) x;
165 (void) y;
166
167 switch (key) {
168 case GLUT_KEY_UP:
169 Xrot += step;
170 break;
171 case GLUT_KEY_DOWN:
172 Xrot -= step;
173 break;
174 case GLUT_KEY_LEFT:
175 Yrot += step;
176 break;
177 case GLUT_KEY_RIGHT:
178 Yrot -= step;
179 break;
180 }
181 glutPostRedisplay();
182}
183
184
Brian Paulac126091999-10-21 16:39:06 +0000185static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000186{
187 GLUquadricObj *q = gluNewQuadric();
188 CylinderObj = glGenLists(1);
189 glNewList(CylinderObj, GL_COMPILE);
190
191 glTranslatef(0.0, 0.0, -1.0);
192
193 /* cylinder */
194 gluQuadricNormals(q, GL_SMOOTH);
195 gluQuadricTexture(q, GL_TRUE);
196 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
197
198 /* end cap */
199 glTranslatef(0.0, 0.0, 2.0);
200 gluDisk(q, 0.0, 0.6, 24, 1);
201
202 /* other end cap */
203 glTranslatef(0.0, 0.0, -2.0);
204 gluQuadricOrientation(q, GLU_INSIDE);
205 gluDisk(q, 0.0, 0.6, 24, 1);
206
207 glEndList();
208 gluDeleteQuadric(q);
209
210 /* lighting */
211 glEnable(GL_LIGHTING);
212 {
213 GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
214 GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
215 GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
216 glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
217 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
218 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
219 glEnable(GL_LIGHT0);
220 }
221
222 /* fitering = nearest, initially */
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
225
226 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
227 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
228
229 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
230 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
231
232 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
233 printf("Error: couldn't load texture image\n");
234 exit(1);
235 }
236
237 glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
238
239 SetMode(LIT);
Brian Paulac126091999-10-21 16:39:06 +0000240
241 if (argc > 1 && strcmp(argv[1], "-info")==0) {
242 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
243 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
244 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
245 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
246 }
jtgafb833d1999-08-19 00:55:39 +0000247}
248
249
250int main( int argc, char *argv[] )
251{
252 glutInit( &argc, argv );
253 glutInitWindowSize( 400, 400 );
Brian Paul8afa9e52001-03-27 17:35:26 +0000254 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000255
256 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
257
258 glutCreateWindow(argv[0] );
259
Brian Paulac126091999-10-21 16:39:06 +0000260 Init(argc, argv);
jtgafb833d1999-08-19 00:55:39 +0000261
262 glutReshapeFunc( Reshape );
263 glutKeyboardFunc( Key );
264 glutSpecialFunc( SpecialKey );
265 glutDisplayFunc( Display );
266 glutIdleFunc( Idle );
267
268 glutCreateMenu(ModeMenu);
269 glutAddMenuEntry("Lit", LIT);
270 glutAddMenuEntry("Textured", TEXTURED);
271 glutAddMenuEntry("Reflect", REFLECT);
272 glutAddMenuEntry("Point Filtered", POINT_FILTER);
273 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
274 glutAddMenuEntry("Toggle Animation", ANIMATE);
275 glutAddMenuEntry("Quit", QUIT);
276 glutAttachMenu(GLUT_RIGHT_BUTTON);
277
278 glutMainLoop();
279 return 0;
280}