/
Models.cpp
333 lines (265 loc) · 15.7 KB
/
Models.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
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
#include "RadiantTest.h"
#include "imodelsurface.h"
#include "imodelcache.h"
namespace test
{
using ModelTest = RadiantTest;
using AseImportTest = ModelTest;
TEST_F(ModelTest, LwoPolyCount)
{
auto model = GlobalModelCache().getModel("models/darkmod/test/unit_cube.lwo");
EXPECT_TRUE(model);
// The unit cube should have 12 polys (6 quads = 12 tris)
EXPECT_EQ(model->getPolyCount(), 12);
}
TEST_F(ModelTest, AsePolyCount)
{
auto model = GlobalModelCache().getModel("models/darkmod/test/unit_cube.ase");
EXPECT_TRUE(model);
// The unit cube should have 12 polys (6 quads = 12 tris)
EXPECT_EQ(model->getPolyCount(), 12);
}
// #4644: If the *BITMAP material cannot be resolved, the code should not fall back to *MATERIAL_NAME (in TDM/idTech4)
TEST_F(AseImportTest, BitmapFieldPreferredOverMaterialName)
{
auto model = GlobalModelCache().getModel("models/missing_texture.ase");
EXPECT_TRUE(model);
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getDefaultMaterial(), "textures/doesnt_exist");
}
TEST_F(AseImportTest, MaterialNameIsExtractedAndNormalised)
{
auto model = GlobalModelCache().getModel("models/ase/tiles_with_shared_vertex_and_colour.ase");
EXPECT_TRUE(model);
// *BITMAP "\\base\textures\common\clip" is converted to the normalised name
EXPECT_EQ(model->getSurface(0).getDefaultMaterial(), "textures/common/clip");
}
TEST_F(AseImportTest, MaterialAndSurfaceCount)
{
auto model = GlobalModelCache().getModel("models/ase/tiles_two_materials.ase");
EXPECT_TRUE(model);
// This model has several *GEOMOBJECTs, but only two distinct materials => two surfaces
EXPECT_EQ(model->getSurfaceCount(), 2);
EXPECT_EQ(model->getSurface(0).getDefaultMaterial(), "material");
EXPECT_EQ(model->getSurface(1).getDefaultMaterial(), "tork");
// Only 1 material, only 1 surface
model = GlobalModelCache().getModel("models/ase/tiles_with_shared_vertex_and_colour.ase");
EXPECT_TRUE(model);
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getDefaultMaterial(), "textures/common/clip");
}
TEST_F(AseImportTest, VertexAndTriangleCount)
{
auto model = GlobalModelCache().getModel("models/ase/single_triangle.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 3);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 1);
// Model has 8 vertices, but they have different normals/UVs due to being shared among the sides
// so these 8 prototypes should be all instantiated to a total of 24 vertices
model = GlobalModelCache().getModel("models/ase/testcube.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 24);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 12);
model = GlobalModelCache().getModel("models/ase/testsphere.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 962);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 960);
// Model has 6 separated quad objects (total: 12 tris), 1 material, no dupes
model = GlobalModelCache().getModel("models/ase/exploded_cube.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 24);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 12);
model = GlobalModelCache().getModel("models/ase/separated_tiles.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 16);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 8);
model = GlobalModelCache().getModel("models/ase/tiles_with_shared_vertex.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 13);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 8);
model = GlobalModelCache().getModel("models/ase/tiles_with_shared_vertex_and_colour.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 13);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 8);
model = GlobalModelCache().getModel("models/ase/tiles.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 4);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 2);
model = GlobalModelCache().getModel("models/ase/tiles_two_materials.ase");
EXPECT_EQ(model->getSurfaceCount(), 2);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 8);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 4);
EXPECT_EQ(model->getSurface(1).getNumVertices(), 8);
EXPECT_EQ(model->getSurface(1).getNumTriangles(), 4);
model = GlobalModelCache().getModel("models/ase/merged_cube.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 24);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 12);
model = GlobalModelCache().getModel("models/ase/gauge_needle.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_EQ(model->getSurface(0).getNumVertices(), 14);
EXPECT_EQ(model->getSurface(0).getNumTriangles(), 11);
}
TEST_F(AseImportTest, TriangleWindingCW)
{
// Triangle windings in DR need to be CW, whereas ASE stores them in CCW order
// This test assumes that the model indices are translated correctly to have CW windings
auto model = GlobalModelCache().getModel("models/ase/single_triangle.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_NO_THROW(dynamic_cast<const model::IIndexedModelSurface&>(model->getSurface(0)));
const auto& surface = static_cast<const model::IIndexedModelSurface&>(model->getSurface(0));
const auto& vertices = surface.getVertexArray();
const auto& indices = surface.getIndexArray();
EXPECT_EQ(vertices.size(), 3);
EXPECT_EQ(indices.size(), 3);
// Take the cross-product of the first two vectors of the winding
const auto& a = vertices[indices[0]].vertex;
const auto& b = vertices[indices[1]].vertex;
const auto& c = vertices[indices[2]].vertex;
auto normal = (b - a).crossProduct(c - b).getNormalised();
// We know the triangle in the ASE file is facing upwards,
// For CW order, the cross-product will point in the opposite direction
// of the normal, i.e. downwards
EXPECT_NEAR(normal.z(), -1.0, 1e-4);
}
TEST_F(AseImportTest, UVOffsetKeyword)
{
// Test Cube doesn't have any offset
auto model = GlobalModelCache().getModel("models/ase/testcube.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_NEAR(model->getSurface(0).getVertex(0).texcoord.x(), 0, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(0).texcoord.y(), 1, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(1).texcoord.x(), 0, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(1).texcoord.y(), 0, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(2).texcoord.x(), 1, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(2).texcoord.y(), 1, 1e-5);
// Model has UVW_U_OFFSET == 0.5 and UVW_V_OFFSET == 0.3
model = GlobalModelCache().getModel("models/ase/testcube_uv_offset.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
// U_OFFSET is negated and applied => -0.5
EXPECT_NEAR(model->getSurface(0).getVertex(0).texcoord.x(), -0.5, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(0).texcoord.y(), 1.3, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(1).texcoord.x(), -0.5, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(1).texcoord.y(), 0.3, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(2).texcoord.x(), 0.5, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(2).texcoord.y(), 1.3, 1e-5);
}
TEST_F(AseImportTest, UVTilingKeyword)
{
// Default testcube.ase is already tested in the UVOffsetKeyword test
// Model has UVW_U_TILING == 2 and UVW_V_TILING == 3
auto model = GlobalModelCache().getModel("models/ase/testcube_uv_tiling.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
EXPECT_NEAR(model->getSurface(0).getVertex(0).texcoord.x(), 0, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(0).texcoord.y(), 3, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(1).texcoord.x(), 0, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(1).texcoord.y(), 0, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(2).texcoord.x(), 2, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(2).texcoord.y(), 3, 1e-5);
}
TEST_F(AseImportTest, UVAngleKeyword)
{
// Default testcube.ase is already tested in the UVOffsetKeyword test
// Model has UVW_ANGLE == 1.570796 (pi/2)
auto model = GlobalModelCache().getModel("models/ase/testcube_uv_angle.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
auto sinValue = sin(math::PI / 2);
auto cosValue = cos(math::PI / 2);
EXPECT_NEAR(model->getSurface(0).getVertex(0).texcoord.x(), 0 * cosValue + 1 * sinValue, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(0).texcoord.y(), 0 * -sinValue + 1 * cosValue, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(1).texcoord.x(), 0 * cosValue + 0 * sinValue, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(1).texcoord.y(), 0 * -sinValue + 0 * cosValue, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(2).texcoord.x(), 1 * cosValue + 1 * sinValue, 1e-5);
EXPECT_NEAR(model->getSurface(0).getVertex(2).texcoord.y(), 1 * -sinValue + 1 * cosValue, 1e-5);
}
bool surfaceHasVertexWith(const model::IModelSurface& surface,
const std::function<bool(const ArbitraryMeshVertex& vertex)>& predicate)
{
bool found = false;
for (auto i = 0; i < surface.getNumVertices(); ++i)
{
if (predicate(surface.getVertex(i)))
{
found = true;
break;
}
}
return found;
}
void expectVertexWithNormal(const model::IModelSurface& surface, const Vertex3f& vertex, const Normal3f& normal)
{
EXPECT_TRUE(surfaceHasVertexWith(surface, [&](const ArbitraryMeshVertex& v)->bool
{
return v.vertex.isEqual(vertex, 1e-5) && v.normal.isEqual(normal, 1e-5);
})) << "Could not find a vertex with xyz = " << vertex << " and normal " << normal;
}
TEST_F(AseImportTest, VertexNormals)
{
auto model = GlobalModelCache().getModel("models/ase/testcube.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
// Check for a few specific vertex/normal combinations
expectVertexWithNormal(model->getSurface(0), Vertex3f(-16, -16, 16), Normal3f(-1, 0, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-16, -16, -16), Normal3f(-1, 0, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-16, 16, -16), Normal3f(-1, 0, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-16, 16, 16), Normal3f(0, 1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-16, 16, -16), Normal3f(0, 1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(16, 16, -16), Normal3f(0, 1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(16, 16, 16), Normal3f(1, 0, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(16, 16, -16), Normal3f(1, 0, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(16, -16, -16), Normal3f(1, 0, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(16, -16, 16), Normal3f(0, -1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(16, -16, -16), Normal3f(0, -1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-16, -16, -16), Normal3f(0, -1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(16, 16, -16), Normal3f(0, 0, -1));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-16, 16, -16), Normal3f(0, 0, -1));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-16, -16, -16), Normal3f(0, 0, -1));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-16, 16, 16), Normal3f(0, 0, 1));
expectVertexWithNormal(model->getSurface(0), Vertex3f(16, 16, 16), Normal3f(0, 0, 1));
expectVertexWithNormal(model->getSurface(0), Vertex3f(16, -16, 16), Normal3f(0, 0, 1));
}
void expectVertexWithColour(const model::IModelSurface& surface, const Vertex3f& vertex, const Vector3& colour)
{
EXPECT_TRUE(surfaceHasVertexWith(surface, [&](const ArbitraryMeshVertex& v)->bool
{
return v.vertex.isEqual(vertex, 1e-5) && v.colour.isEqual(colour, 1e-5);
})) << "Could not find a vertex with xyz = " << vertex << " and colour " << colour;
}
TEST_F(AseImportTest, VertexColours)
{
auto model = GlobalModelCache().getModel("models/ase/tiles_with_shared_vertex_and_colour.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
// Check for a few specific vertex/colour combinations
expectVertexWithColour(model->getSurface(0), Vertex3f(56, 56, 2), Vector3(0, 0, 0));
expectVertexWithColour(model->getSurface(0), Vertex3f(56, 18, 2), Vector3(0, 0, 0));
expectVertexWithColour(model->getSurface(0), Vertex3f(19, 18, 2), Vector3(0.9882, 0.9882, 0.9882));
expectVertexWithColour(model->getSurface(0), Vertex3f(19, 56, 2), Vector3(1, 1, 1));
expectVertexWithColour(model->getSurface(0), Vertex3f(-19, -19, 2), Vector3(0, 0, 0));
expectVertexWithColour(model->getSurface(0), Vertex3f(19, -19, 2), Vector3(0, 0, 0));
expectVertexWithColour(model->getSurface(0), Vertex3f(-19, 56, 2), Vector3(0, 0, 0));
expectVertexWithColour(model->getSurface(0), Vertex3f(19, 56, 2), Vector3(1, 1, 1));
expectVertexWithColour(model->getSurface(0), Vertex3f(-19, 18, 2), Vector3(0.9216, 0.9216, 0.9216));
expectVertexWithColour(model->getSurface(0), Vertex3f(56, -19, 2), Vector3(0.7373, 0.7373, 0.7373));
expectVertexWithColour(model->getSurface(0), Vertex3f(19, -19, 2), Vector3(0, 0, 0));
}
// Tests the NODE_TM transform application to vertex normals
TEST_F(AseImportTest, VertexNormalTransformation)
{
auto model = GlobalModelCache().getModel("models/ase/gauge_needle.ase");
EXPECT_EQ(model->getSurfaceCount(), 1);
// Check for a few specific vertex/colour combinations (values taken directly from the TDM parse result)
expectVertexWithNormal(model->getSurface(0), Vertex3f(-0.140799999, -0.745599985, 0.125799999), Normal3f(0, -1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(0.188199997, -0.745599985, 0.125900000), Normal3f(0, -1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-0.0364999995, -0.745599985, 0.0203000009), Normal3f(0, -1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(0.0839999989, -0.745599985, 0.0203000009), Normal3f(0, -1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-0.140900001, -0.745599985, 0.331900001), Normal3f(0.000499708927, -0.999999583, 0.000800181471));
expectVertexWithNormal(model->getSurface(0), Vertex3f(0.188199997, -0.745599985, 0.331999987), Normal3f(-3.27272573e-07, -0.999999583, 0.000899999577));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-0.0218000002, -0.744899988, 2.23850012), Normal3f(0.0139053408, -0.999789357, -0.0150947841));
expectVertexWithNormal(model->getSurface(0), Vertex3f(0.0679000020, -0.742100000, 2.23850012), Normal3f(0.0298263635, -0.996775806, -0.0744873509));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-0.133800000, -0.745599985, 2.29660010), Normal3f(1.23635652e-06, -0.999994278, -0.00339998049));
expectVertexWithNormal(model->getSurface(0), Vertex3f(0.179800004, -0.745599985, 2.29670000), Normal3f(0.00370575022, -0.999861956, -0.0161980372));
expectVertexWithNormal(model->getSurface(0), Vertex3f(0.0229000002, -0.745599985, 2.69810009), Normal3f(0, -1, 0));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-0.0218000002, -0.744899988, 2.23850012), Normal3f(-0.869799972, 0.00000000, -0.493404597));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-0.133800000, -0.745599985, 2.29660010), Normal3f(-0.460478902, 0.00000000, -0.887670696));
expectVertexWithNormal(model->getSurface(0), Vertex3f(-0.140900001, -0.745599985, 0.331900001), Normal3f(-0.998054981, -0.00000000, 0.0623391047));
}
}