blob: 61c8d908b06b4b47f1aff081c41aa324f1eb8497 [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;
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
Brian Paulac126091999-10-21 16:39:06 +0000174static void Init( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000175{
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);
Brian Paulac126091999-10-21 16:39:06 +0000229
230 if (argc > 1 && strcmp(argv[1], "-info")==0) {
231 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
232 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
233 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
234 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
235 }
jtgafb833d1999-08-19 00:55:39 +0000236}
237
238
239int main( int argc, char *argv[] )
240{
241 glutInit( &argc, argv );
242 glutInitWindowSize( 400, 400 );
Brian Paul8afa9e52001-03-27 17:35:26 +0000243 glutInitWindowPosition( 0, 0 );
jtgafb833d1999-08-19 00:55:39 +0000244
245 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
246
247 glutCreateWindow(argv[0] );
248
Brian Paulac126091999-10-21 16:39:06 +0000249 Init(argc, argv);
jtgafb833d1999-08-19 00:55:39 +0000250
251 glutReshapeFunc( Reshape );
252 glutKeyboardFunc( Key );
253 glutSpecialFunc( SpecialKey );
254 glutDisplayFunc( Display );
255 glutIdleFunc( Idle );
256
257 glutCreateMenu(ModeMenu);
258 glutAddMenuEntry("Lit", LIT);
259 glutAddMenuEntry("Textured", TEXTURED);
260 glutAddMenuEntry("Reflect", REFLECT);
261 glutAddMenuEntry("Point Filtered", POINT_FILTER);
262 glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
263 glutAddMenuEntry("Toggle Animation", ANIMATE);
264 glutAddMenuEntry("Quit", QUIT);
265 glutAttachMenu(GLUT_RIGHT_BUTTON);
266
267 glutMainLoop();
268 return 0;
269}