| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "imodule.h" |
| 4 |
|
| 5 |
#include <stdexcept> |
| 6 |
#include "inode.h" |
| 7 |
#include "math/Vector2.h" |
| 8 |
#include "math/Vector3.h" |
| 9 |
#include "render/VertexNT.h" |
| 10 |
|
| 11 |
// This is thrown by the internal patch routines |
| 12 |
class GenericPatchException : |
| 13 |
public std::runtime_error |
| 14 |
{ |
| 15 |
public: |
| 16 |
// Constructor |
| 17 |
GenericPatchException(const std::string& what): |
| 18 |
std::runtime_error(what) |
| 19 |
{} |
| 20 |
}; |
| 21 |
|
| 22 |
/* greebo: A PatchControl consists of a vertex and a set of texture coordinates. |
| 23 |
* Multiple PatchControls form a PatchControlArray or (together with width and height) a PatchControlMatrix. |
| 24 |
*/ |
| 25 |
struct PatchControl |
| 26 |
{ |
| 27 |
Vector3 vertex; // The coordinates of the control point |
| 28 |
Vector2 texcoord; // The texture coordinates of this point |
| 29 |
}; |
| 30 |
|
| 31 |
/** |
| 32 |
* A structure representing the fully tesselated patch |
| 33 |
* Can be acquired through the IPatch interface for |
| 34 |
* exporting the geometry to an external app. |
| 35 |
*/ |
| 36 |
struct PatchMesh |
| 37 |
{ |
| 38 |
std::size_t width; // width of this mesh |
| 39 |
std::size_t height; // height of this mesh |
| 40 |
|
| 41 |
/// Geometry with normals and texture coordinates |
| 42 |
std::vector<VertexNT> vertices; |
| 43 |
}; |
| 44 |
|
| 45 |
struct PatchRenderIndices |
| 46 |
{ |
| 47 |
// The indices, arranged in the way it's expected by GL_QUAD_STRIPS |
| 48 |
// The number of indices is (lenStrips*numStrips) |
| 49 |
std::vector<unsigned int> indices; |
| 50 |
|
| 51 |
// Strip index layout |
| 52 |
std::size_t numStrips; |
| 53 |
std::size_t lenStrips; |
| 54 |
}; |
| 55 |
|
| 56 |
typedef BasicVector2<unsigned int> Subdivisions; |
| 57 |
|
| 58 |
// The abstract base class for a Doom3-compatible patch |
| 59 |
class IPatch |
| 60 |
{ |
| 61 |
public: |
| 62 |
// An observer can attach itself to a specific patch instance. |
| 63 |
// to get notified about changes. |
| 64 |
class Observer |
| 65 |
{ |
| 66 |
public: |
| 67 |
/** |
| 68 |
* Is called when the dimensions and/or the |
| 69 |
* values of one or more control points get altered. |
| 70 |
*/ |
| 71 |
virtual void onPatchControlPointsChanged() = 0; |
| 72 |
|
| 73 |
/** |
| 74 |
* Is called when the patch shader is changed. |
| 75 |
*/ |
| 76 |
virtual void onPatchTextureChanged() = 0; |
| 77 |
|
| 78 |
/** |
| 79 |
* Is called by the Patch destructor. After this call |
| 80 |
* the observer is automatically detached. |
| 81 |
*/ |
| 82 |
virtual void onPatchDestruction() = 0; |
| 83 |
}; |
| 84 |
|
| 85 |
virtual void attachObserver(Observer* observer) = 0; |
| 86 |
virtual void detachObserver(Observer* observer) = 0; |
| 87 |
|
| 88 |
virtual ~IPatch() {} |
| 89 |
|
| 90 |
// Resizes the patch to the given dimensions |
| 91 |
virtual void setDims(std::size_t width, std::size_t height) = 0; |
| 92 |
|
| 93 |
// Get the patch dimensions |
| 94 |
virtual std::size_t getWidth() const = 0; |
| 95 |
virtual std::size_t getHeight() const = 0; |
| 96 |
|
| 97 |
// Return a defined patch control vertex at <row>,<col> |
| 98 |
virtual PatchControl& ctrlAt(std::size_t row, std::size_t col) = 0; |
| 99 |
virtual const PatchControl& ctrlAt(std::size_t row, std::size_t col) const = 0; |
| 100 |
|
| 101 |
// Returns a copy of the fully tesselated patch geometry (slow!) |
| 102 |
virtual PatchMesh getTesselatedPatchMesh() const = 0; |
| 103 |
|
| 104 |
// Returns a copy of the render indices which can be passed to GL_QUAD_STRIPS (slow) |
| 105 |
virtual PatchRenderIndices getRenderIndices() const = 0; |
| 106 |
|
| 107 |
/** |
| 108 |
* greebo: Inserts two columns before and after the column with index <colIndex>. |
| 109 |
* Throws an GenericPatchException if an error occurs. |
| 110 |
*/ |
| 111 |
virtual void insertColumns(std::size_t colIndex) = 0; |
| 112 |
|
| 113 |
/** |
| 114 |
* greebo: Inserts two rows before and after the row with index <rowIndex>. |
| 115 |
* Throws an GenericPatchException if an error occurs. |
| 116 |
*/ |
| 117 |
virtual void insertRows(std::size_t rowIndex) = 0; |
| 118 |
|
| 119 |
/** |
| 120 |
* greebo: Removes columns or rows right before and after the col/row |
| 121 |
* with the given index, reducing the according dimension by 2. |
| 122 |
*/ |
| 123 |
virtual void removePoints(bool columns, std::size_t index) = 0; |
| 124 |
|
| 125 |
/** |
| 126 |
* greebo: Appends two rows or columns at the beginning or the end. |
| 127 |
*/ |
| 128 |
virtual void appendPoints(bool columns, bool beginning) = 0; |
| 129 |
|
| 130 |
// Updates the patch tesselation matrix, call this everytime you're done with your PatchControl changes |
| 131 |
virtual void controlPointsChanged() = 0; |
| 132 |
|
| 133 |
// Check if the patch has invalid control points or width/height are zero |
| 134 |
virtual bool isValid() const = 0; |
| 135 |
|
| 136 |
// Check whether all control vertices are in the same 3D spot (with minimal tolerance) |
| 137 |
virtual bool isDegenerate() const = 0; |
| 138 |
|
| 139 |
// Shader handling |
| 140 |
virtual const std::string& getShader() const = 0; |
| 141 |
virtual void setShader(const std::string& name) = 0; |
| 142 |
|
| 143 |
// greebo: returns true if the patch's shader is visible, false otherwise |
| 144 |
virtual bool hasVisibleMaterial() const = 0; |
| 145 |
|
| 146 |
// Returns the texture aspect ratio width/height |
| 147 |
virtual float getTextureAspectRatio() const = 0; |
| 148 |
|
| 149 |
/** |
| 150 |
* greebo: Sets/gets whether this patch is a patchDef3 (fixed tesselation) |
| 151 |
*/ |
| 152 |
virtual bool subdivisionsFixed() const = 0; |
| 153 |
|
| 154 |
/** greebo: Returns the x,y subdivision values (for tesselation) |
| 155 |
*/ |
| 156 |
virtual const Subdivisions& getSubdivisions() const = 0; |
| 157 |
|
| 158 |
/** greebo: Sets the subdivision of this patch |
| 159 |
* |
| 160 |
* @isFixed: TRUE, if this patch should be a patchDef3 (fixed tesselation) |
| 161 |
* @divisions: a two-component vector containing the desired subdivisions |
| 162 |
*/ |
| 163 |
virtual void setFixedSubdivisions(bool isFixed, const Subdivisions& divisions) = 0; |
| 164 |
|
| 165 |
virtual void undoSave() = 0; |
| 166 |
|
| 167 |
// This translates the texture as much towards the origin in texture space as possible |
| 168 |
// without changing its appearance. |
| 169 |
virtual void normaliseTexture() = 0; |
| 170 |
|
| 171 |
// Flips the control point matrix such that the patch is facing the opposite direction |
| 172 |
virtual void invertMatrix() = 0; |
| 173 |
|
| 174 |
/** |
| 175 |
* greebo: This algorithm will transpose the patch matrix |
| 176 |
* such that the actual control vertex contents remain the same |
| 177 |
* but their indices in the patch matrix change. |
| 178 |
* Rows become columns and vice versa. |
| 179 |
*/ |
| 180 |
virtual void transposeMatrix() = 0; |
| 181 |
|
| 182 |
// Tries to rearrange the row vertices to be spaced out more evenly |
| 183 |
virtual void redisperseRows() = 0; |
| 184 |
|
| 185 |
// Tries to rearrange the column vertices to be spaced out more evenly |
| 186 |
virtual void redisperseColumns() = 0; |
| 187 |
|
| 188 |
// Insert or remove columns or rows at the beginning or at the end |
| 189 |
virtual void insertRemove(bool insert, bool column, bool first) = 0; |
| 190 |
|
| 191 |
virtual void translateTexture(float s, float t) = 0; |
| 192 |
|
| 193 |
// Rotates the texture of the patch by the given angle (in degrees) |
| 194 |
virtual void rotateTexture(float angle) = 0; |
| 195 |
|
| 196 |
// Scales the patch texture by the given factors (pass 1.05 for a +0.05 scale) |
| 197 |
virtual void scaleTexture(float s, float t) = 0; |
| 198 |
|
| 199 |
virtual void fitTexture(float repeatS, float repeatT) = 0; |
| 200 |
|
| 201 |
// Flips the texture by the given flipAxis (0 == x-axis, 1 == y-axis) |
| 202 |
virtual void flipTexture(int axis) = 0; |
| 203 |
|
| 204 |
// Applies the "natural" scale to this patch |
| 205 |
virtual void scaleTextureNaturally() = 0; |
| 206 |
|
| 207 |
enum class AlignEdge |
| 208 |
{ |
| 209 |
Top, |
| 210 |
Bottom, |
| 211 |
Left, |
| 212 |
Right, |
| 213 |
}; |
| 214 |
|
| 215 |
// Alligns the assigned texture at the given edge (if possible) |
| 216 |
virtual void alignTexture(AlignEdge type) = 0; |
| 217 |
|
| 218 |
// Reverts any transform that has been applied since the last time freezeTransform() was called |
| 219 |
virtual void revertTransform() = 0; |
| 220 |
|
| 221 |
// Returns the transformable control point (not the one returned by ctrlAt) - this one is |
| 222 |
// part of the "transformed" working set of the patch which will either be saved on freezeTransform() |
| 223 |
// or discarded on revertTransform(). |
| 224 |
virtual PatchControl& getTransformedCtrlAt(std::size_t row, std::size_t col) = 0; |
| 225 |
|
| 226 |
// Promotes the current transformed state to the new base state |
| 227 |
virtual void freezeTransform() = 0; |
| 228 |
|
| 229 |
// Updates the patch tesselation based on the transformed set of control vertices |
| 230 |
// Setting force to true will update the tesselation even if controlPointsChanged() |
| 231 |
// hasn't been called in the meantime. |
| 232 |
virtual void updateTesselation(bool force = false) = 0; |
| 233 |
}; |
| 234 |
|
| 235 |
namespace patch |
| 236 |
{ |
| 237 |
|
| 238 |
// The cap types for a patch |
| 239 |
enum class CapType |
| 240 |
{ |
| 241 |
Nocap, // "None" was already #defined somewhere |
| 242 |
Bevel, |
| 243 |
EndCap, |
| 244 |
InvertedBevel, |
| 245 |
InvertedEndCap, |
| 246 |
Cylinder, |
| 247 |
}; |
| 248 |
|
| 249 |
enum class PatchEditVertexType : std::size_t |
| 250 |
{ |
| 251 |
Corners, |
| 252 |
Inside, |
| 253 |
NumberOfVertexTypes, |
| 254 |
}; |
| 255 |
|
| 256 |
class IPatchSettings |
| 257 |
{ |
| 258 |
public: |
| 259 |
virtual ~IPatchSettings() {} |
| 260 |
|
| 261 |
virtual const Vector3& getVertexColour(PatchEditVertexType type) const = 0; |
| 262 |
virtual void setVertexColour(PatchEditVertexType type, const Vector3& value) = 0; |
| 263 |
|
| 264 |
virtual sigc::signal<void>& signal_settingsChanged() = 0; |
| 265 |
}; |
| 266 |
|
| 267 |
enum class PatchDefType |
| 268 |
{ |
| 269 |
Def2, |
| 270 |
Def3, |
| 271 |
}; |
| 272 |
|
| 273 |
/** |
| 274 |
* Patch management module interface. |
| 275 |
*/ |
| 276 |
class IPatchModule : |
| 277 |
public RegisterableModule |
| 278 |
{ |
| 279 |
public: |
| 280 |
virtual ~IPatchModule() {} |
| 281 |
|
| 282 |
// Create a patch and return the sceneNode |
| 283 |
virtual scene::INodePtr createPatch(PatchDefType type) = 0; |
| 284 |
|
| 285 |
virtual IPatchSettings& getSettings() = 0; |
| 286 |
}; |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
class Patch; |
| 291 |
class IPatchNode |
| 292 |
{ |
| 293 |
public: |
| 294 |
virtual ~IPatchNode() {} |
| 295 |
|
| 296 |
/** |
| 297 |
* greebo: Retrieves the actual patch from a PatchNode, only works from within the main module. |
| 298 |
*/ |
| 299 |
virtual Patch& getPatchInternal() = 0; |
| 300 |
|
| 301 |
// Get access to the patch interface |
| 302 |
virtual IPatch& getPatch() = 0; |
| 303 |
}; |
| 304 |
typedef std::shared_ptr<IPatchNode> IPatchNodePtr; |
| 305 |
|
| 306 |
inline bool Node_isPatch(const scene::INodePtr& node) |
| 307 |
{ |
| 308 |
return node->getNodeType() == scene::INode::Type::Patch; |
| 309 |
//return std::dynamic_pointer_cast<IPatchNode>(node) != NULL; |
| 310 |
} |
| 311 |
|
| 312 |
inline IPatch* Node_getIPatch(const scene::INodePtr& node) |
| 313 |
{ |
| 314 |
auto patchNode = std::dynamic_pointer_cast<IPatchNode>(node); |
| 315 |
|
| 316 |
if (patchNode) |
| 317 |
{ |
| 318 |
return &patchNode->getPatch(); |
| 319 |
} |
| 320 |
|
| 321 |
return nullptr; |
| 322 |
} |
| 323 |
|
| 324 |
// Casts a node onto a patch |
| 325 |
inline Patch* Node_getPatch(const scene::INodePtr& node) |
| 326 |
{ |
| 327 |
auto patchNode = std::dynamic_pointer_cast<IPatchNode>(node); |
| 328 |
|
| 329 |
if (patchNode) |
| 330 |
{ |
| 331 |
return &patchNode->getPatchInternal(); |
| 332 |
} |
| 333 |
|
| 334 |
return nullptr; |
| 335 |
} |
| 336 |
|
| 337 |
const char* const MODULE_PATCH = "PatchModule"; |
| 338 |
|
| 339 |
inline patch::IPatchModule& GlobalPatchModule() |
| 340 |
{ |
| 341 |
static module::InstanceReference<patch::IPatchModule> _reference(MODULE_PATCH); |
| 342 |
return _reference; |
| 343 |
} |