Presentation is loading. Please wait.

Presentation is loading. Please wait.

From Gears to The Machine in 13 Steps Carolyn Smith COSC 4331 5/6/2009.

Similar presentations


Presentation on theme: "From Gears to The Machine in 13 Steps Carolyn Smith COSC 4331 5/6/2009."— Presentation transcript:

1 From Gears to The Machine in 13 Steps Carolyn Smith COSC /6/2009

2 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

3 Step 1: Change gears to rings Original 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();

4 Step 2: Change ring radii & width Original 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();

5 Step 3: Remove translation Original 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();

6 Step 4: Change rotation axes Original 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 , 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 , 1.0, 0.0, 1.0);

7 Step 5: Add fourth gear Code 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 , 0.0, 1.0, 0.0); glCallList(gear4); glPopMatrix();

8 Step 6: Resize window Code to add: glutInitWindowSize(700, 700); glutInitWindowPosition(100,150);

9 Step 7: Begin to construct base Code 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

10 Step 8: Define background color Code to add: //in draw() glClearColor(0.0f, 0.1f, 0.3f, 0.0f);

11 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);

12 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.

13 Step 11: Add base supports Code 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

14 Step 12: Zoom out for default view Original 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);

15 Step 13: Add key-controlled zoom Original 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;

16 Source: “gears.h” by Brian Paul, converted to GLUT by Mark J. Kilgard Retrieved from Dr. Wright's ACL directory


Download ppt "From Gears to The Machine in 13 Steps Carolyn Smith COSC 4331 5/6/2009."

Similar presentations


Ads by Google