-
Notifications
You must be signed in to change notification settings - Fork 5
/
SceneObject.cpp
177 lines (141 loc) · 4.74 KB
/
SceneObject.cpp
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
/*
* SceneObject.cpp
*
* Created on: 2014/10/19
* Author: Dimitri Kourkoulis
* http://dimitros.be
* License: BSD 3-Clause License (see LICENSE file)
*/
#include "SceneObject.hpp"
#include <sstream>
#include <iomanip>
#include "Exception.hpp"
#include "ModelLoader.hpp"
#include "WavefrontLoader.hpp"
#include <memory>
using namespace std;
namespace small3d {
void SceneObject::initPropVectors() {
this->colour = shared_ptr<glm::vec4>(new glm::vec4(0, 0, 0, 0));
this->offset = shared_ptr<glm::vec3>(new glm::vec3(0, 0, 0));
this->rotation = shared_ptr<glm::vec3>(new glm::vec3(0, 0, 0));
}
SceneObject::SceneObject(const string &name, const string &modelPath,
const int &numFrames, const string &texturePath,
const string &boundingBoxesPath) {
initLogger();
this->name = name;
animating = false;
framesWaited = 0;
frameDelay = 1;
currentFrame = 0;
this->numFrames = numFrames;
unique_ptr<ModelLoader> loader(new WavefrontLoader());
if (numFrames > 1) {
LOGINFO("Loading " + name + " animated model (this may take a while):");
for (int idx = 0; idx < numFrames; ++idx) {
stringstream lss;
lss << "Frame " << idx + 1 << " of " << numFrames << "...";
LOGINFO(lss.str());
stringstream ss;
ss << setfill('0') << setw(6) << idx + 1;
string frameNum = ss.str();
Model model1;
loader->load(modelPath + "_" + frameNum + ".obj", model1);
model.push_back(model1);
}
}
else {
Model model1;
loader->load(modelPath, model1);
model.push_back(model1);
}
if (texturePath != "") {
this->texture = shared_ptr<Image>(
new Image(texturePath));
}
this->initPropVectors();
if (boundingBoxesPath != "") {
boundingBoxes = shared_ptr<BoundingBoxes>(new BoundingBoxes());
boundingBoxes->loadFromFile(boundingBoxesPath);
}
}
SceneObject::~SceneObject() {
}
Model &SceneObject::getModel() {
return model[currentFrame];
}
const shared_ptr<Image> &SceneObject::getTexture() const {
return texture;
}
const string SceneObject::getName() {
return name;
}
const shared_ptr<glm::vec4> &SceneObject::getColour() {
return colour;
}
void SceneObject::setColour(const float &r, const float &g, const float &b, const float &a) {
colour = shared_ptr<glm::vec4>(new glm::vec4(r, g, b, a));
}
const shared_ptr<glm::vec3> &SceneObject::getOffset() const {
return offset;
}
void SceneObject::setOffset(const float &x, const float &y, const float &z) {
offset = shared_ptr<glm::vec3>(new glm::vec3(x, y, z));
}
const shared_ptr<glm::vec3> &SceneObject::getRotation() const {
return rotation;
}
void SceneObject::setRotation(const float &x, const float &y,
const float &z) {
rotation = shared_ptr<glm::vec3>(new glm::vec3(x, y, z));
}
void SceneObject::startAnimating() {
animating = true;
}
void SceneObject::stopAnimating() {
animating = false;
}
void SceneObject::resetAnimation() {
currentFrame = 0;
}
void SceneObject::setFrameDelay(const int &delay) {
this->frameDelay = delay;
}
void SceneObject::animate() {
if (animating) {
++framesWaited;
if (framesWaited == frameDelay) {
framesWaited = 0;
++currentFrame;
if (currentFrame == numFrames) {
currentFrame = 0;
}
}
}
}
bool SceneObject::collidesWithPoint(float x, float y, float z) {
if (boundingBoxes == NULL) {
throw Exception("No bounding boxes have been provided for " + name + ", so collision detection is not enabled.");
}
boundingBoxes->offset = *this->offset;
boundingBoxes->rotation = *this->rotation;
return boundingBoxes->pointIsWithin(x, y, z);
}
bool SceneObject::collidesWithSceneObject(shared_ptr<SceneObject> otherObject) {
if (boundingBoxes == NULL) {
throw Exception("No bounding boxes have been provided for " + name + ", so collision detection is not enabled.");
}
if (otherObject->boundingBoxes == NULL) {
throw Exception(
"No bounding boxes have been provided for " + otherObject->name + ", so collision detection is not enabled.");
}
boundingBoxes->offset = *offset;
boundingBoxes->rotation = *rotation;
otherObject->boundingBoxes->offset = *otherObject->offset;
otherObject->boundingBoxes->rotation = *otherObject->rotation;
// Checking whether the boxes of this object are within the boxes of the other object or vice versa
return boundingBoxes->boxesAreWithin(otherObject->boundingBoxes) ||
otherObject->boundingBoxes->boxesAreWithin(boundingBoxes);
}
}