//SIMPLE SOLUTION FOR ASSIGNMENT 1, part 1 #include // Header File For The GLUT Library #include // Header File For The OpenGL32 Library #include // Header File For The GLu32 Library /* ASCII code for the escape key. */ #define ESCAPE 27 /* The number of our GLUT window */ int window; float vx = 0, vy = 90, vz = 150; float coord[10][2] = { {-45.0f, -90.0f}, {-35.0f, 75.0f}, {-25.0f, -50.0f}, {-25.0f, 25.0f}, {-25.0f, -5.0f}, { 25.0f, 90.0f}, { 22.0f, -70.0f}, { 25.0f, 50.0f}, { 40.0f, -30.0f}, { 25.0f, 10.0f}, }; /* A general OpenGL initialization function. Sets all of the initial parameters. */ void init(int Width, int Height) // We call this right after our OpenGL window is created. { glClearColor(0.0f, 0.4f, 0.9f, 0.0f); // This Will Clear The Background Color To Black glClearDepth(1.0); // Enables Clearing Of The Depth Buffer glDepthFunc(GL_LESS); // The Type Of Depth Test To Do glEnable(GL_DEPTH_TEST); // Enables Depth Testing glShadeModel(GL_FLAT); } /* The function called when our window is resized */ void resizeScene(int width, int height) { if (height==0) // Prevent A Divide By Zero If The Window Is Too Small height=1; glViewport(0, 0, width, height); // Reset The Current Viewport And Perspective Transformation glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 500.0f); glMatrixMode(GL_MODELVIEW); } /* draw a single tree. */ void drawTree(float x, float z) { glPushMatrix(); // glMatrixMode(GL_MODELVIEW); glLineWidth(6); glBegin(GL_LINES); glColor3f(0.6f, 0.4f, 0.1f); glVertex3f(x, 0.0f, z); glVertex3f(x, 20.0f, z); glEnd(); glTranslatef(x, 30.0f, z); glColor3f(0.0f, 0.6f, 0.2f); glutSolidSphere(10.0f, 100, 100); glTranslatef(-x, -30.0f, -z); glPopMatrix(); } drawRoad() { // road line (dashed). glLineStipple(1, 0x00FF); glLineWidth(4); glEnable(GL_LINE_STIPPLE); glBegin(GL_LINES); glColor3f(1, 1, 0); glVertex3f(0.0f, 0.0f, 100.0f); glVertex3f(0.0f, 0.0f, -100.0f); glEnd(); glDisable(GL_LINE_STIPPLE); // road. glBegin(GL_QUADS); glColor3f(0.3f, 0.3f, 0.3f); glVertex3f(-15.0f, 0.0f, 100.0f); glVertex3f( 15.0f, 0.0f, 100.0f); glVertex3f( 15.0f, 0.0f,-100.0f); glVertex3f(-15.0f, 0.0f,-100.0f); glEnd(); } drawGreen() { // left plane. glBegin(GL_QUADS); glColor3f(0.2f, 0.8f, 0.0f); glVertex3f(-150.0f, 0.0f, 100.0f); glVertex3f(-15.0f, 0.0f, 100.0f); glVertex3f(-15.0f, 0.0f,-100.0f); glVertex3f(-150.0f, 0.0f,-100.0f); glEnd(); // right plane. glBegin(GL_QUADS); glColor3f(0.2f, 0.8f, 0.0f); glVertex3f(15.0f, 0.0f, 100.0f); glVertex3f(150.0f, 0.0f, 100.0f); glVertex3f(150.0f, 0.0f,-100.0f); glVertex3f(15.0f, 0.0f,-100.0f); glEnd(); } drawHouse(float x, float y, float z) { glPushMatrix(); glTranslatef(x,y,z); // move it somewhere glPushMatrix(); glColor3f(0.5,0.5,0.5); glTranslatef(0,20/2,0); glutSolidCube(20); glPopMatrix(); //roof glBegin(GL_POLYGON); glColor3f(0.9,0.1,0.1); glVertex3f(-10,20,10); glVertex3f(0,30,0); glVertex3f(10,20,10); glEnd(); glBegin(GL_POLYGON); glColor3f(0.9,0.1,0.1); glVertex3f(10,20,10); glVertex3f(0,30,0); glVertex3f(10,20,-10); glEnd(); glBegin(GL_POLYGON); glColor3f(0.9,0.1,0.1); glVertex3f(-10,20,10); glVertex3f(0,30,0); glVertex3f(-10,20,-10); glEnd(); glBegin(GL_POLYGON); glColor3f(0.9,0.1,0.1); glVertex3f(-10,20,-10); glVertex3f(0,30,0); glVertex3f(-10,20,10); glEnd(); glPopMatrix(); } /* The main drawing function. */ void drawScene() { int i = 0; glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // reset modelview matrix to identity (original coordinate system.) glLoadIdentity(); gluLookAt(vx, vy, vz, 0, 0, 0, 0, 1, 0); // always imagine that you are drawing from the top (arial view) on // the xz-plane. Remember that positive z is downwards (outside of // your screen, towards you.) glLineWidth(1); glBegin(GL_LINES); glColor3f(0.0f, 0.0f, 0.0f); // x-axis. glVertex3f(-1000.0f, 0.0f, 0.0f); glVertex3f( 1000.0f, 0.0f, 0.0f); // y-axis. glVertex3f(0.0f,-1000.0f, 0.0f); glVertex3f(0.0f, 1000.0f, 0.0f); // z-axis. glVertex3f(0.0f, 0.0f,-1000.0f); glVertex3f(0.0f, 0.0f, 1000.0f); glEnd(); drawRoad(); drawGreen(); for (i=0; i<3; i++) { drawHouse(-50, 0, i*40 - 30); } for (i = 0; i < 10; i++) { drawTree(coord[i][0], coord[i][1]); } // swap the buffers to display, since double buffering is used. glutSwapBuffers(); } /* The function called whenever a key is pressed. */ void keyPressed(unsigned char key, int x, int y) { /* If escape is pressed, kill everything. */ if (key == ESCAPE) { /* shut down our window */ glutDestroyWindow(window); /* exit the program...normal termination. */ exit(0); } glutPostRedisplay(); } /* The function called whenever a special key is pressed. */ void special(int key, int x, int y) { // assign your keys here for controlling rotations ... glutPostRedisplay(); } int main(int argc, char **argv) { /* Initialize GLUT state */ glutInit(&argc, argv); /* Select type of Display mode: Double buffer RGBA color Alpha components supported Depth buffered for automatic clipping */ glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); /* get a 640 x 480 window */ glutInitWindowSize(640, 480); /* the window starts at the upper left corner of the screen */ glutInitWindowPosition(0, 0); /* Open a window */ window = glutCreateWindow(""); /* Register the function to do all our OpenGL drawing. */ glutDisplayFunc(&drawScene); /* Go fullscreen. This is as soon as possible. */ // glutFullScreen(); /* Register the function called when our window is resized. */ glutReshapeFunc(&resizeScene); /* Register the function called when the keyboard is pressed. */ glutKeyboardFunc(&keyPressed); /* Register the function called when the keyboard is pressed. */ glutSpecialFunc(&special); /* Initialize our window. */ init(640, 480); /* Start Event Processing Engine */ glutMainLoop(); return 1; }