-
Notifications
You must be signed in to change notification settings - Fork 47
/
MaterialExport.cpp
177 lines (127 loc) · 6.16 KB
/
MaterialExport.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
#include "RadiantTest.h"
#include "ishaders.h"
#include "string/trim.h"
#include "materials/ParseLib.h"
#include <fmt/format.h>
namespace test
{
using MaterialExportTest = RadiantTest;
inline void expectDefinitionContains(const MaterialPtr& material, const std::string& expectedContainedString)
{
EXPECT_NE(material->getDefinition().find(expectedContainedString), std::string::npos)
<< "Material definition doesn't contain " << expectedContainedString << " as expected.\n"
<< "Definition was: \n" << material->getDefinition();
}
inline void expectDefinitionDoesNotContain(const MaterialPtr& material, const std::string& expectedContainedString)
{
EXPECT_EQ(material->getDefinition().find(expectedContainedString), std::string::npos)
<< "Material definition contains " << expectedContainedString << " but that shouldn't be the case.\n"
<< "Definition was: \n" << material->getDefinition();
}
TEST_F(MaterialExportTest, Description)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/empty");
EXPECT_EQ(string::trim_copy(material->getDefinition()), "");
constexpr auto description = "testdescription, with commas, spaces and extra";
material->setDescription(description);
expectDefinitionContains(material, fmt::format("description \"{0}\"", description));
constexpr auto doubleQuoted = "testdescription with \"quotes\"";
constexpr auto singleQuoted = "testdescription with 'quotes'";
material->setDescription(doubleQuoted);
expectDefinitionContains(material, fmt::format("description \"{0}\"", singleQuoted));
}
TEST_F(MaterialExportTest, PolygonOffset)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/empty");
EXPECT_EQ(string::trim_copy(material->getDefinition()), "");
material->setPolygonOffset(0.0);
expectDefinitionContains(material, fmt::format("polygonOffset {0}", 0.0));
material->setPolygonOffset(-1.5);
expectDefinitionContains(material, fmt::format("polygonOffset {0}", -1.5));
material->setPolygonOffset(+1.5);
expectDefinitionContains(material, fmt::format("polygonOffset {0}", 1.5));
material->clearMaterialFlag(Material::FLAG_POLYGONOFFSET);
expectDefinitionDoesNotContain(material, "polygonOffset");
}
TEST_F(MaterialExportTest, SurfaceType)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/empty");
EXPECT_EQ(string::trim_copy(material->getDefinition()), "");
for (const auto& pair : shaders::SurfaceTypeMapping)
{
material->setSurfaceType(pair.second);
expectDefinitionContains(material, pair.first);
}
// Test resetting the surface type to default which should clear the type
auto lastSurfaceType = shaders::getStringForSurfaceType(material->getSurfaceType());
EXPECT_NE(lastSurfaceType, std::string());
material->setSurfaceType(Material::SURFTYPE_DEFAULT);
expectDefinitionDoesNotContain(material, "lastSurfaceType");
}
TEST_F(MaterialExportTest, MaterialFlags)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/empty");
EXPECT_EQ(string::trim_copy(material->getDefinition()), "");
for (const auto& pair : shaders::MaterialFlagKeywords)
{
material->setMaterialFlag(pair.second);
expectDefinitionContains(material, pair.first);
material->clearMaterialFlag(pair.second);
expectDefinitionDoesNotContain(material, pair.first);
}
}
TEST_F(MaterialExportTest, ClampType)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/empty");
EXPECT_EQ(string::trim_copy(material->getDefinition()), "");
material->setClampType(CLAMP_NOREPEAT);
expectDefinitionContains(material, "clamp");
material->setClampType(CLAMP_ZEROCLAMP);
expectDefinitionContains(material, "zeroclamp");
material->setClampType(CLAMP_ALPHAZEROCLAMP);
expectDefinitionContains(material, "alphazeroclamp");
material->setClampType(CLAMP_REPEAT); // this is the default => no keyword necessary
expectDefinitionDoesNotContain(material, "clamp");
expectDefinitionDoesNotContain(material, "zeroclamp");
expectDefinitionDoesNotContain(material, "alphazeroclamp");
}
TEST_F(MaterialExportTest, CullType)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/empty");
EXPECT_EQ(string::trim_copy(material->getDefinition()), "");
material->setCullType(Material::CULL_FRONT);
expectDefinitionContains(material, "backsided");
material->setCullType(Material::CULL_NONE);
expectDefinitionContains(material, "twosided");
material->setCullType(Material::CULL_BACK); // This is the default
expectDefinitionDoesNotContain(material, "twosided");
expectDefinitionDoesNotContain(material, "backsided");
}
TEST_F(MaterialExportTest, GuiSurf)
{
auto material = GlobalMaterialManager().getMaterial("textures/exporttest/guisurf1");
expectDefinitionContains(material, "guiSurf\tguis/lvlmaps/genericmap.gui");
// Mark the definition as modified by setting the description
material->setDescription("-");
material->setDescription("");
expectDefinitionContains(material, "guisurf guis/lvlmaps/genericmap.gui");
material = GlobalMaterialManager().getMaterial("textures/exporttest/guisurf2");
expectDefinitionContains(material, "guiSurf\tentity");
// Mark the definition as modified by setting the description
material->setDescription("-");
material->setDescription("");
expectDefinitionContains(material, "guisurf entity");
material = GlobalMaterialManager().getMaterial("textures/exporttest/guisurf3");
expectDefinitionContains(material, "guiSurf\tentity2");
// Mark the definition as modified by setting the description
material->setDescription("-");
material->setDescription("");
expectDefinitionContains(material, "guisurf entity2");
material = GlobalMaterialManager().getMaterial("textures/exporttest/guisurf4");
expectDefinitionContains(material, "guiSurf\tentity3");
// Mark the definition as modified by setting the description
material->setDescription("-");
material->setDescription("");
expectDefinitionContains(material, "guisurf entity3");
}
}