-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
227 lines (174 loc) · 6.75 KB
/
main.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
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
/*
* main.cpp
*
* Created on: 2014/10/18
* Author: Dimitri Kourkoulis
* http://dimitros.be
* License: BSD 3-Clause License (see LICENSE file)
*/
// Without undefining strict ANSI, compilation in MinGW fails when C++11 is enabled
#ifdef __MINGW32__
#ifdef __STRICT_ANSI__
#undef __STRICT_ANSI__
#endif
#endif
#include "google/gtest/gtest.h"
#include "Logger.hpp"
#include <iostream>
#include <memory>
#include <sstream>
#include "Image.hpp"
#include "Model.hpp"
#include "BoundingBoxes.hpp"
#include "SceneObject.hpp"
#include "ModelLoader.hpp"
#include "WavefrontLoader.hpp"
#include "Sound.hpp"
/* MinGW produces the following linking error, if the unit tests
* are linked to the renderer:
* undefined reference to `SDL_SetMainReady'
* This started occurring when GLEW was removed from the small3d block
* and placed in an independent block. It probably has something to do
* with the order in which the SDL libraries are linked (see
* http://www.cplusplus.com/forum/beginner/110753/). It does not occur
* in the sample game, only in these unit tests, when they are built
* under MinGW.
*/
#ifndef __MINGW32__
#include "Renderer.hpp"
#endif
/* bii data directives */
// bii://dimitrikourk/small3d/resources/images/testImage.png
// bii://dimitrikourk/small3d/resources/models/Cube/CubeNoTexture.obj
// bii://dimitrikourk/small3d/resources/models/Cube/Cube.obj
// bii://dimitrikourk/small3d/samplegame/resources/models/GoatBB/GoatBB.obj
// bii://dimitrikourk/small3d/resources/models/UnspecifiedAnimal/UnspecifiedAnimalWithTexture.obj
// bii://dimitrikourk/small3d/resources/models/UnspecifiedAnimal/UnspecifiedAnimalWithTextureRedBlackNumbers.png
// bii://dimitrikourk/small3d/samplegame/resources/sounds/bah.ogg
using namespace small3d;
using namespace std;
TEST(LoggerTest, LogSomething) {
deleteLogger();
ostringstream oss;
initLogger(oss);
LOGINFO("It works");
EXPECT_TRUE(oss.str().find("It works") != (string::npos));
LOGERROR("Error test");
EXPECT_TRUE(oss.str().find("Error test") != (string::npos));
deleteLogger();
}
TEST(ImageTest, LoadImage) {
unique_ptr<Image> image(new Image("dimitrikourk/small3d/resources/images/testImage.png"));
cout << "Image width " << image->getWidth() << ", height " << image->getHeight() << endl;
float *imageData = image->getData();
int x = 0, y = 0;
while (y < image->getHeight()) {
x = 0;
while (x < image->getWidth()) {
float *colour = &imageData[4 * y * image->getWidth() + 4 * x];
EXPECT_GE(colour[0], 0.0f);
EXPECT_LE(colour[0], 1.0f);
EXPECT_GE(colour[1], 0.0f);
EXPECT_LE(colour[1], 1.0f);
EXPECT_GE(colour[2], 0.0f);
EXPECT_LE(colour[2], 1.0f);
EXPECT_EQ(1.0f, colour[3]);
// Uncomment the following to actually see RGB values for each test image pixel
// cout << "At (" << x << ", " << y << ") R: " << setprecision( 2 ) << colour[0] << endl;
// cout << "At (" << x << ", " << y << ") G: " << setprecision( 2 ) << colour[1] << endl;
// cout << "At (" << x << ", " << y << ") B: " << setprecision( 2 ) << colour[2] << endl;
++x;
}
++y;
}
}
TEST(ModelTest, LoadModel) {
Model model;
unique_ptr<ModelLoader> loader(new WavefrontLoader());
loader->load("dimitrikourk/small3d/resources/models/Cube/Cube.obj", model);
EXPECT_NE(0, model.vertexData.size());
EXPECT_NE(0, model.indexData.size());
EXPECT_NE(0, model.normalsData.size());
EXPECT_NE(0, model.textureCoordsData.size());
cout << "Vertex data component count: "
<< model.vertexData.size() << endl << "Index count: "
<< model.indexData.size() << endl
<< "Normals data component count: "
<< model.normalsData.size() << endl
<< "Texture coordinates count: "
<< model.textureCoordsData.size() << endl;
Model modelWithNoTexture;
loader->load("dimitrikourk/small3d/resources/models/Cube/CubeNoTexture.obj", modelWithNoTexture);
EXPECT_NE(0, modelWithNoTexture.vertexData.size());
EXPECT_NE(0, modelWithNoTexture.indexData.size());
EXPECT_NE(0, modelWithNoTexture.normalsData.size());
EXPECT_EQ(0, modelWithNoTexture.textureCoordsData.size());
cout << "Vertex data component count: "
<< modelWithNoTexture.vertexData.size() << endl << "Index count: "
<< modelWithNoTexture.indexData.size() << endl
<< "Normals data component count: "
<< modelWithNoTexture.normalsData.size() << endl
<< "Texture coordinates count: "
<< modelWithNoTexture.textureCoordsData.size() << endl;
}
TEST(BoundingBoxesTest, LoadBoundingBoxes) {
unique_ptr<BoundingBoxes> bboxes(new BoundingBoxes());
bboxes->loadFromFile("dimitrikourk/small3d/samplegame/resources/models/GoatBB/GoatBB.obj");
EXPECT_EQ(16, bboxes->vertices.size());
EXPECT_EQ(12, bboxes->facesVertexIndexes.size());
cout << "Bounding boxes vertices: " << endl;
for (int idx = 0; idx < 16; idx++) {
cout << bboxes->vertices[idx][0] << ", " <<
bboxes->vertices[idx][1] << ", " <<
bboxes->vertices[idx][2] << ", " << endl;
}
cout << "Bounding boxes faces vertex indexes: " << endl;
for (int idx = 0; idx < 12; idx++) {
cout << bboxes->facesVertexIndexes[idx][0] << ", " <<
bboxes->facesVertexIndexes[idx][1] << ", " <<
bboxes->facesVertexIndexes[idx][2] << ", " <<
bboxes->facesVertexIndexes[idx][3] << ", " << endl;
}
bboxes->offset.x = 0.0f;
bboxes->offset.y = 0.1f;
bboxes->offset.z = 0.1f;
bboxes->rotation = glm::vec3(0.0f, 0.0f, 0.0f);
EXPECT_FALSE(bboxes->pointIsWithin(0.1f, 0.1f, 0.1f));
}
/*
//This cannot run on the CI environment because there is no video device available there.
// Cannot run this with MinGW (see comment above Renderer.h include directive)
#ifndef __MINGW32__
TEST(RendererTest, StartAndUse) {
shared_ptr<Logger> log(new Logger(cout));
shared_ptr<Configuration> cfg(new Configuration(log));
shared_ptr<SceneObject> object(
new SceneObject("animal",
"dimitrikourk/small3d/resources/models/UnspecifiedAnimal/UnspecifiedAnimalWithTexture.obj",
cfg, log, 1, "dimitrikourk/small3d/resources/models/UnspecifiedAnimal/UnspecifiedAnimalWithTextureRedBlackNumbers.png"));
shared_ptr<vector<shared_ptr<SceneObject> > > scene(
new vector<shared_ptr<SceneObject> >());
scene->push_back(object);
unique_ptr<Renderer> renderer(new Renderer(cfg, log));
renderer->init(640, 480, false);
}
#endif
*/
TEST(SoundTest, Load) {
shared_ptr<Sound> snd(new Sound());
snd->load("dimitrikourk/small3d/samplegame/resources/sounds/bah.ogg", "bah");
//snd->play("bah");
//Pa_Sleep(3*1000);
}
int main(int argc, char **argv) {
// Set up a console, if using MinGW
// This is because the mwindows linker flag,
// used by blocks referenced by small3d,
// eliminates the default one.
#ifdef __MINGW32__
AllocConsole();
freopen("CONOUT$", "w", stdout);
#endif
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}