From: Tim R. <ti...@us...> - 2004-07-17 19:34:43
|
Update of /cvsroot/csdopenglnet/csdOpenGL/samples In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14180 Modified Files: gears.cs Log Message: gears example - the beginning not what I want, but not so bad :) Index: gears.cs =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/samples/gears.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** gears.cs 17 Jul 2004 18:37:49 -0000 1.1 --- gears.cs 17 Jul 2004 19:34:35 -0000 1.2 *************** *** 4,7 **** --- 4,15 ---- public class Lesson01GLUT : csdGL { + protected float angle = 0.0f; + protected uint gear1; + protected uint gear2; + protected uint gear3; + protected const double view_rotx = 20.0; + protected const double view_roty = 30.0; + protected const double view_rotz = 0.0; + public Lesson01GLUT() { glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); *************** *** 9,13 **** glutCreateWindow("Gears"); ! initGL(); // initialize the OpenGL context glutDisplayFunc( new cb0_glutDisplayFunc( draw ) ); --- 17,21 ---- glutCreateWindow("Gears"); ! initGL(); glutDisplayFunc( new cb0_glutDisplayFunc( draw ) ); *************** *** 27,34 **** --- 35,48 ---- protected void redraw() { + angle += 2.0f; glutPostRedisplay(); } protected void initGL() { + float[] pos = { 5.0f, 5.0f, 10.0f, 0.0f }; + float[] red = { 0.8f, 0.1f, 0.0f, 1.0f }; + float[] green = { 0.0f, 0.8f, 0.2f, 1.0f }; + float[] blue = { 0.2f, 0.2f, 1.0f, 1.0f }; + /* glShadeModel( GL_SMOOTH ); glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); *************** *** 37,40 **** --- 51,83 ---- glDepthFunc( GL_LEQUAL ); glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); + */ + glLightfv( GL_LIGHT0, GL_POSITION, pos ); + glEnable( GL_CULL_FACE ); + glEnable( GL_LIGHTING ); + glEnable( GL_LIGHT0 ); + glEnable( GL_DEPTH_TEST ); + + /* gears */ + gear1 = glGenLists( 1 ); + glNewList( gear1, GL_COMPILE ); + glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red ); + gear( 1.0, 4.0, 1.0, 20, 0.7 ); + glEndList(); + + gear2 = glGenLists( 1 ); + glNewList( gear2, GL_COMPILE ); + glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green ); + gear( 0.5, 2.0, 2.0, 10, 0.7 ); + glEndList(); + + gear3 = glGenLists( 1 ); + glNewList( gear3, GL_COMPILE ); + glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue ); + gear( 1.3, 2.0, 0.5, 10, 0.7 ); + glEndList(); + + glEnable( GL_NORMALIZE ); + + // glEnd(); } *************** *** 47,62 **** glLoadIdentity (); ! glFrustum (-1.0, 1.0, -h, h, 5.0, 5.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); ! } protected void draw() { glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); ! glLoadIdentity(); glutSwapBuffers(); } protected void keyboard( byte key, int x, int y ) { if (key==27) System.Environment.Exit(0); --- 90,236 ---- glLoadIdentity (); ! glFrustum (-1.0, 1.0, -h, h, 5.0, 60.0 ); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); ! glTranslatef( 0.0f, 0.0f, -40.0f ); ! } protected void draw() { glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); ! ! glPushMatrix(); ! glRotated( view_rotx, 1.0, 0.0, 0.0 ); ! glRotated( view_roty, 0.0, 1.0, 0.0 ); ! glRotated( view_rotz, 0.0, 0.0, 1.0 ); ! ! glPushMatrix(); ! glTranslated( -3.0, -2.0, 0.0 ); ! glRotated( angle, 0.0, 0.0, 1.0 ); ! glCallList( gear1 ); ! glPopMatrix(); ! ! glPushMatrix(); ! glTranslated( 3.1, -2.0, 0.0 ); ! glRotated( -2.0 * angle - 9.0, 0.0, 0.0, 1.0 ); ! glCallList( gear2 ); ! glPopMatrix(); ! ! glPushMatrix(); ! glTranslated( -3.1, 4.2, 0.0 ); ! glRotated( -2.0 * angle - 25.0, 0.0, 0.0, 1.0 ); ! glCallList( gear3 ); ! glPopMatrix(); glutSwapBuffers(); } + protected void gear( double inner_radius, double outer_radius, double width, int teeth, double tooth_depth ) { + double r0 = inner_radius; + double r1 = outer_radius - tooth_depth / 2.0; + double r2 = outer_radius + tooth_depth / 2.0; + double da = 2.0f * System.Math.PI / teeth / 4.0; + int i; + double ang, u, v, len; + + glShadeModel( GL_FLAT ); + + /* front face */ + glBegin( GL_QUAD_STRIP ); + for ( i=0; i<=teeth; i++ ) { + ang = i * 2.0f * System.Math.PI; + glVertex3d( r0 * System.Math.Cos( ang ), r0 * System.Math.Sin( ang ), width * 0.5 ); + glVertex3d( r1 * System.Math.Cos( ang ), r1 * System.Math.Sin( ang ), width * 0.5 ); + if ( i<teeth ) { + glVertex3d( r0 * System.Math.Cos( ang ), r0 * System.Math.Sin( ang ), width * 0.5 ); + glVertex3d( r1 * System.Math.Cos( ang + 3 * da ), r1 * System.Math.Sin( ang + 3 * da ), width * 0.5 ); + } + } + glEnd(); + + /* front side of teeth */ + glBegin( GL_QUADS ); + da = 2.0 * System.Math.PI / teeth / 4.0; + for ( i=0; i<teeth; i++ ) { + ang = i * 2.0 * System.Math.PI / teeth; + glVertex3d( r1 * System.Math.Cos( ang ), r1 * System.Math.Sin( ang ), width * 0.5 ); + glVertex3d( r2 * System.Math.Cos( ang + da ), r2 * System.Math.Sin( ang + da ), width * 0.5 ); + glVertex3d( r2 * System.Math.Cos( ang + 2 * da ), r2 * System.Math.Sin( ang + 2 * da ), width * 0.5 ); + glVertex3d( r1 * System.Math.Cos( ang + 3 * da ), r1 * System.Math.Sin( ang + 3 * da ), width * 0.5 ); + } + glEnd(); + + glNormal3d( 0.0, 0.0, -1.0 ); + + /* draw back face */ + glBegin( GL_QUAD_STRIP ); + for ( i=0; i<teeth; i++ ) { + ang = i * 2.0 * System.Math.PI / teeth; + glVertex3d( r1 * System.Math.Cos( ang ), r1 * System.Math.Sin( ang ), -width * 0.5 ); + glVertex3d( r0 * System.Math.Cos( ang ), r0 * System.Math.Sin( ang ), -width * 0.5 ); + if (i<teeth) { + glVertex3d( r1 * System.Math.Cos( ang + 3 * da ), r1 * System.Math.Sin( ang + 3 * da ), -width * 0.5 ); + glVertex3d( r0 * System.Math.Cos( ang ), r0 * System.Math.Sin( ang ), -width * 0.5 ); + } + } + glEnd(); + + /* draw back side of teeth */ + glBegin( GL_QUADS ); + da = 2.0 * System.Math.PI / teeth / 4.0; + for ( i=0; i<teeth; i++ ) { + ang = i * 2.0 * System.Math.PI / teeth; + glVertex3d( r1 * System.Math.Cos( ang + 3 * da ), r1 * System.Math.Sin( ang + 3 * da ), -width * 0.5 ); + glVertex3d( r2 * System.Math.Cos( ang + 2 * da ), r2 * System.Math.Sin( ang + 2 * da ), -width * 0.5 ); + glVertex3d( r2 * System.Math.Cos( ang + da ), r2 * System.Math.Sin( ang + da ), -width * 0.5 ); + glVertex3d( r1 * System.Math.Cos( ang ), r1 * System.Math.Sin( ang ), -width * 0.5 ); + } + glEnd(); + + /* draw outward faces of teeth */ + glBegin( GL_QUAD_STRIP ); + for ( i=0; i<teeth; i++ ) { + ang = i * 2.0 * System.Math.PI / teeth; + glVertex3d( r1 * System.Math.Cos( ang ), r1 * System.Math.Sin( ang ), width * 0.5 ); + glVertex3d( r1 * System.Math.Cos( ang ), r1 * System.Math.Sin( ang ), -width * 0.5 ); + + u = r2 * System.Math.Cos( ang + da ) - r1 * System.Math.Cos( ang ); + v = r2 * System.Math.Sin( ang + da ) - r1 * System.Math.Sin( ang ); + len = System.Math.Sqrt( u * u + v * v ); + u /= len; + v /= len; + glNormal3d( v, -u, 0.0 ); + + glVertex3d( r2 * System.Math.Cos( ang + da ), r2 * System.Math.Sin( ang + da ), width * 0.5 ); + glVertex3d( r2 * System.Math.Cos( ang + da ), r2 * System.Math.Sin( ang + da ), -width * 0.5 ); + glVertex3d( System.Math.Cos( ang ), System.Math.Sin( ang ), 0.0 ); + glVertex3d( r2 * System.Math.Cos( ang + 2 * da ), r2 * System.Math.Sin( ang + 2 * da ), width * 0.5 ); + glVertex3d( r2 * System.Math.Cos( ang + 2 * da ), r2 * System.Math.Sin( ang + 2 * da ), -width * 0.5 ); + + u = r1 * System.Math.Cos( ang + 3 * da ) - r2 * System.Math.Cos( ang + 2 * da ); + v = r1 * System.Math.Sin( ang + 3 * da ) - r2 * System.Math.Sin( ang + 2 * da ); + glNormal3d( v, -u, 0.0 ); + + glVertex3d( r1 * System.Math.Cos( ang + 3 * da ), r1 + System.Math.Sin( ang + 3 * da ), width * 0.5 ); + glVertex3d( r1 * System.Math.Cos( ang + 3 * da ), r1 + System.Math.Sin( ang + 3 * da ), -width * 0.5 ); + + glNormal3d( System.Math.Cos( ang ), System.Math.Sin( ang ), 0.0 ); + } + glVertex3d( r1 * System.Math.Cos( 0 ), r1 * System.Math.Sin( 0 ), width * 0.5 ); + glVertex3d( r1 * System.Math.Cos( 0 ), r1 * System.Math.Sin( 0 ), -width * 0.5 ); + glEnd(); + + glShadeModel( GL_SMOOTH ); + + /* draw inside radius cylinder */ + glBegin( GL_QUAD_STRIP ); + for ( i=0; i<=teeth; i++ ) { + ang = i * 2.0 * System.Math.PI / teeth; + glNormal3d( -System.Math.Cos( ang ), -System.Math.Sin( ang ), 0.0 ); + glVertex3d( r0 * System.Math.Cos( ang ), r0 * System.Math.Sin( ang ), -width * 0.5 ); + glVertex3d( r0 * System.Math.Cos( ang ), r0 * System.Math.Sin( ang ), width * 0.5 ); + } + glEnd(); + } + protected void keyboard( byte key, int x, int y ) { if (key==27) System.Environment.Exit(0); |