Added a new demo (from Nigel) showing some of the basic GLUT geometry
[freeglut] / progs / demos / shapes / shapes.c
1 #include <GL/freeglut.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6
7 static int slices = 16;
8 static int stacks = 16;
9
10 static void 
11 reshape(int width, int height)
12 {
13         float ar;
14         
15         glViewport( 0, 0, width, height);
16         glMatrixMode(GL_PROJECTION);
17         glLoadIdentity();
18         ar = (float) width / (float) height;
19         glFrustum ( -ar, ar, -1.0, 1.0, 2.0, 100.0);
20         
21         glMatrixMode(GL_MODELVIEW) ;
22         glLoadIdentity() ;
23 }
24
25 static void 
26 display(void)
27 {
28         float t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
29         float a = t*90.0;
30
31         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
32
33         glPushMatrix();
34                 glTranslatef(-2.4,1.2,-6);
35                 glRotatef(60,1,0,0);
36                 glRotatef(a,0,0,1);
37                 glColor3f(1,0,0);
38                 glutSolidSphere(1,slices,stacks);
39         glPopMatrix();
40
41         glPushMatrix();
42                 glTranslatef(0,1.2,-6);
43                 glRotatef(60,1,0,0);
44                 glRotatef(a,0,0,1);
45                 glColor3f(1,0,0);
46                 glutSolidCone(1,1,slices,stacks);
47         glPopMatrix();
48
49         glPushMatrix();
50                 glTranslatef(2.4,1.2,-6);
51                 glRotatef(60,1,0,0);
52                 glRotatef(a,0,0,1);
53                 glColor3f(1,0,0);
54                 glutSolidTorus(0.2,0.8,slices,stacks);
55         glPopMatrix();
56
57         glPushMatrix();
58                 glTranslatef(-2.4,-1.2,-6);
59                 glRotatef(60,1,0,0);
60                 glRotatef(a,0,0,1);
61                 glColor3f(1,0,0);
62                 glutWireSphere(1,slices,stacks);
63         glPopMatrix();
64
65         glPushMatrix();
66                 glTranslatef(0,-1.2,-6);
67                 glRotatef(60,1,0,0);
68                 glRotatef(a,0,0,1);
69                 glColor3f(1,0,0);
70                 glutWireCone(1,1,slices,stacks);
71         glPopMatrix();
72
73         glPushMatrix();
74                 glTranslatef(2.4,-1.2,-6);
75                 glRotatef(60,1,0,0);
76                 glRotatef(a,0,0,1);
77                 glColor3f(1,0,0);
78                 glutWireTorus(0.2,0.8,slices,stacks);
79         glPopMatrix();
80
81         glutSwapBuffers();
82 }
83
84
85 static void 
86 key(unsigned char key, int x, int y)
87 {
88         switch (key) {
89                 case 27 : 
90                 case 'q':
91                 exit(0);
92                 break;
93
94          case '+':
95                 slices++;
96                 stacks++;
97                 break;
98                 
99          case '-':
100                 slices--;
101                 stacks--;
102                 break;
103         }
104         
105         glutPostRedisplay();
106 }
107
108 void 
109 idle()
110 {
111         glutPostRedisplay();
112 }
113
114 GLfloat light_ambient[]  = { 0.0, 0.0, 0.0, 1.0 };
115 GLfloat light_diffuse[]  = { 1.0, 1.0, 1.0, 1.0 };
116 GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
117 GLfloat light_position[] = { 2.0, 5.0, 5.0, 0.0 };
118
119 GLfloat mat_ambient[]    = { 0.7, 0.7, 0.7, 1.0 };
120 GLfloat mat_diffuse[]    = { 0.8, 0.8, 0.8, 1.0 };
121 GLfloat mat_specular[]   = { 1.0, 1.0, 1.0, 1.0 };
122 GLfloat high_shininess[] = { 100.0 };
123
124 int 
125 main(int argc, char *argv[])
126 {
127         glutInit(&argc, argv);
128         glutInitWindowSize(640,480);
129         glutInitWindowPosition (40,40);
130         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
131
132         glutCreateWindow("FreeGLUT Shapes");
133
134         glutReshapeFunc(reshape);
135         glutDisplayFunc(display);
136         glutKeyboardFunc(key);
137         glutIdleFunc(idle);
138
139         glClearColor(1,1,1,1);
140         glEnable(GL_CULL_FACE);
141     glCullFace(GL_BACK);
142
143     glEnable(GL_DEPTH_TEST);
144     glDepthFunc(GL_LESS);
145
146     glEnable(GL_LIGHT0);
147     glEnable(GL_NORMALIZE);
148     glEnable(GL_COLOR_MATERIAL);
149         glEnable(GL_LIGHTING);
150
151     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
152     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
153     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
154     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
155
156     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
157     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
158     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
159     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
160
161         glutMainLoop();
162
163         return EXIT_SUCCESS;
164 }