jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame^] | 1 | /* $ID$ */ |
| 2 | |
| 3 | /* |
| 4 | * Spinning gears demo for Linux SVGA/Mesa interface in 32K color mode. |
| 5 | * |
| 6 | * Compile with: gcc vgears.c -I../include -L../lib -lMesaGL -lX11 -lXext |
| 7 | * -lvga -lm -o vgears |
| 8 | * |
| 9 | * This program is in the public domain. |
| 10 | * Brian Paul, January 1996 |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <vga.h> |
| 15 | #include <math.h> |
| 16 | #include "GL/svgamesa.h" |
| 17 | #include "GL/gl.h" |
| 18 | |
| 19 | |
| 20 | int width = 800, height = 600; |
| 21 | |
| 22 | SVGAMesaContext vmc; |
| 23 | |
| 24 | |
| 25 | |
| 26 | /* |
| 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. |
| 29 | * |
| 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 |
| 35 | */ |
| 36 | static void gear( GLfloat inner_radius, GLfloat outer_radius, GLfloat width, |
| 37 | GLint teeth, GLfloat tooth_depth ) |
| 38 | { |
| 39 | GLint i; |
| 40 | GLfloat r0, r1, r2; |
| 41 | GLfloat angle, da; |
| 42 | GLfloat u, v, len; |
| 43 | |
| 44 | r0 = inner_radius; |
| 45 | r1 = outer_radius - tooth_depth/2.0; |
| 46 | r2 = outer_radius + tooth_depth/2.0; |
| 47 | |
| 48 | da = 2.0*M_PI / teeth / 4.0; |
| 49 | |
| 50 | glShadeModel( GL_FLAT ); |
| 51 | |
| 52 | glNormal3f( 0.0, 0.0, 1.0 ); |
| 53 | |
| 54 | /* draw front face */ |
| 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 ); |
| 62 | } |
| 63 | glEnd(); |
| 64 | |
| 65 | /* draw front sides of teeth */ |
| 66 | glBegin( GL_QUADS ); |
| 67 | da = 2.0*M_PI / teeth / 4.0; |
| 68 | for (i=0;i<teeth;i++) { |
| 69 | angle = i * 2.0*M_PI / teeth; |
| 70 | |
| 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 ); |
| 75 | } |
| 76 | glEnd(); |
| 77 | |
| 78 | |
| 79 | glNormal3f( 0.0, 0.0, -1.0 ); |
| 80 | |
| 81 | /* draw back face */ |
| 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 ); |
| 89 | } |
| 90 | glEnd(); |
| 91 | |
| 92 | /* draw back sides of teeth */ |
| 93 | glBegin( GL_QUADS ); |
| 94 | da = 2.0*M_PI / teeth / 4.0; |
| 95 | for (i=0;i<teeth;i++) { |
| 96 | angle = i * 2.0*M_PI / teeth; |
| 97 | |
| 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 ); |
| 102 | } |
| 103 | glEnd(); |
| 104 | |
| 105 | |
| 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; |
| 110 | |
| 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 ); |
| 116 | u /= len; |
| 117 | v /= len; |
| 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 ); |
| 130 | } |
| 131 | |
| 132 | glVertex3f( r1*cos(0), r1*sin(0), width*0.5 ); |
| 133 | glVertex3f( r1*cos(0), r1*sin(0), -width*0.5 ); |
| 134 | |
| 135 | glEnd(); |
| 136 | |
| 137 | |
| 138 | glShadeModel( GL_SMOOTH ); |
| 139 | |
| 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 ); |
| 147 | } |
| 148 | glEnd(); |
| 149 | |
| 150 | } |
| 151 | |
| 152 | |
| 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; |
| 156 | |
| 157 | static GLuint limit; |
| 158 | static GLuint count = 1; |
| 159 | |
| 160 | |
| 161 | static void draw( void ) |
| 162 | { |
| 163 | angle += 2.0; |
| 164 | |
| 165 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 166 | |
| 167 | glPushMatrix(); |
| 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 ); |
| 171 | |
| 172 | glPushMatrix(); |
| 173 | glTranslatef( -3.0, -2.0, 0.0 ); |
| 174 | glRotatef( angle, 0.0, 0.0, 1.0 ); |
| 175 | glCallList(gear1); |
| 176 | glPopMatrix(); |
| 177 | |
| 178 | glPushMatrix(); |
| 179 | glTranslatef( 3.1, -2.0, 0.0 ); |
| 180 | glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 ); |
| 181 | glCallList(gear2); |
| 182 | glPopMatrix(); |
| 183 | |
| 184 | glPushMatrix(); |
| 185 | glTranslatef( -3.1, 4.2, 0.0 ); |
| 186 | glRotatef( -2.0*angle-25.0, 0.0, 0.0, 1.0 ); |
| 187 | glCallList(gear3); |
| 188 | glPopMatrix(); |
| 189 | |
| 190 | glPopMatrix(); |
| 191 | |
| 192 | SVGAMesaSwapBuffers(); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | static void init( void ) |
| 197 | { |
| 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 }; |
| 202 | |
| 203 | GLfloat w = (float) width / (float) height; |
| 204 | GLfloat h = 1.0; |
| 205 | |
| 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 ); |
| 211 | |
| 212 | /* make the gears */ |
| 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 ); |
| 217 | glEndList(); |
| 218 | |
| 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 ); |
| 223 | glEndList(); |
| 224 | |
| 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 ); |
| 229 | glEndList(); |
| 230 | |
| 231 | glEnable( GL_NORMALIZE ); |
| 232 | |
| 233 | |
| 234 | glViewport( 0, 0, width, height ); |
| 235 | glMatrixMode(GL_PROJECTION); |
| 236 | glLoadIdentity(); |
| 237 | if (width>height) { |
| 238 | GLfloat w = (GLfloat) width / (GLfloat) height; |
| 239 | glFrustum( -w, w, -1.0, 1.0, 5.0, 60.0 ); |
| 240 | } |
| 241 | else { |
| 242 | GLfloat h = (GLfloat) height / (GLfloat) width; |
| 243 | glFrustum( -1.0, 1.0, -h, h, 5.0, 60.0 ); |
| 244 | } |
| 245 | |
| 246 | glMatrixMode(GL_MODELVIEW); |
| 247 | glLoadIdentity(); |
| 248 | glTranslatef( 0.0, 0.0, -40.0 ); |
| 249 | } |
| 250 | |
| 251 | void setup( void ) |
| 252 | { |
| 253 | vga_init(); |
| 254 | |
| 255 | vga_setmode(G800x600x32K); |
| 256 | /* gl_setcontextvga(G800x600x32K);*/ |
| 257 | |
| 258 | vmc = SVGAMesaCreateContext(GL_TRUE); |
| 259 | SVGAMesaMakeCurrent( vmc ); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | void end( void ) |
| 264 | { |
| 265 | SVGAMesaDestroyContext( vmc ); |
| 266 | |
| 267 | vga_setmode( TEXT ); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | int main( int argc, char *argv[] ) |
| 272 | { |
| 273 | int i; |
| 274 | |
| 275 | setup(); |
| 276 | init(); |
| 277 | for (i=0;i<4;i++) { |
| 278 | draw(); /*SVGAMesaSwapBuffers();*/ |
| 279 | } |
| 280 | end(); |
| 281 | return 0; |
| 282 | } |