3 * Spinning gears demo for Linux SVGA/Mesa interface in 32K color mode.
5 * Compile with: gcc vgears.c -I../include -L../lib -lMesaGL -lX11 -lXext
8 * This program is in the public domain.
9 * Brian Paul, January 1996
15 #include "GL/svgamesa.h"
19 int width
= 800, height
= 600;
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.
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
35 static void gear( GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
36 GLint teeth
, GLfloat tooth_depth
)
44 r1
= outer_radius
- tooth_depth
/2.0;
45 r2
= outer_radius
+ tooth_depth
/2.0;
47 da
= 2.0*M_PI
/ teeth
/ 4.0;
49 glShadeModel( GL_FLAT
);
51 glNormal3f( 0.0, 0.0, 1.0 );
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 );
64 /* draw front sides of teeth */
66 da
= 2.0*M_PI
/ teeth
/ 4.0;
67 for (i
=0;i
<teeth
;i
++) {
68 angle
= i
* 2.0*M_PI
/ teeth
;
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 );
78 glNormal3f( 0.0, 0.0, -1.0 );
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 );
91 /* draw back sides of teeth */
93 da
= 2.0*M_PI
/ teeth
/ 4.0;
94 for (i
=0;i
<teeth
;i
++) {
95 angle
= i
* 2.0*M_PI
/ teeth
;
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 );
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
;
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
);
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 );
131 glVertex3f( r1
*cos(0), r1
*sin(0), width
*0.5 );
132 glVertex3f( r1
*cos(0), r1
*sin(0), -width
*0.5 );
137 glShadeModel( GL_SMOOTH
);
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 );
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;
157 static GLuint count
= 1;
160 static void draw( void )
164 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
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 );
172 glTranslatef( -3.0, -2.0, 0.0 );
173 glRotatef( angle
, 0.0, 0.0, 1.0 );
178 glTranslatef( 3.1, -2.0, 0.0 );
179 glRotatef( -2.0*angle
-9.0, 0.0, 0.0, 1.0 );
184 glTranslatef( -3.1, 4.2, 0.0 );
185 glRotatef( -2.0*angle
-25.0, 0.0, 0.0, 1.0 );
191 SVGAMesaSwapBuffers();
195 static void init( void )
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 };
202 GLfloat w
= (float) width
/ (float) height
;
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
);
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 );
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 );
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 );
230 glEnable( GL_NORMALIZE
);
233 glViewport( 0, 0, width
, height
);
234 glMatrixMode(GL_PROJECTION
);
237 GLfloat w
= (GLfloat
) width
/ (GLfloat
) height
;
238 glFrustum( -w
, w
, -1.0, 1.0, 5.0, 60.0 );
241 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
242 glFrustum( -1.0, 1.0, -h
, h
, 5.0, 60.0 );
245 glMatrixMode(GL_MODELVIEW
);
247 glTranslatef( 0.0, 0.0, -40.0 );
254 vga_setmode(G800x600x32K
);
255 /* gl_setcontextvga(G800x600x32K);*/
257 vmc
= SVGAMesaCreateContext(GL_TRUE
);
258 SVGAMesaMakeCurrent( vmc
);
264 SVGAMesaDestroyContext( vmc
);
270 int main( int argc
, char *argv
[] )
277 draw(); /*SVGAMesaSwapBuffers();*/