-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyramid.cpp
More file actions
160 lines (123 loc) · 3.51 KB
/
Pyramid.cpp
File metadata and controls
160 lines (123 loc) · 3.51 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
#include "stdafx.h"
#include "Pyramid.h"
#include "normals.h"
//#include <math.h>
//---------------------------------------------------------------------
// Method: init
// Function: Initializes the class data members height and width
// Parameters:
// h - desired height for the pyramid object
// w - desired width for the pyramid object
//
// Returns: Void
// Called By:
// Calls: None
//---------------------------------------------------------------------
void Pyramid::init(GLfloat h, GLfloat w, GLfloat xPos, GLfloat zPos)
{
x = xPos;
z = zPos;
height = h;
width = w;
radius = (float) sqrt(2 * width * width) / 2.0f;
forwardCollided = false;
backwardCollided = false;
float p[5][3]; // storage array for the points on the pyramid
float norm[3]; // array for unit normal
p[0][0] = -width/2.0f; // p[0] -0+
p[0][1] = 0.0f;
p[0][2] = width/2.0f;
p[1][0] = -width/2.0f; // p[1] -0-
p[1][1] = 0.0f;
p[1][2] = -width/2.0f;
p[2][0] = width/2.0f; // p[2] +0-
p[2][1] = 0.0f;
p[2][2] = -width/2.0f;
p[3][0] = width/2.0f; // p[3] +0+
p[3][1] = 0.0f;
p[3][2] = width/2.0f;
p[4][0] = 0.0f; // p[4] 0h0
p[4][1] = height;
p[4][2] = 0.0f;
pyramidList = glGenLists(1);
glNewList(pyramidList, GL_COMPILE);
glBegin(GL_QUADS);
glNormal3f(0.0, -1.0, 0.0);
glVertex3fv(p[0]);
glVertex3fv(p[1]);
glVertex3fv(p[2]);
glVertex3fv(p[3]);
glEnd();
glBegin(GL_TRIANGLE_FAN);
findUnitNormal(p[4], p[0], p[3], norm);
glNormal3fv(norm);
glVertex3fv(p[4]);
glVertex3fv(p[0]);
glVertex3fv(p[3]);
findUnitNormal(p[4], p[3], p[2], norm);
glNormal3fv(norm);
glVertex3fv(p[4]);
glVertex3fv(p[3]);
glVertex3fv(p[2]);
findUnitNormal(p[4], p[2], p[1], norm);
glNormal3fv(norm);
glVertex3fv(p[4]);
glVertex3fv(p[2]);
glVertex3fv(p[1]);
findUnitNormal(p[4], p[1], p[0], norm);
glNormal3fv(norm);
glVertex3fv(p[4]);
glVertex3fv(p[1]);
glVertex3fv(p[0]);
glEnd();
glEndList();
} // end init
//---------------------------------------------------------------------
// Method: draw
// Function: Draws a pyramid centered at the origin with the height and
// width given by the class data members. The pyramid is
// oriented in the xz plane pointing along the positive y axis.
// Parameters:
// None
//
// Returns: Void
// Called By:
// Calls: None
//---------------------------------------------------------------------
void Pyramid::draw()
{
glCallList(pyramidList);
} // end draw
GLfloat Pyramid::getHeight() {
return height;
} // end getHeight
GLfloat Pyramid::getWidth() {
return width;
} // edn getWidth
GLfloat Pyramid::getRadius() {
return radius;
} // end getRadius
GLfloat Pyramid::getX() {
return x;
} // end getX
GLfloat Pyramid::getZ() {
return z;
} // end getZ
void Pyramid::forwardCollide() {
forwardCollided = true;
} // end forwardCollide
void Pyramid::backwardCollide() {
backwardCollided = true;
} // end backwardCollide
void Pyramid::unForwardCollide() {
forwardCollided = false;
} // end unForwardCollide
void Pyramid::unBackwardCollide() {
backwardCollided = false;
} // end unBackwardCollide
bool Pyramid::isForwardCollided() {
return forwardCollided;
} // end isForwardCollided
bool Pyramid::isBackwardCollided() {
return backwardCollided;
} // end isBackwardCollided