/
Texturing.cpp
181 lines (142 loc) · 4.32 KB
/
Texturing.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
#include "Texturing.h"
#include "selection/textool/FaceNode.h"
#include "selection/textool/PatchNode.h"
#include "messages/TextureChanged.h"
namespace selection
{
namespace algorithm
{
namespace
{
inline void applyTransform(const textool::INode::Ptr& node, const Matrix3& transform)
{
node->beginTransformation();
node->transform(transform);
node->commitTransformation();
}
}
TextureNodeProcessor::operator std::function<bool(const textool::INode::Ptr& node)>()
{
return [this](const textool::INode::Ptr& node)
{
return processNode(node);
};
}
bool TextureBoundsAccumulator::processNode(const textool::INode::Ptr& node)
{
_bounds.includeAABB(node->localAABB());
return true;
}
bool TextureNodeManipulator::processNode(const textool::INode::Ptr& node)
{
applyTransform(node, _transform);
return true;
}
TextureNodeManipulator::TextureNodeManipulator() :
_numProcessedNodes(0)
{}
TextureNodeManipulator::~TextureNodeManipulator()
{
// Dispatch the texture changed signal if we processed at least one node
if (_numProcessedNodes)
{
radiant::TextureChangedMessage::Send();
}
}
TextureFlipper::TextureFlipper(const Vector2& flipCenter, int axis)
{
auto flipMatrix = Matrix3::getIdentity();
if (axis == 0)
{
flipMatrix.xx() = -1;
}
else // axis == 1
{
flipMatrix.yy() = -1;
}
_transform = Matrix3::getTranslation(-flipCenter);
_transform.premultiplyBy(flipMatrix);
_transform.premultiplyBy(Matrix3::getTranslation(+flipCenter));
}
void TextureFlipper::FlipNode(const textool::INode::Ptr& node, int flipAxis)
{
const auto& bounds = node->localAABB();
TextureFlipper flipper({ bounds.origin.x(), bounds.origin.y() }, flipAxis);
flipper.processNode(node);
}
void TextureFlipper::FlipPatch(IPatch& patch, int flipAxis)
{
FlipNode(std::make_shared<textool::PatchNode>(patch), flipAxis);
}
void TextureFlipper::FlipFace(IFace& face, int flipAxis)
{
FlipNode(std::make_shared<textool::FaceNode>(face), flipAxis);
}
// Rotation
TextureRotator::TextureRotator(const Vector2& pivot, double angle)
{
_transform = Matrix3::getTranslation(-pivot);
_transform.premultiplyBy(Matrix3::getRotation(angle));
_transform.premultiplyBy(Matrix3::getTranslation(pivot));
}
void TextureRotator::RotatePatch(IPatch& patch, double angle)
{
RotateNode(std::make_shared<textool::PatchNode>(patch), angle);
}
void TextureRotator::RotateFace(IFace& face, double angle)
{
RotateNode(std::make_shared<textool::FaceNode>(face), angle);
}
void TextureRotator::RotateNode(const textool::INode::Ptr& node, double angle)
{
const auto& bounds = node->localAABB();
TextureRotator rotator({ bounds.origin.x(), bounds.origin.y() }, angle);
rotator.processNode(node);
}
// Scaling
TextureScaler::TextureScaler(const Vector2& pivot, const Vector2& scale)
{
_transform = Matrix3::getTranslation(-pivot);
_transform.premultiplyBy(Matrix3::getScale(scale));
_transform.premultiplyBy(Matrix3::getTranslation(pivot));
}
void TextureScaler::ScalePatch(IPatch& patch, const Vector2& scale)
{
ScaleNode(std::make_shared<textool::PatchNode>(patch), scale);
}
void TextureScaler::ScaleFace(IFace& face, const Vector2& scale)
{
ScaleNode(std::make_shared<textool::FaceNode>(face), scale);
}
void TextureScaler::ScaleNode(const textool::INode::Ptr& node, const Vector2& scale)
{
const auto& bounds = node->localAABB();
TextureScaler scaler({ bounds.origin.x(), bounds.origin.y() }, scale);
scaler.processNode(node);
}
// Normalise
TextureNormaliser::TextureNormaliser(const Vector2& boundsCenter)
{
Vector2 roundedCenter
{
boundsCenter.x() > 0 ? floor(boundsCenter.x()) : ceil(boundsCenter.x()),
boundsCenter.y() > 0 ? floor(boundsCenter.y()) : ceil(boundsCenter.y())
};
_transform = Matrix3::getTranslation(-roundedCenter);
}
void TextureNormaliser::NormalisePatch(IPatch& patch)
{
NormaliseNode(std::make_shared<textool::PatchNode>(patch));
}
void TextureNormaliser::NormaliseFace(IFace& face)
{
NormaliseNode(std::make_shared<textool::FaceNode>(face));
}
void TextureNormaliser::NormaliseNode(const textool::INode::Ptr& node)
{
const auto& bounds = node->localAABB();
TextureNormaliser normaliser({ bounds.origin.x(), bounds.origin.y() });
normaliser.processNode(node);
}
}
}