/* Using stacks and affine transformations to construct a mobile. Written by Caroline Geiersbach and Scott D. Anderson scott.anderson@acm.org Summer 2003 */ #include #include //bounding box dimensions GLfloat width = 80; GLfloat height = 60; GLfloat depth2 = 5; // half the depth //bar lengths GLfloat bLength1=35; // length of first bar from top GLfloat bLength2=20; // length of second bar GLfloat bLength3=15; // length of third bar GLfloat bLength4=8; // length of fourth bar //string lengths GLfloat sLength1=10; // length of first string from top GLfloat sLength2=7; // length of second string GLfloat sLength3=15; // length of third string GLfloat sLength4=15; // length of fourth string //draws a "bar" of unit height and width; takes length as a parameter void drawBar(GLfloat length) { glPushMatrix(); twColorName(TW_GRAY); glScalef(length,1,1); glutSolidCube(1); glPopMatrix(); } // draws a red string of length len down the negative y axis and moves // coordinate system to the end of the string void drawString(GLfloat len) { twColorName(TW_RED); glBegin(GL_LINES); glVertex3f(0,0,0); glVertex3f(0,-len,0); glEnd(); glTranslatef(0,-len,0); } //resizes twDrawBarn, translated so that barn is drawn from the center //(x and z directions) and from the top (y direction). void drawBarn() { glPushMatrix(); glRotatef(75,0,1,0); //turn so sides of barn can be seen glTranslatef(-2,-6,3); //center barn at end of string glScalef(4,6,6); twTriple side = {0.2,0.2,0.9}; //color of sides twTriple roof = {0.3,0.3,0.3}; //color of roof twTriple ends = {0.3,0.3,0.9}; //color of ends twSolidBarn(ends,side,roof); glPopMatrix(); } // ================================================================ void mobileLR() { glPushMatrix(); drawString(sLength2); drawBarn(); glPopMatrix(); } void mobileLLR() { glPushMatrix(); drawString(sLength3); twTriple darkCyan = {0, 0.5, 0.5}; twColor(darkCyan,0,0); glTranslatef(0,-2,0); //translate down the size of the radius of the sphere glutSolidSphere(3,20,20); // sphere glPopMatrix(); } void mobileLLLR() { glPushMatrix(); drawString(sLength4); twTriple darkPink = {0.5, 0, 0.5}; twColor(darkPink, 0, 0); //set color for tetrahedron glScalef(2,2,2); glutSolidTetrahedron(); // tetrahedron glPopMatrix(); } void mobileLLLL() { glPushMatrix(); drawString(sLength4); twTriple greenish = {0.2, 0.8, 0.2}; twColor(greenish,0,0); //set color for icosahedron glScalef(2,2,2); glutSolidIcosahedron(); glPopMatrix(); } void mobileLLL() { glPushMatrix(); drawString(sLength3); drawBar(bLength4); glTranslatef(bLength4/2,0,0); mobileLLLR(); glTranslatef(-bLength4,0,0); mobileLLLL(); glPopMatrix(); } void mobileLL() { glPushMatrix(); drawString(sLength2); drawBar(bLength3); glTranslatef(bLength3/2,0,0); mobileLLR(); glTranslatef(-bLength3,0,0); mobileLLL(); glPopMatrix(); } void mobileL() { glPushMatrix(); drawString(sLength1); drawBar(bLength2); glTranslatef(bLength2/2,0,0); mobileLR(); glTranslatef(-bLength2,0,0); mobileLL(); glPopMatrix(); } void mobileRRRR() { glPushMatrix(); drawString(sLength4); twTriple lightRed = {0.2,0.2,1}; twColor(lightRed,0,0); //set color for teapot glutSolidTeapot(1.5); glPopMatrix(); } void mobileRL() { glPushMatrix(); drawString(sLength2); twTriple darkYellow = {0.8, 0.8, 0.0}; twColor(darkYellow,0,0); glTranslatef(0,-3,0); //translate down the size of the torus's outer radius glutSolidTorus(1,3,10,10); /* TORUS */ glPopMatrix(); } void mobileRRL() { glPushMatrix(); drawString(sLength3); twTriple darkGreen = {0.5,0.1,0.1}; twColor(darkGreen,0,0); glScalef(10,10,10); twTeddyBear(); // bear glPopMatrix(); } void mobileRRRL() { glPushMatrix(); drawString(sLength4); twColorName(TW_CYAN); glScalef(2,2,2); glutSolidOctahedron(); // octahedron glPopMatrix(); } void mobileRRR() { glPushMatrix(); drawString(sLength3); drawBar(bLength4); glTranslatef(bLength4/2,0,0); mobileRRRR(); glTranslatef(-bLength4,0,0); mobileRRRL(); glPopMatrix(); } void mobileRR() { glPushMatrix(); drawString(sLength2); drawBar(bLength3); glTranslatef(bLength3/2,0,0); mobileRRR(); glTranslatef(-bLength3,0,0); mobileRRL(); glPopMatrix(); } void mobileR() { glPushMatrix(); drawString(sLength1); drawBar(bLength2); glTranslatef(bLength2/2,0,0); mobileRR(); glTranslatef(-bLength2,0,0); mobileRL(); glPopMatrix(); } void mobile() { glPushMatrix(); drawString(10); //top middle string drawBar(bLength1); //draw top bar glTranslatef(bLength1/2,0,0); mobileR(); glTranslatef(-bLength1,0,0); mobileL(); glPopMatrix(); } void display(void) { twDisplayInit(); twCamera(); //sets up camera based on bounding box coords. glPushMatrix(); glTranslatef(width/2,height,0); mobile(); glPopMatrix(); glFlush(); glutSwapBuffers(); //necessary for animation } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(600,600); glutCreateWindow(argv[0]); glutDisplayFunc(display); twBoundingBox(0,width,0,height,-depth2,+depth2); twMainInit(); glutMainLoop(); return 0; }