Package the visual C project file with the distribution
[freeglut] / progs / demos / shapes / shapes.c
1 /*
2  * FreeGLUT Shapes Demo
3  *
4  * Written by Nigel Stewart November 2003
5  *
6  * This program is test harness for the sphere, cone 
7  * and torus shapes in FreeGLUT.
8  *
9  * Spinning wireframe and smooth shaded shapes are
10  * displayed until the ESC or q key is pressed.  The
11  * number of geometry stacks and slices can be adjusted
12  * using the + and - keys.
13  */
14
15 #include <GL/freeglut.h>
16
17 #include <stdlib.h>
18
19 static int slices = 16;
20 static int stacks = 16;
21
22 /* GLUT callback Handlers */
23
24 static void 
25 resize(int width, int height)
26 {
27     const float ar = (float) width / (float) height;
28     
29     glViewport(0, 0, width, height);
30     glMatrixMode(GL_PROJECTION);
31     glLoadIdentity();
32     glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
33     
34     glMatrixMode(GL_MODELVIEW);
35     glLoadIdentity() ;
36 }
37
38 static void 
39 display(void)
40 {
41     const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
42     const double a = t*90.0;
43
44     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
45     glColor3d(1,0,0);
46
47     glPushMatrix();
48         glTranslated(-2.4,1.2,-6);
49         glRotated(60,1,0,0);
50         glRotated(a,0,0,1);
51         glutSolidSphere(1,slices,stacks);
52     glPopMatrix();
53
54     glPushMatrix();
55         glTranslated(0,1.2,-6);
56         glRotated(60,1,0,0);
57         glRotated(a,0,0,1);
58         glutSolidCone(1,1,slices,stacks);
59     glPopMatrix();
60
61     glPushMatrix();
62         glTranslated(2.4,1.2,-6);
63         glRotated(60,1,0,0);
64         glRotated(a,0,0,1);
65         glutSolidTorus(0.2,0.8,slices,stacks);
66     glPopMatrix();
67
68     glPushMatrix();
69         glTranslated(-2.4,-1.2,-6);
70         glRotated(60,1,0,0);
71         glRotated(a,0,0,1);
72         glutWireSphere(1,slices,stacks);
73     glPopMatrix();
74
75     glPushMatrix();
76         glTranslated(0,-1.2,-6);
77         glRotated(60,1,0,0);
78         glRotated(a,0,0,1);
79         glutWireCone(1,1,slices,stacks);
80     glPopMatrix();
81
82     glPushMatrix();
83         glTranslated(2.4,-1.2,-6);
84         glRotated(60,1,0,0);
85         glRotated(a,0,0,1);
86         glutWireTorus(0.2,0.8,slices,stacks);
87     glPopMatrix();
88
89     glutSwapBuffers();
90 }
91
92
93 static void 
94 key(unsigned char key, int x, int y)
95 {
96     switch (key) 
97     {
98         case 27 : 
99         case 'q':
100             exit(0);
101             break;
102
103         case '+':
104             slices++;
105             stacks++;
106             break;
107
108         case '-':
109             if (slices>3 && stacks>3)
110             {
111                 slices--;
112                 stacks--;
113             }
114             break;
115     }
116
117     glutPostRedisplay();
118 }
119
120 static void 
121 idle(void)
122 {
123     glutPostRedisplay();
124 }
125
126 const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
127 const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
128 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
129 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
130
131 const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
132 const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
133 const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
134 const GLfloat high_shininess[] = { 100.0f };
135
136 /* Program entry point */
137
138 int 
139 main(int argc, char *argv[])
140 {
141     glutInit(&argc, argv);
142     glutInitWindowSize(640,480);
143     glutInitWindowPosition (40,40);
144     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
145
146     glutCreateWindow("FreeGLUT Shapes");
147
148     glutReshapeFunc(resize);
149     glutDisplayFunc(display);
150     glutKeyboardFunc(key);
151     glutIdleFunc(idle);
152
153     glClearColor(1,1,1,1);
154     glEnable(GL_CULL_FACE);
155     glCullFace(GL_BACK);
156
157     glEnable(GL_DEPTH_TEST);
158     glDepthFunc(GL_LESS);
159
160     glEnable(GL_LIGHT0);
161     glEnable(GL_NORMALIZE);
162     glEnable(GL_COLOR_MATERIAL);
163     glEnable(GL_LIGHTING);
164
165     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
166     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
167     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
168     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
169
170     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
171     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
172     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
173     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
174
175     glutMainLoop();
176
177     return EXIT_SUCCESS;
178 }
179