-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·436 lines (350 loc) · 10.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* Hostile Takeover:
* CS248 Final Project, Fall 2004
*
* Written by Jonathan Reeves
* jrreeves@stanford.edu
*
* A simple 3D video game demonstrating the use of OpenGL for graphics
* rendering with a number of advanced techniques and optimizations to make
* it interesting. See the README file which came with this package for more
* details. It also includes a list of sources which were consulted while
* building this software package.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <time.h>
#include "main.h"
/* some paramters you can mess with... */
int screenWidth = SCREEN_WIDTH;
int screenHeight = SCREEN_HEIGHT;
float mouseSens = MOUSE_SENS;
/* the camera */
camera_t cam;
/* physical dimensions of the screen */
GLint middleX;
GLint middleY;
/* used to test the position of the camera relative to terrain */
GLfloat curX, curY, curZ;
GLfloat diff[4] = {0.2, 0.2, 0.2, 1.0};
projectile_t *proj = NULL;
/* initialization of everything */
void init(void){
/* starting position and view */
float sPosX, sPosY, sPosZ; /* player starting position */
float sViewX, sViewY, sViewZ; /* player starting forward vector */
srand(time(NULL));
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
/* setup light properties */
SetupLights();
/* setup the fonts */
FontBuildAll();
FogPlaneSetup(40.0, 50.0);
printf("loading terrain...\n");
ground = TerrainLoadMap("maps/size256edge.ppm", "textures/lawn.ppm");
printf("loading water...\n");
water = TerrainMakeWater(ground->sizeX, ground->sizeZ, waterLevel,
"textures/water.ppm");
sizeX = (GLfloat)ground->sizeX;
sizeZ = (GLfloat)ground->sizeZ;
/* get ready to set up the first player (you) */
sPosX = (sizeX-1)/2.0;
sPosZ = (sizeZ-1)/2.0;
sViewX = 1.0;
sViewZ = sizeZ;
sPosY = TerrainGetHeight(ground, sPosX, sPosZ) + MOD_HEIGHT2;
sViewY = TerrainGetHeight(ground, sViewX, sViewZ);
printf("loading player 1...\n");
sod = PlayerNew();
PlayerLoad(sod, "models/sod/sod.md2", "models/sod/sod.ppm");
PlayerSetPosition(sod, sPosX, sPosY, sPosZ);
PlayerSetUpVector(sod, 0.0, 1.0, 0.0);
PlayerSetForwardByAngle(sod, 0.0, 30.0);
PlayerSetAnimation(sod, PLAYER_ANIM_IDLE0);
printf("loading %d ogros into a flock...\n", 6);
flocks[0] = AIFlockNew();
AIFlockLoadAll(flocks[0], "models/ogro/ogro.md2",
"models/ogro/ogro.ppm", 6);
printf("loading %d bobafetts into a flock...\n", 6);
flocks[1] = AIFlockNew();
AIFlockLoadAll(flocks[1], "models/boba/bobafett.md2",
"models/boba/bobafett.ppm", 6);
/* set up the camera */
CameraInit(&cam);
CameraSetUpVector(&cam, 0.0, 1.0, 0.0);
CameraFollowPlayer(&cam, sod, camDistance, camHeight);
printf("setting up skybox...\n");
/* arguments are width, length, height, centerX, centerZ */
sky = SkyboxLoad(400, 400, 100, (sizeX-1)/2.0, (sizeZ-1)/2.0);
plist = PListNew();
}
/* what happens when you resize the window */
void reshape(int w, int h){
middleX = w >> 1; /* fast divide by 2 */
middleY = h >> 1;
glutWarpPointer(middleX, middleY);
screenWidth = w;
screenHeight = h;
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* load a fake projection matrix for the frustum culling */
gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.5, 80.0);
FrustumUpdateProjection(&frust);
/* load the real projection matrix */
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.5, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* these four globals are used only in display() to calculate and show fps */
float lastTime = 0.0;
float curTime = 0.0;
float diffTime = 0.0;
float fps, dfps;
int count = 0;
/* the display function... */
void display(void){
curTime = (float)clock()/CLOCKS_PER_SEC;
diffTime = curTime - lastTime;
fps = 1.0/diffTime;
lastTime = curTime;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* needs to be done for mouse look to work correctly */
glutWarpPointer(middleX, middleY);
/* up date the protagonist's position and state */
PlayerUpdate(sod);
/* set the camera on the protagonist */
CameraFollowPlayer(&cam, sod, camDistance, camHeight);
/* protagonist is set, now process the antagonists */
if(flocks[0]->nBots != 0){
AIFlockUpdate(flocks[0], sod);
}
if(flocks[1]->nBots != 0){
AIFlockUpdate(flocks[1], sod);
}
/* run through the projectile list and update */
PListUpdate(plist, sod, flocks, 2);
/* preprocessing is done, now update the screen */
glPushMatrix();
CameraUpdateView(&cam);
FrustumUpdateView(&frust);
/* draw the sky box now that blending is disabled */
glColor3f(1.0, 1.0, 1.0);
SkyboxDraw(sky);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, position0);
glEnable(GL_LIGHT0);
/* draw the protagonist */
glPushMatrix();
glMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
#ifdef NOFRUST
PlayerAnimate(sod, 0.12);
#else
PlayerAnimateCulled(sod, &frust, 0.2);
#endif
glPopMatrix();
glEnable(GL_FOG);
/* draw the antagonist */
#ifdef NOFRUST
AIFlockDraw(flocks[0]);
AIFlockDraw(flocks[1]);
#else
AIFlockDrawCulled(flocks[0]);
AIFlockDrawCulled(flocks[1]);
#endif
glDisable(GL_LIGHT0);
glDisable(GL_BLEND);
/* draw the terrain */
glDisable(GL_LIGHTING);
#ifdef NOFRUST
TerrainDrawShaded(ground);
#else
TerrainDrawShadedCulled(ground, &frust);
#endif
FogPlaneDraw(sod->yaw, cam.position.x, waterLevel-5.0, cam.position.z);
/* draw the projectiles */
PListDraw(plist);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
/* draw the water */
glColor4f(0.5, 0.5, 1.0, 0.6);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
#ifdef NOFRUST
TerrainDraw(water);
#else
TerrainDrawCulled(water, &frust);
#endif
glPopMatrix();
glDisable(GL_FOG);
glDisable(GL_BLEND);
glEnable(GL_CULL_FACE);
/* display frames per second, and enemies */
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glPushMatrix();
if(!(count % 20)){
dfps = fps;
count = 0;
} count++;
glColor4f(0.0, 0.5, 1.0, 1.0);
glTranslatef(0.0, 0.0, -1.0);
glRasterPos2f(-0.45, -0.3);
glPrint(0, "total enemies: %d", flocks[0]->nBots + flocks[1]->nBots);
glRasterPos2f(-0.45, -0.34);
glPrint(0, "fps: %f", dfps);
glPopMatrix();
/* display your health */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glBegin(GL_QUADS);
glColor4f(1.0, 1.0, 0.0, 0.6);
glVertex2f(55.0, 55.0);
glVertex2f(55.0 + ((sod->health)*IMAX_HEALTH*340.0), 55.0);
glVertex2f(55.0 + ((sod->health)*IMAX_HEALTH*340.0), 35.0);
glVertex2f(55.0, 35.0);
glColor4f(1.0, 1.0, 1.0, 0.3);
glVertex2f(50.0, 60.0);
glVertex2f(400.0, 60.0);
glVertex2f(400.0, 30.0);
glVertex2f(50.0, 30.0);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
/* all done */
glutSwapBuffers();
}
/* yay mouse look... */
void mouseMotionFunc(int x, int y){
vector_t xaxis = {1.0, 0.0, 0.0};
sod->yaw += ((float)(middleX - x)) * mouseSens;
sod->pitch -= ((float)(middleY - y)) * mouseSens;
/* put a maximum span on the pitch so we can't flip over */
if(sod->pitch >= 70.0)
sod->pitch = 70.0;
else if(sod->pitch <= -70.0)
sod->pitch = -70.0;
/* put a maximum span on the yaw so we don't overflow the angles */
if(sod->yaw >= 360.0)
sod->yaw = sod->yaw - 360.0;
else if(sod->yaw <= 0.0)
sod->yaw = sod->yaw + 360.0;
/* use fast rotations for canonical basis rotations */
VectorRotateZ(&xaxis, sod->pitch);
VectorRotateY(&xaxis, sod->yaw);
PlayerSetForward(sod, xaxis.x, xaxis.y, xaxis.z);
}
/* yay mouse click... */
void mouseFunc(int button, int state, int x, int y){
if(sod->state != PLAYER_DEAD){
if(button == GLUT_LEFT_BUTTON){
switch(state){
case GLUT_DOWN:
proj = ProjectileNew(&sod->forward, &sod->position, PROJ_SPEED, 0);
PListInsertAfter(plist, proj);
break;
}
}
}
}
/* yay keyboard.. */
void keyboardDownFunc(unsigned char key, int x, int y){
switch(key){
case 27:
/* all done, cleanup */
Cleanup();
break;
case 'w':
sod->fFlag = 1;
break;
case 's':
sod->bFlag = 1;
break;
case 'a':
sod->lFlag = 1;
break;
case 'd':
sod->rFlag = 1;
break;
case 'q':
sod->jFlag = 1;
break;
default:
break;
}
if(sod->rFlag || sod->lFlag || sod->fFlag || sod->bFlag)
PlayerProcessEvent(sod, PLAYER_RUNNING);
else if(sod->jFlag)
PlayerProcessEvent(sod, PLAYER_JUMPING);
else
PlayerProcessEvent(sod, PLAYER_IDLE);
}
/* yay keyboard up... */
void keyboardUpFunc(unsigned char key, int x, int y){
switch(key){
case 'w':
sod->fFlag = 0;
break;
case 's':
sod->bFlag = 0;
break;
case 'a':
sod->lFlag = 0;
break;
case 'd':
sod->rFlag = 0;
break;
case 'q':
sod->jFlag = 0;
break;
default:
break;
}
if(sod->rFlag || sod->lFlag || sod->fFlag || sod->bFlag)
PlayerProcessEvent(sod, PLAYER_RUNNING);
else if(sod->jFlag)
PlayerProcessEvent(sod, PLAYER_JUMPING);
else
PlayerProcessEvent(sod, PLAYER_IDLE);
}
/* yay main... */
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(screenWidth, screenHeight);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutPassiveMotionFunc(mouseMotionFunc);
glutMotionFunc(mouseMotionFunc);
glutMouseFunc(mouseFunc);
glutKeyboardFunc(keyboardDownFunc);
glutKeyboardUpFunc(keyboardUpFunc);
glutSetCursor(GLUT_CURSOR_NONE);
init();
glutMainLoop();
return 0;
}