/* Demo of the classic barn object. Implemented Fall 2003 Scott D. Anderson */ #include #include // for sqrt #ifdef MAC #include #include #include #else #include #endif const int barnWidth =30; const int barnHeight=40; const int barnLength=50; GLfloat barn[10][3] = { {0,0,0}, //left, bottom, front {barnWidth,0,0}, // right, bottom, front {barnWidth,barnHeight,0}, // right, top, front {0,barnHeight,0}, // left, top, front {barnWidth/2.0,barnHeight+barnWidth/2.0,0}, // ridge, front {0,0,-barnLength}, // left, bottom, back {barnWidth,0,-barnLength}, // right, bottom, back {barnWidth,barnHeight,-barnLength}, //right, top, back {0,barnHeight,-barnLength}, // left, top, back {barnWidth/2.0,barnHeight+barnWidth/2.0,-barnLength},// ridge, back }; // Almost every facet is a different color so that we can see them. void drawBarn() { int i; glColor3f(1,0,0); // set color to red glBegin(GL_POLYGON); // front { glVertex3fv(barn[0]); glVertex3fv(barn[1]); glVertex3fv(barn[2]); glVertex3fv(barn[3]); } glEnd(); glBegin(GL_POLYGON); // front ridge { glVertex3fv(barn[3]); glVertex3fv(barn[2]); glVertex3fv(barn[4]); } glEnd(); glColor3f(0,1,0); glBegin(GL_POLYGON); // back { glVertex3fv(barn[5]); glVertex3fv(barn[6]); glVertex3fv(barn[7]); glVertex3fv(barn[8]); } glEnd(); glBegin(GL_POLYGON); // back ridge { glVertex3fv(barn[7]); glVertex3fv(barn[8]); glVertex3fv(barn[9]); } glEnd(); glColor3f(0.5,0,0.5); glBegin(GL_POLYGON); // left side { glVertex3fv(barn[0]); glVertex3fv(barn[3]); glVertex3fv(barn[8]); glVertex3fv(barn[5]); } glEnd(); glColor3f(0.5,0,0); glBegin(GL_POLYGON); // right side { glVertex3fv(barn[1]); glVertex3fv(barn[2]); glVertex3fv(barn[7]); glVertex3fv(barn[6]); } glEnd(); glColor3f(0.5,0.5,0); glBegin(GL_POLYGON); // left side roof { glVertex3fv(barn[3]); glVertex3fv(barn[4]); glVertex3fv(barn[9]); glVertex3fv(barn[8]); } glEnd(); glBegin(GL_POLYGON); /* right side roof */ { glVertex3fv(barn[2]); glVertex3fv(barn[4]); glVertex3fv(barn[9]); glVertex3fv(barn[7]); } glEnd(); } void init() { glDisable(GL_LIGHTING); glShadeModel(GL_FLAT); // Use a simple non-distorting (letterbox) perspective camera with // 90 degree fovy on a 4:3 shaped window, so the frustum is based on // the horizontal, which makes things just a little more complex. // Since we can't move the camera around without using TW, we'll // place the camera above and to the right of the barn, so you can // see the front, right side, and the roof. GLfloat BBheight = barnHeight+barnWidth/2.0; GLfloat BBdepth = barnLength; GLfloat BBwidth = barnWidth; GLfloat BBradius = 0.5*sqrt(BBheight*BBheight+BBdepth*BBdepth+BBwidth*BBwidth); GLfloat EyeRadius = BBradius*sqrt(2); // Compute Center of BB GLfloat CenterX = BBwidth/2; GLfloat CenterY = BBheight/2; GLfloat CenterZ = -BBdepth/2; // Place Eye equally far from Center along each axis GLfloat EyeOrthoDist = EyeRadius/sqrt(3); GLfloat EyeX = CenterX+EyeOrthoDist; GLfloat EyeY = CenterY+EyeOrthoDist; GLfloat EyeZ = CenterZ+EyeOrthoDist; EyeX = CenterX; EyeY = CenterY; EyeZ = CenterZ+EyeRadius; GLfloat fovy = 90; GLfloat aspectRatio = 4.0/3.0; GLfloat near = EyeRadius - BBradius; GLfloat far = EyeRadius + BBradius; // Some debugging print statements: printf("BBradius = %f, EyeRadius =%f\n",BBradius,EyeRadius); printf("Center = (%f,%f,%f)\n",CenterX,CenterY,CenterZ); printf("aspect ratio = %f\nEyeRadius = %f, near=%f far=%f\nVRP=(%f,%f,%f)\n", aspectRatio,EyeRadius,near,far,EyeX,EyeY,EyeZ); printf("EyeOrthoDist = %f\n",EyeOrthoDist); // Okay, finally ready for the OpenGL calls glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(fovy,aspectRatio,near,far); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(EyeX,EyeY,EyeZ, CenterX,CenterY,CenterZ, 0,1,0); } void display(void) { glClearColor(0.75,0.75,0.75,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawBarn(); glFlush(); glutSwapBuffers(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(400,300); glutCreateWindow(argv[0]); glutDisplayFunc(display); glEnable(GL_DEPTH_TEST); init(); glutMainLoop(); return 0; }