from gears to the machine in 13 steps carolyn smith cosc 4331 5/6/2009

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

Upload: claudia-vince

Post on 03-Jan-2016

26 views

Category:

Documents


1 download

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 Presentation

TRANSCRIPT

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

From Gears to The Machine in 13 Steps

Carolyn SmithCOSC 43315/6/2009

Page 2: 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); 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

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

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

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

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

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

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

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

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

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

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

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

Step 6: Resize windowCode to add:

glutInitWindowSize(700, 700); glutInitWindowPosition(100,150);

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

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

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

Step 8: Define background colorCode to add:

//in draw() glClearColor(0.0f, 0.1f, 0.3f, 0.0f);

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

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

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

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.

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

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

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

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

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

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;

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

Source:“gears.h” by Brian Paul, converted to GLUT by Mark J. Kilgard

Retrieved from Dr. Wright's ACL directory