from gears to the machine in 13 steps carolyn smith cosc 4331 5/6/2009
DESCRIPTION
From Gears to The Machine in 13 Steps Carolyn Smith COSC 4331 5/6/2009. How does gears.c work? - gear(GLfloat inner_radius, Glfoat outer_radius, Glfloat width, Glint teeth, Glfoat tooth_depth) - draw(void): glPushMatrix(); glTranslatef(-3.0, -2.0, 0.0); - PowerPoint PPT PresentationTRANSCRIPT
From Gears to The Machine in 13 Steps
Carolyn SmithCOSC 43315/6/2009
How does gears.c work?
- gear(GLfloat inner_radius, Glfoat outer_radius, Glfloat width, Glint teeth, Glfoat tooth_depth)
- draw(void): glPushMatrix(); glTranslatef(-3.0, -2.0, 0.0); glRotatef(angle, 0.0, 0.0, 1.0); glCallList(gear1); GlPopMatrix();
- void key(unsigned char k [...]) //keyboard input for rotation of view on z-axis
- void reshape(int width, int height) //responds to resizing of window by scaling gears appropriately
- int limit = 0 by default, and int count = 1; limit can be set using an argument at run time, and animation ceases when count = limit
Step 1: Change gears to ringsOriginal code:
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();
Modified code: gear1 = glGenLists(1); glNewList(gear1, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); gear(1.0, 4.0, 1.0, 20, 0.0); glEndList();
Step 2: Change ring radii & widthOriginal code:
gear1 = glGenLists(1); glNewList(gear1, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); gear(1.0, 4.0, 1.0, 20, 0); glEndList();
Modified code: gear1 = glGenLists(1); glNewList(gear1, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); gear(1.0, 1.5, 0.5, 20, 0.0); glEndList();
Step 3: Remove translationOriginal code:
glPushMatrix(); glTranslatef(-3.0, -2.0, 0.0); glRotatef(angle, 0.0, 0.0, 1.0); glCallList(gear1); glPopMatrix();
Modified code: glPushMatrix(); glTranslatef(0.0, 0.0, 0.0); // or just remove altogether glRotatef(angle, 0.0, 0.0, 1.0); glCallList(gear1); glPopMatrix();
Step 4: Change rotation axesOriginal code (all rotating about z-axis):
Gear1: glRotatef(angle, 0.0, 0.0, 1.0);Gear2: glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);Gear3: glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
Modified code:Gear1: glRotatef(angle, 0.0, 1.0, 1.0);Gear2: glRotatef(-2.0 * angle - 9.0, 1.0, 1.0, 0.0);Gear3: glRotatef(-2.0 * angle - 25.0, 1.0, 0.0, 1.0);
Step 5: Add fourth gearCode to add:
//(define new color, “yellow”) static GLfloat yellow[4] = {1.0, 1.0, 0.0, 1.0};
… gear4 = glGenLists(1); glNewList(gear4, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow); gear(7.0, 7.5, 1.0, 20, 0.0); glEndList(); glEnable(GL_NORMALIZE);
… glPushMatrix(); glRotatef(-2.0 * angle - 25.0, 0.0, 1.0, 0.0); glCallList(gear4); glPopMatrix();
Step 6: Resize windowCode to add:
glutInitWindowSize(700, 700); glutInitWindowPosition(100,150);
Step 7: Begin to construct baseCode to add:static GLint gear5; // "holder"-- non-rotating gear… static GLfloat slate[4] = {0.4, 0.5, 0.6, 1.0);… gear5 = glGenLists(1); glNewList(gear5, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, slate); gear(8.0, 8.5, 1.0, 20, 0.0); glEndList(); glEnable(GL_NORMALIZE);… //in draw() glCallList(gear5); // no rotation matrix
Step 8: Define background colorCode to add:
//in draw() glClearColor(0.0f, 0.1f, 0.3f, 0.0f);
Step 9: Add pod
Code to add://(define new color, white) static GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};… pod = glGenLists(1); glNewList(pod, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white); glutSolidSphere(0.3, 15, 15); glEndList();…//(in draw()) glCallList(pod);
Step 10: Change camera rotation
Original Code:static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
Modified Code:static GLfloat view_rotx = 105.0, view_roty = 0.0, view_rotz =
45.0;
Rotation variables are used in draw() to set up camera angle with each redraw, and can be modified using the key() function.
Step 11: Add base supportsCode to add:
// in draw() glPushMatrix(); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, slate); glTranslatef(0.0, 8.0, 0.0); glutSolidCube(1.5); glTranslatef(0.0, 0.0, 1.5); glutSolidCube(1.8); glTranslatef(0.0, 0.0, 1.8); glutSolidCube(2.1); glTranslatef(0.0, 0.0, 2.1); glutSolidCube(2.4); glTranslatef(0.0, 0.0, 2.4); glutSolidCube(2.7); GlPopMatrix();
// repeat for other 3 legs with appropriate translation values
Step 12: Zoom out for default viewOriginal code:
static void reshape(int width, int height){ GLfloat h = (GLfloat) height / (GLfloat) width; glViewport(0, 0, (GLint) width, (GLint) height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -40.0);}
Modified line: glTranslatef(0.0, 2.0, -50.0);
Step 13: Add key-controlled zoomOriginal code:static void key(char k ...){ switch (k) { case 'z': view_rotz += 5.0; break; case 'Z': view_rotz -= 5.0; break; case 27: /* Escape */ exit(0); break; default: return; } glutPostRedisplay();}
Modified line:// added two cases and converted z-axis camera
// translation to a variable, view_zoom. case 'i': view_zoom += 0.2; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 2.0, view_zoom); break; case 'o': view_zoom -= 0.2; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 2.0, view_zoom);
break;
Source:“gears.h” by Brian Paul, converted to GLUT by Mark J. Kilgard
Retrieved from Dr. Wright's ACL directory