-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.cpp
More file actions
424 lines (354 loc) · 9.18 KB
/
objects.cpp
File metadata and controls
424 lines (354 loc) · 9.18 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
#include "objects.h"
#include <iostream>
#include <sstream>
#include "Shader.h"
#include "targa.h"
#include "GameEngine.h"
#include "Utils.h"
Object::Object(GameEngine* engine)
{
m_position = new Vector3();
m_scale = new Vector3(1.0f, 1.0f, 1.0f);
m_velocity = new Vector3();
m_acceleration = new Vector3();
m_pitch = 0.0f;
m_yaw = 0.0f;
m_engine = engine;
m_collider = NULL;
m_alive = true;
m_can_delete = false;
m_time_since_death = 0.0f;
}
Object::~Object()
{
delete m_position;
delete m_scale;
delete m_velocity;
delete m_acceleration;
delete m_collider;
}
const char* Object::getType()
{
return "Object";
}
void Object::onPrepare(GLfloat dt)
{
if (!isAlive())
{
m_time_since_death += dt;
if (m_time_since_death >= 10.0f)
setDelete(true);
}
else
{
this->setVelocity(
this->getVelocity().x + this->getAcceleration().x,
this->getVelocity().y + this->getAcceleration().y,
this->getVelocity().z + this->getAcceleration().z
);
this->setPosition(
this->getPosition().x + this->getVelocity().x,
this->getPosition().y + this->getVelocity().y,
this->getPosition().z + this->getVelocity().z
);
//Check if object has left the game area
if (this->getPosition().x < -1000.0f || this->getPosition().x > 1000.0f ||
this->getPosition().y < -10.0f || this->getPosition().y > 1000.0f ||
this->getPosition().z < -1000.0f || this->getPosition().z > 1000.0f)
{
setAlive(false);
setDelete(true);
}
}
}
void Object::onRender()
{
glPushMatrix();
glTranslatef(this->getPosition().x, this->getPosition().y, this->getPosition().z);
glRotatef(this->getPitch(), 1.0f, 0.0f, 0.0f);
glRotatef(this->getYaw(), 0.0f, 1.0f, 0.0f);
glScalef(this->getScale().x, this->getScale().y, this->getScale().z);
}
void Object::onPostRender()
{
glPopMatrix();
}
void Object::drawBoundingSphere()
{
}
void Object::setPosition(GLfloat x, GLfloat y, GLfloat z)
{
m_position->x = x;
m_position->y = y;
m_position->z = z;
}
void Object::setPosition(Vector3 position)
{
*m_position = position;
}
Vector3 Object::getPosition()
{
return *m_position;
}
void Object::setScale(GLfloat x, GLfloat y, GLfloat z)
{
m_scale->x = x;
m_scale->y = y;
m_scale->z = z;
}
Vector3 Object::getScale()
{
return *m_scale;
}
void Object::setVelocity(GLfloat x, GLfloat y, GLfloat z)
{
m_velocity->x = x;
m_velocity->y = y;
m_velocity->z = z;
}
void Object::setVelocity(Vector3 velocity)
{
*m_velocity = velocity;
}
Vector3 Object::getVelocity()
{
return *m_velocity;
}
void Object::setAcceleration(GLfloat x, GLfloat y, GLfloat z)
{
m_acceleration->x = x;
m_acceleration->y = y;
m_acceleration->z = z;
}
void Object::setAcceleration(Vector3 acceleration)
{
*m_acceleration = acceleration;
}
Vector3 Object::getAcceleration()
{
return *m_acceleration;
}
void Object::adjustPitch(GLfloat angle)
{
m_pitch += angle;
if (m_pitch > 90.0f)
m_pitch = 90.0f;
else if (m_pitch < -90.0f)
m_pitch = -90.0f;
}
void Object::adjustYaw(GLfloat angle)
{
m_yaw += angle;
if (m_yaw >= 360.0f)
m_yaw -= 360.0f;
else if (m_yaw < 0.0f)
m_yaw += 360.0f;
}
/**
* setPitch
* Sets pitch to the given angle.
* WARNING: Use adjustPitch wherever possible as this method does not limit pitch.
**/
void Object::setPitch(GLfloat angle)
{
m_pitch = angle;
}
/**
* setYaw
* Sets yaw to the given angle.
* WARNING: Use adjustYaw wherever possible as this method does not limit yaw.
**/
void Object::setYaw(GLfloat angle)
{
m_yaw = angle;
}
void Object::kill()
{
m_alive = false;
}
Box* Object::getCollider()
{
//TODO implement default collider
if (!m_collider)
{
m_collider = new Box(NULL, 5.0f);
}
return m_collider;
}
/**
* getVertices
* Build and return a vector containing the object's vertices.
**/
std::vector<Vector3*> Box::getVertices(Vector3 translation)
{
std::vector<Vector3*> vertices(8);
vertices[0] = new Vector3(m_x2 + translation.x, m_y2 + translation.y, m_z2 + translation.z);
vertices[1] = new Vector3(m_x1 + translation.x, m_y2 + translation.y, m_z2 + translation.z);
vertices[2] = new Vector3(m_x1 + translation.x, m_y1 + translation.y, m_z2 + translation.z);
vertices[3] = new Vector3(m_x2 + translation.x, m_y1 + translation.y, m_z2 + translation.z);
vertices[4] = new Vector3(m_x2 + translation.x, m_y2 + translation.y, m_z1 + translation.z);
vertices[5] = new Vector3(m_x2 + translation.x, m_y1 + translation.y, m_z1 + translation.z);
vertices[6] = new Vector3(m_x1 + translation.x, m_y1 + translation.y, m_z1 + translation.z);
vertices[7] = new Vector3(m_x1 + translation.x, m_y2 + translation.y, m_z1 + translation.z);
return vertices;
}
Box::Box(GameEngine* engine, GLfloat length)
: Object(engine)
{
GLfloat hl = length / 2.0f;
initBox(-hl, hl, -hl, hl, -hl, hl);
}
Box::Box(GameEngine* engine, GLfloat x1, GLfloat x2, GLfloat y1, GLfloat y2, GLfloat z1, GLfloat z2)
: Object(engine)
{
initBox(x1, x2, y1, y2, z1, z2);
}
void Box::initBox(GLfloat x1, GLfloat x2, GLfloat y1, GLfloat y2, GLfloat z1, GLfloat z2)
{
m_x1 = x1;
m_y1 = y1;
m_z1 = z1;
m_x2 = x2;
m_y2 = y2;
m_z2 = z2;
GLdouble colorArray[] = {
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0,
};
m_colors.resize(96);
for (int i = 0; i < 96; ++i)
m_colors[i] = colorArray[i];
//Front
GLfloat vertexArray[] = {
//Front
m_x2, m_y2, m_z2,
m_x1, m_y2, m_z2,
m_x1, m_y1, m_z2,
m_x2, m_y1, m_z2,
//Back
m_x2, m_y2, m_z1,
m_x2, m_y1, m_z1,
m_x1, m_y1, m_z1,
m_x1, m_y2, m_z1,
//Left
m_x1, m_y2, m_z2,
m_x1, m_y2, m_z1,
m_x1, m_y1, m_z1,
m_x1, m_y1, m_z2,
//Right
m_x2, m_y2, m_z1,
m_x2, m_y2, m_z2,
m_x2, m_y1, m_z2,
m_x2, m_y1, m_z1,
//Top
m_x2, m_y2, m_z1,
m_x1, m_y2, m_z1,
m_x1, m_y2, m_z2,
m_x2, m_y2, m_z2,
//Bottom
m_x2, m_y1, m_z2,
m_x1, m_y1, m_z2,
m_x1, m_y1, m_z1,
m_x2, m_y1, m_z1,
};
m_vertices.resize(72);
for (int i = 0; i < 72; ++i)
m_vertices[i] = vertexArray[i];
glGenBuffers(1, &m_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_vertices.size(), &m_vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &m_colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLdouble) * m_colors.size(), &m_colors[0], GL_STATIC_DRAW);
}
void Box::onPrepare(GLfloat dt)
{
Object::onPrepare(dt);
}
void Box::onRender()
{
Object::onRender();
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
glVertexAttribPointer(1, 4, GL_DOUBLE, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(1);
ShaderProgram *shaderProgram = m_engine->m_basicProgram;
shaderProgram->bind();
shaderProgram->sendMatrices();
glDrawArrays(GL_QUADS, 0, 24);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
/*GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
std::stringstream str;
str << "Error drawing box: error code " << err;
MessageBox(NULL, str.str().c_str(), "OpenGL Error!", MB_ICONERROR | MB_OK);
}*/
}
Crosshair::Crosshair(GameEngine *engine) :
Object(engine),
m_buffer(0),
m_textureCoords(0),
m_texture(0)
{
GLfloat vertices[] =
{16.0f, 16.0f, 0.0f,
-16.0f, 16.0f, 0.0f,
-16.0f, -16.0f, 0.0f,
16.0f, -16.0f, 0.0f};
GLushort textureCoords[] =
{1, 1,
0, 1,
0, 0,
1, 0};
glGenBuffers(1, &m_buffer);
glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), &vertices, GL_STATIC_DRAW);
glGenBuffers(1, &m_textureCoords);
glBindBuffer(GL_ARRAY_BUFFER, m_textureCoords);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLushort), &textureCoords, GL_STATIC_DRAW);
Utils::loadTexture("crosshair.tga", m_texture);
}
void Crosshair::onRender()
{
Object::onRender();
glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_textureCoords);
glVertexAttribPointer(1, 2, GL_SHORT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(1);
ShaderProgram *shaderProgram = m_engine->m_hudProgram;
shaderProgram->bind();
shaderProgram->sendMatrices();
shaderProgram->sendUniform("texture0", 0);
glBindTexture(GL_TEXTURE_2D, m_texture);
glDrawArrays(GL_QUADS, 0, 4);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
}