4 * Spinning gears demo for Linux SVGA/Mesa interface in 32K color mode.
6 * Compile with: gcc vgears.c -I../include -L../lib -lMesaGL -lX11 -lXext
9 * This program is in the public domain.
10 * Brian Paul, January 1996
16 #include "GL/svgamesa.h"
20 int width
= 800, height
= 600;
27 * Draw a gear wheel. You'll probably want to call this function when
28 * building a display list since we do a lot of trig here.
30 * Input: inner_radius - radius of hole at center
31 * outer_radius - radius at center of teeth
32 * width - width of gear
33 * teeth - number of teeth
34 * tooth_depth - depth of tooth
36 static void gear( GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
37 GLint teeth
, GLfloat tooth_depth
)
45 r1
= outer_radius
- tooth_depth
/2.0;
46 r2
= outer_radius
+ tooth_depth
/2.0;
48 da
= 2.0*M_PI
/ teeth
/ 4.0;
50 glShadeModel( GL_FLAT
);
52 glNormal3f( 0.0, 0.0, 1.0 );
55 glBegin( GL_QUAD_STRIP
);
56 for (i
=0;i
<=teeth
;i
++) {
57 angle
= i
* 2.0*M_PI
/ teeth
;
58 glVertex3f( r0
*cos(angle
), r0
*sin(angle
), width
*0.5 );
59 glVertex3f( r1
*cos(angle
), r1
*sin(angle
), width
*0.5 );
60 glVertex3f( r0
*cos(angle
), r0
*sin(angle
), width
*0.5 );
61 glVertex3f( r1
*cos(angle
+3*da
), r1
*sin(angle
+3*da
), width
*0.5 );
65 /* draw front sides of teeth */
67 da
= 2.0*M_PI
/ teeth
/ 4.0;
68 for (i
=0;i
<teeth
;i
++) {
69 angle
= i
* 2.0*M_PI
/ teeth
;
71 glVertex3f( r1
*cos(angle
), r1
*sin(angle
), width
*0.5 );
72 glVertex3f( r2
*cos(angle
+da
), r2
*sin(angle
+da
), width
*0.5 );
73 glVertex3f( r2
*cos(angle
+2*da
), r2
*sin(angle
+2*da
), width
*0.5 );
74 glVertex3f( r1
*cos(angle
+3*da
), r1
*sin(angle
+3*da
), width
*0.5 );
79 glNormal3f( 0.0, 0.0, -1.0 );
82 glBegin( GL_QUAD_STRIP
);
83 for (i
=0;i
<=teeth
;i
++) {
84 angle
= i
* 2.0*M_PI
/ teeth
;
85 glVertex3f( r1
*cos(angle
), r1
*sin(angle
), -width
*0.5 );
86 glVertex3f( r0
*cos(angle
), r0
*sin(angle
), -width
*0.5 );
87 glVertex3f( r1
*cos(angle
+3*da
), r1
*sin(angle
+3*da
), -width
*0.5 );
88 glVertex3f( r0
*cos(angle
), r0
*sin(angle
), -width
*0.5 );
92 /* draw back sides of teeth */
94 da
= 2.0*M_PI
/ teeth
/ 4.0;
95 for (i
=0;i
<teeth
;i
++) {
96 angle
= i
* 2.0*M_PI
/ teeth
;
98 glVertex3f( r1
*cos(angle
+3*da
), r1
*sin(angle
+3*da
), -width
*0.5 );
99 glVertex3f( r2
*cos(angle
+2*da
), r2
*sin(angle
+2*da
), -width
*0.5 );
100 glVertex3f( r2
*cos(angle
+da
), r2
*sin(angle
+da
), -width
*0.5 );
101 glVertex3f( r1
*cos(angle
), r1
*sin(angle
), -width
*0.5 );
106 /* draw outward faces of teeth */
107 glBegin( GL_QUAD_STRIP
);
108 for (i
=0;i
<teeth
;i
++) {
109 angle
= i
* 2.0*M_PI
/ teeth
;
111 glVertex3f( r1
*cos(angle
), r1
*sin(angle
), width
*0.5 );
112 glVertex3f( r1
*cos(angle
), r1
*sin(angle
), -width
*0.5 );
113 u
= r2
*cos(angle
+da
) - r1
*cos(angle
);
114 v
= r2
*sin(angle
+da
) - r1
*sin(angle
);
115 len
= sqrt( u
*u
+ v
*v
);
118 glNormal3f( v
, -u
, 0.0 );
119 glVertex3f( r2
*cos(angle
+da
), r2
*sin(angle
+da
), width
*0.5 );
120 glVertex3f( r2
*cos(angle
+da
), r2
*sin(angle
+da
), -width
*0.5 );
121 glNormal3f( cos(angle
), sin(angle
), 0.0 );
122 glVertex3f( r2
*cos(angle
+2*da
), r2
*sin(angle
+2*da
), width
*0.5 );
123 glVertex3f( r2
*cos(angle
+2*da
), r2
*sin(angle
+2*da
), -width
*0.5 );
124 u
= r1
*cos(angle
+3*da
) - r2
*cos(angle
+2*da
);
125 v
= r1
*sin(angle
+3*da
) - r2
*sin(angle
+2*da
);
126 glNormal3f( v
, -u
, 0.0 );
127 glVertex3f( r1
*cos(angle
+3*da
), r1
*sin(angle
+3*da
), width
*0.5 );
128 glVertex3f( r1
*cos(angle
+3*da
), r1
*sin(angle
+3*da
), -width
*0.5 );
129 glNormal3f( cos(angle
), sin(angle
), 0.0 );
132 glVertex3f( r1
*cos(0), r1
*sin(0), width
*0.5 );
133 glVertex3f( r1
*cos(0), r1
*sin(0), -width
*0.5 );
138 glShadeModel( GL_SMOOTH
);
140 /* draw inside radius cylinder */
141 glBegin( GL_QUAD_STRIP
);
142 for (i
=0;i
<=teeth
;i
++) {
143 angle
= i
* 2.0*M_PI
/ teeth
;
144 glNormal3f( -cos(angle
), -sin(angle
), 0.0 );
145 glVertex3f( r0
*cos(angle
), r0
*sin(angle
), -width
*0.5 );
146 glVertex3f( r0
*cos(angle
), r0
*sin(angle
), width
*0.5 );
153 static GLfloat view_rotx
=20.0, view_roty
=30.0, view_rotz
=0.0;
154 static GLint gear1
, gear2
, gear3
;
155 static GLfloat angle
= 0.0;
158 static GLuint count
= 1;
161 static void draw( void )
165 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
168 glRotatef( view_rotx
, 1.0, 0.0, 0.0 );
169 glRotatef( view_roty
, 0.0, 1.0, 0.0 );
170 glRotatef( view_rotz
, 0.0, 0.0, 1.0 );
173 glTranslatef( -3.0, -2.0, 0.0 );
174 glRotatef( angle
, 0.0, 0.0, 1.0 );
179 glTranslatef( 3.1, -2.0, 0.0 );
180 glRotatef( -2.0*angle
-9.0, 0.0, 0.0, 1.0 );
185 glTranslatef( -3.1, 4.2, 0.0 );
186 glRotatef( -2.0*angle
-25.0, 0.0, 0.0, 1.0 );
192 SVGAMesaSwapBuffers();
196 static void init( void )
198 static GLfloat pos
[4] = {5.0, 5.0, 10.0, 1.0 };
199 static GLfloat red
[4] = {0.8, 0.1, 0.0, 1.0 };
200 static GLfloat green
[4] = {0.0, 0.8, 0.2, 1.0 };
201 static GLfloat blue
[4] = {0.2, 0.2, 1.0, 1.0 };
203 GLfloat w
= (float) width
/ (float) height
;
206 glLightfv( GL_LIGHT0
, GL_POSITION
, pos
);
207 glEnable( GL_CULL_FACE
);
208 glEnable( GL_LIGHTING
);
209 glEnable( GL_LIGHT0
);
210 glEnable( GL_DEPTH_TEST
);
213 gear1
= glGenLists(1);
214 glNewList(gear1
, GL_COMPILE
);
215 glMaterialfv( GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
216 gear( 1.0, 4.0, 1.0, 20, 0.7 );
219 gear2
= glGenLists(1);
220 glNewList(gear2
, GL_COMPILE
);
221 glMaterialfv( GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
222 gear( 0.5, 2.0, 2.0, 10, 0.7 );
225 gear3
= glGenLists(1);
226 glNewList(gear3
, GL_COMPILE
);
227 glMaterialfv( GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
228 gear( 1.3, 2.0, 0.5, 10, 0.7 );
231 glEnable( GL_NORMALIZE
);
234 glViewport( 0, 0, width
, height
);
235 glMatrixMode(GL_PROJECTION
);
238 GLfloat w
= (GLfloat
) width
/ (GLfloat
) height
;
239 glFrustum( -w
, w
, -1.0, 1.0, 5.0, 60.0 );
242 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
243 glFrustum( -1.0, 1.0, -h
, h
, 5.0, 60.0 );
246 glMatrixMode(GL_MODELVIEW
);
248 glTranslatef( 0.0, 0.0, -40.0 );
255 vga_setmode(G800x600x32K
);
256 /* gl_setcontextvga(G800x600x32K);*/
258 vmc
= SVGAMesaCreateContext(GL_TRUE
);
259 SVGAMesaMakeCurrent( vmc
);
265 SVGAMesaDestroyContext( vmc
);
271 int main( int argc
, char *argv
[] )
278 draw(); /*SVGAMesaSwapBuffers();*/