1
2/*
3 * Textured cylinder demo: lighting, texturing, reflection mapping.
4 *
5 * Command line options:
6 *    -info      print GL implementation information
7 *
8 *
9 * Brian Paul  May 1997  This program is in the public domain.
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <math.h>
15#include <string.h>
16#include "glut_wrap.h"
17
18#include "readtex.h"
19
20#define TEXTURE_FILE DEMOS_DATA_DIR "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 GLint Win = -1;
31
32static GLuint CylinderObj = 0;
33static GLboolean Animate = GL_TRUE;
34
35static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
36static GLfloat DXrot = 50.0, DYrot = 125.0;
37
38/* performance info */
39static GLint T0 = 0;
40static GLint Frames = 0;
41
42
43static void Idle( void )
44{
45   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
52   if (Animate) {
53      Xrot += DXrot * dt;
54      Yrot += DYrot * dt;
55      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();
74
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);
82         fflush(stdout);
83         T0 = t;
84         Frames = 0;
85      }
86   }
87}
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;
129      if (Animate)
130         glutIdleFunc(Idle);
131      else
132         glutIdleFunc(NULL);
133   }
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) {
157      case ' ':
158      Animate = !Animate;
159      if (Animate)
160         glutIdleFunc(Idle);
161      else
162         glutIdleFunc(NULL);
163      break;
164      case 27:
165         glutDestroyWindow(Win);
166         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
197static void Init( int argc, char *argv[] )
198{
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);
252
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   }
259}
260
261
262int main( int argc, char *argv[] )
263{
264   glutInitWindowSize( 400, 400 );
265   glutInit( &argc, argv );
266   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
267
268   Win = glutCreateWindow(argv[0] );
269
270   Init(argc, argv);
271
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}
291