/* Creates a bicycle object. Demonstrates affine transforms. Note on the objects used: glutSolidTorus is drawn in the xy plane from its center; twSolidCylinder is centered in the xz plane and is drawn downward from y = 0. Written by Caroline Geiersbach and Scott D. Anderson scott.anderson@acm.org Summer 2003 */ #include #include #include #include //colors used twTriple tireColor = {0.1,0.1,0.1}; twTriple frameColor = {0.2,0,0.6}; //draws one tire and its spokes void drawTire() { int i; glPushMatrix(); twColor(tireColor,0.9,50.0); glutSolidTorus(0.3,3.0,30,30); for(i=0;i<12;i++) { //spokes glRotatef(30,0,0,1); twSolidCylinder(0.03,0.03,3.0,10,10); } glPopMatrix(); } //draws handlebars void drawHandlebar() { glPushMatrix(); twSolidCylinder(0.2,0.2,3,20,20); glRotatef(90,1,0,0); glTranslatef(0,3,0); twSolidCylinder(0.2,0.2,6,20,20); glPopMatrix(); } //draws seat void drawSeat() { glPushMatrix(); twSolidCylinder(0.2,0.2,2,20,20); //post glRotatef(90,0,0,1); glScalef(0.5,1,1); //flatten seat (y-direction) twSolidCylinder(1,0.2,2.5,20,20); //seat glPopMatrix(); } //used three times in the bicycle, oriented downwards (y-direction) void parallelBars(GLfloat length) { glPushMatrix(); glTranslatef(0,0,0.5); twSolidCylinder(0.1,0.1,length,20,20); glTranslatef(0,0,-1); twSolidCylinder(0.1,0.1,length,20,20); //connect the bars glRotatef(-90,1,0,0); twSolidCylinder(0.1,0.1,1,20,20); glTranslatef(0,0,-length); twSolidCylinder(0.1,0.1,1,20,20); glPopMatrix(); } void frame() { twColor(frameColor,0.9,100); //draw center bar glPushMatrix(); glTranslatef(-5,0,0); glRotatef(90,0,0,1); twSolidCylinder(0.2,0.2,10,20,20); glPopMatrix(); //draw back frame glPushMatrix(); glTranslatef(-5,0,0); GLfloat angle = atan(2.0/5.0)*180/M_PI; glRotatef(-angle,0,0,1); parallelBars(sqrt(29)); glRotatef(angle*2,0,0,1); twSolidCylinder(0.2,0.2,sqrt(29),20,20); glPopMatrix(); //draw front frame glPushMatrix(); glTranslatef(5,0,0); glRotatef(angle,0,0,1); parallelBars(sqrt(29)); GLfloat angle2 = atan(8.0/5.0)*180/M_PI; glRotatef(-angle2-angle,0,0,1); twSolidCylinder(0.2,0.2,sqrt(89),20,20); glPopMatrix(); } void drawBicycle() { //draw back tire glPushMatrix(); glTranslatef(-7,-5,0); drawTire(); twColor(frameColor,0.9,100); //return to frame color glRotatef(90,0,0,1); parallelBars(4); glPopMatrix(); //draw front tire glPushMatrix(); glTranslatef(7,-5,0); drawTire(); glPopMatrix(); //draw frame frame(); //draw handlebars glPushMatrix(); glTranslatef(5,3,0); drawHandlebar(); glPopMatrix(); //draw seat glPushMatrix(); glTranslatef(-5,2,0); drawSeat(); glPopMatrix(); } void display(void) { twDisplayInit(); twCamera(); // sets up camera based on bounding box coords. glEnable(GL_LIGHTING); twLighting2(); drawBicycle(); glutSwapBuffers(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(650,650); glutCreateWindow(argv[0]); twBoundingBox(-11,11,-9,5,-4,4); twMainInit(); glutDisplayFunc(display); glutMainLoop(); return 0; }