00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "Objects.h"
00010 #include <stdlib.h>
00011 #include <GL/glut.h>
00012 #include <stdio.h>
00013 #include <math.h>
00014 #include <time.h>
00015 #include <ctype.h>
00016
00017
00018 #include "MenuItems.h"
00019 #include "Misc.h"
00020 #include "Controls.h"
00021 #include "Collision.h"
00022 #include "IMGLoader.h"
00023
00024 #define WINDOW_NAME "Ping Pong"
00025
00026
00027 void display(void);
00028 void myReshape(int w, int h);
00029 void camera();
00030 void mouseFunc(int button, int state, int x, int y);
00031 void idleFunc();
00032
00033
00034
00035 void initializeVariables()
00036 {
00037 gravity = 1;
00038
00039 gameIsPaused = 1;
00040 gravityIsOn = 1;
00041
00042
00043 img = NULL;
00044 exitMenu = 0;
00045
00046 friction = 0.09;
00047
00048 racket.location[0] = 12.5;
00049 racket.location[1] = 10;
00050 racket.location[2] = 35;
00051 racket.radius = 20;
00052 racket.mass = 0.03;
00053
00054 racket.velocity[0] = 0;
00055 racket.velocity[1] = 0;
00056 racket.velocity[2] = 0;
00057
00058
00059
00060 resetBall();
00061
00062
00063
00064
00065
00066
00067
00068
00069 ball.radius = 0.5;
00070 ball.mass = 0.01;
00071
00072
00073
00074 net.center[0] = 13;
00075 net.center[1] = 2.5;
00076 net.center[2] = 2;
00077
00078 net.XYZ[0] = 26.0;
00079 net.XYZ[1] = 5.0;
00080 net.XYZ[2] = 0.4;
00081
00082 net.coef = -0.3;
00083
00084
00085
00086
00087
00088 tableUp.center[0] = 12.5;
00089 tableUp.center[1] = 13;
00090 tableUp.center[2] = -0.1;
00091
00092 tableUp.XYZ[0] = 25.0;
00093 tableUp.XYZ[1] = 26.0;
00094 tableUp.XYZ[2] = 0.4;
00095
00096 tableUp.coef = -0.8;
00097
00098
00099
00100
00101
00102 tableDown.center[0] = 12.5;
00103 tableDown.center[1] = -0.1;
00104 tableDown.center[2] = 13;
00105
00106 tableDown.XYZ[0] = 70;
00107 tableDown.XYZ[1] = 0.4;
00108 tableDown.XYZ[2] = 70;
00109
00110
00111
00112
00113
00114 tableDown.coef = -0.8;
00115
00116
00117
00118 }
00119
00120 int main()
00121 {
00122
00123 initializeVariables();
00124 mainMenu();
00125
00126 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00127 glutInitWindowSize(640, 480);
00128 glutCreateWindow( WINDOW_NAME );
00129 glutReshapeFunc(myReshape);
00130
00131 glutDisplayFunc(display);
00132
00133 glClearColor(RGB(0x4A), RGB(0x77), RGB(0xD5), 0);
00134
00135 glutSpecialFunc(kbSpecialFunc);
00136 glutKeyboardFunc(kbDown);
00137 glutKeyboardUpFunc(kbUp);
00138 glutIdleFunc(idleFunc);
00139 glutMouseFunc(mouseFunc);
00140
00141 pauseGame();
00142
00143 glEnable(GL_DEPTH_TEST);
00144
00145 glutMainLoop();
00146
00147 return 0;
00148 }
00149
00150 void camera()
00151 {
00152
00153 gluLookAt( 12.5, 10.0, 50.0,
00154 12.5, 0.0, 0.0,
00155 0.0, 1.0, 0.0 );
00156
00157
00158
00159
00160
00161
00162
00163 glutPostRedisplay();
00164 }
00165
00166 void display(void)
00167 {
00168 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00169 glLoadIdentity();
00170 glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
00171
00172 checkKeyPressed();
00173
00174 if(gameIsPaused)
00175 {
00176 printMenu();
00177 glFlush();
00178 glutSwapBuffers();
00179 return;
00180 }
00181
00182 camera();
00183 drawBall();
00184 drawShadows();
00185 drawTable();
00186
00187 drawPlane();
00188
00189 glEnable(GL_LIGHTING);
00190 drawRacket(racket.location[0], racket.location[1], racket.location[2]);
00191
00192 glDisable(GL_LIGHTING);
00193
00194
00195
00196
00197 glFlush();
00198
00199 glutSwapBuffers();
00200
00201 return;
00202 }
00203
00204
00205 void myReshape(int width, int height)
00206 {
00207
00208 if (height==0)
00209 {
00210 height=1;
00211 }
00212
00213 glViewport(0, 0, width, height);
00214 glMatrixMode(GL_PROJECTION);
00215 glLoadIdentity();
00216
00217
00218
00219 if(gameIsPaused)
00220 {
00221 gluOrtho2D(0.0,768.0, 0.0,480.0);
00222 glutReshapeWindow(640, 480);
00223 }
00224 else
00225 {
00226 gluPerspective(45.0f,((GLfloat)width)/((GLfloat)height+40),0.01f,100.0f);
00227 glutReshapeWindow(768,480);
00228 }
00229 glMatrixMode(GL_MODELVIEW);
00230 glLoadIdentity();
00231 }
00232
00233 float forces(int i, int j)
00234 {
00235 if(!1)
00236 return(0.0);
00237 else if (j==1)
00238 return(-gravity);
00239 else
00240 return(0.0);
00241 }
00242
00243 float forces222(int i, int j, float velocity)
00244 {
00245 float frict = 1;
00246 if(velocity > 0)
00247 {
00248 frict = -friction;
00249 }
00250 else if(velocity < 0)
00251 {
00252 frict = friction;
00253 }
00254
00255
00256 if(j==1)
00257 {
00258 return((-(gravity-frict)));
00259 }
00260
00261 return(frict);
00262 }
00263
00264
00265
00266
00267 void idleFunc()
00268 {
00269 static float lastTime = 0.0f, presentTime = 0.0f;
00270
00271 int i = 0, j;
00272 float dt;
00273
00274 presentTime = glutGet(GLUT_ELAPSED_TIME);
00275
00276 if(gameIsPaused)
00277 {
00278 glutPostRedisplay();
00279 lastTime = presentTime;
00280 return;
00281 }
00282
00283 dt = 0.001*(presentTime - lastTime);
00284
00285
00286
00287
00288 for(j=0; j<3;j++)
00289 {
00290 ball.oldLocation[j] = ball.location[j];
00291
00292 ball.location[j]+=dt*ball.velocity[j];
00293 ball.velocity[j]+=dt*forces222(i, j, ball.velocity[j])/ball.mass;
00294
00295
00296
00297
00298
00299 racket.location[j]+=dt*racket.velocity[j];
00300 racket.velocity[j]+=dt*forces(i, j)/racket.mass;
00301
00302
00303
00304
00305
00306 }
00307
00308 racket.velocity[0]*=0.9;
00309 racket.velocity[2]*=0.9;
00310
00311
00312
00313 collisionDetection();
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 racketCollision();
00330
00331
00332 if(ball.velocity[2] > 0)
00333 {
00334 if(ball.location[2] > racket.location[2]-10 && ball.location[2] < racket.location[2]+10)
00335 {
00336 if(racket.location[1] < ball.location[1])
00337 {
00338
00339 racket.location[1] = ball.location[1];
00340 }
00341
00342
00343 }
00344 }
00345
00346
00347 if(ball.location[1] >= 5)
00348 {
00349
00350
00351
00352
00353
00354 }
00355
00356
00357
00358
00359 if(ball.location[2] <= -4.1 || ball.location[2] >= 50 || ball.location[1] <= -10.1)
00360 {
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 resetBall();
00373 }
00374
00375 lastTime = presentTime;
00376 glutPostRedisplay();
00377 }