/
FaceNode.h
168 lines (132 loc) · 3.68 KB
/
FaceNode.h
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
#pragma once
#include "ibrush.h"
#include "NodeBase.h"
#include "math/Matrix3.h"
#include "SelectableVertex.h"
namespace textool
{
class FaceNode :
public NodeBase,
public IFaceNode
{
private:
IFace& _face;
mutable AABB _bounds;
std::vector<SelectableVertex> _vertices;
public:
FaceNode(IFace& face) :
_face(face)
{
for (auto& vertex : _face.getWinding())
{
_vertices.emplace_back(vertex.texcoord);
}
}
IFace& getFace() override
{
return _face;
}
void beginTransformation() override
{
_face.undoSave();
}
void revertTransformation() override
{
_face.revertTransform();
}
void applyTransformToSelected(const Matrix3& transform) override
{
for (auto& vertex : _face.getWinding())
{
vertex.texcoord = transform * vertex.texcoord;
}
Vector3 vertices[3] = { _face.getWinding().at(0).vertex, _face.getWinding().at(1).vertex, _face.getWinding().at(2).vertex };
Vector2 texcoords[3] = { _face.getWinding().at(0).texcoord, _face.getWinding().at(1).texcoord, _face.getWinding().at(2).texcoord };
_face.setTexDefFromPoints(vertices, texcoords);
}
void commitTransformation() override
{
_face.freezeTransform();
}
const AABB& localAABB() const
{
_bounds = AABB();
for (const auto& vertex : _face.getWinding())
{
_bounds.includePoint({ vertex.texcoord.x(), vertex.texcoord.y(), 0 });
}
return _bounds;
}
void testSelect(Selector& selector, SelectionTest& test) override
{
// Arrange the UV coordinates in a Vector3 array for testing
std::vector<Vector3> uvs;
uvs.reserve(_face.getWinding().size());
for (const auto& vertex : _face.getWinding())
{
uvs.emplace_back(vertex.texcoord.x(), vertex.texcoord.y(), 0);
}
test.BeginMesh(Matrix4::getIdentity(), true);
SelectionIntersection best;
test.TestPolygon(VertexPointer(uvs.data(), sizeof(Vector3)), uvs.size(), best);
if (best.isValid())
{
Selector_add(selector, *this);
}
}
bool hasSelectedComponents() const override
{
return false;
}
void clearComponentSelection() override
{
}
void testSelectComponents(Selector& selector, SelectionTest& test) override
{
}
void render(SelectionMode mode) override
{
glEnable(GL_BLEND);
glBlendColor(0, 0, 0, 0.3f);
glBlendFunc(GL_CONSTANT_ALPHA_EXT, GL_ONE_MINUS_CONSTANT_ALPHA_EXT);
if (mode == SelectionMode::Surface && isSelected())
{
glColor3f(1, 0.5f, 0);
}
else if (mode == SelectionMode::Vertex)
{
glColor3f(0.6f, 0.6f, 0.6f);
}
else
{
glColor3f(0.8f, 0.8f, 0.8f);
}
glBegin(GL_TRIANGLE_FAN);
for (const auto& vertex : _face.getWinding())
{
glVertex2d(vertex.texcoord[0], vertex.texcoord[1]);
}
glEnd();
glDisable(GL_BLEND);
if (mode == SelectionMode::Vertex)
{
glPointSize(5);
glBegin(GL_POINTS);
for (const auto& vertex : _vertices)
{
if (vertex.isSelected())
{
glColor3f(1, 0.5f, 0);
}
else
{
glColor3f(0.8f, 0.8f, 0.8f);
}
glVertex2d(vertex.getVertex().x(), vertex.getVertex().y());
}
glEnd();
}
glDisable(GL_BLEND);
}
};
}