Debian Codegraph @

include/ibrush.h

raw
1 #pragma once
2
3 #include "inode.h"
4 #include "imodule.h"
5
6 #include "math/Vector2.h"
7 #include "math/Vector3.h"
8 #include "math/Matrix3.h"
9 #include "math/Matrix4.h"
10 #include <vector>
11
12 class Plane3;
13
14 const std::string RKEY_ENABLE_TEXTURE_LOCK("user/ui/brush/textureLock");
15
16 namespace brush
17 {
18
19 // Helper class hosting brush-related settings
20 class IBrushSettings
21 {
22 public:
23 virtual ~IBrushSettings() {}
24
25 virtual const Vector3& getVertexColour() const = 0;
26 virtual void setVertexColour(const Vector3& colour) = 0;
27
28 virtual const Vector3& getSelectedVertexColour() const = 0;
29 virtual void setSelectedVertexColour(const Vector3& colour) = 0;
30
31 virtual sigc::signal<void>& signal_settingsChanged() = 0;
32 };
33
34 class BrushCreator :
35 public RegisterableModule
36 {
37 public:
38 virtual scene::INodePtr createBrush() = 0;
39
40 virtual IBrushSettings& getSettings() = 0;
41 };
42
43 enum class PrefabType : int
44 {
45 Cuboid = 0,
46 Prism,
47 Cone,
48 Sphere,
49 NumPrefabTypes,
50 };
51
52 // Public constants
53 const std::size_t c_brush_maxFaces = 1024;
54
55 const std::size_t PRISM_MIN_SIDES = 3;
56 const std::size_t PRISM_MAX_SIDES = c_brush_maxFaces - 2;
57
58 const std::size_t CONE_MIN_SIDES = 3;
59 const std::size_t CONE_MAX_SIDES = 32;
60
61 const std::size_t SPHERE_MIN_SIDES = 3;
62 const std::size_t SPHERE_MAX_SIDES = 7;
63
64 }
65
66 // The structure defining a single corner point of an IWinding
67 struct WindingVertex
68 {
69 Vector3 vertex; // The 3D coordinates of the point
70 Vector2 texcoord; // The UV coordinates
71 Vector3 tangent; // The tangent
72 Vector3 bitangent; // The bitangent
73 Vector3 normal; // The normals
74 std::size_t adjacent; // The index of the adjacent WindingVertex
75
76 // greebo: This operator is needed to enable scripting support
77 // using boost::python's vector_indexing_suite.
78 bool operator==(const WindingVertex& other) const
79 {
80 return (vertex == other.vertex && texcoord == other.texcoord &&
81 tangent == other.tangent && bitangent == other.bitangent &&
82 normal == other.normal && adjacent == other.adjacent);
83 }
84 };
85
86 // A Winding consists of several connected WindingVertex objects,
87 // each of which holding information about a single corner point.
88 typedef std::vector<WindingVertex> IWinding;
89
90 /**
91 * greebo: The texture definition structure containing the scale,
92 * rotation and shift values of an applied texture.
93 * At some places this is referred to as "fake" texture coordinates.
94 * This is not what is actually saved to the .map file, but it makes
95 * texture manipulations in the Surface Inspector much more human-readable.
96 */
97 struct ShiftScaleRotation
98 {
99 double shift[2];
100 double rotate;
101 double scale[2];
102
103 ShiftScaleRotation()
104 {
105 shift[0] = shift[1] = 0;
106 rotate = 0;
107 scale[0] = scale[1] = 1;
108 }
109 };
110
111 class IBrush;
112
113 // Interface for a face plane
114 class IFace
115 {
116 public:
117 // Destructor
118 virtual ~IFace() {}
119
120 // Returns a reference to the brush containing this face
121 virtual IBrush& getBrush() = 0;
122
123 // Submits the current state to the UndoSystem, to make further actions undo-able
124 virtual void undoSave() = 0;
125
126 // Returns true if the texture of this face is not filtered out
127 // This doesn't take into account whether the owning brush is visible or not
128 virtual bool isVisible() const = 0;
129
130 // Shader accessors
131 virtual const std::string& getShader() const = 0;
132 virtual void setShader(const std::string& name) = 0;
133
134 // Shifts the texture by the given s,t amount in texture space
135 virtual void shiftTexdef(float s, float t) = 0;
136
137 // Convenience wrapper to shift the assigned texture by the given amount of pixels
138 // the passed values are scaled accordingly and passed on to shiftTexdef()
139 virtual void shiftTexdefByPixels(float s, float t) = 0;
140
141 // Scales the tex def by the given factors in texture space
142 virtual void scaleTexdef(float s, float t) = 0;
143
144 // Rotates the texture by the given angle
145 virtual void rotateTexdef(float angle) = 0;
146
147 // Returns the amount of texture pixels per game unit shown on this face
148 // This is based on the image returned by the material, usually the editor image
149 virtual Vector2 getTexelScale() const = 0;
150
151 // Returns the texture aspect ratio width/height
152 virtual float getTextureAspectRatio() const = 0;
153
154 // Fits the texture on this face
155 virtual void fitTexture(float s_repeat, float t_repeat) = 0;
156
157 // Flips the texture by the given flipAxis (0 == x-axis, 1 == y-axis)
158 virtual void flipTexture(unsigned int flipAxis) = 0;
159
160 // This translates the texture as much towards the origin in texture space as possible without changing the world appearance.
161 virtual void normaliseTexture() = 0;
162
163 enum class AlignEdge
164 {
165 Top,
166 Bottom,
167 Left,
168 Right,
169 };
170
171 // If possible, aligns the assigned texture at the given anchor edge
172 virtual void alignTexture(AlignEdge alignType) = 0;
173
174 // Reverts any transform that has been applied since the last time freezeTransform() was called
175 virtual void revertTransform() = 0;
176
177 // Promotes the current transformed state to the new base state
178 virtual void freezeTransform() = 0;
179
180 // Get access to the actual Winding object
181 virtual IWinding& getWinding() = 0;
182 virtual const IWinding& getWinding() const = 0;
183
184 virtual const Plane3& getPlane3() const = 0;
185
186 /**
187 * The matrix used to project world coordinates to U/V space, after the winding vertices
188 * have been transformed to this face's axis base system.
189 * The components of this matrix correspond to the matrix values written to the idTech4
190 * brushDef3 face definition (with <zx, zy> holding the translation part):
191 * e.g. ( plane ) ( ( xx yx zx ) ( yx yy zy ) ) "textures/path/to/material" 0 0 0
192 */
193 virtual Matrix3 getProjectionMatrix() const = 0;
194
195 virtual void setProjectionMatrix(const Matrix3& projection) = 0;
196
197 // Constructs the texture projection matrix from the given (world) vertex and texture coords.
198 // Three vertices and their UV coordinates are enough to construct the texdef.
199 virtual void setTexDefFromPoints(const Vector3 points[3], const Vector2 uvs[3]) = 0;
200
201 /**
202 * Calculates and returns the texture definition as shift/scale/rotate.
203 * This is not what is actually saved to the .map file, but it makes
204 * texture manipulations in the Surface Inspector much more human-readable.
205 */
206 virtual ShiftScaleRotation getShiftScaleRotation() const = 0;
207 virtual void setShiftScaleRotation(const ShiftScaleRotation& scr) = 0;
208
209 // Transforms this face plane with the given transformation matrix
210 virtual void transform(const Matrix4& transformation) = 0;
211
212 // Emitted from this IFace's destructor, as last sign of life
213 virtual sigc::signal<void>& signal_faceDestroyed() = 0;
214 };
215
216 // Plane classification info used by splitting and CSG algorithms
217 struct BrushSplitType
218 {
219 std::size_t counts[3];
220
221 BrushSplitType()
222 {
223 counts[0] = 0;
224 counts[1] = 0;
225 counts[2] = 0;
226 }
227
228 BrushSplitType& operator+=(const BrushSplitType& other)
229 {
230 counts[0] += other.counts[0];
231 counts[1] += other.counts[1];
232 counts[2] += other.counts[2];
233 return *this;
234 }
235 };
236
237 // Brush Interface
238 class IBrush
239 {
240 public:
241 virtual ~IBrush() {}
242
243 // Returns the number of faces for this brush
244 virtual std::size_t getNumFaces() const = 0;
245
246 // Get a reference to the face by index in [0..getNumFaces).
247 virtual IFace& getFace(std::size_t index) = 0;
248
249 // Const variant of the above
250 virtual const IFace& getFace(std::size_t index) const = 0;
251
252 // Add a new face to this brush, using the given plane object, returns a reference to the new face
253 virtual IFace& addFace(const Plane3& plane) = 0;
254
255 // Add a new face to this brush, using the given plane, projection matrix and material name
256 virtual IFace& addFace(const Plane3& plane, const Matrix3& textureProjection, const std::string& material) = 0;
257
258 // Removes all faces from this brush
259 virtual void clear() = 0;
260
261 // Returns true when this brush has no faces
262 virtual bool empty() const = 0;
263
264 // Returns true if any face of the brush contributes to the final B-Rep.
265 virtual bool hasContributingFaces() const = 0;
266
267 // Remove any faces from this brush that are not contributing anything to the resulting polyehdron
268 // These are planes that have the same normal as an existing face and are superceded by them
269 // This method is meant to be used during map loading to remove redundancy parsed from legacy maps
270 virtual void removeRedundantFaces() = 0;
271
272 // Removes faces that do not contribute to the brush.
273 // This is useful for cleaning up after CSG operations on the brush.
274 // Note: removal of empty faces is not performed during direct brush manipulations,
275 // because it would make a manipulation irreversible if it created an empty face.
276 virtual void removeEmptyFaces() = 0;
277
278 // Sets the shader of all faces to the given name
279 virtual void setShader(const std::string& newShader) = 0;
280
281 // Returns TRUE if any of the faces has the given shader
282 virtual bool hasShader(const std::string& name) = 0;
283
284 // Returns TRUE if any of the brush's faces has a visible material, FALSE if all faces are effectively hidden
285 virtual bool hasVisibleMaterial() const = 0;
286
287 /**
288 * greebo: This is used by the filter system (for example) to trigger
289 * an update of the cached visibility flags. This enables a brush
290 * to quickly cull its hidden faces without issuing lots of internal calls.
291 */
292 virtual void updateFaceVisibility() = 0;
293
294 // Saves the current state to the undo stack.
295 // Call this before manipulating the brush to make your action undo-able.
296 virtual void undoSave() = 0;
297
298 enum DetailFlag
299 {
300 Structural = 0,
301 Detail = 1 << 27, // 134217728
302 };
303
304 /**
305 * Q3-compatibility feature, get the value of the detail/structural flag
306 */
307 virtual DetailFlag getDetailFlag() const = 0;
308
309 /**
310 * Q3-compatibility feature, set the detail/structural flag
311 */
312 virtual void setDetailFlag(DetailFlag newValue) = 0;
313
314 // Classify this brush against the given plane, used by clipper and CSG algorithms
315 virtual BrushSplitType classifyPlane(const Plane3& plane) const = 0;
316
317 // Method used internally to recalculate the brush windings
318 virtual void evaluateBRep() const = 0;
319 };
320
321 // Forward-declare the Brush object, only accessible from main binary
322 class Brush;
323
324 class IBrushNode
325 {
326 public:
327 virtual ~IBrushNode() {}
328 /** greebo: Retrieves the contained Brush from the BrushNode
329 */
330 virtual Brush& getBrush() = 0;
331
332 // Returns the IBrush interface
333 virtual IBrush& getIBrush() = 0;
334 };
335 typedef std::shared_ptr<IBrushNode> IBrushNodePtr;
336
337 inline bool Node_isBrush(const scene::INodePtr& node)
338 {
339 return node->getNodeType() == scene::INode::Type::Brush;
340 //return std::dynamic_pointer_cast<IBrushNode>(node) != NULL;
341 }
342
343 // Casts the node onto a BrushNode and returns the Brush pointer
344 inline Brush* Node_getBrush(const scene::INodePtr& node)
345 {
346 IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(node);
347 if (brushNode != NULL) {
348 return &brushNode->getBrush();
349 }
350 return NULL;
351 }
352
353 // Casts the node onto a BrushNode and returns the IBrush pointer
354 inline IBrush* Node_getIBrush(const scene::INodePtr& node)
355 {
356 IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(node);
357 if (brushNode != NULL) {
358 return &brushNode->getIBrush();
359 }
360 return NULL;
361 }
362
363 const char* const MODULE_BRUSHCREATOR("Doom3BrushCreator");
364
365 inline brush::BrushCreator& GlobalBrushCreator()
366 {
367 static module::InstanceReference<brush::BrushCreator> _reference(MODULE_BRUSHCREATOR);
368 return _reference;
369 }