/* Dackral Phillips CSE 540 Lab #2 This is a program which displays several primitives as well as animates objects. The program includes a plane that makes a crash landing, but unlike the movies, it doesn't burst into flames (not all crashes cause this effect, you know). The background contains an animated windmill. It also adjusts the fram rate using an operating system watchdog timer*/ #include #include #include #define PI 3.141592 static GLfloat w = 0.0; static GLint x = 0; static GLint y = 0; static GLint z = 0; void init() { glClearColor(0.8f, 0.8f, 1.0f, 1.0f); glShadeModel(GL_SMOOTH); } void reshape(GLsizei w, GLsizei h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 250.0,0.0, 250.0,-1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void drawlandscape() { /* Draw a box of grass */ glBegin(GL_QUADS); glColor3f(0.0, 1.0, 0.0); glVertex2f(250.0, 0.0); glColor3f(0.0, 0.9, 0.0); glVertex2f(250.0, 50.0); glColor3f(0.0, 0.8, 0.0); glVertex2f(0.0, 50.0); glColor3f(0.0, 0.7, 0.0); glVertex2f(0.0, 0.0); glEnd(); /* An attempt at a few snow covered mountains */ glBegin(GL_TRIANGLES); glColor3f(0.0, 0.0, 0.6); glVertex2f(250.0, 50.0); glColor3f(1.0, 1.0, 1.0); glVertex2f(200.0, 150.0); glColor3f(0.0, 0.0, 0.5); glVertex2f(150.0, 50.0); glBegin(GL_TRIANGLES); glColor3f(0.0, 0.0, 0.5); glVertex2f(200.0, 50.0); glColor3f(1.0, 1.0, 1.0); glVertex2f(150.0, 150.0); glColor3f(0.0, 0.0, 0.5); glVertex2f(100.0, 50.0); glColor3f(0.0, 0.0, 0.7); glVertex2f(150.0, 50.0); glColor3f(1.0, 1.0, 1.0); glVertex2f(100.0, 150.0); glColor3f(0.0, 0.0, 0.5); glVertex2f(50.0, 50.0); glColor3f(0.0, 0.0, 0.5); glVertex2f(100.0, 50.0); glColor3f(1.0, 1.0, 1.0); glVertex2f(50.0, 150.0); glColor3f(0.0, 0.0, 0.5); glVertex2f(0.0, 50.0); glEnd(); /* Draw the body of a windmill */ glBegin(GL_QUADS); glColor3f(0.6, 0.6, 0.0); glVertex2f(145.0, 50.0); glVertex2f(135.0, 100.0); glVertex2f(115.0, 100.0); glVertex2f(105.0, 50.0); glEnd(); glutSwapBuffers(); glFlush(); } void drawwindmill() { /* Draw a windmill */ glPushMatrix(); glTranslatef(125.0, 90.0, 0.0); glRotatef(z, 0.0, 0.0, 1.0); glTranslatef(-125.0, -90.0, 0.0); glBegin(GL_TRIANGLES); glColor3f(0.8, 0.8, 0.8); glVertex2f(125.0, 90.0); glVertex2f(140.0, 120.0); glVertex2f(160.0, 120.0); glVertex2f(125.0, 90.0); glVertex2f(110.0, 120.0); glVertex2f(90.0, 120.0); glVertex2f(160.0, 60.0); glVertex2f(140.0, 60.0); glVertex2f(125.0, 90.0); glVertex2f(90.0, 60.0); glVertex2f(110.0, 60.0); glVertex2f(125.0, 90.0); glEnd(); z = z + 10; glPopMatrix(); glutSwapBuffers(); glFlush(); } void drawplane() { /* Draw a plane */ glPushMatrix(); glTranslatef(x, y, 0.0); if ((y <= -200) && (x > -150)) { glRotatef(w, 0.0, 0.0, 1.0); if (w < 10) { w = w + 1; y = y - 3; } } glBegin(GL_TRIANGLES); glColor3f(0.0, 0.0, 0.0); glVertex2f(245.0, 230.0); glVertex2f(245.0, 240.0); glVertex2f(215.0, 230.0); glColor3f(0.2, 0.2, 0.2); glVertex2f(244.0, 228.0); glVertex2f(244.0, 235.0); glVertex2f(228.0, 235.0); glEnd(); if (x > -130) { x = x - 2; } if (y > -200) { y = y - 20; } glPopMatrix(); glutSwapBuffers(); glFlush(); } void display() { glClear (GL_COLOR_BUFFER_BIT); drawlandscape(); drawplane(); drawwindmill(); } void keyboard(unsigned char key, int x, int y) { switch(key) { case 27: exit(0); break; } } void Timer( int value) { display(); glutTimerFunc(30, Timer, 1); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(50,50); glutInitWindowSize(500,500); glutCreateWindow("CSE 540 Lab #2"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutTimerFunc(30, Timer, 1); glutMainLoop(); }