/* Dackral Phillips CSE 540 Lab #5 Section A Dack Golf - This is a 3D golf course that works according to real world physics. Keys for game play: Up/Dn - Accellerate/Decelerate the ball Lft/Rght - Change the angle of the ball 1, 2, 3 - Change camera views : Overhead, Club, Ball n - new game Space - Putt the ball Esc, Exit the program */ /* The following are for compile on windows */ #include #include /* End Windows Includes, begin Unix Includes */ #include #include #include #include #define PI 3.141592654 static GLfloat ballx = -180.0; static GLfloat bally = 5.0; static GLfloat ballz = 180; static GLfloat clubcamx = -190.0; static GLfloat clubcamy = 10.0; static GLfloat clubcamz = 180; static GLfloat ballcamx = -190.0; static GLfloat ballcamy = 10.0; static GLfloat ballcamz = 180; static GLfloat wally = 25.0; static GLfloat rad = 0; static GLint angle_of_shot = 90; static int angle1 = 0; static int angle2 = 9; static int angle3 = 0; static char angle[3] = "090"; static GLfloat friction = .04; static GLfloat acc = 0; static GLfloat powermeter = 20.0; static GLfloat accx = 0; static GLfloat accz = 0; static GLfloat startfr = 1, endfr = 0, framerate = 1; static GLsizei x, y; static GLint ballrot = 0; static int camera = 1; static int strokes = 0; static int stroke1 = 0; static int stroke2 = 0; static char stroke[2] = {"00"}; static int togglex = 1; static int togglez = -1; static int toggleacc = 0; static int togglewall = 1; static int hill = 0; static int hole = 0; static GLfloat ambient[]={0.2, 0.2, 0.2, 1.0}; static GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0}; static GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; static GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; static GLfloat light_position[]={0.0, 0.0, 1.0, 0.0}; static GLfloat light_position2[]={0.0, 0.0, -1.0, 0.0}; static GLfloat light_position3[]={0.0, 1.0, 0.0, 0.0}; static GLfloat light_position4[]={0.0, -1.0, 0.0, 0.0}; void init() { glClearColor(0.6f, 0.6f, 1.0f, 1.0f); glShadeModel(GL_SMOOTH); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_LIGHTING); } void reshape(GLsizei w, GLsizei h) { x = w; y = h; } void lights() { glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT1, GL_POSITION, light_position2); glLightfv(GL_LIGHT2, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT2, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT2, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT2, GL_POSITION, light_position3); glLightfv(GL_LIGHT3, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT3, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT3, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT3, GL_POSITION, light_position4); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glEnable(GL_LIGHT3); } void obstacle1() { glBegin(GL_QUADS); glColor3f(0.0, 0.0, 1.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(-160.0, 20.0, 60.0); glVertex3f(-160.0, 20.0, 20.0); glVertex3f(-200.0, 20.0, 20.0); glVertex3f(-200.0, 20.0, 60.0); glNormal3f(1.0, 0.0, 0.0); glVertex3f(-160.0, 0.0, 20.0); glVertex3f(-160.0, 20.0, 20.0); glVertex3f(-160.0, 20.0, 60.0); glVertex3f(-160.0, 0.0, 60.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-200.0, 0.0, 60.0); glVertex3f(-200.0, 20.0, 60.0); glVertex3f(-200.0, 20.0, 20.0); glVertex3f(-200.0, 0.0, 20.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(-160.0, 0.0, 60.0); glVertex3f(-160.0, 20.0, 60.0); glVertex3f(-200.0, 20.0, 60.0); glVertex3f(-200.0, 0.0, 60.0); glNormal3f(0.0, 0.0, -1.0); glVertex3f(-200.0, 0.0, 20.0); glVertex3f(-200.0, 20.0, 20.0); glVertex3f(-160.0, 20.0, 20.0); glVertex3f(-160.0, 0.0, 20.0); glColor3f(1.0, 1.0, 1.0); glEnd(); } void obstacle2() { glBegin(GL_QUADS); glColor3f(1.0, 1.0, 0.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(200.0, 20.0, 60.0); glVertex3f(200.0, 20.0, 20.0); glVertex3f(160.0, 20.0, 20.0); glVertex3f(160.0, 20.0, 60.0); glNormal3f(1.0, 0.0, 0.0); glVertex3f(200.0, 0.0, 20.0); glVertex3f(200.0, 20.0, 20.0); glVertex3f(200.0, 20.0, 60.0); glVertex3f(200.0, 0.0, 60.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(160.0, 0.0, 60.0); glVertex3f(160.0, 20.0, 60.0); glVertex3f(160.0, 20.0, 20.0); glVertex3f(160.0, 0.0, 20.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(200.0, 0.0, 60.0); glVertex3f(200.0, 20.0, 60.0); glVertex3f(160.0, 20.0, 60.0); glVertex3f(160.0, 0.0, 60.0); glNormal3f(0.0, 0.0, -1.0); glVertex3f(160.0, 0.0, 20.0); glVertex3f(160.0, 20.0, 20.0); glVertex3f(200.0, 20.0, 20.0); glVertex3f(200.0, 0.0, 20.0); glColor3f(1.0, 1.0, 1.0); glEnd(); } void moving_obstacle() { GLUquadricObj* lefthydraulic = gluNewQuadric(); GLUquadricObj* righthydraulic = gluNewQuadric(); glBegin(GL_QUADS); if (togglewall == 1) { wally = wally + .5; } else if (togglewall == -1) { wally = wally - .5; } if (wally == 30) { togglewall = -1; } else if (wally == 10) { togglewall = 1; } glColor3f(1.0, 0.0, 0.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(10.0, (wally + 20), -105.0); glVertex3f(10.0, (wally + 20), -210.0); glVertex3f(-10.0, (wally + 20), -210.0); glVertex3f(-10.0, (wally + 20), -105.0); glNormal3f(0.0, -1.0, 0.0); glVertex3f(10.0, wally, -210.0); glVertex3f(10.0, wally, -105.0); glVertex3f(-10.0, wally, -105.0); glVertex3f(-10.0, wally, -210.0); glNormal3f(1.0, 0.0, 0.0); glVertex3f(10.0, wally, -210.0); glVertex3f(10.0, (wally + 20), -210.0); glVertex3f(10.0, (wally + 20), -105.0); glVertex3f(10.0, wally, -105.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-10.0, wally, -105.0); glVertex3f(-10.0, (wally + 20), -105.0); glVertex3f(-10.0, (wally + 20), -210.0); glVertex3f(-10.0, wally, -210.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(10.0, wally, -105.0); glVertex3f(10.0, (wally + 20), -105.0); glVertex3f(-10.0, (wally + 20), -105.0); glVertex3f(-10.0, wally, -105.0); glNormal3f(0.0, 0.0, -1.0); glVertex3f(-10.0, wally, -210.0); glVertex3f(-10.0, (wally + 20), -210.0); glVertex3f(-10.0, (wally + 20), -210.0); glVertex3f(-10.0, wally, -210.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(10.0, (wally + 25), -85.0); glVertex3f(10.0, (wally + 25), -230.0); glVertex3f(-10.0, (wally + 25), -230.0); glVertex3f(-10.0, (wally + 25), -85.0); glNormal3f(0.0, -1.0, 0.0); glVertex3f(10.0, (wally + 20), -230.0); glVertex3f(10.0, (wally + 20), -85.0); glVertex3f(-10.0, (wally + 20), -85.0); glVertex3f(-10.0, (wally + 20), -230.0); glNormal3f(1.0, 0.0, 0.0); glVertex3f(10.0, (wally + 20), -230.0); glVertex3f(10.0, (wally + 25), -230.0); glVertex3f(10.0, (wally + 25), -85.0); glVertex3f(10.0, (wally + 20), -85.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-10.0, (wally + 20), -85.0); glVertex3f(-10.0, (wally + 25), -85.0); glVertex3f(-10.0, (wally + 25), -230.0); glVertex3f(-10.0, (wally + 20), -230.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(10.0, (wally + 20), -85.0); glVertex3f(10.0, (wally + 25), -85.0); glVertex3f(-10.0, (wally + 25), -85.0); glVertex3f(-10.0, (wally + 20), -85.0); glNormal3f(0.0, 0.0, -1.0); glVertex3f(-10.0, (wally + 20), -230.0); glVertex3f(-10.0, (wally + 25), -230.0); glVertex3f(-10.0, (wally + 25), -230.0); glVertex3f(-10.0, (wally + 20), -230.0); glEnd(); /* Add hydralic lifts on the side so it doesn't look like the wall is floating */ glColor3f(0.0, 0.0, 0.0); glPushMatrix(); glTranslatef(0.0, (wally + 25.0), -220.0); glRotatef(90, 1.0, 0.0, 0.0); gluCylinder(lefthydraulic, 5.0, 5.0, 30, 40, 60); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, 26.0, -220.0); glColor3f(1.0, 0.0, 0.0); glutSolidSphere(8.0, 10, 20); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, (wally + 25.0), -95.0); glRotatef(90, 1.0, 0.0, 0.0); glColor3f(0.0, 0.0, 0.0); gluCylinder(righthydraulic, 5.0, 5.0, 30, 40, 60); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, 26.0, -95.0); glColor3f(1.0, 0.0, 0.0); glutSolidSphere(8.0, 10, 20); glPopMatrix(); glColor3f(1.0, 1.0, 1.0); } void greens() { GLUquadricObj* golfhole = gluNewQuadric(); glBegin(GL_QUADS); /* Basic outline of the green */ glNormal3f(0.0, 1.0, 0.0); glColor3f(0.0, 0.4, 0.0); /* Starting plane */ glVertex3f(-120.0, 0.0, 210.0); glVertex3f(-120.0, 0.0, 0.0); glVertex3f(-240.0, 0.0, 0.0); glVertex3f(-240.0, 0.0, 210.0); /* Ending plane */ glVertex3f(240.0, 0.0, 210.0); glVertex3f(240.0, 0.0, 0.0); glVertex3f(120.0, 0.0, 0.0); glVertex3f(120.0, 0.0, 210.0); /* Elevated center plane */ glVertex3f(240.0, 10.0, -105.0); glVertex3f(240.0, 10.0, -210.0); glVertex3f(-240.0, 10.0, -210.0); glVertex3f(-240.0, 10.0, -105.0); /* Upward Ramp */ glNormal3f(0.0, 0.9954954726, 0.0948090926); glVertex3f(-120.0, 0.0, 0.0); glVertex3f(-120.0, 10.0, -105.0); glVertex3f(-240.0, 10.0, -105.0); glVertex3f(-240.0, 0.0, 0.0); /* Downward Ramp */ glVertex3f(240.0, 0.0, 0.0); glVertex3f(240.0, 10.0, -105.0); glVertex3f(120.0, 10.0, -105.0); glVertex3f(120.0, 0.0, 0.0); /* Front guard rail */ glNormal3f(0.0, 0.0, 1.0); glColor3f(0.6, 0.6, 0.6); glVertex3f(260.0, 0.0, 230.0); glVertex3f(260.0, 10.0, 230.0); glVertex3f(-260.0, 10.0, 230.0); glVertex3f(-260.0, 0.0, 230.0); glNormal3f(0.0, 0.0, -1.0); glVertex3f(-260.0, 0.0, 210.0); glVertex3f(-260.0, 10.0, 210.0); glVertex3f(260.0, 10.0, 210.0); glVertex3f(260.0, 0.0, 210.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(260.0, 10.0, 230.0); glVertex3f(260.0, 10.0, 210.0); glVertex3f(-260.0, 10.0, 210.0); glVertex3f(-260.0, 10.0, 230.0); /* Left guard rail for beginning plane */ glNormal3f(1.0, 0.0, 0.0); glVertex3f(-240.0, 0.0, 0.0); glVertex3f(-240.0, 10.0, 0.0); glVertex3f(-240.0, 10.0, 210.0); glVertex3f(-240.0, 0.0, 210.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-260.0, 0.0, 230.0); glVertex3f(-260.0, 10.0, 230.0); glVertex3f(-260.0, 10.0, 0.0); glVertex3f(-260.0, 0.0, 0.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(-240.0, 10.0, 210.0); glVertex3f(-240.0, 10.0, 0.0); glVertex3f(-260.0, 10.0, 0.0); glVertex3f(-260.0, 10.0, 210.0); /* Median guard rail */ glNormal3f(1.0, 0.0, 0.0); glVertex3f(120.0, 0.0, 0.0); glVertex3f(120.0, 10.0, 0.0); glVertex3f(120.0, 10.0, 210.0); glVertex3f(120.0, 0.0, 210.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-120.0, 0.0, 210.0); glVertex3f(-120.0, 10.0, 210.0); glVertex3f(-120.0, 10.0, 0.0); glVertex3f(-120.0, 0.0, 0.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(120.0, 10.0, 210.0); glVertex3f(120.0, 10.0, 0.0); glVertex3f(-120.0, 10.0, 0.0); glVertex3f(-120.0, 10.0, 210.0); /* Median ramp guard rail */ glNormal3f(0.0, 0.9823385664, 0.1871121079); glVertex3f(120.0, 10.0, 0.0); glVertex3f(120.0, 30.0, -105.0); glVertex3f(-120.0, 30.0, -105.0); glVertex3f(-120.0, 10.0, -0.0); /* Right guard rail for ending plane */ glNormal3f(1.0, 0.0, 0.0); glVertex3f(260.0, 0.0, 0.0); glVertex3f(260.0, 10.0, 0.0); glVertex3f(260.0, 10.0, 230.0); glVertex3f(260.0, 0.0, 230.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(240.0, 0.0, 210.0); glVertex3f(240.0, 10.0, 210.0); glVertex3f(240.0, 10.0, 0.0); glVertex3f(240.0, 0.0, 0.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(260.0, 10.0, 210.0); glVertex3f(260.0, 10.0, 0.0); glVertex3f(240.0, 10.0, 0.0); glVertex3f(240.0, 10.0, 210.0); /* Left guard rail for elevated plane */ glNormal3f(1.0, 0.0, 0.0); glVertex3f(-240.0, 10.0, -230.0); glVertex3f(-240.0, 30.0, -230.0); glVertex3f(-240.0, 30.0, -105.0); glVertex3f(-240.0, 10.0, -105.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-260.0, 0.0, -105.0); glVertex3f(-260.0, 30.0, -105.0); glVertex3f(-260.0, 30.0, -210.0); glVertex3f(-260.0, 0.0, -210.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(-240.0, 30.0, -105.0); glVertex3f(-240.0, 30.0, -210.0); glVertex3f(-260.0, 30.0, -210.0); glVertex3f(-260.0, 30.0, -105.0); /* Right guard rail for elevated plane */ glNormal3f(1.0, 0.0, 0.0); glVertex3f(260.0, 0.0, -230.0); glVertex3f(260.0, 30.0, -230.0); glVertex3f(260.0, 30.0, -105.0); glVertex3f(260.0, 0.0, -105.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(240.0, 10.0, -105.0); glVertex3f(240.0, 30.0, -105.0); glVertex3f(240.0, 30.0, -210.0); glVertex3f(240.0, 10.0, -210.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(260.0, 30.0, -105.0); glVertex3f(260.0, 30.0, -210.0); glVertex3f(240.0, 30.0, -210.0); glVertex3f(240.0, 30.0, -105.0); /* Back guard rail for elevated plane */ glNormal3f(0.0, 1.0, 0.0); glVertex3f(260.0, 30.0, -210.0); glVertex3f(260.0, 30.0, -230.0); glVertex3f(-260.0, 30.0, -230.0); glVertex3f(-260.0, 30.0, -210.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(260.0, 10.0, -210.0); glVertex3f(260.0, 30.0, -210.0); glVertex3f(-260.0, 30.0, -210.0); glVertex3f(-260.0, 10.0, -210.0); glNormal3f(0.0, 0.0, -1.0); glVertex3f(-260.0, 0.0, -230.0); glVertex3f(-260.0, 30.0, -230.0); glVertex3f(260.0, 30.0, -230.0); glVertex3f(260.0, 0.0, -230.0); /* Front guard rail on elevated plane */ glNormal3f(0.0, 1.0, 0.0); glVertex3f(120.0, 30.0, -85.0); glVertex3f(120.0, 30.0, -105.0); glVertex3f(-120.0, 30.0, -105.0); glVertex3f(-120.0, 30.0, -85.0); glNormal3f(0.0, 0.0, -1.0); glVertex3f(-120.0, 10.0, -105.0); glVertex3f(-120.0, 30.0, -105.0); glVertex3f(120.0, 30.0, -105.0); glVertex3f(120.0, 10.0, -105.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(120.0, 0.0, -85.0); glVertex3f(120.0, 30.0, -85.0); glVertex3f(-120.0, 30.0, -85.0); glVertex3f(-120.0, 0.0, -85.0); /* Left ramp left guard rail */ glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-260.0, 0.0, 0.0); glVertex3f(-260.0, 10.0, 0.0); glVertex3f(-260.0, 30.0, -105.0); glVertex3f(-260.0, 0.0, -105.0); glNormal3f(1.0, 0.0, 0.0); glVertex3f(-240.0, 0.0, -105.0); glVertex3f(-240.0, 30.0, -105.0); glVertex3f(-240.0, 10.0, 0.0); glVertex3f(-240.0, 0.0, 0.0); glNormal3f(0.0, 0.9823385664, 0.1871121079); glVertex3f(-240.0, 10.0, 0.0); glVertex3f(-240.0, 30.0, -105.0); glVertex3f(-260.0, 30.0, -105.0); glVertex3f(-260.0, 10.0, 0.0); /* Left ramp right guard rail */ glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-120.0, 0.0, 0.0); glVertex3f(-120.0, 10.0, 0.0); glVertex3f(-120.0, 30.0, -105.0); glVertex3f(-120.0, 0.0, -105.0); /* Right ramp left guard rail */ glNormal3f(1.0, 0.0, 0.0); glVertex3f(120.0, 0.0, -105.0); glVertex3f(120.0, 30.0, -105.0); glVertex3f(120.0, 10.0, 0.0); glVertex3f(120.0, 0.0, 0.0); /* Right ramp right guard rail */ glNormal3f(1.0, 0.0, 0.0); glVertex3f(260.0, 0.0, -105.0); glVertex3f(260.0, 30.0, -105.0); glVertex3f(260.0, 10.0, 0.0); glVertex3f(260.0, 0.0, 0.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(240.0, 0.0, 0.0); glVertex3f(240.0, 10.0, 0.0); glVertex3f(240.0, 30.0, -105.0); glVertex3f(240.0, 0.0, -105.0); glNormal3f(0.0, 0.9823385664, 0.1871121079); glVertex3f(260.0, 10.0, 0.0); glVertex3f(260.0, 30.0, -105.0); glVertex3f(240.0, 30.0, -105.0); glVertex3f(240.0, 10.0, 0.0); /* Foundation for the golf course */ glNormal3f(1.0, 0.0, 0.0); glVertex3f(260.0, -5.0, -230.0); glVertex3f(260.0, 0.0, -230.0); glVertex3f(260.0, 0.0, 230.0); glVertex3f(260.0, -5.0, 230.0); glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-260.0, -5.0, 230.0); glVertex3f(-260.0, 0.0, 230.0); glVertex3f(-260.0, 0.0, -230.0); glVertex3f(-260.0, -5.0, -230.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(260.0, -5.0, 230.0); glVertex3f(260.0, 0.0, 230.0); glVertex3f(-260.0, 0.0, 230.0); glVertex3f(-260.0,-5.0, 230.0); glNormal3f(0.0, 0.0, -1.0); glVertex3f(-260.0, -5.0, -230.0); glVertex3f(-260.0, 0.0, -230.0); glVertex3f(260.0, 0.0, -230.0); glVertex3f(260.0, -5.0, -230.0); glNormal3f(0.0, -1.0, 0.0); glVertex3f(260.0, 0.0, -230.0); glVertex3f(260.0, 0.0, 230.0); glVertex3f(-260.0, 0.0, 230.0); glVertex3f(-260.0, 0.0,-230.0); /* Surround course with green plane so it won't look like course floats in space */ glNormal3f(0.0, 1.0, 0.0); glColor3f(0.0, 0.4, 0.0); glVertex3f(1000.0, 0.0, 1000.0); glVertex3f(1000.0, 0.0, -1000.0); glVertex3f(-1000.0, 0.0, -1000.0); glVertex3f(-1000.0, 0.0, 1000.0); glEnd(); /* Hole */ glPushMatrix(); glTranslatef(180, 2.0, 180); glRotatef(-90, 1.0, 0.0, 0.0); glColor3f(0.0, 0.0, 0.0); gluDisk(golfhole, 0.0, 10.0, 10, 10); glColor3f(1.0, 1.0, 1.0); glPopMatrix(); } void club() { GLUquadricObj* topdisc = gluNewQuadric(); GLUquadricObj* handle = gluNewQuadric(); GLUquadricObj* metal = gluNewQuadric(); GLUquadricObj* bottomdisc = gluNewQuadric(); GLUquadricObj* clubhead = gluNewQuadric(); glPushMatrix(); glTranslatef(0.0, 60.0, 0.0); glRotatef(-90, 1.0, 0.0, 0.0); glColor3f(0.0, 0.0, 0.0); gluDisk(topdisc, 0.0, 2.0, 10, 10); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, 40.0, 0.0); glRotatef(-90, 1.0, 0.0, 0.0); gluCylinder(handle, 2.0, 2.0, 20, 20, 40); glPopMatrix(); glPushMatrix(); glRotatef(-90, 1.0, 0.0, 0.0); glColor3f(0.5, 0.5, 0.5); gluCylinder(metal, 2.0, 2.0, 40, 20, 60); gluDisk(bottomdisc, 0.0, 2.0, 10, 10); glPopMatrix(); glPushMatrix(); glTranslatef(4.5, 7.0, 2.0); glRotatef(90, 0.0, 1.0, 0.0); gluSphere(clubhe , .0, 3, 8); glPopMatrix(); glColor3f(1.0, 1.0, 1.0); } void draw_club() { glPushMatrix(); glTranslatef(-10.0, 0.0, -5.0); glRotatef(-80, 0.0, 1.0, 0.0); club(); glPopMatrix(); } void ballarrow() { glPushMatrix(); glTranslatef(ballx, bally, ballz); glRotatef(angle_of_shot, 0.0, 1.0, 0.0); glBegin(GL_QUADS); glColor3f(1.0, 0.0, 0.0); glNormal3f(0.0, 1.0, 0.0); glVertex3f(10.0, 5.0, 5.0); glVertex3f(15.0, 5.0, 5.0); glVertex3f(15.0, 5.0, -5.0); glVertex3f(10.0, 5.0, -5.0); glEnd(); glBegin(GL_TRIANGLES); glNormal3f(0.0, 1.0, 0.0); glVertex3f(15.0, 5.0, 8.0); glVertex3f(25.0, 5.0, 0.0); glVertex3f(15.0, 5.0, -8.0); glColor3f(1.0, 1.0, 1.0); glEnd(); draw_club(); glPopMatrix(); } float nextballx() { float tempx = ((acc - (acc * friction)) * cos(rad)); if (tempx < 0) { tempx = tempx * -1; } if (togglex == 1) { tempx = ballx + tempx; } else if (togglex == -1) { tempx = ballx - tempx; } return tempx; } float nextballz() { float tempz = ((acc - (acc * friction)) * sin(rad)); if (tempz < 0) { tempz = tempz * -1; } if (togglez == 1) { tempz = ballz + tempz; } else if (togglez == -1) { tempz = ballz - tempz; } return tempz; } void ball() { if (toggleacc == 0) { /* Determine direction based on angle */ if ((angle_of_shot > 90) && (angle_of_shot < 270)) { togglex = -1; } else if ((angle_of_shot < 90) || ((angle_of_shot > 270) && (angle_of_shot < 360))) { togglex = 1; } else { togglex = 0; } if ((angle_of_shot > 0) && (angle_of_shot < 180)) { togglez = -1; } else if ((angle_of_shot > 180) && (angle_of_shot < 360)) { togglez = 1; } else { togglez = 0; } rad = (angle_of_shot * PI) / 180; if (hole != 1) { clubcamx = ballx - (30 * cos(rad)); clubcamy = bally + 30; clubcamz = ballz + (30 * sin(rad)); ballcamx = ballx - (30 * cos(rad)); ballcamy = bally + 30; ballcamz = ballz + (30 * sin(rad)); } if (hole == 1) { ballcamx = clubcamx; ballcamy = clubcamy; ballcamz = clubcamz; } /* if the ball is under the wall, set the acceleration to 0 and stop the wall from moving*/ if (((ballz <= -110) && (ballz >= -205)) && ((ballx >= -15) && (ballx <= 15)) && (wally == 20) && (togglewall == -1)) { acc = 0; togglewall = 0; } } if (toggleacc == 1) { /* Add one to the total number of strokes */ /* Physics: Collision detection & Conservation of Momentum */ /* X Coordinate collision */ if ((ballx >= -235) && (ballx < -125) && (togglex == -1) && (nextballx() < -235)) { ballx = -235; togglex = 1; } else if ((ballx <= 235) && (ballx > 125) && (togglex == 1) && (nextballx() > 235)) { ballx = 235; togglex = -1; } else if (ballx > 235) { ballx = 235; togglex = -1; } else if (ballx < -235) { ballx = -235; togglex = 1; } if ((ballx <= -120) && (ballx > -235) && (togglex == 1) && (nextballx() >= -125) && (ballz > -110)) { ballx = -125; togglex = -1; } else if ((ballx >= 120) && (ballx < 235) && (togglex == -1) && (nextballx() <= 125) && (ballz > -110)) { ballx = 125; togglex = 1; } else if ((ballx > -120) && (ballx < 125) && (togglex == 1) && (ballz > -110)) { ballx = -125; togglex = -1; } else if ((ballx < 120) && (ballx > 125) && (togglex == -1) && (ballz > -110)) { ballx = 125; togglex = 1; } /* Z Coordinate Collision */ if ((ballz >= -210) && (ballz < -125) && (togglez == -1) && (nextballz() <= -205)) { ballz = -205; togglez = 1; } else if ((ballz <= 210) && (ballz > 125) && (togglez == 1) && (nextballz() >= 205)) { ballz = 205; togglez = -1; } else if (ballz > 205) { ballz = 205; togglez = -1; } else if (ballz < -205) { ballz = -205; togglez = 1; } if ((ballz <= -110) && (ballz > -205) && (togglez == 1) && (nextballz() >= -110) && ((ballx > -124) && (ballx < 124))) { ballz = -110; togglez = -1; } else if ((ballz > -110) && (togglez == 1) && (nextballz() >= -110) && ((ballx > -124) && (ballx < 124))) { ballz = -110; togglez = -1; } /* Y Coordinate elevation and de-elevation */ if (ballz <= -105) { bally = 15; } else if (ballz >= 0) { bally = 5; } else if ((ballz < 0) && (ballz> -105)) { bally = (-1 * ballz * sin(.0949517063)) + 5; } /* Obstacle Z Coordinate Collision */ if (((ballx >= -205) && (ballx <= -155)) && ((ballz < 205) && (ballz >= 65)) && (nextballz() < 65) && (togglez == -1)) { ballz = 65; togglez = 1; } else if (((ballx >= 155) && (ballx <= 205)) && ((ballz < 205) && (ballz >= 65)) && (nextballz() < 65) && (togglez == -1)) { ballz = 65; togglez = 1; } else if (((ballx >= -205) && (ballx <= -155)) && ((ballz > -205) && (ballz <= 15)) && (nextballz() > 15) && (togglez == 1)) { ballz = 15; togglez = -1; } else if (((ballx >= 155) && (ballx <= 205)) && ((ballz > -205) && (ballz <= 15)) && (nextballz() > 15) && (togglez == 1)) { ballz = 15; togglez = -1; } /* Obstacle X Coordinate Collision */ if (((ballz >= 15) && (ballz <= 65)) && ((ballx > -235) && (ballx <= -205)) && (nextballx() > -205) && (togglex == 1)) { ballx = -205; togglex = -1; } else if (((ballz >= 15) && (ballz <= 65)) && ((ballx > 115) && (ballx <= 155)) && (nextballx() > 155) && (togglex == 1)) { ballx = 155; togglex = -1; } else if (((ballz >= 15) && (ballz <= 65)) && ((ballx < -115) && (ballx >= -155)) && (nextballx() < -155) && (togglex == -1)) { ballx = -155; togglex = 1; } else if (((ballz >= 15) && (ballz <= 65)) && ((ballx < 235) && (ballx >= 205)) && (nextballx() < 205) && (togglex == -1)) { ballx = 205; togglex = 1; } else if (((ballz >= 15) && (ballz <= 65)) && ((ballx > -165) && (ballx <= -155)) && (togglex == -1)) { ballx = -155; togglex = 1; } else if (((ballz >= 15) && (ballz <= 65)) && ((ballx > 195) && (ballx <= 205)) && (togglex == -1)) { ballx = 205; togglex = 1; } /* Moving Obstacle Collision */ if ((ballx >= -235) && (ballx < -5) && (togglex == 1) && (nextballx() > -15) && ((ballz <= -110) && (ballz >= -205)) && (wally < 20)) { ballx = -15; togglex = -1; } else if ((ballx <= 235) && (ballx > 5) && (togglex == -1) && (nextballx() < 15) && ((ballz <= -110) && (ballz >= -205)) && (wally < 20)) { ballx = 15; togglex = 1; } else if (((ballz <= -110) && (ballz >= -205)) && ((ballx >= -15) && (ballx <= 15)) && (wally == 20) && (togglewall == -1) && (wally == 20)) { acc = 0; togglewall = 0; } /* check if the ball is stuck under the wall */ if ((togglewall == 0) && (acc < 20)) { acc = 0; } else if ((togglewall == 0) && (acc >= 20)) { togglewall = -1; } /* ball falls in the hole if the acceleration is less than or equal to 12*/ if (((ballx >= 172.5) && (ballx <= 187.5)) && ((ballz >= 172.5) && (ballz <= 187.5)) && (acc <= 12)) { bally = bally - 10; acc = 0; ballx = 182; ballz = 180; hole = 1; } /* Accelerating the ball in the proper direction */ if (togglex == 1) { ballx = ballx + accx; } else if (togglex == -1) { ballx = ballx - accx; } if (togglez == 1) { ballz = ballz + accz; } else if (togglez == -1) { ballz = ballz - accz; } ballcamx = ballx - (20 * cos(rad)); ballcamy = bally + 20; ballcamz = ballz + (20 * sin(rad)); /* Friction */ if (((ballz <= 0) && (ballz >= -105)) && (togglez == -1)) { friction = .1; hill = 1; } else if (((ballz <= 0) && (ballz >= -105)) && (togglez == 1)) { friction = -.03; } else { friction = .04; hill = 0; } if ((hill == 1) && (acc == 0)) { togglez = 1; acc = 1; } /* Accelerate the ball in the various directions */ if (acc > 0) { acc = acc - (acc * friction); powermeter = (acc * 2.0) + 20.0; } if (acc < 0.15) { acc = 0; } rad = (angle_of_shot * PI) / 180; accx = (acc * cos(rad)); if (accx < 0) { accx = (accx * -1); } accz = (acc * sin(rad)); if (accz < 0) { accz = (accz * -1); } } if ((toggleacc == 1) && (acc == 0) && (hill != 1)) { toggleacc = 0; powermeter = 20.0; acc = 0; strokes = strokes + 1; if ((ballx < 120) && (ballz < -105)) { angle_of_shot = 0; } else if (ballx > 120) { angle_of_shot = 270; } else { angle_of_shot = 90; } togglez = 0; togglex = 0; } glPushMatrix(); glTranslatef(ballx, bally, ballz); if (toggleacc == 1) { glRotatef(ballrot, sin(rad), 0.0, cos(rad)); if (togglez == -1) { ballrot = ballrot + 1 +(int)acc; } else { ballrot = ballrot + 1 +(int)acc; } } glutSolidSphere(5, 20, 20); glPopMatrix(); } void flag() { GLUquadricObj* flagtop = gluNewQuadric(); GLUquadricObj* flagpole = gluNewQuadric(); glPushMatrix(); glTranslatef(0.0, 60.0, 0.0); glRotatef(-90, 1.0, 0.0, 0.0); glColor3f(0.4, 0.4, 0.0); gluDisk(flagtop, 0.0, 3.0, 10, 10); glPopMatrix(); glPushMatrix(); glRotatef(-90, 1.0, 0.0, 0.0); gluCylinder(flagpole, 3.0, 3.0, 60, 20, 100); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, 30.0, 0.0); glBegin(GL_TRIANGLES); glColor3f(1.0, 1.0, 1.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(0.0, 15.0, 0.0); glVertex3f(0.0, 30.0, 0.0); glVertex3f(20.0, 25.0, 0.0); glColor3f(1.0, 1.0, 1.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(0.0, 15.0, 0.0); glVertex3f(20.0, 25.0, 0.0); glVertex3f(0.0, 30.0, 0.0); glEnd(); glPopMatrix(); } void draw_flag() { glPushMatrix(); glTranslatef(180.0, 0.0, 180.0); flag(); glPopMatrix(); } void bitmap_output(int x, int y, char *string, void *font) { int len, i; glRasterPos2f(x,y); len = (int) strlen(string); for (i=0; i 0) && (toggleacc == 0)) { acc = acc - 1; powermeter = powermeter - 2.0; } break; case GLUT_KEY_LEFT: if (toggleacc == 0) { angle_of_shot = angle_of_shot + 5; if (angle_of_shot >= 360) { angle_of_shot = angle_of_shot - 360; } else if (angle_of_shot < 0) { angle_of_shot = angle_of_shot + 360; } } break; case GLUT_KEY_RIGHT: if (toggleacc == 0) { angle_of_shot = angle_of_shot - 5; if (angle_of_shot >= 360) { angle_of_shot = angle_of_shot - 360; } else if (angle_of_shot < 0) { angle_of_shot = angle_of_shot + 360; } } break; } } void Timer( int value) { display(); glutTimerFunc(60, Timer, 1); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowPosition(150, 50); glutInitWindowSize(500,500); glutCreateWindow("Dack Golf"); glEnable(GL_DEPTH_TEST); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(arrows); glutTimerFunc(80, Timer, 1); glutMainLoop(); }