This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawing.c
456 lines (417 loc) · 11.5 KB
/
drawing.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include "project.h"
/*
* BEGIN DISPLAY AND DRAW FUNCTIONS
*/
/*
* drawAxes
* ------
* Draws the axes if desired
*/
void drawAxes(void)
{
/* axes length */
const double len=5.0;
/* Draw axes - no lighting */
if (axes) {
glDisable(GL_LIGHTING);
glColor3fv(white);
glBegin(GL_LINES);
glVertex3d(0.0,0.0,0.0);
glVertex3d(len,0.0,0.0);
glVertex3d(0.0,0.0,0.0);
glVertex3d(0.0,len,0.0);
glVertex3d(0.0,0.0,0.0);
glVertex3d(0.0,0.0,len);
glEnd();
/* Label axes */
glRasterPos3d(len,0.0,0.0);
print("X");
glRasterPos3d(0.0,len,0.0);
print("Y");
glRasterPos3d(0.0,0.0,len);
print("Z");
glEnable(GL_LIGHTING);
}
}
/*
* drawParameters
* ------
* Draws the parameters if desired
*/
void drawParameters(void)
{
if (vals) {
glColor3fv(white);
/* Display parameters */
printAt(5,5,"Angle=%d,%d Dim=%.1f FOV=%d Light=%s",
th,ph,dim,fov,light?"On":"Off");
if (light) {
printAt(5,45,"Distance=%d Elevation=%.1f",distance,lightY);
printAt(5,25,"Ambient=%d Diffuse=%d Specular=%d Emission=%d Shininess=%.0f",
ambient,diffuse,specular,emission,shinyvec[0]);
}
}
}
/*
* drawGrid
* ------
* Draws the grid if desired
*/
void drawGrid(void)
{
/* Draw grid - no lighting */
if (grid) {
int i,j;
glDisable(GL_LIGHTING);
glColor3fv(white);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(2,1);
glBegin(GL_LINES);
/* horizontal z */
for (i=-21;i<21;i+=2){
for (j=-19;j<23;j+=40){
glVertex3d(i,-2.8,j);
}
}
/* horizontal x */
for (i=-19;i<23;i+=2){
for (j=-21;j<20;j+=40){
glVertex3d(j,-2.8,i);
}
}
glEnd();
glDisable(GL_POLYGON_OFFSET_FILL);
glEnable(GL_LIGHTING);
}
}
/*
* drawBackground
* ------
* Draws the background skybox
*/
void drawBackground(double D)
{
glColor3fv(white);
glEnable(GL_TEXTURE_2D);
/* Sides */
glBindTexture(GL_TEXTURE_2D,backgrounds[BACK_RIGHT]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-D,-D,-D);
glTexCoord2f(1,0); glVertex3f(+D,-D,-D);
glTexCoord2f(1,1); glVertex3f(+D,+D,-D);
glTexCoord2f(0,1); glVertex3f(-D,+D,-D);
glEnd();
glBindTexture(GL_TEXTURE_2D,backgrounds[BACK_FRONT]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(+D,-D,-D);
glTexCoord2f(1,0); glVertex3f(+D,-D,+D);
glTexCoord2f(1,1); glVertex3f(+D,+D,+D);
glTexCoord2f(0,1); glVertex3f(+D,+D,-D);
glEnd();
glBindTexture(GL_TEXTURE_2D,backgrounds[BACK_LEFT]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(+D,-D,+D);
glTexCoord2f(1,0); glVertex3f(-D,-D,+D);
glTexCoord2f(1,1); glVertex3f(-D,+D,+D);
glTexCoord2f(0,1); glVertex3f(+D,+D,+D);
glEnd();
glBindTexture(GL_TEXTURE_2D,backgrounds[BACK_BACK]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-D,-D,+D);
glTexCoord2f(1,0); glVertex3f(-D,-D,-D);
glTexCoord2f(1,1); glVertex3f(-D,+D,-D);
glTexCoord2f(0,1); glVertex3f(-D,+D,+D);
glEnd();
/* Up */
glBindTexture(GL_TEXTURE_2D,backgrounds[BACK_UP]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-D,+D,-D);
glTexCoord2f(1,0); glVertex3f(+D,+D,-D);
glTexCoord2f(1,1); glVertex3f(+D,+D,+D);
glTexCoord2f(0,1); glVertex3f(-D,+D,+D);
glEnd();
/* Down */
glBindTexture(GL_TEXTURE_2D,backgrounds[BACK_DOWN]);
glBegin(GL_QUADS);
glTexCoord2f(1,1); glVertex3f(+D,-D,-D);
glTexCoord2f(0,1); glVertex3f(-D,-D,-D);
glTexCoord2f(0,0); glVertex3f(-D,-D,+D);
glTexCoord2f(1,0); glVertex3f(+D,-D,+D);
glEnd();
glDisable(GL_TEXTURE_2D);
}
/*
* drawLight
* ------
* Draws the light (rotating sphere) if desired
*/
void drawLight(void)
{
/* Light switch */
if (light) {
/* Translate intensity to color vectors */
float Ambient[] = {0.01*ambient ,0.01*ambient ,0.01*ambient ,1.0};
float Diffuse[] = {0.01*diffuse ,0.01*diffuse ,0.01*diffuse ,1.0};
float Specular[] = {0.01*specular,0.01*specular,0.01*specular,1.0};
/* Light position */
float Position[] = {lightY,distance*Sin(lightPh),distance*Cos(lightPh),1.0};
/* Draw light position as sphere (still no lighting here) */
glColor3fv(white);
sphere(Position[0],Position[1],Position[2] , 0.1,0);
/* OpenGL should normalize normal vectors */
glEnable(GL_NORMALIZE);
/* Enable lighting */
glEnable(GL_LIGHTING);
/* glColor sets ambient and diffuse color materials */
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
/* Enable light 0 */
glEnable(GL_LIGHT0);
/* Set ambient, diffuse, specular components and position of light 0 */
glLightfv(GL_LIGHT0,GL_AMBIENT ,Ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE ,Diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,Specular);
glLightfv(GL_LIGHT0,GL_POSITION,Position);
}
else
glDisable(GL_LIGHTING);
}
/*
* drawBoard
* ------
* draw the board
*/
void drawBoard(void)
{
board();
}
/*
* drawPath
* ------
* fixed path for the game.
*/
void drawPath(void)
{
path();
}
/*
* drawForests
* ------
* Draws forestry
*/
void drawForests(void)
{
/* backside forests */
evergreenForest1(-22,0,-10, 1,1,1, 90);
/* gap here for keep */
evergreenForest1(-22,0,-20, 1,1,1, 90);
/* rightside forests */
evergreenForest1(-15,0,-20, 1,1,1, 0);
evergreenForest1(-5,0,-20, 1,1,1, 0);
evergreenForest1(5,0,-20, 1,1,1, 0);
evergreenForest1(15,0,-20, 1,1,1, 0);
evergreenForest1(20,0,-20, 1,1,1, 0);
/* frontside forests */
evergreenForest1(20,0,-15, 1,1,1, -90);
evergreenForest1(20,0,-10, 1,1,1, -90);
/* gap here for minions */
evergreenForest1(20,0,10, 1,1,1, -90);
evergreenForest1(20,0,20, 1,1,1, -90);
/* leftside forests */
evergreenForest1(15,0,22, 1,1,1, 180);
evergreenForest1(10,0,22, 1,1,1, 180);
evergreenForest1(0,0,22, 1,1,1, 180);
evergreenForest1(-10,0,22, 1,1,1, 180);
evergreenForest1(-20,0,22, 1,1,1, 180);
}
/*
* drawKeep
* ------
* Draws the keep
*/
void drawKeep(void)
{
keep(-32,0,10, 1,1,1, 0);
}
/*
* drawShots
* ------
* Draws the shots from the towers
*/
void drawShots(void)
{
int i;
for (i=0;i<Length(shots);i++) {
if (shots[i].inPlay == 1) shotModel(shots[i]);
}
}
/*
* drawMinions
* ------
* draw the minions that are in play
* TODO: add object selection for minions
*/
void drawMinions(void)
{
int i,k;
for (k=0;k<waveNumber;k++) {
for (i=0;i<Length(waves[k].m);i++) {
if (waves[k].m[i].inPlay == 1) minionModel(waves[k].m[i]);
}
}
}
/*
* drawObjects
* ------
* draw the current objects (towers)
*/
void drawObjects(void)
{
int i;
/* preview tower */
if (preview_tower.id != DEF_OBJ_SEL) {
tower t = {preview_tower.id,preview_tower.type,preview_tower.inPlay,
{preview_tower.translation.x,preview_tower.translation.y,preview_tower.translation.z},
{1,1,1},{0,0,0},preview_tower.texture,{1,1,1},
preview_tower.name,1,preview_tower.range,preview_tower.damage,
preview_tower.fireRate,0,preview_tower.cost};
/* awesome opacity for the preview */
showAttackRadius = 1;
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ONE_MINUS_DST_COLOR);
if (t.type == OBJ_BASIC) basicTower(t);
else if (t.type == OBJ_ADV) advancedTower(t);
else if (t.type == OBJ_CONE) coneTower(t);
else if (t.type == OBJ_ADV_CONE) advancedConeTower(t);
else if (t.type == OBJ_SQUARE) squareTower(t);
else if (t.type == OBJ_ADV_SQUARE) advancedSquareTower(t);
else if (t.type == OBJ_FIRE) fireTower(t);
else if (t.type == OBJ_FIRE2) fireTower2(t);
else if (t.type == OBJ_ICE) iceTower(t);
else if (t.type == OBJ_ICE2) iceTower2(t);
else if (t.type == OBJ_EARTH) earthTower(t);
else if (t.type == OBJ_EARTH2) earthTower2(t);
else if (t.type == OBJ_POISON) poisonTower(t);
else if (t.type == OBJ_POISON2) poisonTower2(t);
glDisable(GL_BLEND);
showAttackRadius = 0;
}
/* towers */
for (i = 0; i < Length(towers); i++) {
if (towers[i].inPlay) {
tower t = {0,towers[i].type,towers[i].inPlay,
{towers[i].translation.x,towers[i].translation.y,towers[i].translation.z},
{1,1,1},{0,0,0},towers[i].texture,
{towers[i].rgb.r,towers[i].rgb.g,towers[i].rgb.b},
towers[i].name,towers[i].level,towers[i].range,towers[i].damage,
towers[i].fireRate,towers[i].lastFired,towers[i].cost,towers[i].description};
/* draw the objects */
if (renderMode == DEF_SELECT) {
glDisable(GL_DITHER);
glDisable(GL_LIGHTING);
glColor3ub(towers[i].rgb.r,towers[i].rgb.g,towers[i].rgb.b);
}
if (t.type == OBJ_BASIC) basicTower(t);
else if (t.type == OBJ_ADV) advancedTower(t);
else if (t.type == OBJ_CONE) coneTower(t);
else if (t.type == OBJ_ADV_CONE) advancedConeTower(t);
else if (t.type == OBJ_SQUARE) squareTower(t);
else if (t.type == OBJ_ADV_SQUARE) advancedSquareTower(t);
else if (t.type == OBJ_FIRE) fireTower(t);
else if (t.type == OBJ_FIRE2) fireTower2(t);
else if (t.type == OBJ_ICE) iceTower(t);
else if (t.type == OBJ_ICE2) iceTower2(t);
else if (t.type == OBJ_EARTH) earthTower(t);
else if (t.type == OBJ_EARTH2) earthTower2(t);
else if (t.type == OBJ_POISON) poisonTower(t);
else if (t.type == OBJ_POISON2) poisonTower2(t);
if (renderMode == DEF_SELECT) {
glEnable(GL_DITHER);
glEnable(GL_LIGHTING);
}
}
}
}
/*
* drawShadows
* ------
* draw shadows for minions and objects on the screen
*/
void drawShadows()
{
/* position of the light */
float Position[] = {lightY,distance*Sin(lightPh),distance*Cos(lightPh),1.0};
/* Save what is glEnabled */
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
/* Enable stencil operations */
glEnable(GL_STENCIL_TEST);
/*
* Step 1: Set stencil buffer to 1 where there are shadows
*/
/* Existing value of stencil buffer doesn't matter */
glStencilFunc(GL_ALWAYS,1,0xFFFFFFFF);
/* Set the value to 1 (REF=1 in StencilFunc)
only if Z-buffer would allow write */
glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);
/* Make Z-buffer and color buffer read-only */
glDepthMask(0);
glColorMask(0,0,0,0);
/* Draw flattened scene */
glPushMatrix();
shadowProjection(Position,E,N);
/* only draw objects that we want shadows for */
drawForests();
drawKeep();
drawMinions();
drawObjects();
glPopMatrix();
/* Make Z-buffer and color buffer read-write */
glDepthMask(1);
glColorMask(1,1,1,1);
/*
* Step 2: Draw shadow masked by stencil buffer
*/
/* Set the stencil test draw where stencil buffer is > 0 */
glStencilFunc(GL_LESS,0,0xFFFFFFFF);
/* Make the stencil buffer read-only */
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
/* Enable blending */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0,0,0,0.5);
/* Draw the shadow over the entire floor */
glBegin(GL_QUADS);
glVertex3f(-DEF_D_FLOOR,DEF_Y_FLOOR,-DEF_D_FLOOR);
glVertex3f(+DEF_D_FLOOR,DEF_Y_FLOOR,-DEF_D_FLOOR);
glVertex3f(+DEF_D_FLOOR,DEF_Y_FLOOR,+DEF_D_FLOOR);
glVertex3f(-DEF_D_FLOOR,DEF_Y_FLOOR,+DEF_D_FLOOR);
glEnd();
/* Undo glEnables */
glPopAttrib();
}
/*
* drawScene
* ------
* draws the entire scene
*/
void drawScene(void)
{
if (renderMode == DEF_RENDER) {
drawAxes();
drawGrid();
drawParameters();
drawBackground(3.5*dim);
drawLight();
/* anything with lighting should be drawn after the light */
drawBoard();
drawPath();
drawForests();
drawKeep();
drawShots();
}
drawMinions();
drawObjects();
/* only draw shadows on rendering, not selection */
if (renderMode == DEF_RENDER) drawShadows();
}