/
Face.cpp
834 lines (687 loc) · 23.5 KB
/
Face.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
#include "Face.h"
#include "ivolumetest.h"
#include "ifilter.h"
#include "itextstream.h"
#include "irenderable.h"
#include "math/Matrix3.h"
#include "shaderlib.h"
#include "texturelib.h"
#include "Winding.h"
#include "selection/algorithm/Texturing.h"
#include "Brush.h"
#include "BrushNode.h"
#include "BrushModule.h"
// The structure that is saved in the undostack
class Face::SavedState :
public IUndoMemento
{
public:
FacePlane::SavedState _planeState;
TextureProjection _texdefState;
std::string _materialName;
SavedState(const Face& face) :
_planeState(face.getPlane()),
_texdefState(face.getProjection()),
_materialName(face.getShader())
{}
virtual ~SavedState() {}
void exportState(Face& face) const
{
_planeState.exportState(face.getPlane());
face.setShader(_materialName);
face.getProjection().assign(_texdefState);
}
};
Face::Face(Brush& owner) :
_owner(owner),
_shader(texdef_name_default(), _owner.getBrushNode().getRenderSystem()),
_undoStateSaver(nullptr),
_faceIsVisible(true)
{
setupSurfaceShader();
m_plane.initialiseFromPoints(
Vector3(0, 0, 0), Vector3(64, 0, 0), Vector3(0, 64, 0)
);
planeChanged();
shaderChanged();
}
Face::Face(
Brush& owner,
const Vector3& p0,
const Vector3& p1,
const Vector3& p2,
const std::string& shader,
const TextureProjection& projection
) :
_owner(owner),
_shader(shader, _owner.getBrushNode().getRenderSystem()),
_texdef(projection),
_undoStateSaver(nullptr),
_faceIsVisible(true)
{
setupSurfaceShader();
m_plane.initialiseFromPoints(p0, p1, p2);
planeChanged();
shaderChanged();
}
Face::Face(Brush& owner, const Plane3& plane) :
_owner(owner),
_shader("", _owner.getBrushNode().getRenderSystem()),
_undoStateSaver(nullptr),
_faceIsVisible(true)
{
setupSurfaceShader();
m_plane.setPlane(plane);
planeChanged();
shaderChanged();
}
Face::Face(Brush& owner, const Plane3& plane, const Matrix4& texdef,
const std::string& shader) :
_owner(owner),
_shader(shader, _owner.getBrushNode().getRenderSystem()),
_undoStateSaver(nullptr),
_faceIsVisible(true)
{
setupSurfaceShader();
m_plane.setPlane(plane);
_texdef.matrix = TextureMatrix(texdef);
planeChanged();
shaderChanged();
}
Face::Face(Brush& owner, const Face& other) :
IFace(other),
IUndoable(other),
_owner(owner),
m_plane(other.m_plane),
_shader(other._shader.getMaterialName(), _owner.getBrushNode().getRenderSystem()),
_texdef(other.getProjection()),
_undoStateSaver(nullptr),
_faceIsVisible(other._faceIsVisible)
{
setupSurfaceShader();
planepts_assign(m_move_planepts, other.m_move_planepts);
planeChanged();
}
Face::~Face()
{
_surfaceShaderRealised.disconnect();
}
void Face::setupSurfaceShader()
{
_surfaceShaderRealised = _shader.signal_Realised().connect(
sigc::mem_fun(*this, &Face::realiseShader));
// If we're already in realised state, call realiseShader right away
if (_shader.isRealised())
{
realiseShader();
}
}
IBrush& Face::getBrush()
{
return _owner;
}
Brush& Face::getBrushInternal()
{
return _owner;
}
void Face::planeChanged()
{
revertTransform();
_owner.onFacePlaneChanged();
}
void Face::realiseShader()
{
_owner.onFaceShaderChanged();
}
void Face::connectUndoSystem(IMapFileChangeTracker& changeTracker)
{
assert(!_undoStateSaver);
_shader.setInUse(true);
_undoStateSaver = GlobalUndoSystem().getStateSaver(*this, changeTracker);
}
void Face::disconnectUndoSystem(IMapFileChangeTracker& changeTracker)
{
assert(_undoStateSaver);
_undoStateSaver = nullptr;
GlobalUndoSystem().releaseStateSaver(*this);
_shader.setInUse(false);
}
void Face::undoSave()
{
if (_undoStateSaver)
{
_undoStateSaver->save(*this);
}
}
// undoable
IUndoMementoPtr Face::exportState() const
{
return IUndoMementoPtr(new SavedState(*this));
}
void Face::importState(const IUndoMementoPtr& data)
{
undoSave();
std::static_pointer_cast<SavedState>(data)->exportState(*this);
planeChanged();
_owner.onFaceConnectivityChanged();
texdefChanged();
_owner.onFaceShaderChanged();
}
void Face::flipWinding() {
m_plane.reverse();
planeChanged();
}
bool Face::intersectVolume(const VolumeTest& volume) const
{
if (!m_winding.empty())
{
const Plane3& plane = m_planeTransformed.getPlane();
return volume.TestPlane(Plane3(plane.normal(), -plane.dist()));
}
else
{
// Empty winding, return false
return false;
}
}
bool Face::intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
{
if (m_winding.size() > 0)
{
return volume.TestPlane(Plane3(plane3().normal(), -plane3().dist()), localToWorld);
}
else
{
// Empty winding, return false
return false;
}
}
void Face::renderWireframe(RenderableCollector& collector, const Matrix4& localToWorld,
const IRenderEntity& entity) const
{
collector.addRenderable(*entity.getWireShader(), m_winding, localToWorld,
nullptr, &entity);
}
void Face::setRenderSystem(const RenderSystemPtr& renderSystem)
{
_shader.setRenderSystem(renderSystem);
// Update the visibility flag, we might have switched shaders
const ShaderPtr& shader = _shader.getGLShader();
if (shader)
{
_faceIsVisible = shader->getMaterial()->isVisible();
}
else
{
_faceIsVisible = false; // no shader => not visible
}
}
void Face::translate(const Vector3& translation)
{
if (GlobalBrush().textureLockEnabled())
{
m_texdefTransformed.transformLocked(_shader.getWidth(), _shader.getHeight(),
m_plane.getPlane(), Matrix4::getTranslation(translation));
}
m_planeTransformed.translate(translation);
_owner.onFacePlaneChanged();
updateWinding();
}
void Face::transform(const Matrix4& matrix)
{
if (GlobalBrush().textureLockEnabled())
{
m_texdefTransformed.transformLocked(_shader.getWidth(), _shader.getHeight(), m_plane.getPlane(), matrix);
}
// Transform the FacePlane using the given matrix
m_planeTransformed.transform(matrix);
_owner.onFacePlaneChanged();
updateWinding();
}
void Face::assign_planepts(const PlanePoints planepts)
{
m_planeTransformed.initialiseFromPoints(
planepts[0], planepts[1], planepts[2]
);
_owner.onFacePlaneChanged();
updateWinding();
}
/// \brief Reverts the transformable state of the brush to identity.
void Face::revertTransform()
{
m_planeTransformed = m_plane;
planepts_assign(m_move_planeptsTransformed, m_move_planepts);
m_texdefTransformed = _texdef;
updateWinding();
emitTextureCoordinates();
}
void Face::freezeTransform() {
undoSave();
m_plane = m_planeTransformed;
planepts_assign(m_move_planepts, m_move_planeptsTransformed);
_texdef = m_texdefTransformed;
updateWinding();
}
void Face::updateWinding() {
m_winding.updateNormals(m_plane.getPlane().normal());
}
void Face::update_move_planepts_vertex(std::size_t index, PlanePoints planePoints) {
std::size_t numpoints = getWinding().size();
ASSERT_MESSAGE(index < numpoints, "update_move_planepts_vertex: invalid index");
std::size_t opposite = getWinding().opposite(index);
std::size_t adjacent = getWinding().wrap(opposite + numpoints - 1);
planePoints[0] = getWinding()[opposite].vertex;
planePoints[1] = getWinding()[index].vertex;
planePoints[2] = getWinding()[adjacent].vertex;
// winding points are very inaccurate, so they must be quantised before using them to generate the face-plane
planepts_quantise(planePoints, GRID_MIN);
}
void Face::snapto(float snap) {
if (contributes()) {
PlanePoints planePoints;
update_move_planepts_vertex(0, planePoints);
planePoints[0].snap(snap);
planePoints[1].snap(snap);
planePoints[2].snap(snap);
assign_planepts(planePoints);
freezeTransform();
SceneChangeNotify();
if (!m_plane.getPlane().isValid()) {
rError() << "WARNING: invalid plane after snap to grid\n";
}
}
}
void Face::testSelect(SelectionTest& test, SelectionIntersection& best) {
m_winding.testSelect(test, best);
}
void Face::testSelect_centroid(SelectionTest& test, SelectionIntersection& best) {
test.TestPoint(m_centroid, best);
}
void Face::shaderChanged()
{
emitTextureCoordinates();
_owner.onFaceShaderChanged();
// Update the visibility flag, but leave out the contributes() check
const ShaderPtr& shader = getFaceShader().getGLShader();
if (shader)
{
_faceIsVisible = shader->getMaterial()->isVisible();
}
else
{
_faceIsVisible = false; // no shader => not visible
}
planeChanged();
SceneChangeNotify();
}
const std::string& Face::getShader() const
{
return _shader.getMaterialName();
}
void Face::setShader(const std::string& name)
{
undoSave();
_shader.setMaterialName(name);
shaderChanged();
}
void Face::revertTexdef()
{
m_texdefTransformed = _texdef;
}
void Face::texdefChanged()
{
revertTexdef();
emitTextureCoordinates();
// Fire the signal to update the Texture Tools
signal_texdefChanged().emit();
}
const TextureProjection& Face::getProjection() const
{
return _texdef;
}
TextureProjection& Face::getProjection()
{
return _texdef;
}
Matrix4 Face::getProjectionMatrix()
{
return getProjection().getTransform();
}
void Face::setProjectionMatrix(const Matrix4& projection)
{
getProjection().setTransform(projection);
texdefChanged();
}
void Face::GetTexdef(TextureProjection& projection) const
{
projection = _texdef;
}
void Face::SetTexdef(const TextureProjection& projection)
{
undoSave();
_texdef.assign(projection);
texdefChanged();
}
void Face::setTexdef(const TexDef& texDef)
{
TextureProjection projection;
// Construct the BPTexDef out of the TexDef by using the according constructor
projection.matrix = TextureMatrix(texDef);
// The bprimitive texdef needs to be scaled using our current texture dims
auto width = static_cast<double>(_shader.getWidth());
auto height = static_cast<double>(_shader.getHeight());
projection.matrix.coords[0][0] /= width;
projection.matrix.coords[0][1] /= width;
projection.matrix.coords[0][2] /= width;
projection.matrix.coords[1][0] /= height;
projection.matrix.coords[1][1] /= height;
projection.matrix.coords[1][2] /= height;
SetTexdef(projection);
}
ShiftScaleRotation Face::getShiftScaleRotation() const
{
auto texdef = _texdef.matrix.getFakeTexCoords();
auto ssr = texdef.toShiftScaleRotation();
// These values are going to show up in the Surface Inspector, so
// we need to make some adjustments:
// We want the shift values appear in pixels of the editor image,
// so scale up the UV values by the editor image dimensions
ssr.shift[0] *= _shader.getWidth();
ssr.shift[1] *= _shader.getHeight();
// We only need to display shift values in the range of the texture dimensions
ssr.shift[0] = float_mod(ssr.shift[0], _shader.getWidth());
ssr.shift[1] = float_mod(ssr.shift[1], _shader.getHeight());
// Surface Inspector wants to display values such that scale == 1.0 means:
// a 512-unit wide face can display the full 512px of the editor image.
// The corresponding texture matrix transform features a scale value like 1/512
// to scale the 512 XYZ coord down to 1.0 in UV space.
// Now, getFakeTexCoords() yields the reciprocal 1/scale (=> 512), to have larger scale
// values correspond to a higher "texture zoom" factor (is more intuitive that way):
// => 1024 in getFakeTexcoords() means a 512 editor image appears twice as large visually,
// even though the UV coords shrunk only span half the range.
// We divide by the image dims to receive the 1.0-like values we want to see in the entry box.
ssr.scale[0] /= _shader.getWidth();
ssr.scale[1] /= _shader.getHeight();
return ssr;
#if 0
TextureProjection curProjection = _texdef;
// Multiply the texture dimensions to the projection matrix such that
// the shift/scale/rotation represent pixel values within the image.
Vector2 shaderDims(_shader.getWidth(), _shader.getHeight());
TextureMatrix bpTexDef = curProjection.matrix;
bpTexDef.applyShaderDimensions(static_cast<std::size_t>(shaderDims[0]), static_cast<std::size_t>(shaderDims[1]));
// Calculate the "fake" texture properties (shift/scale/rotation)
TexDef texdef = bpTexDef.getFakeTexCoords();
if (shaderDims != Vector2(0, 0))
{
// normalize again to hide the ridiculously high scale values that get created when using texlock
texdef.normalise(shaderDims[0], shaderDims[1]);
}
return texdef.getShiftScaleRotation();
#endif
}
void Face::setShiftScaleRotation(const ShiftScaleRotation& ssr)
{
// We need to do the opposite adjustments as in Face::getShiftScaleRotation()
// The incoming values are scaled up and down, respectively.
auto texdef = TexDef::CreateFromShiftScaleRotation(ssr);
// Scale the pixel value in SSR to relative UV coords
texdef.setShift({ texdef.getShift().x() / _shader.getWidth(),
texdef.getShift().y() / _shader.getHeight() });
// Add the texture dimensions to the scale.
texdef.setScale({ texdef.getScale().x() * _shader.getWidth(),
texdef.getScale().y() * _shader.getHeight() });
// Construct the BPTexDef out of this TexDef
TextureProjection projection;
projection.matrix = TextureMatrix(texdef);
SetTexdef(projection);
}
// Returns the index pair forming an edge, keeping the winding direction intact
inline std::pair<std::size_t, std::size_t> getEdgeIndexPair(std::size_t first, std::size_t second, std::size_t windingSize)
{
if (first > second || second == windingSize - 1 && first == 0)
{
std::swap(first, second);
}
return std::make_pair(first, second);
}
void Face::applyShaderFromFace(const Face& other)
{
undoSave();
// Apply the material of the other face
setShader(other.getShader());
// Retrieve the textureprojection from the source face
TextureProjection projection;
other.GetTexdef(projection);
// The list of shared vertices (other face index => this face index)
std::vector<std::pair<std::size_t, std::size_t>> sharedVertices;
// Let's see whether this face is sharing any 3D coordinates with the other one
// It's important to iterate over ascending indices of the other face, since we need to keep the winding order
for (std::size_t i = 0; i < other.m_winding.size(); ++i)
{
for (std::size_t j = 0; j < m_winding.size(); ++j)
{
// Check if the vertices are matching
if (math::isNear(m_winding[j].vertex, other.m_winding[i].vertex, 0.001))
{
// Match found, add to list
sharedVertices.emplace_back(std::make_pair(i, j));
break;
}
}
}
// Do we have a shared edge?
if (sharedVertices.size() == 2)
{
auto edgeIndices = getEdgeIndexPair(sharedVertices[0].first, sharedVertices[1].first, other.m_winding.size());
// We wrap the texture around the shared edge, check the UV scale perpendicular to that edge
auto edgeCenter = (other.m_winding[edgeIndices.first].vertex + other.m_winding[edgeIndices.second].vertex) * 0.5;
// Construct an edge vector, following the winding direction
auto edge = other.m_winding[edgeIndices.second].vertex - other.m_winding[edgeIndices.first].vertex;
// Construct a vector that is orthogonal to the edge, pointing outwards
auto outwardsDirection = edge.cross(other.m_planeTransformed.getPlane().normal());
// Pick a point outside face, placing that orthogonal vector on the edge center
auto extrapolatedPoint = edgeCenter + outwardsDirection;
auto extrapolationLength = outwardsDirection.getLength();
auto extrapolatedTexcoords = other.m_texdefTransformed.getTextureCoordsForVertex(
extrapolatedPoint, other.m_planeTransformed.getPlane().normal(), Matrix4::getIdentity()
);
// Construct an edge vector on this target face, keeping the winding order
edgeIndices = getEdgeIndexPair(sharedVertices[0].second, sharedVertices[1].second, m_winding.size());
auto targetFaceEdge = m_winding[edgeIndices.second].vertex - m_winding[edgeIndices.first].vertex;
auto inwardsDirection = -targetFaceEdge.cross(m_planeTransformed.getPlane().normal()).getNormalised();
// Calculate a point on this face plane, with the same distance from the edge center as on the source face
auto pointOnThisFacePlane = edgeCenter + inwardsDirection * extrapolationLength;
// Now we have 3 vertices and 3 texcoords to calculate the matching texdef
Vector3 vertices[3] =
{
m_winding[sharedVertices[0].second].vertex,
m_winding[sharedVertices[1].second].vertex,
pointOnThisFacePlane
};
// Use the shared texcoords we found on the other face, and the third one we calculated
Vector2 texcoords[3] =
{
other.m_winding[sharedVertices[0].first].texcoord,
other.m_winding[sharedVertices[1].first].texcoord,
extrapolatedTexcoords
};
setTexDefFromPoints(vertices, texcoords);
_texdef = m_texdefTransformed; // freeze that matrix
return;
}
else
{
// Just use the other projection, as-is
SetTexdef(projection);
}
}
void Face::setTexDefFromPoints(const Vector3 points[3], const Vector2 uvs[3])
{
// Calculate the texture projection for these desired set of UVs and XYZ
// The texture projection matrix is applied to the vertices after they have been
// transformed by the axis base transform (which depends on this face's normal):
// T * AB * vertex = UV
//
// Applying AB to the vertices will yield: T * P = texcoord
// with P containing the axis-based transformed vertices.
//
// If the above should be solved for T, expanding the above multiplication
// sets up six equations to calculate the 6 unknown components of T.
//
// We can arrange the 6 equations in matrix form: T * A = B
// T is the 3x3 texture matrix.
// A contains the XY coords in its columns (Z is ignored since we
// applied the axis base), B contains the UV coords in its columns.
// The third component of all columns in both matrices is 1.
//
// We can solve the above by inverting A: T = B * inv(A)
// Get the axis base for this face, we need the XYZ points in that state
// to reverse-calculate the desired texture transform
auto axisBase = getBasisTransformForNormal(getPlane3().normal());
// Rotate the three incoming world vertices into the local face plane
Vector3 localPoints[] =
{
axisBase * points[0],
axisBase * points[1],
axisBase * points[2],
};
// Arrange the XYZ coords into the columns of matrix A
auto xyz = Matrix3::byColumns(localPoints[0].x(), localPoints[0].y(), 1,
localPoints[1].x(), localPoints[1].y(), 1,
localPoints[2].x(), localPoints[2].y(), 1);
auto uv = Matrix3::byColumns(uvs[0].x(), uvs[0].y(), 1,
uvs[1].x(), uvs[1].y(), 1,
uvs[2].x(), uvs[2].y(), 1);
auto textureMatrix = uv * xyz.getFullInverse();
m_texdefTransformed.setTransform(textureMatrix);
emitTextureCoordinates();
// Fire the signal to update the Texture Tools
signal_texdefChanged().emit();
}
void Face::shiftTexdef(float s, float t)
{
undoSave();
_texdef.shift(s, t);
texdefChanged();
}
void Face::shiftTexdefByPixels(float sPixels, float tPixels)
{
// Scale down the s,t translation using the active texture dimensions
shiftTexdef(sPixels / _shader.getWidth(), tPixels / _shader.getHeight());
}
void Face::scaleTexdef(float sFactor, float tFactor)
{
selection::algorithm::TextureScaler::ScaleFace(*this, { sFactor, tFactor });
}
void Face::rotateTexdef(float angle)
{
selection::algorithm::TextureRotator::RotateFace(*this, degrees_to_radians(angle));
}
void Face::fitTexture(float s_repeat, float t_repeat) {
undoSave();
_texdef.fitTexture(_shader.getWidth(), _shader.getHeight(), m_plane.getPlane().normal(), m_winding, s_repeat, t_repeat);
texdefChanged();
}
void Face::flipTexture(unsigned int flipAxis)
{
selection::algorithm::TextureFlipper::FlipFace(*this, flipAxis);
}
void Face::alignTexture(AlignEdge align)
{
undoSave();
_texdef.alignTexture(align, m_winding);
texdefChanged();
}
void Face::emitTextureCoordinates()
{
m_texdefTransformed.emitTextureCoordinates(m_winding, m_planeTransformed.getPlane().normal(), Matrix4::getIdentity());
}
void Face::applyDefaultTextureScale()
{
_texdef.matrix.addScale(_shader.getWidth(), _shader.getHeight());
texdefChanged();
}
const Vector3& Face::centroid() const {
return m_centroid;
}
void Face::construct_centroid() {
// Take the plane and let the winding calculate the centroid
m_centroid = m_winding.centroid(plane3());
}
const Winding& Face::getWinding() const {
return m_winding;
}
Winding& Face::getWinding() {
return m_winding;
}
const Plane3& Face::plane3() const
{
_owner.onFaceEvaluateTransform();
return m_planeTransformed.getPlane();
}
const Plane3& Face::getPlane3() const
{
return m_plane.getPlane();
}
FacePlane& Face::getPlane() {
return m_plane;
}
const FacePlane& Face::getPlane() const {
return m_plane;
}
Matrix4 Face::getTexDefMatrix() const
{
return _texdef.matrix.getTransform();
}
SurfaceShader& Face::getFaceShader() {
return _shader;
}
const SurfaceShader& Face::getFaceShader() const {
return _shader;
}
bool Face::contributes() const {
return m_winding.size() > 2;
}
bool Face::is_bounded() const {
for (Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i) {
if (i->adjacent == brush::c_brush_maxFaces) {
return false;
}
}
return true;
}
void Face::normaliseTexture() {
undoSave();
Winding::const_iterator nearest = m_winding.begin();
// Find the vertex with the minimal distance to the origin
for (Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i) {
if (nearest->texcoord.getLength() > i->texcoord.getLength()) {
nearest = i;
}
}
Vector2 texcoord = nearest->texcoord;
// The floored values
Vector2 floored(floor(fabs(texcoord[0])), floor(fabs(texcoord[1])));
// The signs of the original texcoords (needed to know which direction it should be shifted)
Vector2 sign(texcoord[0]/fabs(texcoord[0]), texcoord[1]/fabs(texcoord[1]));
Vector2 shift;
shift[0] = (fabs(texcoord[0]) > 1.0E-4) ? -floored[0] * sign[0] * _shader.getWidth() : 0.0f;
shift[0] = (fabs(texcoord[1]) > 1.0E-4) ? -floored[1] * sign[1] * _shader.getHeight() : 0.0f;
// Shift the texture (note the minus sign, the FaceTexDef negates it yet again).
_texdef.shift(static_cast<float>(-shift[0]), static_cast<float>(shift[1]));
texdefChanged();
}
bool Face::isVisible() const
{
return _faceIsVisible;
}
void Face::updateFaceVisibility()
{
_faceIsVisible = contributes() && getFaceShader().getGLShader()->getMaterial()->isVisible();
}
sigc::signal<void>& Face::signal_texdefChanged()
{
static sigc::signal<void> _sigTexdefChanged;
return _sigTexdefChanged;
}