Remove CVS keywords.
[mesa.git] / progs / xdemos / vgears.c
1
2 /*
3 * Spinning gears demo for Linux SVGA/Mesa interface in 32K color mode.
4 *
5 * Compile with: gcc vgears.c -I../include -L../lib -lMesaGL -lX11 -lXext
6 * -lvga -lm -o vgears
7 *
8 * This program is in the public domain.
9 * Brian Paul, January 1996
10 */
11
12
13 #include <vga.h>
14 #include <math.h>
15 #include "GL/svgamesa.h"
16 #include "GL/gl.h"
17
18
19 int width = 800, height = 600;
20
21 SVGAMesaContext vmc;
22
23
24
25 /*
26 * Draw a gear wheel. You'll probably want to call this function when
27 * building a display list since we do a lot of trig here.
28 *
29 * Input: inner_radius - radius of hole at center
30 * outer_radius - radius at center of teeth
31 * width - width of gear
32 * teeth - number of teeth
33 * tooth_depth - depth of tooth
34 */
35 static void gear( GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
36 GLint teeth, GLfloat tooth_depth )
37 {
38 GLint i;
39 GLfloat r0, r1, r2;
40 GLfloat angle, da;
41 GLfloat u, v, len;
42
43 r0 = inner_radius;
44 r1 = outer_radius - tooth_depth/2.0;
45 r2 = outer_radius + tooth_depth/2.0;
46
47 da = 2.0*M_PI / teeth / 4.0;
48
49 glShadeModel( GL_FLAT );
50
51 glNormal3f( 0.0, 0.0, 1.0 );
52
53 /* draw front face */
54 glBegin( GL_QUAD_STRIP );
55 for (i=0;i<=teeth;i++) {
56 angle = i * 2.0*M_PI / teeth;
57 glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
58 glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
59 glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
60 glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
61 }
62 glEnd();
63
64 /* draw front sides of teeth */
65 glBegin( GL_QUADS );
66 da = 2.0*M_PI / teeth / 4.0;
67 for (i=0;i<teeth;i++) {
68 angle = i * 2.0*M_PI / teeth;
69
70 glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
71 glVertex3f( r2*cos(angle+da), r2*sin(angle+da), width*0.5 );
72 glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 );
73 glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
74 }
75 glEnd();
76
77
78 glNormal3f( 0.0, 0.0, -1.0 );
79
80 /* draw back face */
81 glBegin( GL_QUAD_STRIP );
82 for (i=0;i<=teeth;i++) {
83 angle = i * 2.0*M_PI / teeth;
84 glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
85 glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
86 glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
87 glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
88 }
89 glEnd();
90
91 /* draw back sides of teeth */
92 glBegin( GL_QUADS );
93 da = 2.0*M_PI / teeth / 4.0;
94 for (i=0;i<teeth;i++) {
95 angle = i * 2.0*M_PI / teeth;
96
97 glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
98 glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
99 glVertex3f( r2*cos(angle+da), r2*sin(angle+da), -width*0.5 );
100 glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
101 }
102 glEnd();
103
104
105 /* draw outward faces of teeth */
106 glBegin( GL_QUAD_STRIP );
107 for (i=0;i<teeth;i++) {
108 angle = i * 2.0*M_PI / teeth;
109
110 glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
111 glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
112 u = r2*cos(angle+da) - r1*cos(angle);
113 v = r2*sin(angle+da) - r1*sin(angle);
114 len = sqrt( u*u + v*v );
115 u /= len;
116 v /= len;
117 glNormal3f( v, -u, 0.0 );
118 glVertex3f( r2*cos(angle+da), r2*sin(angle+da), width*0.5 );
119 glVertex3f( r2*cos(angle+da), r2*sin(angle+da), -width*0.5 );
120 glNormal3f( cos(angle), sin(angle), 0.0 );
121 glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 );
122 glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
123 u = r1*cos(angle+3*da) - r2*cos(angle+2*da);
124 v = r1*sin(angle+3*da) - r2*sin(angle+2*da);
125 glNormal3f( v, -u, 0.0 );
126 glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
127 glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
128 glNormal3f( cos(angle), sin(angle), 0.0 );
129 }
130
131 glVertex3f( r1*cos(0), r1*sin(0), width*0.5 );
132 glVertex3f( r1*cos(0), r1*sin(0), -width*0.5 );
133
134 glEnd();
135
136
137 glShadeModel( GL_SMOOTH );
138
139 /* draw inside radius cylinder */
140 glBegin( GL_QUAD_STRIP );
141 for (i=0;i<=teeth;i++) {
142 angle = i * 2.0*M_PI / teeth;
143 glNormal3f( -cos(angle), -sin(angle), 0.0 );
144 glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
145 glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
146 }
147 glEnd();
148
149 }
150
151
152 static GLfloat view_rotx=20.0, view_roty=30.0, view_rotz=0.0;
153 static GLint gear1, gear2, gear3;
154 static GLfloat angle = 0.0;
155
156 static GLuint limit;
157 static GLuint count = 1;
158
159
160 static void draw( void )
161 {
162 angle += 2.0;
163
164 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
165
166 glPushMatrix();
167 glRotatef( view_rotx, 1.0, 0.0, 0.0 );
168 glRotatef( view_roty, 0.0, 1.0, 0.0 );
169 glRotatef( view_rotz, 0.0, 0.0, 1.0 );
170
171 glPushMatrix();
172 glTranslatef( -3.0, -2.0, 0.0 );
173 glRotatef( angle, 0.0, 0.0, 1.0 );
174 glCallList(gear1);
175 glPopMatrix();
176
177 glPushMatrix();
178 glTranslatef( 3.1, -2.0, 0.0 );
179 glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 );
180 glCallList(gear2);
181 glPopMatrix();
182
183 glPushMatrix();
184 glTranslatef( -3.1, 4.2, 0.0 );
185 glRotatef( -2.0*angle-25.0, 0.0, 0.0, 1.0 );
186 glCallList(gear3);
187 glPopMatrix();
188
189 glPopMatrix();
190
191 SVGAMesaSwapBuffers();
192 }
193
194
195 static void init( void )
196 {
197 static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0 };
198 static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0 };
199 static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0 };
200 static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0 };
201
202 GLfloat w = (float) width / (float) height;
203 GLfloat h = 1.0;
204
205 glLightfv( GL_LIGHT0, GL_POSITION, pos );
206 glEnable( GL_CULL_FACE );
207 glEnable( GL_LIGHTING );
208 glEnable( GL_LIGHT0 );
209 glEnable( GL_DEPTH_TEST );
210
211 /* make the gears */
212 gear1 = glGenLists(1);
213 glNewList(gear1, GL_COMPILE);
214 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red );
215 gear( 1.0, 4.0, 1.0, 20, 0.7 );
216 glEndList();
217
218 gear2 = glGenLists(1);
219 glNewList(gear2, GL_COMPILE);
220 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
221 gear( 0.5, 2.0, 2.0, 10, 0.7 );
222 glEndList();
223
224 gear3 = glGenLists(1);
225 glNewList(gear3, GL_COMPILE);
226 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue );
227 gear( 1.3, 2.0, 0.5, 10, 0.7 );
228 glEndList();
229
230 glEnable( GL_NORMALIZE );
231
232
233 glViewport( 0, 0, width, height );
234 glMatrixMode(GL_PROJECTION);
235 glLoadIdentity();
236 if (width>height) {
237 GLfloat w = (GLfloat) width / (GLfloat) height;
238 glFrustum( -w, w, -1.0, 1.0, 5.0, 60.0 );
239 }
240 else {
241 GLfloat h = (GLfloat) height / (GLfloat) width;
242 glFrustum( -1.0, 1.0, -h, h, 5.0, 60.0 );
243 }
244
245 glMatrixMode(GL_MODELVIEW);
246 glLoadIdentity();
247 glTranslatef( 0.0, 0.0, -40.0 );
248 }
249
250 void setup( void )
251 {
252 vga_init();
253
254 vga_setmode(G800x600x32K);
255 /* gl_setcontextvga(G800x600x32K);*/
256
257 vmc = SVGAMesaCreateContext(GL_TRUE);
258 SVGAMesaMakeCurrent( vmc );
259 }
260
261
262 void end( void )
263 {
264 SVGAMesaDestroyContext( vmc );
265
266 vga_setmode( TEXT );
267 }
268
269
270 int main( int argc, char *argv[] )
271 {
272 int i;
273
274 setup();
275 init();
276 for (i=0;i<4;i++) {
277 draw(); /*SVGAMesaSwapBuffers();*/
278 }
279 end();
280 return 0;
281 }