-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHouse.cpp
More file actions
468 lines (349 loc) · 11.1 KB
/
House.cpp
File metadata and controls
468 lines (349 loc) · 11.1 KB
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
457
458
459
460
461
462
463
464
465
466
#include "stdafx.h"
#include "House.h"
#include <math.h>
//---------------------------------------------------------------------
// Method: init
// Author: Rick Sanders
// Function: Draws a 3D house (Base plus Roof).
// .
// The house is oriented in the the xz plane.
// Compiles a display list to be used when drawing the house.
// Parameters:
// x - x coordinate of the middle of the house
// y - y coordinate of the middle of the house
// z - z coordinate of the middle of the house
// houseWidth - the width of the house and base of the roof.
// houseLength - the length of the side of the house.
// baseHeight - the height of the base of the house.
// roofHeight - the height of the roof of the house.
//
// Returns: None
// Called By:
// Calls: None
//---------------------------------------------------------------------
void House::init(float xPos, float yPos, float zPos, float houseWidth,
float houseLength, float baseHeight, float roofHeight,
int siding1Tex, int shingleTex)
{
x = xPos;
y = yPos;
z = zPos;
explosion = 0;
destroyed = false;
forwardCollided = false;
backwardCollided = false;
damageCapacity = (houseWidth * houseLength * baseHeight) / 50.0f;
pointValue = (int) damageCapacity;
siding1Texture = siding1Tex;
shingleTexture = shingleTex;
GLfloat v[3]={0};
GLfloat house_spec[] = {0.25, 0.25, 0.25, 1.0};
float p[15][3]; // array to hold coords for the house faces.
float offset = 0.01f;
short color = 1;
houseList = glGenLists(1);
glNewList(houseList, GL_COMPILE);
GLfloat halfWidth = houseWidth/2.0f;
GLfloat halfBaseHeight = baseHeight/2.0f;
GLfloat halfRoofHeight = roofHeight/2.0f;
GLfloat halfLength = houseLength/2.0f;
GLfloat radius = (GLfloat)(sqrt(houseWidth * houseWidth + houseLength * houseLength)/ 2.0f);
// define the two colors
GLfloat color1[3] = { 0.0f, 0.0f, 0.0f };
GLfloat color2[3] = { 0.5f, 0.5f, 0.5f };
GLfloat color3[3] = { 1.0f, 1.0f, 1.0f };
glNormal3f(0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT, GL_SPECULAR, house_spec);
glColor3fv(color2);
glPointSize(2);
// Back face.
p[0][0]=x-halfWidth; // p[0] -+-
p[0][1]=y+halfBaseHeight;
p[0][2]=z-halfLength;
p[1][0]=x+halfWidth; // p[1] ++-
p[1][1]=y+halfBaseHeight;
p[1][2]=z-halfLength;
p[2][0]=x+halfWidth; // p[2] +--
p[2][1]=y-halfBaseHeight;
p[2][2]=z-halfLength;
p[3][0]=x-halfWidth; // p[3] ---
p[3][1]=y-halfBaseHeight;
p[3][2]=z-halfLength;
p[4][0]=x+halfWidth; // p[4] +++
p[4][1]=y+halfBaseHeight;
p[4][2]=z+halfLength;
p[5][0]=x-halfWidth; // p[5] -++
p[5][1]=y+halfBaseHeight;
p[5][2]=z+halfLength;
p[6][0]=x-halfWidth; // p[6] --+
p[6][1]=y-halfBaseHeight;
p[6][2]=z+halfLength;
p[7][0]=x+halfWidth; // p[7] +-+
p[7][1]=y-halfBaseHeight;
p[7][2]=z+halfLength;
p[8][0]=x; // p[8] back of the top of the roof
p[8][1]=y+halfBaseHeight+roofHeight;
p[8][2]=z-halfLength;
p[9][0]=x; // p[9] front of the top of the roof
p[9][1]=y+halfBaseHeight+roofHeight;
p[9][2]=z+halfLength;
p[10][0] = x - halfWidth/10.0f;// p[10] bottom left of door
p[10][1] = y - halfBaseHeight;
p[10][2] = z + halfLength + offset;
p[11][0] = x + halfWidth/10.0f;// p[11] bottom right of door
p[11][1] = y - halfBaseHeight;
p[11][2] = z + halfLength + offset;
p[12][0] = x + halfWidth/10.0f;// p[12] top right of door
p[12][1] = y;
p[12][2] = z + halfLength + offset;
p[13][0] = x - halfWidth/10.0f;// p[13] top left of door
p[13][1] = y;
p[13][2] = z + halfLength + offset;
p[14][0] = x - (halfWidth/20.0f);// p[14] door handle location
p[14][1] = y - (halfBaseHeight/2.0f);
p[14][2] = z + halfLength + (2.0f * offset);
// enable texture mapping for the house
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, siding1Texture);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glMaterialfv(GL_FRONT, GL_SPECULAR, house_spec);
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0);
glTexCoord2f(0.0, 0.0);
glVertex3f(x-halfWidth, y-halfBaseHeight, z+halfLength);
glTexCoord2f(1.0, 0.0);
glVertex3f(x+halfWidth, y-halfBaseHeight, z+halfLength);
glTexCoord2f(1.0, 3.0);
glVertex3f(x+halfWidth, y+halfBaseHeight, z+halfLength);
glTexCoord2f(0.0, 3.0);
glVertex3f(x-halfWidth, y+halfBaseHeight, z+halfLength);
glEnd();
/* glBegin( GL_POLYGON );// front face of base
// glColor3f(0.5, 0.5, 0.5);
glVertex3fv(p[5]);
glVertex3fv(p[6]);
glVertex3fv(p[7]);
glVertex3fv(p[4]);
glEnd();*/
glBegin(GL_QUADS);
glNormal3f(1.0, 0.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3f(x+halfWidth, y-halfBaseHeight, z+halfLength);
glTexCoord2f(1.0, 0.0);
glVertex3f(x+halfWidth, y-halfBaseHeight, z-halfLength);
glTexCoord2f(1.0, 3.0);
glVertex3f(x+halfWidth, y+halfBaseHeight, z-halfLength);
glTexCoord2f(0.0, 3.0);
glVertex3f(x+halfWidth, y+halfBaseHeight, z+halfLength);
glEnd();
/* glBegin( GL_POLYGON );// right face of base
// glColor3f(1.0, 1.0, 0.0);
glVertex3fv(p[4]);
glVertex3fv(p[7]);
glVertex3fv(p[2]);
glVertex3fv(p[1]);
glEnd(); */
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, -1.0);
glTexCoord2f(0.0, 0.0);
glVertex3f(x+halfWidth, y-halfBaseHeight, z-halfLength);
glTexCoord2f(1.0, 0.0);
glVertex3f(x-halfWidth, y-halfBaseHeight, z-halfLength);
glTexCoord2f(1.0, 3.0);
glVertex3f(x-halfWidth, y+halfBaseHeight, z-halfLength);
glTexCoord2f(0.0, 3.0);
glVertex3f(x+halfWidth, y+halfBaseHeight, z-halfLength);
glEnd();
/* glBegin( GL_POLYGON );// rear face of base
// glColor3f(0.0, 1.0, 1.0);
glVertex3fv(p[1]);
glVertex3fv(p[2]);
glVertex3fv(p[3]);
glVertex3fv(p[0]);
glEnd();
glBegin(GL_POLYGON);// bottom face of base
// glColor3f(0.0, 0.5, 1.0);
glNormal3f(0.0, -1.0, 0.0);
glVertex3fv(p[2]);
glVertex3fv(p[7]);
glVertex3fv(p[6]);
glVertex3fv(p[3]);
glEnd();*/
glBegin(GL_QUADS);
glNormal3f(-1.0, 0.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3f(x-halfWidth, y-halfBaseHeight, z-halfLength);
glTexCoord2f(1.0, 0.0);
glVertex3f(x-halfWidth, y-halfBaseHeight, z+halfLength);
glTexCoord2f(1.0, 3.0);
glVertex3f(x-halfWidth, y+halfBaseHeight, z+halfLength);
glTexCoord2f(0.0, 3.0);
glVertex3f(x-halfWidth, y+halfBaseHeight, z-halfLength);
glEnd();
/* glBegin(GL_POLYGON);// left face of base
// glColor3f(0.0, 1.0, 0.0);
glVertex3fv(p[6]);
glVertex3fv(p[5]);
glVertex3fv(p[0]);
glVertex3fv(p[3]);
glEnd();
glBegin (GL_POLYGON);// top face
// glColor3f(1.0, 0.0, 1.0);
glNormal3f(0.0, 1.0, 0.0);
glVertex3fv(p[0]);
glVertex3fv(p[5]);
glVertex3fv(p[4]);
glVertex3fv(p[1]);
glEnd();*/
// the base of the house is drawn.
glBegin(GL_TRIANGLES);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0, 0.0);
glVertex3f(x-halfWidth, y+halfBaseHeight, z+halfLength);
glTexCoord2f(1.0, 0.0);
glVertex3f(x+halfWidth, y+halfBaseHeight, z+halfLength);
glTexCoord2f(0.5, 2.0);
glVertex3f(x, y+halfBaseHeight+roofHeight, z+halfLength);
glEnd();
/* glBegin (GL_POLYGON);// front face of roof
// glColor3f(1.0f, 0.0f, 0.0f);
glVertex3fv(p[5]);
glVertex3fv(p[4]);
glVertex3fv(p[9]);
glEnd();*/
glBegin(GL_TRIANGLES);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0, 0.0);
glVertex3f(x+halfWidth, y+halfBaseHeight, z-halfLength);
glTexCoord2f(1.0, 0.0);
glVertex3f(x-halfWidth, y+halfBaseHeight, z-halfLength);
glTexCoord2f(0.5, 2.0);
glVertex3f(x, y+halfBaseHeight+roofHeight, z-halfLength);
glEnd();
/* glBegin (GL_POLYGON);// rear face of roof
// glColor3f(1.0f, 0.0f, 0.0f);
glVertex3fv(p[0]);
glVertex3fv(p[8]);
glVertex3fv(p[1]);
glEnd();*/
glBindTexture(GL_TEXTURE_2D, shingleTexture);
glBegin(GL_QUADS);
glNormal3f(-0.707f, 0.707f, 0.0f);
glTexCoord2f(0.0, 0.0);
glVertex3f(x-halfWidth, y+halfBaseHeight, z-halfLength);
glTexCoord2f(1.0, 0.0);
glVertex3f(x-halfWidth, y+halfBaseHeight, z+halfLength);
glTexCoord2f(1.0, 3.0);
glVertex3f(x, y+halfBaseHeight+roofHeight, z+halfLength);
glTexCoord2f(0.0, 3.0);
glVertex3f(x, y+halfBaseHeight+roofHeight, z-halfLength);
glEnd();
/* glBegin (GL_POLYGON);// left roof top
glColor3fv(color1);
glVertex3fv(p[0]);
glVertex3fv(p[5]);
glVertex3fv(p[9]);
glVertex3fv(p[8]);
glEnd();*/
glBegin(GL_QUADS);
glNormal3f(0.707f, 0.707f, 0.0f);
glTexCoord2f(0.0, 0.0);
glVertex3f(x+halfWidth, y+halfBaseHeight, z+halfLength);
glTexCoord2f(1.0, 0.0);
glVertex3f(x+halfWidth, y+halfBaseHeight, z-halfLength);
glTexCoord2f(1.0, 3.0);
glVertex3f(x, y+halfBaseHeight+roofHeight, z-halfLength);
glTexCoord2f(0.0, 3.0);
glVertex3f(x, y+halfBaseHeight+roofHeight, z+halfLength);
glEnd();
/* glBegin (GL_POLYGON);// right roof top
// glColor3f(0.0f, 0.0f, 0.0f);
glVertex3fv(p[4]);
glVertex3fv(p[1]);
glVertex3fv(p[8]);
glVertex3fv(p[9]);
glEnd();*/
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_2D);
// the roof of the house is drawn.
glBegin (GL_POLYGON);// door
glColor3fv(color3);
glNormal3f(0.0f, 0.0, 1.0f);
glVertex3fv(p[10]);
glVertex3fv(p[11]);
glVertex3fv(p[12]);
glVertex3fv(p[13]);
glEnd();
glBegin (GL_POINTS);// door knob
glColor3fv(color1);
glNormal3f(0.0f, 0.0, 1.0f);
glVertex3fv(p[14]);
glEnd();
// the door of the house is drawn.
glEndList();
} // end initHouse
//---------------------------------------------------------------------
// Method: draw
// Function: Draws a house object with the size and location specified
// by the class data members.
// Parameters: None
//
// Returns: Void
// Called By:
// Calls: None
//---------------------------------------------------------------------
void House::draw()
{
glCallList(houseList);
} // end draw
GLfloat House::getRadius() {
return radius;
} // end getRadius
GLfloat House::getX() {
return x;
} // end getX
GLfloat House::getZ() {
return z;
} // end getZ
bool House::isDestroyed() {
return destroyed;
} // end isDestroyed
void House::startExplosion() {
explosion = new Explosion();
explosion->init(radius);
} // end startExplosion
void House::destroy() {
destroyed = true;
} // end destroy
Explosion* House::getExplosion() {
return explosion;
} // end getExplosion
void House::takeDamage(GLfloat damage) {
damageCapacity -= damage;
if (damageCapacity <= 0.0) {
destroyed = true;
} // end if
} // end takeDamage
void House::forwardCollide() {
forwardCollided = true;
} // end forwardCollide
void House::backwardCollide() {
backwardCollided = true;
} // end backwardCollide
void House::unForwardCollide() {
forwardCollided = false;
} // end unForwardcollide
void House::unBackwardCollide() {
backwardCollided = false;
} // end unBackwardcollide
bool House::isForwardCollided() {
return forwardCollided;
} // end isForwardCollided
bool House::isBackwardCollided() {
return backwardCollided;
} // end isForwardCollided
int House::getPointValue() {
return pointValue;
} // end getPointValue
// *********************** END HOUSE CLASS *****************************