//Jackie Weber //10/21/05 #include #include #include #include const int wallHeight=10; const int doorHeight=8; const double tablePostLeftX=6; const double tablePostRightX=11; const double tablePostTopZ=-10; const double tablePostBottomZ=-3; const double tablePostSize=1; const double chair1LeftX=-11; const double chair1RightX=-9; const double chair1TopZ=-7; const double chair1BottomZ=-5; const double rotate1=0; const double chair2LeftX=-5; const double chair2RightX=-7; const double chair2TopZ=-2; const double chair2BottomZ=0; const double rotate2=90; const double chair3LeftX=-5; const double chair3RightX=-7; const double chair3TopZ=-13; const double chair3BottomZ=-11; const double rotate3=270; //rug function const double rugScaleX=6; const double rugScaleY=1; const double rugScaleZ=4; const double rugSize=1; //rug1 const double r1=0.3; const double g1=0.4; const double b1=0.9; const double x1=0; const double z1=-15; //rug2 - colors not constant since they can be changed in callbacks double r2=0.4; double g2=1; double b2=0.2; const double x2=1; const double z2=5; //ball1 const double ballr1=1; const double ballg1=0; const double ballb1=0; double ballx1=9; //x and z not constant since they can be changed in callbacks double ballz1=-15; //ball2 const double ballr2=0; const double ballg2=1; const double ballb2=0; double ballx2=19; const double ballz2=-8.5; const GLfloat rose[3]={0.7,0.1,0.1}; const GLfloat purple[3]={.4,0,.4}; const GLfloat brown[3]={184.0/250.0, 115.0/250.0, 51.0/250.0}; float random01(){ float n= (float) random(); float d= (float) RAND_MAX; return n/d; } twTriple walls[20]= { {-10,0,10},//front left bottom 0 {-10, wallHeight, 10},//front left top 1 {10,wallHeight,10},//front right top 2 {10,0,10},//front right bottom 3 {25,0,-10},//right (more front)side middle bottom 4 {25,wallHeight,-10},//right(more front) side middle top 5 {10,wallHeight,-20},//right (more back) side top 6 {10,0,-20},//right (more back) side bottom 7 {-10,wallHeight,-20},//left back top 8 {-10,0,-20},//left back bottom 9 {-25,0,-10},//left (more front)side middle bottom 10 {-25,wallHeight,-10},//left(more front) side middle top 11 {-5,0,10},//left bottom of doorway 12 {5,0,10},//right bottom of doorway 13 {5,doorHeight,10},//right top of doorway 14 {-5,doorHeight,10},//left top of doorway 15 {-10,doorHeight,10},//left middle of front wall 16 {10,doorHeight,10},//right middle of front wall 17 }; void tablePosts(double x,double z){ glPushMatrix(); glColor3fv(brown); glTranslatef(x,1.6,z); glScalef(1,3,1); glutSolidCube(tablePostSize); glPopMatrix(); } void tableTop(float x1,float x2,float z1,float z2){//x1 should be right, x2 left, z1 top and z2 bottom twColorName(TW_WHITE); glTranslatef((x1+x2)/2.0,3.5,(z1+z2)/2.0); glPushMatrix(); glScalef(x1-x2+tablePostSize,1,z1-z2-tablePostSize); glutSolidCube(1); glPopMatrix(); } void chair(float chairRightX,float chairLeftX,float chairTopZ,float chairBottomZ, float rotate){ glPushMatrix(); tablePosts(chairRightX,chairTopZ); tablePosts(chairRightX,chairBottomZ); tablePosts(chairLeftX,chairBottomZ); tablePosts(chairLeftX,chairTopZ); tableTop(chairRightX,chairLeftX, chairTopZ, chairBottomZ); glRotatef(rotate,0,1,0); glTranslatef(-(chairRightX-chairLeftX)/2.0,2,(chairTopZ-chairBottomZ)/2.0+1); glRotatef(90,0,0,1); twColorName(TW_ORANGE); glScalef(chairRightX-chairLeftX+1,1,chairTopZ-chairBottomZ-1); glutSolidCube(1); glPopMatrix(); } void rug(float r, float g, float b, float x, float y){ glPushMatrix(); GLfloat colorRug[3]={r,g,b}; glColor3fv(colorRug); glTranslatef(x,0.1,y); glScalef(rugScaleX,rugScaleY,rugScaleZ); glRotatef(90,1,0,0); twDisk(rugSize,20); glPopMatrix(); } void ball(float r, float g, float b, float x, float z){ glPushMatrix(); glTranslatef(x,1,z); GLfloat colorBall[3]={r,g,b}; glColor3fv(colorBall); glutSolidSphere(1,10,15); glPopMatrix(); } void legos(float x, float z,float r, float g, float b){ GLfloat color[3]={r,g,b}; const double legoScale=0.5; const double legoTransY=0.7; glColor3fv(color); glPushMatrix(); glTranslatef(x,legoTransY,z); glScalef(legoScale,legoScale,legoScale); glutSolidCube(1); glPopMatrix(); } void drawCreative(){ GLfloat rose[3]={0.7,0.1,0.1}; GLfloat purple[3]={.4,0,.4}; GLfloat brown[3]={184.0/250.0, 115.0/250.0, 51.0/250.0}; GLfloat floor[3]={.1,.3,random01()}; glColor3fv(rose);//front wall - made up of three quads glBegin(GL_QUADS); { glVertex3fv(walls[16]); glVertex3fv(walls[0]); glVertex3fv(walls[12]); glVertex3fv(walls[15]); } glEnd(); glBegin(GL_QUADS); { glVertex3fv(walls[1]); glVertex3fv(walls[16]); glVertex3fv(walls[17]); glVertex3fv(walls[2]); } glEnd(); glBegin(GL_QUADS); { glVertex3fv(walls[13]); glVertex3fv(walls[3]); glVertex3fv(walls[17]); glVertex3fv(walls[14]); } glEnd(); twColorName(TW_BLUE);//wall to the right of the front wall glBegin(GL_QUADS); { glVertex3fv(walls[2]); glVertex3fv(walls[3]); glVertex3fv(walls[4]); glVertex3fv(walls[5]); } glEnd(); twColorName(TW_ORANGE);//to the right of the blue wall, farther back glBegin(GL_QUADS); { glVertex3fv(walls[4]); glVertex3fv(walls[5]); glVertex3fv(walls[6]); glVertex3fv(walls[7]); } glEnd(); glColor3fv(purple);//back wall glBegin(GL_QUADS); { glVertex3fv(walls[7]); glVertex3fv(walls[6]); glVertex3fv(walls[8]); glVertex3fv(walls[9]); } glEnd(); twColorName(TW_GREEN);//farther back of the two left walls glBegin(GL_QUADS); { glVertex3fv(walls[9]); glVertex3fv(walls[8]); glVertex3fv(walls[11]); glVertex3fv(walls[10]); } glEnd(); twColorName(TW_YELLOW);//closer to the front of the two left walls glBegin(GL_QUADS); { glVertex3fv(walls[11]); glVertex3fv(walls[10]); glVertex3fv(walls[0]); glVertex3fv(walls[1]); } glEnd(); //glColor3fv(floor);//flashing blues twColorName(TW_BLACK); glBegin(GL_POLYGON);//the floor { glVertex3fv(walls[0]); glVertex3fv(walls[3]); glVertex3fv(walls[4]); glVertex3fv(walls[7]); glVertex3fv(walls[9]); glVertex3fv(walls[10]); } glEnd(); glPushMatrix(); tablePosts(tablePostLeftX,tablePostTopZ);//tableposts for table tablePosts(tablePostRightX,tablePostTopZ); tablePosts(tablePostLeftX,tablePostBottomZ); tablePosts(tablePostRightX,tablePostBottomZ); tableTop(tablePostRightX,tablePostLeftX,tablePostTopZ,tablePostBottomZ);//top of table legos(2,1,0,1,0);//legos! more like blocks, really legos(1,2,0,0,1); legos(-1,-1,1,0,0); legos(-1,1,1,1,0); legos(-.5,.5,1,1,0); legos(.4,-.3,1,0,0); legos(-2,-2,0,1,0); legos(1,-2,0,0,1); glPopMatrix(); chair(chair1RightX,chair1LeftX,chair1TopZ,chair1BottomZ,rotate1); //chair(chair2RightX, chair2LeftX, chair2TopZ, chair2BottomZ,rotate2);//for some reason, I couldn't get this second chair or the third to work with variables... chair(-5,-7,-2,0,90); chair(-5,-7,-13,-11,270); //chair(chair3RightX,chair3LeftX, chair3TopZ, chair3BottomZ, rotate3); rug(r1,g1,b1,x1,z1); rug(r2,g2,b2,x2,z2); ball(ballr1,ballg1,ballb1,ballx1,ballz1); ball(ballr2,ballg2,ballb2,ballx2,ballz2); } void display (void){ twDisplayInit(); twCamera(); drawCreative(); glFlush(); glutSwapBuffers(); } void modify(unsigned char key, int x, int y){ switch(key){ case 'b' : if(ballx2>-22)ballx2=ballx2-0.5;glutPostRedisplay();break; case 'n':if(ballx2<22)ballx2=ballx2+0.5;glutPostRedisplay();break; case '+':if(ballz1<-2.3)ballz1++,ballx1--;glutPostRedisplay();break; case '-':if(ballz1>-17)ballz1--,ballx1++;glutPostRedisplay();break; case '2': r2=random01(),g2=random01(),b2=random01();glutPostRedisplay();break; } } int main(int argc, char ** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(500,500); glutCreateWindow(argv[0]); glutDisplayFunc(display); twBoundingBox(-26,26,-3,12,-21,20); twMainInit(); twKeyCallback('b',modify,"ball2 rolls to the left"); twKeyCallback('n',modify,"ball2 rolls to the right"); twKeyCallback('+', modify, "ball1 rolls forward and to the left"); twKeyCallback('-',modify, "ball1 rolls backward and to the right"); twKeyCallback('2',modify, "changes color of second rug"); glutMainLoop(); return 0; }