/************************************************************ * File: house.cc * Author: C.Yap * Date: Sep 23, 2001. * Synopsis: * OpenGL program for lecture 2 * Illustrates the use of display lists in GLUT. * ************************************************************/ #include // needed for use of cout #include // needed for math functions exp(x), etc // #include #include ///////////////////////////////////////////////////////////// // constants ///////////////////////////////////////////////////////////// int screenWidth = 640; int screenHeight = 480; #define MY_HOUSE 1 #define M_PI 3.14159265358979323846 ///////////////////////////////////////////////////////////// // callback functions ///////////////////////////////////////////////////////////// void house(float r, float g, float b, float x, float y, float z, float scale, float angle) { glPushMatrix(); glPolygonMode(GL_FRONT, GL_FILL); glTranslatef(x,y,z); glColor3f(r, g, b); glRotatef(angle, 0.0, 0.0, 1.0); glScalef(scale, scale, scale); glCallList(MY_HOUSE); glPopMatrix(); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); // clear screen // Exercise: what would happen if you do not clear screen? glColor3f(0.0f, 0.0f, 1.0f); // pen is blue // draw houses float del = 0.1; for (int i=0; i< 10; i++) { house(1.0 -i*del, 0, i*del, i*55, 10, 0, 0.5, 10); } for (int i=0; i< 10; i++) { house(0, 1.0 -i*del, i*del, i*75, 100, 0, 0.7, -20); } float up = 45*tan(15*M_PI/180); for (int i=0; i< 10; i++) { house(1.0 -i*del, i*del, 0, i*45, 200+i*up, 0, 0.4, 15); } glFlush(); }//myDisplay() ///////////////////////////////////////////////////////////// // MAIN PROGRAM ///////////////////////////////////////////////////////////// void main(int argc, char** argv) { ///////////////////////////////////////////////////////////// // initialization and create screen window ///////////////////////////////////////////////////////////// glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // display mode (single buffer) glutInitWindowSize(screenWidth, screenHeight); // window size glutInitWindowPosition(100, 150); // position of top right corner glutCreateWindow("Pretty Rows of Houses"); // open it glClearColor(1.0, 1.0, 1.0, 0.0); // background is white (default black) glColor3f(0.0f, 0.0f, 1.0f); // pen is blue (default black) glPointSize(2.0); // dot is 2pixels wide (default 1) glMatrixMode(GL_PROJECTION); // set camera shape glLoadIdentity(); // initialize projection matrix gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight); // set matrix for 2D graphics glMatrixMode(GL_MODELVIEW); // go back to default ///////////////////////////////////////////////////////////// // Modeling ///////////////////////////////////////////////////////////// GLint w = 100; GLint h = 50; GLint rw = 20; GLint rh = 20; GLint dw = 8; GLint dh = 30; glNewList(MY_HOUSE, GL_COMPILE); glBegin(GL_POLYGON); glVertex2d (0, 0); glVertex2d (w, 0); glVertex2d (w, h); glVertex2d (w-rw, h+rh); glVertex2d (rw, h+rh); glVertex2d (0, h); glEnd(); glColor3f(0, 0, 0); glBegin(GL_LINES); // roofline: glVertex2d (0,h); glVertex2d (w,h); // door: glVertex2d ((w/2) - dw, 0); glVertex2d ((w/2) - dw, dh); glVertex2d ((w/2) - dw, dh); glVertex2d ((w/2) + dw, dh); glVertex2d ((w/2) + dw, dh); glVertex2d ((w/2) + dw, 0); glEnd(); glEndList(); ///////////////////////////////////////////////////////////// // register callback functions ///////////////////////////////////////////////////////////// glutDisplayFunc(myDisplay); // redraw window event ///////////////////////////////////////////////////////////// glutMainLoop(); // mandatory final call to glut ///////////////////////////////////////////////////////////// }//main ///////////////////////////////////////////////////////////// // The end /////////////////////////////////////////////////////////////