-
Notifications
You must be signed in to change notification settings - Fork 47
/
BrushDef.cpp
287 lines (228 loc) · 8.58 KB
/
BrushDef.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
// greebo: Specialise the float casting function
#define SPECIALISE_STR_TO_FLOAT
#include "BrushDef.h"
#include "string/convert.h"
#include "imap.h"
#include "ibrush.h"
#include "parser/DefTokeniser.h"
#include "math/Matrix4.h"
#include "math/Plane3.h"
#include "shaderlib.h"
#include "i18n.h"
#include <fmt/format.h>
namespace map
{
const std::string& BrushDefParser::getKeyword() const
{
static std::string _keyword("brushDef");
return _keyword;
}
/*
// Example Primitive:
{
brushDef
{
( -1216 -464 232 ) ( -1088 -464 232 ) ( -1088 -80 120 ) ( ( 0.031250 0 14 ) ( -0.000009 0.031250 4.471550 ) ) common/caulk 134217728 4 0
( -1088 -464 248 ) ( -1216 -464 248 ) ( -1216 -80 136 ) ( ( 0 -0.031373 -0.147059 ) ( 0.007812 0 0.049020 ) ) common/caulk 134217728 0 0
( -1088 -560 120 ) ( -1088 -560 136 ) ( -1088 -80 136 ) ( ( 0.031250 0 16.500000 ) ( 0 0.031250 0.250000 ) ) common/caulk 134217728 4 0
( -1088 -80 136 ) ( -1216 -80 136 ) ( -1216 -80 8 ) ( ( 0.031250 0 2 ) ( 0 0.031250 0.250000 ) ) common/caulk 134217728 4 0
( -1216 -400 136 ) ( -1216 -400 120 ) ( -1216 -80 120 ) ( ( 0.031250 0 -16.500000 ) ( 0 0.031250 0.250000 ) ) common/caulk 134217728 4 0
( -1088 -464 232 ) ( -1216 -464 232 ) ( -1216 -464 248 ) ( ( 0.031250 0 -2 ) ( 0 0.031250 0.250000 ) ) common/caulk 134217728 4 0
}
}
*/
scene::INodePtr BrushDefParser::parse(parser::DefTokeniser& tok) const
{
// Create a new brush
scene::INodePtr node = GlobalBrushCreator().createBrush();
// Cast the node, this must succeed
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(node);
assert(brushNode != NULL);
IBrush& brush = brushNode->getIBrush();
tok.assertNextToken("{");
// Parse face tokens until a closing brace is encountered
while (1)
{
std::string token = tok.nextToken();
// Token should be either a "(" (start of face) or "}" (end of brush)
if (token == "}")
{
break; // end of brush
}
else if (token == "(") // FACE
{
// Parse three 3D points to construct a plane
double x = string::to_float(tok.nextToken());
double y = string::to_float(tok.nextToken());
double z = string::to_float(tok.nextToken());
Vector3 p1(x, y, z);
tok.assertNextToken(")");
tok.assertNextToken("(");
x = string::to_float(tok.nextToken());
y = string::to_float(tok.nextToken());
z = string::to_float(tok.nextToken());
Vector3 p2(x, y, z);
tok.assertNextToken(")");
tok.assertNextToken("(");
x = string::to_float(tok.nextToken());
y = string::to_float(tok.nextToken());
z = string::to_float(tok.nextToken());
Vector3 p3(x, y, z);
tok.assertNextToken(")");
// Construct the plane from the three points
Plane3 plane(p3, p2, p1);
// Parse TexDef
Matrix4 texdef;
tok.assertNextToken("(");
tok.assertNextToken("(");
texdef.xx() = string::to_float(tok.nextToken());
texdef.yx() = string::to_float(tok.nextToken());
texdef.tx() = string::to_float(tok.nextToken());
tok.assertNextToken(")");
tok.assertNextToken("(");
texdef.xy() = string::to_float(tok.nextToken());
texdef.yy() = string::to_float(tok.nextToken());
texdef.ty() = string::to_float(tok.nextToken());
tok.assertNextToken(")");
tok.assertNextToken(")");
// Parse Shader, brushDef has an implicit "textures/" not written to the map
std::string shader = GlobalTexturePrefix_get() + tok.nextToken();
// Parse Flags (usually each brush has all faces detail or all faces structural)
IBrush::DetailFlag flag = static_cast<IBrush::DetailFlag>(
string::convert<std::size_t>(tok.nextToken(), IBrush::Structural));
brush.setDetailFlag(flag);
// Ignore the other two flags
tok.skipTokens(2);
// Finally, add the new face to the brush
/*IFace& face = */brush.addFace(plane, texdef, shader);
}
else
{
std::string text = fmt::format(_("BrushDefParser: invalid token '{0}'"), token);
throw parser::ParseException(text);
}
}
// Final outer "}"
tok.assertNextToken("}");
return node;
}
// Legacy brushDef format
const std::string& LegacyBrushDefParser::getKeyword() const
{
static std::string _keyword("("); // The Q3 map reader will just encounter this opening brace
return _keyword;
}
/*
Example Primitive:
// brush 0
{
( 2224 3952 0 ) ( 1424 3952 0 ) ( 1424 3808 0 ) common/caulk 192 192 0 0.125000 0.125000 0 4 0
( 1424 3808 8 ) ( 1424 3952 8 ) ( 2224 3952 8 ) common/caulk 0 0 0 0.500000 0.500000 0 0 0
( 1416 3776 8 ) ( 2216 3776 8 ) ( 2216 3776 0 ) common/caulk 192 64 0 0.125000 0.125000 0 4 0
( 2944 3808 8 ) ( 2944 3952 8 ) ( 2944 3952 0 ) common/caulk -192 64 0 0.125000 0.125000 0 4 0
( 2224 3968 8 ) ( 1424 3968 8 ) ( 1424 3968 0 ) common/caulk 192 64 0 0.125000 0.125000 0 4 0
( 1340 3952 8 ) ( 1340 3808 8 ) ( 1340 3808 0 ) common/caulk -192 64 0 0.125000 0.125000 0 4 0
}
*/
scene::INodePtr LegacyBrushDefParser::parse(parser::DefTokeniser& tok) const
{
// Create a new brush
scene::INodePtr node = GlobalBrushCreator().createBrush();
// Cast the node, this must succeed
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(node);
assert(brushNode != NULL);
IBrush& brush = brushNode->getIBrush();
// Parse face tokens until a closing brace is encountered
while (1)
{
std::string token = tok.nextToken();
// Token should be either a "(" (start of face) or "}" (end of brush)
if (token == "}")
{
break; // end of brush
}
else if (token == "(") // FACE
{
// Parse three 3D points to construct a plane
double x = string::to_float(tok.nextToken());
double y = string::to_float(tok.nextToken());
double z = string::to_float(tok.nextToken());
Vector3 p1(x, y, z);
tok.assertNextToken(")");
tok.assertNextToken("(");
x = string::to_float(tok.nextToken());
y = string::to_float(tok.nextToken());
z = string::to_float(tok.nextToken());
Vector3 p2(x, y, z);
tok.assertNextToken(")");
tok.assertNextToken("(");
x = string::to_float(tok.nextToken());
y = string::to_float(tok.nextToken());
z = string::to_float(tok.nextToken());
Vector3 p3(x, y, z);
tok.assertNextToken(")");
// Construct the plane from the three points
Plane3 plane(p3, p2, p1);
// Parse Shader, brushDef has an implicit "textures/" not written to the map
std::string shader = GlobalTexturePrefix_get() + tok.nextToken();
// Parse texture (shift rotation scale)
float shiftS = string::to_float(tok.nextToken());
float shiftT = string::to_float(tok.nextToken());
float rotation = string::to_float(tok.nextToken());
float scaleS = string::to_float(tok.nextToken());
float scaleT = string::to_float(tok.nextToken());
Matrix4 texdef = getTexDef(shader, shiftS, shiftT, rotation, scaleS, scaleT);
// Parse Flags (usually each brush has all faces detail or all faces structural)
IBrush::DetailFlag flag = static_cast<IBrush::DetailFlag>(
string::convert<std::size_t>(tok.nextToken(), IBrush::Structural));
brush.setDetailFlag(flag);
// Ignore the other two flags
tok.skipTokens(2);
// Finally, add the new face to the brush
/*IFace& face = */brush.addFace(plane, texdef, shader);
}
else
{
std::string text = fmt::format(_("BrushDefParser: invalid token '{0}'"), token);
throw parser::ParseException(text);
}
}
return node;
}
Matrix4 LegacyBrushDefParser::getTexDef(std::string shader, float shiftS, float shiftT, float rotation, float scaleS, float scaleT)
{
Matrix4 transform;
double inverse_scale[2];
float image_width = 0;
float image_height = 0;
TexturePtr texture = GlobalMaterialManager().getMaterialForName( shader )->getEditorImage();
if (texture) {
image_width = texture->getWidth();
image_height = texture->getHeight();
}
if (image_width == 0 || image_height == 0) {
rError() << "LegacyBrushDefParser: Failed to load image: " << shader << std::endl;
image_width = 128;
image_height = 128;
}
#if 0
else {
rMessage() << "LegacyBrushDefParser: Loaded image: " << shader << ", width: " << image_width << ", height: " << image_height << std::endl;
}
#endif
// transform to texdef shift/scale/rotate
inverse_scale[0] = 1 / (scaleS * image_width);
inverse_scale[1] = 1 / (scaleT * -image_height);
transform[12] = shiftS / image_width;
transform[13] = -shiftT / -image_height;
double c = cos(degrees_to_radians(-rotation));
double s = sin(degrees_to_radians(-rotation));
transform[0] = c * inverse_scale[0];
transform[1] = s * inverse_scale[1];
transform[4] = -s * inverse_scale[0];
transform[5] = c * inverse_scale[1];
transform[2] = transform[3] = transform[6] = transform[7] = transform[8] = transform[9] = transform[11] = transform[14] = 0;
transform[10] = transform[15] = 1;
return transform;
}
} // namespace map