/
BrushDef3.cpp
201 lines (160 loc) · 5.55 KB
/
BrushDef3.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
// greebo: Specialise the float casting function
#define SPECIALISE_STR_TO_FLOAT
#include "BrushDef3.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& BrushDef3Parser::getKeyword() const
{
static std::string _keyword("brushDef3");
return _keyword;
}
/*
// Example Primitive
{
brushDef3
{
( 0 0 1 -604 ) ( ( 0.015625 0 255.9375 ) ( 0 0.015625 0 ) ) "textures/darkmod/stone/brick/blocks_brown" 0 0 0
( 0 1 0 -1528 ) ( ( 0.015625 0 0 ) ( 0 0.015625 1 ) ) "textures/darkmod/stone/brick/blocks_brown" 0 0 0
( 1 0 0 -1312 ) ( ( 0.015625 0 255.9375 ) ( 0 0.015625 1 ) ) "textures/darkmod/stone/brick/blocks_brown" 0 0 0
( 0 0 -1 -0 ) ( ( 0.015625 0 255.9375 ) ( 0 0.015625 0 ) ) "textures/darkmod/stone/brick/blocks_brown" 0 0 0
( -1 0 0 -1264 ) ( ( 0.015625 0 0.0625 ) ( 0 0.015625 1 ) ) "textures/darkmod/stone/brick/blocks_brown" 0 0 0
( -0 -1 -0 1524 ) ( ( 0.015625 0 0 ) ( 0 0.015625 1 ) ) "textures/darkmod/stone/brick/blocks_brown" 0 0 0
}
}
*/
// greebo: switch off optimisations for this section - the symptom is that brushes don't get a
// valid d value assigned after the first call to addFace() - the callback triggers a series
// of calls in the DarkRadiant main module (up to the Texture Tool), and after return the plane
// gets wrong values assigned
#if _MSC_VER >= 1600
#pragma optimize( "", off )
#endif
scene::INodePtr BrushDef3Parser::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
{
// Construct a plane and parse its values
Plane3 plane;
plane.normal().x() = string::to_float(tok.nextToken());
plane.normal().y() = string::to_float(tok.nextToken());
plane.normal().z() = string::to_float(tok.nextToken());
plane.dist() = -string::to_float(tok.nextToken()); // negate d
tok.assertNextToken(")");
// Parse TexDef
Matrix3 texdef;
tok.assertNextToken("(");
tok.assertNextToken("(");
texdef.xx() = string::to_float(tok.nextToken());
texdef.yx() = string::to_float(tok.nextToken());
texdef.zx() = string::to_float(tok.nextToken());
tok.assertNextToken(")");
tok.assertNextToken("(");
texdef.xy() = string::to_float(tok.nextToken());
texdef.yy() = string::to_float(tok.nextToken());
texdef.zy() = string::to_float(tok.nextToken());
tok.assertNextToken(")");
tok.assertNextToken(")");
// Parse Shader
std::string shader = 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(_("BrushDef3Parser: invalid token '{0}'"), token);
throw parser::ParseException(text);
}
}
// Final outer "}"
tok.assertNextToken("}");
return node;
}
scene::INodePtr BrushDef3ParserQuake4::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
{
// Construct a plane and parse its values
Plane3 plane;
plane.normal().x() = string::to_float(tok.nextToken());
plane.normal().y() = string::to_float(tok.nextToken());
plane.normal().z() = string::to_float(tok.nextToken());
plane.dist() = -string::to_float(tok.nextToken()); // negate d
tok.assertNextToken(")");
// Parse TexDef
Matrix3 texdef;
tok.assertNextToken("(");
tok.assertNextToken("(");
texdef.xx() = string::to_float(tok.nextToken());
texdef.yx() = string::to_float(tok.nextToken());
texdef.zx() = string::to_float(tok.nextToken());
tok.assertNextToken(")");
tok.assertNextToken("(");
texdef.xy() = string::to_float(tok.nextToken());
texdef.yy() = string::to_float(tok.nextToken());
texdef.zy() = string::to_float(tok.nextToken());
tok.assertNextToken(")");
tok.assertNextToken(")");
// Parse Shader
std::string shader = tok.nextToken();
// Finally, add the new face to the brush
/*IFace& face = */brush.addFace(plane, texdef, shader);
}
else {
std::string text = fmt::format(_("BrushDef3ParserQuake4: invalid token '{0}'"), token);
throw parser::ParseException(text);
}
}
// Final outer "}"
tok.assertNextToken("}");
return node;
}
#if _MSC_VER >= 1600
#pragma optimize( "", on )
#endif
} // namespace map