-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.h
More file actions
319 lines (256 loc) · 6.06 KB
/
objects.h
File metadata and controls
319 lines (256 loc) · 6.06 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
#pragma once
#include <windows.h>
#include "glee.h"
#include <gl/glu.h>
#include <vector>
#include <math.h>
class GameEngine; // Forward declaration of GameEngine class.
class ShaderProgram; // Forward declaration of ShaderProgram class.
class Box;
#define BUFFER_OFFSET(i) ((char*)NULL + i)
struct Vector3
{
GLfloat x, y, z;
Vector3()
{
x = y = z = 0.0f;
}
Vector3(GLfloat x, GLfloat y, GLfloat z)
{
this->x = x;
this->y = y;
this->z = z;
}
GLfloat length()
{
return sqrtf(powf(this->x, 2.0f) + powf(this->y, 2.0f) + powf(this->z, 2.0f));
}
void normalize()
{
GLfloat len = length();
x /= len;
y /= len;
z /= len;
}
Vector3 Vector3::operator+(Vector3 &otherVector)
{
Vector3 ret;
ret.x = x + otherVector.x;
ret.y = y + otherVector.y;
ret.z = z + otherVector.z;
return ret;
}
Vector3 Vector3::operator-(Vector3 &otherVector)
{
Vector3 ret;
ret.x = x - otherVector.x;
ret.y = y - otherVector.y;
ret.z = z - otherVector.z;
return ret;
}
Vector3 Vector3::operator-()
{
Vector3 ret;
ret.x = -x;
ret.y = -y;
ret.z = -z;
return ret;
}
Vector3 Vector3::operator*(const GLfloat &multiple)
{
Vector3 ret;
ret.x = x * multiple;
ret.y = y * multiple;
ret.z = z * multiple;
return ret;
}
Vector3 Vector3::operator/(const GLfloat &multiple)
{
Vector3 ret;
ret.x = x / multiple;
ret.y = y / multiple;
ret.z = z / multiple;
return ret;
}
Vector3 Vector3::cross(Vector3 &otherVector)
{
Vector3 ret;
ret.x = (y * otherVector.z) - (z * otherVector.y);
ret.y = (z * otherVector.x) - (x * otherVector.z);
ret.z = (x * otherVector.y) - (y * otherVector.x);
return ret;
}
GLfloat Vector3::dot(Vector3 &otherVector)
{
return (this->x * otherVector.x) + (this->y * otherVector.y) + (this->z * otherVector.z);
}
};
struct Sphere
{
Vector3 position;
GLfloat radius;
Sphere::Sphere(Vector3 pos, GLfloat rad)
{
position = pos;
radius = rad;
}
};
struct Color
{
Color()
{
r = g = b = a = 0.0;
}
GLfloat r;
GLfloat g;
GLfloat b;
GLfloat a;
};
class MaterialProps
{
public:
enum PropType {AMBIENT, DIFFUSE, SPECULAR, EMISSIVE};
void setProperty(PropType type, GLfloat r, GLfloat g, GLfloat b, GLfloat a)
{
Color* prop = getProp(type);
prop->r = r;
prop->g = g;
prop->b = b;
prop->a = a;
}
void setShininess(GLfloat shininess)
{
m_shininess = shininess;
}
Color getProperty(PropType type)
{
return *(getProp(type));
}
GLfloat getShininess()
{
return m_shininess;
}
private:
Color* getProp(PropType type)
{
Color* prop;
switch(type)
{
case AMBIENT:
{
prop = &m_ambient;
break;
}
case DIFFUSE:
{
prop = &m_diffuse;
break;
}
case SPECULAR:
{
prop = &m_specular;
break;
}
case EMISSIVE:
{
prop = &m_emissive;
break;
}
default:
{
MessageBox(NULL, "Material Error", "Invalid material property type", MB_ICONERROR | MB_OK);
exit(-1);
}
}
return prop;
}
Color m_ambient;
Color m_diffuse;
Color m_specular;
Color m_emissive;
GLfloat m_shininess;
};
class Object
{
public:
Object(GameEngine* engine); //constructor
~Object(); //destructor
virtual const char* getType();
virtual void onPrepare(GLfloat dt);
virtual void onRender();
virtual void onPostRender();
virtual void drawBoundingSphere();
void setPosition(GLfloat x, GLfloat y, GLfloat z);
void setPosition(Vector3 position);
Vector3 getPosition();
void setScale(GLfloat x, GLfloat y, GLfloat z);
Vector3 getScale();
void setVelocity(GLfloat x, GLfloat y, GLfloat z);
void setVelocity(Vector3 velocity);
Vector3 getVelocity();
void setAcceleration(GLfloat x, GLfloat y, GLfloat z);
void setAcceleration(Vector3 acceleration);
Vector3 getAcceleration();
virtual void adjustPitch(GLfloat angle);
virtual void adjustYaw(GLfloat angle);
void setPitch(GLfloat angle);
void setYaw(GLfloat angle);
GLfloat getPitch() {return m_pitch;}
GLfloat getYaw() {return m_yaw;}
void setAlive(bool alive) {m_alive = alive;}
bool isAlive() {return m_alive;}
void setDelete(bool can_delete) {m_can_delete = can_delete;}
bool canDelete() {return m_can_delete;}
virtual bool collides() {return true;}
virtual void kill();
virtual Box* getCollider();
protected:
Vector3* m_position;
Vector3* m_scale;
Vector3* m_velocity;
Vector3* m_acceleration;
GLfloat m_yaw;
GLfloat m_pitch;
MaterialProps m_materialProps;
std::vector<GLfloat> m_vertices;
std::vector<GLdouble> m_colors;
GameEngine* m_engine; // Pointer to engine that spawned object.
bool m_alive; // If false, object is dead.
bool m_can_delete; // If true, engine will delete object.
GLfloat m_time_since_death;
GLUquadricObj *m_quadratic;
// Pointer to another object used when detecting collisions.
// Usually a bounding box or sphere. Could be a pointer to the object itself.
// At the moment this has to be a Box.
Box* m_collider;
};
class Box : public Object
{
public:
Box(GameEngine* engine, GLfloat length); //constructor
Box(GameEngine* engine, GLfloat x1, GLfloat x2, GLfloat y1, GLfloat y2, GLfloat z1, GLfloat z2); //constructor for boxes with non-uniform edge length
void initBox();
void initBox(GLfloat x1, GLfloat x2, GLfloat y1, GLfloat y2, GLfloat z1, GLfloat z2);
GLfloat getX1() {return m_x1;}
GLfloat getY1() {return m_y1;}
GLfloat getZ1() {return m_z1;}
GLfloat getX2() {return m_x2;}
GLfloat getY2() {return m_y2;}
GLfloat getZ2() {return m_z2;}
std::vector<Vector3*> getVertices(Vector3 translation);
void onPrepare(GLfloat dt);
void onRender();
private:
GLfloat m_x1, m_y1, m_z1, m_x2, m_y2, m_z2;
GLuint m_vertexBuffer, m_colorBuffer;
};
class Crosshair : public Object
{
public:
Crosshair(GameEngine* engine); //constructor
void onRender();
private:
GLuint m_buffer;
GLuint m_texture;
GLuint m_textureCoords;
};