2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_BRUSH_H )
23 #define INCLUDED_BRUSH_H
26 /// \brief The brush primitive.
28 /// A collection of planes that define a convex polyhedron.
29 /// The Boundary-Representation of this primitive is a manifold polygonal mesh.
30 /// Each face polygon is represented by a list of vertices in a \c Winding.
31 /// Each vertex is associated with another face that is adjacent to the edge
32 /// formed by itself and the next vertex in the winding. This information can
33 /// be used to find edge-pairs and vertex-rings.
36 #include "debugging/debugging.h"
40 #include "iselection.h"
47 #include "moduleobserver.h"
52 #include "renderable.h"
53 #include "selectable.h"
57 #include "math/frustum.h"
58 #include "selectionlib.h"
60 #include "texturelib.h"
61 #include "container/container.h"
62 #include "generic/bitfield.h"
63 #include "signal/signalfwd.h"
66 #include "brush_primit.h"
68 const unsigned int BRUSH_DETAIL_FLAG = 27;
69 const unsigned int BRUSH_DETAIL_MASK = (1 << BRUSH_DETAIL_FLAG);
82 #define BRUSH_CONNECTIVITY_DEBUG 0
83 #define BRUSH_DEGENERATE_DEBUG 0
85 template<typename TextOuputStreamType>
86 inline TextOuputStreamType &ostream_write(TextOuputStreamType &ostream, const Matrix4 &m)
88 return ostream << "(" << m[0] << " " << m[1] << " " << m[2] << " " << m[3] << ", "
89 << m[4] << " " << m[5] << " " << m[6] << " " << m[7] << ", "
90 << m[8] << " " << m[9] << " " << m[10] << " " << m[11] << ", "
91 << m[12] << " " << m[13] << " " << m[14] << " " << m[15] << ")";
94 inline void print_vector3(const Vector3 &v)
96 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )\n";
99 inline void print_3x3(const Matrix4 &m)
101 globalOutputStream() << "( " << m.xx() << " " << m.xy() << " " << m.xz() << " ) "
102 << "( " << m.yx() << " " << m.yy() << " " << m.yz() << " ) "
103 << "( " << m.zx() << " " << m.zy() << " " << m.zz() << " )\n";
107 inline bool texdef_sane(const texdef_t &texdef)
109 return fabs(texdef.shift[0]) < (1 << 16)
110 && fabs(texdef.shift[1]) < (1 << 16);
113 inline void Winding_DrawWireframe(const Winding &winding)
115 glVertexPointer(3, GL_FLOAT, sizeof(WindingVertex), &winding.points.data()->vertex);
116 glDrawArrays(GL_LINE_LOOP, 0, GLsizei(winding.numpoints));
119 inline void Winding_Draw(const Winding &winding, const Vector3 &normal, RenderStateFlags state)
121 glVertexPointer(3, GL_FLOAT, sizeof(WindingVertex), &winding.points.data()->vertex);
123 if ((state & RENDER_BUMP) != 0) {
124 Vector3 normals[c_brush_maxFaces];
125 typedef Vector3 *Vector3Iter;
126 for (Vector3Iter i = normals, end = normals + winding.numpoints; i != end; ++i) {
129 if (GlobalShaderCache().useShaderLanguage()) {
130 glNormalPointer(GL_FLOAT, sizeof(Vector3), normals);
131 glVertexAttribPointerARB(c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof(WindingVertex),
132 &winding.points.data()->texcoord);
133 glVertexAttribPointerARB(c_attr_Tangent, 3, GL_FLOAT, 0, sizeof(WindingVertex),
134 &winding.points.data()->tangent);
135 glVertexAttribPointerARB(c_attr_Binormal, 3, GL_FLOAT, 0, sizeof(WindingVertex),
136 &winding.points.data()->bitangent);
138 glVertexAttribPointerARB(11, 3, GL_FLOAT, 0, sizeof(Vector3), normals);
139 glVertexAttribPointerARB(8, 2, GL_FLOAT, 0, sizeof(WindingVertex), &winding.points.data()->texcoord);
140 glVertexAttribPointerARB(9, 3, GL_FLOAT, 0, sizeof(WindingVertex), &winding.points.data()->tangent);
141 glVertexAttribPointerARB(10, 3, GL_FLOAT, 0, sizeof(WindingVertex), &winding.points.data()->bitangent);
144 if (state & RENDER_LIGHTING) {
145 Vector3 normals[c_brush_maxFaces];
146 typedef Vector3 *Vector3Iter;
147 for (Vector3Iter i = normals, last = normals + winding.numpoints; i != last; ++i) {
150 glNormalPointer(GL_FLOAT, sizeof(Vector3), normals);
153 if (state & RENDER_TEXTURE) {
154 glTexCoordPointer(2, GL_FLOAT, sizeof(WindingVertex), &winding.points.data()->texcoord);
158 if ( state & RENDER_FILL ) {
159 glDrawArrays( GL_TRIANGLE_FAN, 0, GLsizei( winding.numpoints ) );
163 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( winding.numpoints ) );
166 glDrawArrays(GL_POLYGON, 0, GLsizei(winding.numpoints));
170 const Winding& winding = winding;
172 if ( state & RENDER_FILL ) {
173 glBegin( GL_POLYGON );
177 glBegin( GL_LINE_LOOP );
180 if ( state & RENDER_LIGHTING ) {
181 glNormal3fv( normal );
184 for ( int i = 0; i < winding.numpoints; ++i )
186 if ( state & RENDER_TEXTURE ) {
187 glTexCoord2fv( &winding.points[i][3] );
189 glVertex3fv( winding.points[i] );
196 #include "shaderlib.h"
198 typedef DoubleVector3 PlanePoints[3];
200 inline bool planepts_equal(const PlanePoints planepts, const PlanePoints other)
202 return planepts[0] == other[0] && planepts[1] == other[1] && planepts[2] == other[2];
205 inline void planepts_assign(PlanePoints planepts, const PlanePoints other)
207 planepts[0] = other[0];
208 planepts[1] = other[1];
209 planepts[2] = other[2];
212 inline void planepts_quantise(PlanePoints planepts, double snap)
214 vector3_snap(planepts[0], snap);
215 vector3_snap(planepts[1], snap);
216 vector3_snap(planepts[2], snap);
219 inline float vector3_max_component(const Vector3 &vec3)
221 return std::max(fabsf(vec3[0]), std::max(fabsf(vec3[1]), fabsf(vec3[2])));
224 inline void edge_snap(Vector3 &edge, double snap)
226 float scale = static_cast<float>( ceil(fabs(snap / vector3_max_component(edge))));
228 vector3_scale(edge, scale);
230 vector3_snap(edge, snap);
233 inline void planepts_snap(PlanePoints planepts, double snap)
235 Vector3 edge01(vector3_subtracted(planepts[1], planepts[0]));
236 Vector3 edge12(vector3_subtracted(planepts[2], planepts[1]));
237 Vector3 edge20(vector3_subtracted(planepts[0], planepts[2]));
239 double length_squared_01 = vector3_dot(edge01, edge01);
240 double length_squared_12 = vector3_dot(edge12, edge12);
241 double length_squared_20 = vector3_dot(edge20, edge20);
243 vector3_snap(planepts[0], snap);
245 if (length_squared_01 < length_squared_12) {
246 if (length_squared_12 < length_squared_20) {
247 edge_snap(edge01, snap);
248 edge_snap(edge12, snap);
249 planepts[1] = vector3_added(planepts[0], edge01);
250 planepts[2] = vector3_added(planepts[1], edge12);
252 edge_snap(edge20, snap);
253 edge_snap(edge01, snap);
254 planepts[1] = vector3_added(planepts[0], edge20);
255 planepts[2] = vector3_added(planepts[1], edge01);
258 if (length_squared_01 < length_squared_20) {
259 edge_snap(edge01, snap);
260 edge_snap(edge12, snap);
261 planepts[1] = vector3_added(planepts[0], edge01);
262 planepts[2] = vector3_added(planepts[1], edge12);
264 edge_snap(edge12, snap);
265 edge_snap(edge20, snap);
266 planepts[1] = vector3_added(planepts[0], edge12);
267 planepts[2] = vector3_added(planepts[1], edge20);
272 inline PointVertex pointvertex_for_planept(const DoubleVector3 &point, const Colour4b &colour)
276 static_cast<float>( point.x()),
277 static_cast<float>( point.y()),
278 static_cast<float>( point.z())
284 inline PointVertex pointvertex_for_windingpoint(const Vector3 &point, const Colour4b &colour)
287 vertex3f_for_vector3(point),
292 inline bool check_plane_is_integer(const PlanePoints &planePoints)
294 return !float_is_integer(planePoints[0][0])
295 || !float_is_integer(planePoints[0][1])
296 || !float_is_integer(planePoints[0][2])
297 || !float_is_integer(planePoints[1][0])
298 || !float_is_integer(planePoints[1][1])
299 || !float_is_integer(planePoints[1][2])
300 || !float_is_integer(planePoints[2][0])
301 || !float_is_integer(planePoints[2][1])
302 || !float_is_integer(planePoints[2][2]);
305 inline void brush_check_shader(const char *name)
307 if (!shader_valid(name)) {
308 globalErrorStream() << "brush face has invalid texture name: '" << name << "'\n";
312 class FaceShaderObserver {
314 virtual void realiseShader() = 0;
316 virtual void unrealiseShader() = 0;
319 typedef ReferencePair<FaceShaderObserver> FaceShaderObserverPair;
322 class ContentsFlagsValue {
328 ContentsFlagsValue(int surfaceFlags, int contentFlags, int value, bool specified) :
329 m_surfaceFlags(surfaceFlags),
330 m_contentFlags(contentFlags),
332 m_specified(specified)
342 inline void ContentsFlagsValue_assignMasked(ContentsFlagsValue &flags, const ContentsFlagsValue &other)
344 bool detail = bitfield_enabled(flags.m_contentFlags, BRUSH_DETAIL_MASK);
347 flags.m_contentFlags = bitfield_enable(flags.m_contentFlags, BRUSH_DETAIL_MASK);
349 flags.m_contentFlags = bitfield_disable(flags.m_contentFlags, BRUSH_DETAIL_MASK);
354 class FaceShader : public ModuleObserver {
358 CopiedString m_shader;
359 ContentsFlagsValue m_flags;
361 SavedState(const FaceShader &faceShader)
363 m_shader = faceShader.getShader();
364 m_flags = faceShader.m_flags;
367 void exportState(FaceShader &faceShader) const
369 faceShader.setShader(m_shader.c_str());
370 faceShader.m_flags = m_flags;
374 CopiedString m_shader;
376 ContentsFlagsValue m_flags;
377 FaceShaderObserverPair m_observers;
381 FaceShader(const char *shader, const ContentsFlagsValue &flags = ContentsFlagsValue(0, 0, 0, false)) :
396 // copy-construction not supported
397 FaceShader(const FaceShader &other);
399 void instanceAttach()
402 m_state->incrementUsed();
405 void instanceDetach()
407 m_state->decrementUsed();
413 ASSERT_MESSAGE(m_state == 0, "shader cannot be captured");
414 brush_check_shader(m_shader.c_str());
415 m_state = GlobalShaderCache().capture(m_shader.c_str());
416 m_state->attach(*this);
421 ASSERT_MESSAGE(m_state != 0, "shader cannot be released");
422 m_state->detach(*this);
423 GlobalShaderCache().release(m_shader.c_str());
429 ASSERT_MESSAGE(!m_realised, "FaceTexdef::realise: already realised");
431 m_observers.forEach([](FaceShaderObserver &observer) {
432 observer.realiseShader();
438 ASSERT_MESSAGE(m_realised, "FaceTexdef::unrealise: already unrealised");
439 m_observers.forEach([](FaceShaderObserver &observer) {
440 observer.unrealiseShader();
445 void attach(FaceShaderObserver &observer)
447 m_observers.attach(observer);
449 observer.realiseShader();
453 void detach(FaceShaderObserver &observer)
456 observer.unrealiseShader();
458 m_observers.detach(observer);
461 const char *getShader() const
463 return m_shader.c_str();
466 void setShader(const char *name)
469 m_state->decrementUsed();
475 m_state->incrementUsed();
479 ContentsFlagsValue getFlags() const
481 ASSERT_MESSAGE(m_realised, "FaceShader::getFlags: flags not valid when unrealised");
482 if (!m_flags.m_specified) {
483 return ContentsFlagsValue(
484 m_state->getTexture().surfaceFlags,
485 m_state->getTexture().contentFlags,
486 m_state->getTexture().value,
493 void setFlags(const ContentsFlagsValue &flags)
495 ASSERT_MESSAGE(m_realised, "FaceShader::setFlags: flags not valid when unrealised");
496 ContentsFlagsValue_assignMasked(m_flags, flags);
499 Shader *state() const
504 std::size_t width() const
507 return m_state->getTexture().width;
512 std::size_t height() const
515 return m_state->getTexture().height;
520 unsigned int shaderFlags() const
523 return m_state->getFlags();
530 class FaceTexdef : public FaceShaderObserver {
532 FaceTexdef(const FaceTexdef &other);
535 FaceTexdef &operator=(const FaceTexdef &other);
540 TextureProjection m_projection;
542 SavedState(const FaceTexdef &faceTexdef)
544 m_projection = faceTexdef.m_projection;
547 void exportState(FaceTexdef &faceTexdef) const
549 Texdef_Assign(faceTexdef.m_projection, m_projection);
553 FaceShader &m_shader;
554 TextureProjection m_projection;
555 bool m_projectionInitialised;
560 const TextureProjection &projection,
561 bool projectionInitialised = true
564 m_projection(projection),
565 m_projectionInitialised(projectionInitialised),
566 m_scaleApplied(false)
568 m_shader.attach(*this);
573 m_shader.detach(*this);
578 ASSERT_MESSAGE(!m_scaleApplied, "texture scale aready added");
579 m_scaleApplied = true;
580 m_projection.m_brushprimit_texdef.addScale(m_shader.width(), m_shader.height());
585 ASSERT_MESSAGE(m_scaleApplied, "texture scale aready removed");
586 m_scaleApplied = false;
587 m_projection.m_brushprimit_texdef.removeScale(m_shader.width(), m_shader.height());
592 if (m_projectionInitialised && !m_scaleApplied) {
597 void unrealiseShader()
599 if (m_projectionInitialised && m_scaleApplied) {
604 void setTexdef(const TextureProjection &projection)
607 Texdef_Assign(m_projection, projection);
611 void shift(float s, float t)
613 ASSERT_MESSAGE(texdef_sane(m_projection.m_texdef), "FaceTexdef::shift: bad texdef");
615 Texdef_Shift(m_projection, s, t);
619 void scale(float s, float t)
622 Texdef_Scale(m_projection, s, t);
626 void rotate(float angle)
629 Texdef_Rotate(m_projection, angle);
633 void fit(const Vector3 &normal, const Winding &winding, float s_repeat, float t_repeat)
635 Texdef_FitTexture(m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat);
638 void emitTextureCoordinates(Winding &winding, const Vector3 &normal, const Matrix4 &localToWorld)
640 Texdef_EmitTextureCoordinates(m_projection, m_shader.width(), m_shader.height(), winding, normal, localToWorld);
643 void transform(const Plane3 &plane, const Matrix4 &matrix)
646 Texdef_transformLocked(m_projection, m_shader.width(), m_shader.height(), plane, matrix);
650 TextureProjection normalised() const
652 brushprimit_texdef_t tmp(m_projection.m_brushprimit_texdef);
653 tmp.removeScale(m_shader.width(), m_shader.height());
654 return TextureProjection(m_projection.m_texdef, tmp, m_projection.m_basis_s, m_projection.m_basis_t);
657 void setBasis(const Vector3 &normal)
660 Normal_GetTransform(normal, basis);
661 m_projection.m_basis_s = Vector3(basis.xx(), basis.yx(), basis.zx());
662 m_projection.m_basis_t = Vector3(-basis.xy(), -basis.yy(), -basis.zy());
666 inline void planepts_print(const PlanePoints &planePoints, TextOutputStream &ostream)
668 ostream << "( " << planePoints[0][0] << " " << planePoints[0][1] << " " << planePoints[0][2] << " ) "
669 << "( " << planePoints[1][0] << " " << planePoints[1][1] << " " << planePoints[1][2] << " ) "
670 << "( " << planePoints[2][0] << " " << planePoints[2][1] << " " << planePoints[2][2] << " )";
674 inline Plane3 Plane3_applyTranslation(const Plane3 &plane, const Vector3 &translation)
676 Plane3 tmp(plane3_translated(Plane3(plane.normal(), -plane.dist()), translation));
677 return Plane3(tmp.normal(), -tmp.dist());
680 inline Plane3 Plane3_applyTransform(const Plane3 &plane, const Matrix4 &matrix)
682 Plane3 tmp(plane3_transformed(Plane3(plane.normal(), -plane.dist()), matrix));
683 return Plane3(tmp.normal(), -tmp.dist());
687 PlanePoints m_planepts;
688 Plane3 m_planeCached;
691 Vector3 m_funcStaticOrigin;
693 static EBrushType m_type;
695 static bool isDoom3Plane()
697 return FacePlane::m_type == eBrushTypeDoom3 || FacePlane::m_type == eBrushTypeQuake4;
702 PlanePoints m_planepts;
705 SavedState(const FacePlane &facePlane)
707 if (facePlane.isDoom3Plane()) {
708 m_plane = facePlane.m_plane;
710 planepts_assign(m_planepts, facePlane.planePoints());
714 void exportState(FacePlane &facePlane) const
716 if (facePlane.isDoom3Plane()) {
717 facePlane.m_plane = m_plane;
718 facePlane.updateTranslated();
720 planepts_assign(facePlane.planePoints(), m_planepts);
721 facePlane.MakePlane();
726 FacePlane() : m_funcStaticOrigin(0, 0, 0)
730 FacePlane(const FacePlane &other) : m_funcStaticOrigin(0, 0, 0)
732 if (!isDoom3Plane()) {
733 planepts_assign(m_planepts, other.m_planepts);
736 m_plane = other.m_plane;
743 if (!isDoom3Plane()) {
745 if ( check_plane_is_integer( m_planepts ) ) {
746 globalErrorStream() << "non-integer planepts: ";
747 planepts_print( m_planepts, globalErrorStream() );
748 globalErrorStream() << "\n";
751 m_planeCached = plane3_for_points(m_planepts);
757 if (!isDoom3Plane()) {
758 vector3_swap(m_planepts[0], m_planepts[2]);
761 m_planeCached = plane3_flipped(m_plane);
766 void transform(const Matrix4 &matrix, bool mirror)
768 if (!isDoom3Plane()) {
771 bool off = check_plane_is_integer( planePoints() );
774 matrix4_transform_point(matrix, m_planepts[0]);
775 matrix4_transform_point(matrix, m_planepts[1]);
776 matrix4_transform_point(matrix, m_planepts[2]);
783 if ( check_plane_is_integer( planePoints() ) ) {
785 globalErrorStream() << "caused by transform\n";
791 m_planeCached = Plane3_applyTransform(m_planeCached, matrix);
796 void offset(float offset)
798 if (!isDoom3Plane()) {
799 Vector3 move(vector3_scaled(m_planeCached.normal(), -offset));
801 vector3_subtract(m_planepts[0], move);
802 vector3_subtract(m_planepts[1], move);
803 vector3_subtract(m_planepts[2], move);
807 m_planeCached.d += offset;
812 void updateTranslated()
814 m_planeCached = Plane3_applyTranslation(m_plane, m_funcStaticOrigin);
819 m_plane = Plane3_applyTranslation(m_planeCached, vector3_negated(m_funcStaticOrigin));
823 PlanePoints &planePoints()
828 const PlanePoints &planePoints() const
833 const Plane3 &plane3() const
835 return m_planeCached;
838 void setDoom3Plane(const Plane3 &plane)
844 const Plane3 &getDoom3Plane() const
849 void copy(const FacePlane &other)
851 if (!isDoom3Plane()) {
852 planepts_assign(m_planepts, other.m_planepts);
855 m_planeCached = other.m_plane;
860 void copy(const Vector3 &p0, const Vector3 &p1, const Vector3 &p2)
862 if (!isDoom3Plane()) {
868 m_planeCached = plane3_for_points(p2, p1, p0);
874 inline void Winding_testSelect(Winding &winding, SelectionTest &test, SelectionIntersection &best)
876 test.TestPolygon(VertexPointer(reinterpret_cast<VertexPointer::pointer>( &winding.points.data()->vertex ),
877 sizeof(WindingVertex)), winding.numpoints, best);
880 const double GRID_MIN = 0.125;
882 inline double quantiseInteger(double f)
884 return float_to_integer(f);
887 inline double quantiseFloating(double f)
889 return float_snapped(f, 1.f / (1 << 16));
892 typedef double ( *QuantiseFunc )(double f);
898 virtual bool filter(const Face &face) const = 0;
901 bool face_filtered(Face &face);
903 void add_face_filter(FaceFilter &filter, int mask, bool invert = false);
905 void Brush_addTextureChangedCallback(const SignalHandler &callback);
907 void Brush_textureChanged();
910 extern bool g_brush_texturelock_enabled;
914 virtual void planeChanged() = 0;
916 virtual void connectivityChanged() = 0;
918 virtual void shaderChanged() = 0;
920 virtual void evaluateTransform() = 0;
924 public OpenGLRenderable,
927 public FaceShaderObserver {
928 std::size_t m_refcount;
930 class SavedState : public UndoMemento {
932 FacePlane::SavedState m_planeState;
933 FaceTexdef::SavedState m_texdefState;
934 FaceShader::SavedState m_shaderState;
936 SavedState(const Face &face) : m_planeState(face.getPlane()), m_texdefState(face.getTexdef()),
937 m_shaderState(face.getShader())
941 void exportState(Face &face) const
943 m_planeState.exportState(face.getPlane());
944 m_shaderState.exportState(face.getShader());
945 m_texdefState.exportState(face.getTexdef());
955 static QuantiseFunc m_quantise;
956 static EBrushType m_type;
958 PlanePoints m_move_planepts;
959 PlanePoints m_move_planeptsTransformed;
962 FacePlane m_planeTransformed;
965 TextureProjection m_texdefTransformed;
971 FaceObserver *m_observer;
972 UndoObserver *m_undoable_observer;
975 // assignment not supported
976 Face &operator=(const Face &other);
978 // copy-construction not supported
979 Face(const Face &other);
983 Face(FaceObserver *observer) :
985 m_shader(texdef_name_default()),
986 m_texdef(m_shader, TextureProjection(), false),
988 m_observer(observer),
989 m_undoable_observer(0),
992 m_shader.attach(*this);
993 m_plane.copy(Vector3(0, 0, 0), Vector3(64, 0, 0), Vector3(0, 64, 0));
994 m_texdef.setBasis(m_plane.plane3().normal());
1003 const TextureProjection &projection,
1004 FaceObserver *observer
1008 m_texdef(m_shader, projection),
1009 m_observer(observer),
1010 m_undoable_observer(0),
1013 m_shader.attach(*this);
1014 m_plane.copy(p0, p1, p2);
1015 m_texdef.setBasis(m_plane.plane3().normal());
1020 Face(const Face &other, FaceObserver *observer) :
1022 m_shader(other.m_shader.getShader(), other.m_shader.m_flags),
1023 m_texdef(m_shader, other.getTexdef().normalised()),
1024 m_observer(observer),
1025 m_undoable_observer(0),
1028 m_shader.attach(*this);
1029 m_plane.copy(other.m_plane);
1030 planepts_assign(m_move_planepts, other.m_move_planepts);
1031 m_texdef.setBasis(m_plane.plane3().normal());
1038 m_shader.detach(*this);
1044 m_observer->planeChanged();
1047 void realiseShader()
1049 m_observer->shaderChanged();
1052 void unrealiseShader()
1056 void instanceAttach(MapFile *map)
1058 m_shader.instanceAttach();
1060 m_undoable_observer = GlobalUndoSystem().observer(this);
1061 GlobalFilterSystem().registerFilterable(*this);
1064 void instanceDetach(MapFile *map)
1066 GlobalFilterSystem().unregisterFilterable(*this);
1067 m_undoable_observer = 0;
1068 GlobalUndoSystem().release(this);
1070 m_shader.instanceDetach();
1073 void render(RenderStateFlags state) const
1075 Winding_Draw(m_winding, m_planeTransformed.plane3().normal(), state);
1078 void updateFiltered()
1080 m_filtered = face_filtered(*this);
1083 bool isFiltered() const
1093 if (m_undoable_observer != 0) {
1094 m_undoable_observer->save(this);
1099 UndoMemento *exportState() const
1101 return new SavedState(*this);
1104 void importState(const UndoMemento *data)
1108 static_cast<const SavedState *>( data )->exportState(*this);
1111 m_observer->connectivityChanged();
1113 m_observer->shaderChanged();
1124 if (--m_refcount == 0) {
1135 bool intersectVolume(const VolumeTest &volume, const Matrix4 &localToWorld) const
1137 return volume.TestPlane(Plane3(plane3().normal(), -plane3().dist()), localToWorld);
1140 void render(Renderer &renderer, const Matrix4 &localToWorld) const
1142 renderer.SetState(m_shader.state(), Renderer::eFullMaterials);
1143 renderer.addRenderable(*this, localToWorld);
1146 void transform(const Matrix4 &matrix, bool mirror)
1148 if (g_brush_texturelock_enabled) {
1149 Texdef_transformLocked(m_texdefTransformed, m_shader.width(), m_shader.height(), m_plane.plane3(), matrix);
1152 m_planeTransformed.transform(matrix, mirror);
1155 ASSERT_MESSAGE( projectionaxis_for_normal( normal ) == projectionaxis_for_normal( plane3().normal() ), "bleh" );
1157 m_observer->planeChanged();
1159 if (g_brush_texturelock_enabled) {
1160 Brush_textureChanged();
1164 void assign_planepts(const PlanePoints planepts)
1166 m_planeTransformed.copy(planepts[0], planepts[1], planepts[2]);
1167 m_observer->planeChanged();
1170 /// \brief Reverts the transformable state of the brush to identity.
1171 void revertTransform()
1173 m_planeTransformed = m_plane;
1174 planepts_assign(m_move_planeptsTransformed, m_move_planepts);
1175 m_texdefTransformed = m_texdef.m_projection;
1178 void freezeTransform()
1181 m_plane = m_planeTransformed;
1182 planepts_assign(m_move_planepts, m_move_planeptsTransformed);
1183 m_texdef.m_projection = m_texdefTransformed;
1186 void update_move_planepts_vertex(std::size_t index, PlanePoints planePoints)
1188 std::size_t numpoints = getWinding().numpoints;
1189 ASSERT_MESSAGE(index < numpoints, "update_move_planepts_vertex: invalid index");
1191 std::size_t opposite = Winding_Opposite(getWinding(), index);
1192 std::size_t adjacent = Winding_wrap(getWinding(), opposite + numpoints - 1);
1193 planePoints[0] = getWinding()[opposite].vertex;
1194 planePoints[1] = getWinding()[index].vertex;
1195 planePoints[2] = getWinding()[adjacent].vertex;
1196 // winding points are very inaccurate, so they must be quantised before using them to generate the face-plane
1197 planepts_quantise(planePoints, GRID_MIN);
1200 void snapto(float snap)
1202 if (contributes()) {
1204 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane before snap to grid" );
1205 planepts_snap( m_plane.planePoints(), snap );
1206 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane after snap to grid" );
1208 PlanePoints planePoints;
1209 update_move_planepts_vertex(0, planePoints);
1210 vector3_snap(planePoints[0], snap);
1211 vector3_snap(planePoints[1], snap);
1212 vector3_snap(planePoints[2], snap);
1213 assign_planepts(planePoints);
1216 SceneChangeNotify();
1217 if (!plane3_valid(m_plane.plane3())) {
1218 globalErrorStream() << "WARNING: invalid plane after snap to grid\n";
1223 void testSelect(SelectionTest &test, SelectionIntersection &best)
1225 Winding_testSelect(m_winding, test, best);
1228 void testSelect_centroid(SelectionTest &test, SelectionIntersection &best)
1230 test.TestPoint(m_centroid, best);
1233 void shaderChanged()
1235 EmitTextureCoordinates();
1236 Brush_textureChanged();
1237 m_observer->shaderChanged();
1240 SceneChangeNotify();
1243 const char *GetShader() const
1245 return m_shader.getShader();
1248 void SetShader(const char *name)
1251 m_shader.setShader(name);
1257 m_texdefTransformed = m_texdef.m_projection;
1260 void texdefChanged()
1263 EmitTextureCoordinates();
1264 Brush_textureChanged();
1267 void GetTexdef(TextureProjection &projection) const
1269 projection = m_texdef.normalised();
1272 void SetTexdef(const TextureProjection &projection)
1275 m_texdef.setTexdef(projection);
1279 void GetFlags(ContentsFlagsValue &flags) const
1281 flags = m_shader.getFlags();
1284 void SetFlags(const ContentsFlagsValue &flags)
1287 m_shader.setFlags(flags);
1288 m_observer->shaderChanged();
1292 void ShiftTexdef(float s, float t)
1295 m_texdef.shift(s, t);
1299 void ScaleTexdef(float s, float t)
1302 m_texdef.scale(s, t);
1306 void RotateTexdef(float angle)
1309 m_texdef.rotate(angle);
1313 void FitTexture(float s_repeat, float t_repeat)
1316 m_texdef.fit(m_plane.plane3().normal(), m_winding, s_repeat, t_repeat);
1320 void EmitTextureCoordinates()
1322 Texdef_EmitTextureCoordinates(m_texdefTransformed, m_shader.width(), m_shader.height(), m_winding,
1323 plane3().normal(), g_matrix4_identity);
1327 const Vector3 ¢roid() const
1332 void construct_centroid()
1334 Winding_Centroid(m_winding, plane3(), m_centroid);
1337 const Winding &getWinding() const
1342 Winding &getWinding()
1347 const Plane3 &plane3() const
1349 m_observer->evaluateTransform();
1350 return m_planeTransformed.plane3();
1353 FacePlane &getPlane()
1358 const FacePlane &getPlane() const
1363 FaceTexdef &getTexdef()
1368 const FaceTexdef &getTexdef() const
1373 FaceShader &getShader()
1378 const FaceShader &getShader() const
1383 bool isDetail() const
1385 return (m_shader.m_flags.m_contentFlags & BRUSH_DETAIL_MASK) != 0;
1388 void setDetail(bool detail)
1391 if (detail && !isDetail()) {
1392 m_shader.m_flags.m_contentFlags |= BRUSH_DETAIL_MASK;
1393 } else if (!detail && isDetail()) {
1394 m_shader.m_flags.m_contentFlags &= ~BRUSH_DETAIL_MASK;
1396 m_observer->shaderChanged();
1399 bool contributes() const
1401 return m_winding.numpoints > 2;
1404 bool is_bounded() const
1406 for (Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i) {
1407 if ((*i).adjacent == c_brush_maxFaces) {
1416 class FaceVertexId {
1418 std::size_t m_vertex;
1421 FaceVertexId(std::size_t face, std::size_t vertex)
1422 : m_face(face), m_vertex(vertex)
1426 std::size_t getFace() const
1431 std::size_t getVertex() const
1437 typedef std::size_t faceIndex_t;
1439 struct EdgeRenderIndices {
1444 : first(0), second(0)
1448 EdgeRenderIndices(const RenderIndex _first, const RenderIndex _second)
1449 : first(_first), second(_second)
1459 : first(c_brush_maxFaces), second(c_brush_maxFaces)
1463 EdgeFaces(const faceIndex_t _first, const faceIndex_t _second)
1464 : first(_first), second(_second)
1469 class RenderableWireframe : public OpenGLRenderable {
1471 void render(RenderStateFlags state) const
1474 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_vertices->colour);
1475 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_vertices->vertex);
1476 glDrawElements(GL_LINES, GLsizei(m_size << 1), RenderIndexTypeID, m_faceVertex.data());
1478 glBegin( GL_LINES );
1479 for ( std::size_t i = 0; i < m_size; ++i )
1481 glVertex3fv( &m_vertices[m_faceVertex[i].first].vertex.x );
1482 glVertex3fv( &m_vertices[m_faceVertex[i].second].vertex.x );
1488 Array<EdgeRenderIndices> m_faceVertex;
1490 const PointVertex *m_vertices;
1495 typedef std::vector<Brush *> brush_vector_t;
1499 virtual bool filter(const Brush &brush) const = 0;
1502 bool brush_filtered(Brush &brush);
1504 void add_brush_filter(BrushFilter &filter, int mask, bool invert = false);
1507 /// \brief Returns true if 'self' takes priority when building brush b-rep.
1508 inline bool plane3_inside(const Plane3 &self, const Plane3 &other, bool selfIsLater)
1510 if (vector3_equal_epsilon(self.normal(), other.normal(), 0.001)) {
1511 // same plane? prefer the one with smaller index
1512 if (self.dist() == other.dist()) {
1515 return self.dist() < other.dist();
1520 typedef SmartPointer<Face> FaceSmartPointer;
1521 typedef std::vector<FaceSmartPointer> Faces;
1523 /// \brief Returns the unique-id of the edge adjacent to \p faceVertex in the edge-pair for the set of \p faces.
1524 inline FaceVertexId next_edge(const Faces &faces, FaceVertexId faceVertex)
1526 std::size_t adjacent_face = faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent;
1527 std::size_t adjacent_vertex = Winding_FindAdjacent(faces[adjacent_face]->getWinding(), faceVertex.getFace());
1529 ASSERT_MESSAGE(adjacent_vertex != c_brush_maxFaces, "connectivity data invalid");
1530 if (adjacent_vertex == c_brush_maxFaces) {
1534 return FaceVertexId(adjacent_face, adjacent_vertex);
1537 /// \brief Returns the unique-id of the vertex adjacent to \p faceVertex in the vertex-ring for the set of \p faces.
1538 inline FaceVertexId next_vertex(const Faces &faces, FaceVertexId faceVertex)
1540 FaceVertexId nextEdge = next_edge(faces, faceVertex);
1541 return FaceVertexId(nextEdge.getFace(),
1542 Winding_next(faces[nextEdge.getFace()]->getWinding(), nextEdge.getVertex()));
1545 class SelectableEdge {
1546 Vector3 getEdge() const
1548 const Winding &winding = getFace().getWinding();
1549 return vector3_mid(winding[m_faceVertex.getVertex()].vertex,
1550 winding[Winding_next(winding, m_faceVertex.getVertex())].vertex);
1555 FaceVertexId m_faceVertex;
1557 SelectableEdge(Faces &faces, FaceVertexId faceVertex)
1558 : m_faces(faces), m_faceVertex(faceVertex)
1562 SelectableEdge &operator=(const SelectableEdge &other)
1564 m_faceVertex = other.m_faceVertex;
1568 Face &getFace() const
1570 return *m_faces[m_faceVertex.getFace()];
1573 void testSelect(SelectionTest &test, SelectionIntersection &best)
1575 test.TestPoint(getEdge(), best);
1579 class SelectableVertex {
1580 Vector3 getVertex() const
1582 return getFace().getWinding()[m_faceVertex.getVertex()].vertex;
1587 FaceVertexId m_faceVertex;
1589 SelectableVertex(Faces &faces, FaceVertexId faceVertex)
1590 : m_faces(faces), m_faceVertex(faceVertex)
1594 SelectableVertex &operator=(const SelectableVertex &other)
1596 m_faceVertex = other.m_faceVertex;
1600 Face &getFace() const
1602 return *m_faces[m_faceVertex.getFace()];
1605 void testSelect(SelectionTest &test, SelectionIntersection &best)
1607 test.TestPoint(getVertex(), best);
1611 class BrushObserver {
1613 virtual void reserve(std::size_t size) = 0;
1615 virtual void clear() = 0;
1617 virtual void push_back(Face &face) = 0;
1619 virtual void pop_back() = 0;
1621 virtual void erase(std::size_t index) = 0;
1623 virtual void connectivityChanged() = 0;
1625 virtual void edge_clear() = 0;
1627 virtual void edge_push_back(SelectableEdge &edge) = 0;
1629 virtual void vertex_clear() = 0;
1631 virtual void vertex_push_back(SelectableVertex &vertex) = 0;
1633 virtual void DEBUG_verify() const = 0;
1636 class BrushVisitor {
1638 virtual void visit(Face &face) const = 0;
1642 public TransformNode,
1647 public FaceObserver,
1652 scene::Node *m_node;
1653 typedef UniqueSet<BrushObserver *> Observers;
1654 Observers m_observers;
1655 UndoObserver *m_undoable_observer;
1662 // cached data compiled from state
1663 Array<PointVertex> m_faceCentroidPoints;
1664 RenderablePointArray m_render_faces;
1666 Array<PointVertex> m_uniqueVertexPoints;
1667 typedef std::vector<SelectableVertex> SelectableVertices;
1668 SelectableVertices m_select_vertices;
1669 RenderablePointArray m_render_vertices;
1671 Array<PointVertex> m_uniqueEdgePoints;
1672 typedef std::vector<SelectableEdge> SelectableEdges;
1673 SelectableEdges m_select_edges;
1674 RenderablePointArray m_render_edges;
1676 Array<EdgeRenderIndices> m_edge_indices;
1677 Array<EdgeFaces> m_edge_faces;
1682 Callback<void()> m_evaluateTransform;
1683 Callback<void()> m_boundsChanged;
1685 mutable bool m_planeChanged; // b-rep evaluation required
1686 mutable bool m_transformChanged; // transform evaluation required
1690 STRING_CONSTANT(Name, "Brush");
1692 Callback<void()> m_lightsChanged;
1695 static Shader *m_state_point;
1698 static EBrushType m_type;
1699 static double m_maxWorldCoord;
1701 Brush(scene::Node &node, const Callback<void()> &evaluateTransform, const Callback<void()> &boundsChanged) :
1703 m_undoable_observer(0),
1705 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1706 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1707 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1708 m_evaluateTransform(evaluateTransform),
1709 m_boundsChanged(boundsChanged),
1710 m_planeChanged(false),
1711 m_transformChanged(false)
1716 Brush(const Brush &other, scene::Node &node, const Callback<void()> &evaluateTransform,
1717 const Callback<void()> &boundsChanged) :
1719 m_undoable_observer(0),
1721 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1722 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1723 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1724 m_evaluateTransform(evaluateTransform),
1725 m_boundsChanged(boundsChanged),
1726 m_planeChanged(false),
1727 m_transformChanged(false)
1732 Brush(const Brush &other) :
1733 TransformNode(other),
1738 FaceObserver(other),
1743 m_undoable_observer(0),
1745 m_render_faces(m_faceCentroidPoints, GL_POINTS),
1746 m_render_vertices(m_uniqueVertexPoints, GL_POINTS),
1747 m_render_edges(m_uniqueEdgePoints, GL_POINTS),
1748 m_planeChanged(false),
1749 m_transformChanged(false)
1756 ASSERT_MESSAGE(m_observers.empty(), "Brush::~Brush: observers still attached");
1759 // assignment not supported
1760 Brush &operator=(const Brush &other);
1762 void setDoom3GroupOrigin(const Vector3 &origin)
1764 //globalOutputStream() << "func_static origin before: " << m_funcStaticOrigin << " after: " << origin << "\n";
1765 for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
1766 (*i)->getPlane().m_funcStaticOrigin = origin;
1767 (*i)->getPlane().updateTranslated();
1768 (*i)->planeChanged();
1773 void attach(BrushObserver &observer)
1775 for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
1776 observer.push_back(*(*i));
1779 for (SelectableEdges::iterator i = m_select_edges.begin(); i != m_select_edges.end(); ++i) {
1780 observer.edge_push_back(*i);
1783 for (SelectableVertices::iterator i = m_select_vertices.begin(); i != m_select_vertices.end(); ++i) {
1784 observer.vertex_push_back(*i);
1787 m_observers.insert(&observer);
1790 void detach(BrushObserver &observer)
1792 m_observers.erase(&observer);
1795 void forEachFace(const BrushVisitor &visitor) const
1797 for (Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
1798 visitor.visit(*(*i));
1802 void forEachFace_instanceAttach(MapFile *map) const
1804 for (Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
1805 (*i)->instanceAttach(map);
1809 void forEachFace_instanceDetach(MapFile *map) const
1811 for (Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
1812 (*i)->instanceDetach(map);
1816 InstanceCounter m_instanceCounter;
1818 void instanceAttach(const scene::Path &path)
1820 if (++m_instanceCounter.m_count == 1) {
1821 m_map = path_find_mapfile(path.begin(), path.end());
1822 m_undoable_observer = GlobalUndoSystem().observer(this);
1823 GlobalFilterSystem().registerFilterable(*this);
1824 forEachFace_instanceAttach(m_map);
1826 ASSERT_MESSAGE(path_find_mapfile(path.begin(), path.end()) == m_map,
1827 "node is instanced across more than one file");
1831 void instanceDetach(const scene::Path &path)
1833 if (--m_instanceCounter.m_count == 0) {
1834 forEachFace_instanceDetach(m_map);
1835 GlobalFilterSystem().unregisterFilterable(*this);
1837 m_undoable_observer = 0;
1838 GlobalUndoSystem().release(this);
1843 const char *name() const
1848 void attach(const NameCallback &callback)
1852 void detach(const NameCallback &callback)
1857 void updateFiltered()
1860 if (brush_filtered(*this)) {
1861 m_node->enable(scene::Node::eFiltered);
1863 m_node->disable(scene::Node::eFiltered);
1871 m_planeChanged = true;
1876 void shaderChanged()
1882 void evaluateBRep() const
1884 if (m_planeChanged) {
1885 m_planeChanged = false;
1886 const_cast<Brush *>( this )->buildBRep();
1890 void transformChanged()
1892 m_transformChanged = true;
1896 typedef MemberCaller<Brush, void(), &Brush::transformChanged> TransformChangedCaller;
1898 void evaluateTransform()
1900 if (m_transformChanged) {
1901 m_transformChanged = false;
1903 m_evaluateTransform();
1907 const Matrix4 &localToParent() const
1909 return g_matrix4_identity;
1917 const AABB &localAABB() const
1920 return m_aabb_local;
1923 VolumeIntersectionValue intersectVolume(const VolumeTest &test, const Matrix4 &localToWorld) const
1925 return test.TestAABB(m_aabb_local, localToWorld);
1928 void renderComponents(SelectionSystem::EComponentMode mode, Renderer &renderer, const VolumeTest &volume,
1929 const Matrix4 &localToWorld) const
1932 case SelectionSystem::eVertex:
1933 renderer.addRenderable(m_render_vertices, localToWorld);
1935 case SelectionSystem::eEdge:
1936 renderer.addRenderable(m_render_edges, localToWorld);
1938 case SelectionSystem::eFace:
1939 renderer.addRenderable(m_render_faces, localToWorld);
1946 void transform(const Matrix4 &matrix)
1948 bool mirror = matrix4_handedness(matrix) == MATRIX4_LEFTHANDED;
1950 for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
1951 (*i)->transform(matrix, mirror);
1955 void snapto(float snap)
1957 for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
1962 void revertTransform()
1964 for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
1965 (*i)->revertTransform();
1969 void freezeTransform()
1971 for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
1972 (*i)->freezeTransform();
1976 /// \brief Returns the absolute index of the \p faceVertex.
1977 std::size_t absoluteIndex(FaceVertexId faceVertex)
1979 std::size_t index = 0;
1980 for (std::size_t i = 0; i < faceVertex.getFace(); ++i) {
1981 index += m_faces[i]->getWinding().numpoints;
1983 return index + faceVertex.getVertex();
1986 void appendFaces(const Faces &other)
1989 for (Faces::const_iterator i = other.begin(); i != other.end(); ++i) {
1994 /// \brief The undo memento for a brush stores only the list of face references - the faces are not copied.
1995 class BrushUndoMemento : public UndoMemento {
1997 BrushUndoMemento(const Faces &faces) : m_faces(faces)
2014 if (m_undoable_observer != 0) {
2015 m_undoable_observer->save(this);
2019 UndoMemento *exportState() const
2021 return new BrushUndoMemento(m_faces);
2024 void importState(const UndoMemento *state)
2027 appendFaces(static_cast<const BrushUndoMemento *>( state )->m_faces);
2030 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2031 (*i)->DEBUG_verify();
2037 return !m_faces.empty() && m_faces.front()->isDetail();
2040 /// \brief Appends a copy of \p face to the end of the face list.
2041 Face *addFace(const Face &face)
2043 if (m_faces.size() == c_brush_maxFaces) {
2047 push_back(FaceSmartPointer(new Face(face, this)));
2048 m_faces.back()->setDetail(isDetail());
2050 return m_faces.back();
2053 /// \brief Appends a new face constructed from the parameters to the end of the face list.
2054 Face *addPlane(const Vector3 &p0, const Vector3 &p1, const Vector3 &p2, const char *shader,
2055 const TextureProjection &projection)
2057 if (m_faces.size() == c_brush_maxFaces) {
2061 push_back(FaceSmartPointer(new Face(p0, p1, p2, shader, projection, this)));
2062 m_faces.back()->setDetail(isDetail());
2064 return m_faces.back();
2067 static void constructStatic(EBrushType type)
2070 Face::m_type = type;
2071 FacePlane::m_type = type;
2073 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_QUAKE;
2074 if (m_type == eBrushTypeQuake3BP || m_type == eBrushTypeDoom3 || m_type == eBrushTypeQuake4) {
2075 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_BRUSHPRIMITIVES;
2076 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
2077 } else if (m_type == eBrushTypeHalfLife) {
2078 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_HALFLIFE;
2079 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
2082 Face::m_quantise = (m_type == eBrushTypeQuake) ? quantiseInteger : quantiseFloating;
2084 m_state_point = GlobalShaderCache().capture("$POINT");
2087 static void destroyStatic()
2089 GlobalShaderCache().release("$POINT");
2092 std::size_t DEBUG_size()
2094 return m_faces.size();
2097 typedef Faces::const_iterator const_iterator;
2099 const_iterator begin() const
2101 return m_faces.begin();
2104 const_iterator end() const
2106 return m_faces.end();
2111 return m_faces.back();
2114 const Face *back() const
2116 return m_faces.back();
2119 void reserve(std::size_t count)
2121 m_faces.reserve(count);
2122 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2123 (*i)->reserve(count);
2127 void push_back(Faces::value_type face)
2129 m_faces.push_back(face);
2130 if (m_instanceCounter.m_count != 0) {
2131 m_faces.back()->instanceAttach(m_map);
2133 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2134 (*i)->push_back(*face);
2135 (*i)->DEBUG_verify();
2141 if (m_instanceCounter.m_count != 0) {
2142 m_faces.back()->instanceDetach(m_map);
2145 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2147 (*i)->DEBUG_verify();
2151 void erase(std::size_t index)
2153 if (m_instanceCounter.m_count != 0) {
2154 m_faces[index]->instanceDetach(m_map);
2156 m_faces.erase(m_faces.begin() + index);
2157 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2159 (*i)->DEBUG_verify();
2163 void connectivityChanged()
2165 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2166 (*i)->connectivityChanged();
2174 if (m_instanceCounter.m_count != 0) {
2175 forEachFace_instanceDetach(m_map);
2178 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2180 (*i)->DEBUG_verify();
2184 std::size_t size() const
2186 return m_faces.size();
2191 return m_faces.empty();
2194 /// \brief Returns true if any face of the brush contributes to the final B-Rep.
2195 bool hasContributingFaces() const
2197 for (const_iterator i = begin(); i != end(); ++i) {
2198 if ((*i)->contributes()) {
2205 /// \brief Removes faces that do not contribute to the brush. This is useful for cleaning up after CSG operations on the brush.
2206 /// Note: removal of empty faces is not performed during direct brush manipulations, because it would make a manipulation irreversible if it created an empty face.
2207 void removeEmptyFaces()
2213 while (i < m_faces.size()) {
2214 if (!m_faces[i]->contributes()) {
2224 /// \brief Constructs \p winding from the intersection of \p plane with the other planes of the brush.
2225 void windingForClipPlane(Winding &winding, const Plane3 &plane) const
2227 FixedWinding buffer[2];
2230 // get a poly that covers an effectively infinite area
2231 Winding_createInfinite(buffer[swap], plane, m_maxWorldCoord + 1);
2233 // chop the poly by all of the other faces
2235 for (std::size_t i = 0; i < m_faces.size(); ++i) {
2236 const Face &clip = *m_faces[i];
2238 if (plane3_equal(clip.plane3(), plane)
2239 || !plane3_valid(clip.plane3()) || !plane_unique(i)
2240 || plane3_opposing(plane, clip.plane3())) {
2244 buffer[!swap].clear();
2246 #if BRUSH_CONNECTIVITY_DEBUG
2247 globalOutputStream() << "clip vs face: " << i << "\n";
2251 // flip the plane, because we want to keep the back side
2252 Plane3 clipPlane(vector3_negated(clip.plane3().normal()), -clip.plane3().dist());
2253 Winding_Clip(buffer[swap], plane, clipPlane, i, buffer[!swap]);
2256 #if BRUSH_CONNECTIVITY_DEBUG
2257 for ( FixedWinding::Points::iterator k = buffer[!swap].points.begin(), j = buffer[!swap].points.end() - 1; k != buffer[!swap].points.end(); j = k, ++k )
2259 if ( vector3_length_squared( vector3_subtracted( ( *k ).vertex, ( *j ).vertex ) ) < 1 ) {
2260 globalOutputStream() << "v: " << std::distance( buffer[!swap].points.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2265 //ASSERT_MESSAGE(buffer[!swap].numpoints != 1, "created single-point winding");
2271 Winding_forFixedWinding(winding, buffer[swap]);
2273 #if BRUSH_CONNECTIVITY_DEBUG
2274 Winding_printConnectivity( winding );
2276 for ( Winding::iterator i = winding.begin(), j = winding.end() - 1; i != winding.end(); j = i, ++i )
2278 if ( vector3_length_squared( vector3_subtracted( ( *i ).vertex, ( *j ).vertex ) ) < 1 ) {
2279 globalOutputStream() << "v: " << std::distance( winding.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2285 void update_wireframe(RenderableWireframe &wire, const bool *faces_visible) const
2287 wire.m_faceVertex.resize(m_edge_indices.size());
2288 wire.m_vertices = m_uniqueVertexPoints.data();
2290 for (std::size_t i = 0; i < m_edge_faces.size(); ++i) {
2291 if (faces_visible[m_edge_faces[i].first]
2292 || faces_visible[m_edge_faces[i].second]) {
2293 wire.m_faceVertex[wire.m_size++] = m_edge_indices[i];
2299 void update_faces_wireframe(Array<PointVertex> &wire, const bool *faces_visible) const
2301 std::size_t count = 0;
2302 for (std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i) {
2303 if (faces_visible[i]) {
2309 Array<PointVertex>::iterator p = wire.begin();
2310 for (std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i) {
2311 if (faces_visible[i]) {
2312 *p++ = m_faceCentroidPoints[i];
2317 /// \brief Makes this brush a deep-copy of the \p other.
2318 void copy(const Brush &other)
2320 for (Faces::const_iterator i = other.m_faces.begin(); i != other.m_faces.end(); ++i) {
2327 void edge_push_back(FaceVertexId faceVertex)
2329 m_select_edges.push_back(SelectableEdge(m_faces, faceVertex));
2330 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2331 (*i)->edge_push_back(m_select_edges.back());
2337 m_select_edges.clear();
2338 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2343 void vertex_push_back(FaceVertexId faceVertex)
2345 m_select_vertices.push_back(SelectableVertex(m_faces, faceVertex));
2346 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2347 (*i)->vertex_push_back(m_select_vertices.back());
2353 m_select_vertices.clear();
2354 for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
2355 (*i)->vertex_clear();
2359 /// \brief Returns true if the face identified by \p index is preceded by another plane that takes priority over it.
2360 bool plane_unique(std::size_t index) const
2363 for (std::size_t i = 0; i < m_faces.size(); ++i) {
2364 if (index != i && !plane3_inside(m_faces[index]->plane3(), m_faces[i]->plane3(), index < i)) {
2371 /// \brief Removes edges that are smaller than the tolerance used when generating brush windings.
2372 void removeDegenerateEdges()
2374 for (std::size_t i = 0; i < m_faces.size(); ++i) {
2375 Winding &winding = m_faces[i]->getWinding();
2376 for (Winding::iterator j = winding.begin(); j != winding.end();) {
2377 std::size_t index = std::distance(winding.begin(), j);
2378 std::size_t next = Winding_next(winding, index);
2379 if (Edge_isDegenerate(winding[index].vertex, winding[next].vertex)) {
2380 #if BRUSH_DEGENERATE_DEBUG
2381 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate edge adjacent to " << winding[index].adjacent << "\n";
2383 Winding &other = m_faces[winding[index].adjacent]->getWinding();
2384 std::size_t adjacent = Winding_FindAdjacent(other, i);
2385 if (adjacent != c_brush_maxFaces) {
2386 other.erase(other.begin() + adjacent);
2396 /// \brief Invalidates faces that have only two vertices in their winding, while preserving edge-connectivity information.
2397 void removeDegenerateFaces()
2399 // save adjacency info for degenerate faces
2400 for (std::size_t i = 0; i < m_faces.size(); ++i) {
2401 Winding °en = m_faces[i]->getWinding();
2403 if (degen.numpoints == 2) {
2404 #if BRUSH_DEGENERATE_DEBUG
2405 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate winding adjacent to " << degen[0].adjacent << ", " << degen[1].adjacent << "\n";
2407 // this is an "edge" face, where the plane touches the edge of the brush
2409 Winding &winding = m_faces[degen[0].adjacent]->getWinding();
2410 std::size_t index = Winding_FindAdjacent(winding, i);
2411 if (index != c_brush_maxFaces) {
2412 #if BRUSH_DEGENERATE_DEBUG
2413 globalOutputStream() << "Brush::buildWindings: face " << degen[0].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[1].adjacent << "\n";
2415 winding[index].adjacent = degen[1].adjacent;
2420 Winding &winding = m_faces[degen[1].adjacent]->getWinding();
2421 std::size_t index = Winding_FindAdjacent(winding, i);
2422 if (index != c_brush_maxFaces) {
2423 #if BRUSH_DEGENERATE_DEBUG
2424 globalOutputStream() << "Brush::buildWindings: face " << degen[1].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[0].adjacent << "\n";
2426 winding[index].adjacent = degen[0].adjacent;
2435 /// \brief Removes edges that have the same adjacent-face as their immediate neighbour.
2436 void removeDuplicateEdges()
2438 // verify face connectivity graph
2439 for (std::size_t i = 0; i < m_faces.size(); ++i) {
2440 //if(m_faces[i]->contributes())
2442 Winding &winding = m_faces[i]->getWinding();
2443 for (std::size_t j = 0; j != winding.numpoints;) {
2444 std::size_t next = Winding_next(winding, j);
2445 if (winding[j].adjacent == winding[next].adjacent) {
2446 #if BRUSH_DEGENERATE_DEBUG
2447 globalOutputStream() << "Brush::buildWindings: face " << i << ": removed duplicate edge adjacent to face " << winding[j].adjacent << "\n";
2449 winding.erase(winding.begin() + next);
2458 /// \brief Removes edges that do not have a matching pair in their adjacent-face.
2459 void verifyConnectivityGraph()
2461 // verify face connectivity graph
2462 for (std::size_t i = 0; i < m_faces.size(); ++i) {
2463 //if(m_faces[i]->contributes())
2465 Winding &winding = m_faces[i]->getWinding();
2466 for (Winding::iterator j = winding.begin(); j != winding.end();) {
2467 #if BRUSH_CONNECTIVITY_DEBUG
2468 globalOutputStream() << "Brush::buildWindings: face " << i << ": adjacent to face " << ( *j ).adjacent << "\n";
2470 // remove unidirectional graph edges
2471 if ((*j).adjacent == c_brush_maxFaces
2472 || Winding_FindAdjacent(m_faces[(*j).adjacent]->getWinding(), i) == c_brush_maxFaces) {
2473 #if BRUSH_CONNECTIVITY_DEBUG
2474 globalOutputStream() << "Brush::buildWindings: face " << i << ": removing unidirectional connectivity graph edge adjacent to face " << ( *j ).adjacent << "\n";
2485 /// \brief Returns true if the brush is a finite volume. A brush without a finite volume extends past the maximum world bounds and is not valid.
2488 for (const_iterator i = begin(); i != end(); ++i) {
2489 if (!(*i)->is_bounded()) {
2496 /// \brief Constructs the polygon windings for each face of the brush. Also updates the brush bounding-box and face texture-coordinates.
2497 bool buildWindings()
2501 m_aabb_local = AABB();
2503 for (std::size_t i = 0; i < m_faces.size(); ++i) {
2504 Face &f = *m_faces[i];
2506 if (!plane3_valid(f.plane3()) || !plane_unique(i)) {
2507 f.getWinding().resize(0);
2509 #if BRUSH_CONNECTIVITY_DEBUG
2510 globalOutputStream() << "face: " << i << "\n";
2512 windingForClipPlane(f.getWinding(), f.plane3());
2514 // update brush bounds
2515 const Winding &winding = f.getWinding();
2516 for (Winding::const_iterator i = winding.begin(); i != winding.end(); ++i) {
2517 aabb_extend_by_point_safe(m_aabb_local, (*i).vertex);
2520 // update texture coordinates
2521 f.EmitTextureCoordinates();
2526 bool degenerate = !isBounded();
2529 // clean up connectivity information.
2530 // these cleanups must be applied in a specific order.
2531 removeDegenerateEdges();
2532 removeDegenerateFaces();
2533 removeDuplicateEdges();
2534 verifyConnectivityGraph();
2540 /// \brief Constructs the face windings and updates anything that depends on them.
2547 class FaceInstanceSet {
2548 typedef SelectionList<FaceInstance> FaceInstances;
2549 FaceInstances m_faceInstances;
2551 void insert(FaceInstance &faceInstance)
2553 m_faceInstances.append(faceInstance);
2556 void erase(FaceInstance &faceInstance)
2558 m_faceInstances.erase(faceInstance);
2561 template<typename Functor>
2562 void foreach(Functor functor)
2564 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
2571 return m_faceInstances.empty();
2574 FaceInstance &last() const
2576 return m_faceInstances.back();
2580 extern FaceInstanceSet g_SelectedFaceInstances;
2582 typedef std::list<std::size_t> VertexSelection;
2584 inline VertexSelection::iterator VertexSelection_find(VertexSelection &self, std::size_t value)
2586 return std::find(self.begin(), self.end(), value);
2589 inline VertexSelection::const_iterator VertexSelection_find(const VertexSelection &self, std::size_t value)
2591 return std::find(self.begin(), self.end(), value);
2594 inline VertexSelection::iterator VertexSelection_insert(VertexSelection &self, std::size_t value)
2596 VertexSelection::iterator i = VertexSelection_find(self, value);
2597 if (i == self.end()) {
2598 self.push_back(value);
2599 return --self.end();
2604 inline void VertexSelection_erase(VertexSelection &self, std::size_t value)
2606 VertexSelection::iterator i = VertexSelection_find(self, value);
2607 if (i != self.end()) {
2612 inline bool triangle_reversed(std::size_t x, std::size_t y, std::size_t z)
2614 return !((x < y && y < z) || (z < x && x < y) || (y < z && z < x));
2617 template<typename Element>
2619 triangle_cross(const BasicVector3<Element> &x, const BasicVector3<Element> y, const BasicVector3<Element> &z)
2621 return vector3_cross(y - x, z - x);
2624 template<typename Element>
2626 triangles_same_winding(const BasicVector3<Element> &x1, const BasicVector3<Element> y1, const BasicVector3<Element> &z1,
2627 const BasicVector3<Element> &x2, const BasicVector3<Element> y2, const BasicVector3<Element> &z2)
2629 return vector3_dot(triangle_cross(x1, y1, z1), triangle_cross(x2, y2, z2)) > 0;
2633 typedef const Plane3 *PlanePointer;
2634 typedef PlanePointer *PlanesIterator;
2636 class VectorLightList : public LightList {
2637 typedef std::vector<const RendererLight *> Lights;
2640 void addLight(const RendererLight &light)
2642 m_lights.push_back(&light);
2650 void evaluateLights() const
2654 void lightsChanged() const
2658 void forEachLight(const RendererLightCallback &callback) const
2660 for (Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i) {
2666 class FaceInstance {
2668 ObservedSelectable m_selectable;
2669 ObservedSelectable m_selectableVertices;
2670 ObservedSelectable m_selectableEdges;
2671 SelectionChangeCallback m_selectionChanged;
2673 VertexSelection m_vertexSelection;
2674 VertexSelection m_edgeSelection;
2677 mutable VectorLightList m_lights;
2679 FaceInstance(Face &face, const SelectionChangeCallback &observer) :
2681 m_selectable(SelectedChangedCaller(*this)),
2682 m_selectableVertices(observer),
2683 m_selectableEdges(observer),
2684 m_selectionChanged(observer)
2688 FaceInstance(const FaceInstance &other) :
2689 m_face(other.m_face),
2690 m_selectable(SelectedChangedCaller(*this)),
2691 m_selectableVertices(other.m_selectableVertices),
2692 m_selectableEdges(other.m_selectableEdges),
2693 m_selectionChanged(other.m_selectionChanged)
2697 FaceInstance &operator=(const FaceInstance &other)
2699 m_face = other.m_face;
2708 const Face &getFace() const
2713 void selectedChanged(const Selectable &selectable)
2715 if (selectable.isSelected()) {
2716 g_SelectedFaceInstances.insert(*this);
2718 g_SelectedFaceInstances.erase(*this);
2720 m_selectionChanged(selectable);
2723 typedef MemberCaller<FaceInstance, void(const Selectable &), &FaceInstance::selectedChanged> SelectedChangedCaller;
2725 bool selectedVertices() const
2727 return !m_vertexSelection.empty();
2730 bool selectedEdges() const
2732 return !m_edgeSelection.empty();
2735 bool isSelected() const
2737 return m_selectable.isSelected();
2740 bool selectedComponents() const
2742 return selectedVertices() || selectedEdges() || isSelected();
2745 bool selectedComponents(SelectionSystem::EComponentMode mode) const
2748 case SelectionSystem::eVertex:
2749 return selectedVertices();
2750 case SelectionSystem::eEdge:
2751 return selectedEdges();
2752 case SelectionSystem::eFace:
2753 return isSelected();
2759 void setSelected(SelectionSystem::EComponentMode mode, bool select)
2762 case SelectionSystem::eFace:
2763 m_selectable.setSelected(select);
2765 case SelectionSystem::eVertex:
2766 ASSERT_MESSAGE(!select, "select-all not supported");
2768 m_vertexSelection.clear();
2769 m_selectableVertices.setSelected(false);
2771 case SelectionSystem::eEdge:
2772 ASSERT_MESSAGE(!select, "select-all not supported");
2774 m_edgeSelection.clear();
2775 m_selectableEdges.setSelected(false);
2782 template<typename Functor>
2783 void SelectedVertices_foreach(Functor functor) const
2785 for (VertexSelection::const_iterator i = m_vertexSelection.begin(); i != m_vertexSelection.end(); ++i) {
2786 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *i);
2787 if (index != c_brush_maxFaces) {
2788 functor(getFace().getWinding()[index].vertex);
2793 template<typename Functor>
2794 void SelectedEdges_foreach(Functor functor) const
2796 for (VertexSelection::const_iterator i = m_edgeSelection.begin(); i != m_edgeSelection.end(); ++i) {
2797 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *i);
2798 if (index != c_brush_maxFaces) {
2799 const Winding &winding = getFace().getWinding();
2800 std::size_t adjacent = Winding_next(winding, index);
2801 functor(vector3_mid(winding[index].vertex, winding[adjacent].vertex));
2806 template<typename Functor>
2807 void SelectedFaces_foreach(Functor functor) const
2810 functor(centroid());
2814 template<typename Functor>
2815 void SelectedComponents_foreach(Functor functor) const
2817 SelectedVertices_foreach(functor);
2818 SelectedEdges_foreach(functor);
2819 SelectedFaces_foreach(functor);
2822 void iterate_selected(AABB &aabb) const
2824 SelectedComponents_foreach([&](const Vector3 &point) {
2825 aabb_extend_by_point_safe(aabb, point);
2829 void iterate_selected(RenderablePointVector &points) const
2831 SelectedComponents_foreach([&](const Vector3 &point) {
2832 const Colour4b colour_selected(0, 0, 255, 255);
2833 points.push_back(pointvertex_for_windingpoint(point, colour_selected));
2837 bool intersectVolume(const VolumeTest &volume, const Matrix4 &localToWorld) const
2839 return m_face->intersectVolume(volume, localToWorld);
2842 void render(Renderer &renderer, const VolumeTest &volume, const Matrix4 &localToWorld) const
2844 if (!m_face->isFiltered() && m_face->contributes() && intersectVolume(volume, localToWorld)) {
2845 renderer.PushState();
2846 if (selectedComponents()) {
2847 renderer.Highlight(Renderer::eFace);
2849 m_face->render(renderer, localToWorld);
2850 renderer.PopState();
2854 void testSelect(SelectionTest &test, SelectionIntersection &best)
2856 if (!m_face->isFiltered()) {
2857 m_face->testSelect(test, best);
2861 void testSelect(Selector &selector, SelectionTest &test)
2863 SelectionIntersection best;
2864 testSelect(test, best);
2866 Selector_add(selector, m_selectable, best);
2870 void testSelect_centroid(Selector &selector, SelectionTest &test)
2872 if (m_face->contributes() && !m_face->isFiltered()) {
2873 SelectionIntersection best;
2874 m_face->testSelect_centroid(test, best);
2876 Selector_add(selector, m_selectable, best);
2881 void selectPlane(Selector &selector, const Line &line, PlanesIterator first, PlanesIterator last,
2882 const PlaneCallback &selectedPlaneCallback)
2884 for (Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i) {
2885 Vector3 v(vector3_subtracted(line_closest_point(line, (*i).vertex), (*i).vertex));
2886 double dot = vector3_dot(getFace().plane3().normal(), v);
2892 Selector_add(selector, m_selectable);
2894 selectedPlaneCallback(getFace().plane3());
2897 void selectReversedPlane(Selector &selector, const SelectedPlanes &selectedPlanes)
2899 if (selectedPlanes.contains(plane3_flipped(getFace().plane3()))) {
2900 Selector_add(selector, m_selectable);
2904 void transformComponents(const Matrix4 &matrix)
2907 m_face->transform(matrix, false);
2909 if (selectedVertices()) {
2910 if (m_vertexSelection.size() == 1) {
2911 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
2912 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
2913 } else if (m_vertexSelection.size() == 2) {
2914 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
2915 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
2916 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
2917 } else if (m_vertexSelection.size() >= 3) {
2918 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
2919 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
2920 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
2921 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
2924 if (selectedEdges()) {
2925 if (m_edgeSelection.size() == 1) {
2926 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
2927 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
2928 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
2929 } else if (m_edgeSelection.size() >= 2) {
2930 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[0]);
2931 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[1]);
2932 matrix4_transform_point(matrix, m_face->m_move_planeptsTransformed[2]);
2933 m_face->assign_planepts(m_face->m_move_planeptsTransformed);
2938 void snapto(float snap)
2940 m_face->snapto(snap);
2943 void snapComponents(float snap)
2948 if (selectedVertices()) {
2949 vector3_snap(m_face->m_move_planepts[0], snap);
2950 vector3_snap(m_face->m_move_planepts[1], snap);
2951 vector3_snap(m_face->m_move_planepts[2], snap);
2952 m_face->assign_planepts(m_face->m_move_planepts);
2953 planepts_assign(m_face->m_move_planeptsTransformed, m_face->m_move_planepts);
2954 m_face->freezeTransform();
2956 if (selectedEdges()) {
2957 vector3_snap(m_face->m_move_planepts[0], snap);
2958 vector3_snap(m_face->m_move_planepts[1], snap);
2959 vector3_snap(m_face->m_move_planepts[2], snap);
2960 m_face->assign_planepts(m_face->m_move_planepts);
2961 planepts_assign(m_face->m_move_planeptsTransformed, m_face->m_move_planepts);
2962 m_face->freezeTransform();
2966 void update_move_planepts_vertex(std::size_t index)
2968 m_face->update_move_planepts_vertex(index, m_face->m_move_planepts);
2971 void update_move_planepts_vertex2(std::size_t index, std::size_t other)
2973 const std::size_t numpoints = m_face->getWinding().numpoints;
2974 ASSERT_MESSAGE(index < numpoints, "select_vertex: invalid index");
2976 const std::size_t opposite = Winding_Opposite(m_face->getWinding(), index, other);
2978 if (triangle_reversed(index, other, opposite)) {
2979 std::swap(index, other);
2983 triangles_same_winding(
2984 m_face->getWinding()[opposite].vertex,
2985 m_face->getWinding()[index].vertex,
2986 m_face->getWinding()[other].vertex,
2987 m_face->getWinding()[0].vertex,
2988 m_face->getWinding()[1].vertex,
2989 m_face->getWinding()[2].vertex
2991 "update_move_planepts_vertex2: error"
2994 m_face->m_move_planepts[0] = m_face->getWinding()[opposite].vertex;
2995 m_face->m_move_planepts[1] = m_face->getWinding()[index].vertex;
2996 m_face->m_move_planepts[2] = m_face->getWinding()[other].vertex;
2997 planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
3000 void update_selection_vertex()
3002 if (m_vertexSelection.size() == 0) {
3003 m_selectableVertices.setSelected(false);
3005 m_selectableVertices.setSelected(true);
3007 if (m_vertexSelection.size() == 1) {
3008 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_vertexSelection.begin());
3010 if (index != c_brush_maxFaces) {
3011 update_move_planepts_vertex(index);
3013 } else if (m_vertexSelection.size() == 2) {
3014 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_vertexSelection.begin());
3015 std::size_t other = Winding_FindAdjacent(getFace().getWinding(), *(++m_vertexSelection.begin()));
3017 if (index != c_brush_maxFaces
3018 && other != c_brush_maxFaces) {
3019 update_move_planepts_vertex2(index, other);
3025 void select_vertex(std::size_t index, bool select)
3028 VertexSelection_insert(m_vertexSelection, getFace().getWinding()[index].adjacent);
3030 VertexSelection_erase(m_vertexSelection, getFace().getWinding()[index].adjacent);
3033 SceneChangeNotify();
3034 update_selection_vertex();
3037 bool selected_vertex(std::size_t index) const
3039 return VertexSelection_find(m_vertexSelection, getFace().getWinding()[index].adjacent) !=
3040 m_vertexSelection.end();
3043 void update_move_planepts_edge(std::size_t index)
3045 std::size_t numpoints = m_face->getWinding().numpoints;
3046 ASSERT_MESSAGE(index < numpoints, "select_edge: invalid index");
3048 std::size_t adjacent = Winding_next(m_face->getWinding(), index);
3049 std::size_t opposite = Winding_Opposite(m_face->getWinding(), index);
3050 m_face->m_move_planepts[0] = m_face->getWinding()[index].vertex;
3051 m_face->m_move_planepts[1] = m_face->getWinding()[adjacent].vertex;
3052 m_face->m_move_planepts[2] = m_face->getWinding()[opposite].vertex;
3053 planepts_quantise(m_face->m_move_planepts, GRID_MIN); // winding points are very inaccurate
3056 void update_selection_edge()
3058 if (m_edgeSelection.size() == 0) {
3059 m_selectableEdges.setSelected(false);
3061 m_selectableEdges.setSelected(true);
3063 if (m_edgeSelection.size() == 1) {
3064 std::size_t index = Winding_FindAdjacent(getFace().getWinding(), *m_edgeSelection.begin());
3066 if (index != c_brush_maxFaces) {
3067 update_move_planepts_edge(index);
3073 void select_edge(std::size_t index, bool select)
3076 VertexSelection_insert(m_edgeSelection, getFace().getWinding()[index].adjacent);
3078 VertexSelection_erase(m_edgeSelection, getFace().getWinding()[index].adjacent);
3081 SceneChangeNotify();
3082 update_selection_edge();
3085 bool selected_edge(std::size_t index) const
3087 return VertexSelection_find(m_edgeSelection, getFace().getWinding()[index].adjacent) != m_edgeSelection.end();
3090 const Vector3 ¢roid() const
3092 return m_face->centroid();
3095 void connectivityChanged()
3097 // This occurs when a face is added or removed.
3098 // The current vertex and edge selections no longer valid and must be cleared.
3099 m_vertexSelection.clear();
3100 m_selectableVertices.setSelected(false);
3101 m_edgeSelection.clear();
3102 m_selectableEdges.setSelected(false);
3106 class BrushClipPlane : public OpenGLRenderable {
3109 static Shader *m_state;
3111 static void constructStatic()
3113 m_state = GlobalShaderCache().capture("$CLIPPER_OVERLAY");
3116 static void destroyStatic()
3118 GlobalShaderCache().release("$CLIPPER_OVERLAY");
3121 void setPlane(const Brush &brush, const Plane3 &plane)
3124 if (plane3_valid(m_plane)) {
3125 brush.windingForClipPlane(m_winding, m_plane);
3127 m_winding.resize(0);
3131 void render(RenderStateFlags state) const
3133 if ((state & RENDER_FILL) != 0) {
3134 Winding_Draw(m_winding, m_plane.normal(), state);
3136 Winding_DrawWireframe(m_winding);
3138 // also draw a line indicating the direction of the cut
3139 Vector3 lineverts[2];
3140 Winding_Centroid(m_winding, m_plane, lineverts[0]);
3141 lineverts[1] = vector3_added(lineverts[0], vector3_scaled(m_plane.normal(), Brush::m_maxWorldCoord * 4));
3143 glVertexPointer(3, GL_FLOAT, sizeof(Vector3), &lineverts[0]);
3144 glDrawArrays(GL_LINES, 0, GLsizei(2));
3148 void render(Renderer &renderer, const VolumeTest &volume, const Matrix4 &localToWorld) const
3150 renderer.SetState(m_state, Renderer::eWireframeOnly);
3151 renderer.SetState(m_state, Renderer::eFullMaterials);
3152 renderer.addRenderable(*this, localToWorld);
3156 inline void Face_addLight(const FaceInstance &face, const Matrix4 &localToWorld, const RendererLight &light)
3158 const Plane3 &facePlane = face.getFace().plane3();
3159 const Vector3 &origin = light.aabb().origin;
3160 Plane3 tmp(plane3_transformed(Plane3(facePlane.normal(), -facePlane.dist()), localToWorld));
3161 if (!plane3_test_point(tmp, origin)
3162 || !plane3_test_point(tmp, vector3_added(origin, light.offset()))) {
3163 face.m_lights.addLight(light);
3168 typedef std::vector<FaceInstance> FaceInstances;
3170 class EdgeInstance : public Selectable {
3171 FaceInstances &m_faceInstances;
3172 SelectableEdge *m_edge;
3174 void select_edge(bool select)
3176 FaceVertexId faceVertex = m_edge->m_faceVertex;
3177 m_faceInstances[faceVertex.getFace()].select_edge(faceVertex.getVertex(), select);
3178 faceVertex = next_edge(m_edge->m_faces, faceVertex);
3179 m_faceInstances[faceVertex.getFace()].select_edge(faceVertex.getVertex(), select);
3182 bool selected_edge() const
3184 FaceVertexId faceVertex = m_edge->m_faceVertex;
3185 if (!m_faceInstances[faceVertex.getFace()].selected_edge(faceVertex.getVertex())) {
3188 faceVertex = next_edge(m_edge->m_faces, faceVertex);
3189 if (!m_faceInstances[faceVertex.getFace()].selected_edge(faceVertex.getVertex())) {
3197 EdgeInstance(FaceInstances &faceInstances, SelectableEdge &edge)
3198 : m_faceInstances(faceInstances), m_edge(&edge)
3202 EdgeInstance &operator=(const EdgeInstance &other)
3204 m_edge = other.m_edge;
3208 void setSelected(bool select)
3210 select_edge(select);
3213 bool isSelected() const
3215 return selected_edge();
3219 void testSelect(Selector &selector, SelectionTest &test)
3221 SelectionIntersection best;
3222 m_edge->testSelect(test, best);
3224 Selector_add(selector, *this, best);
3229 class VertexInstance : public Selectable {
3230 FaceInstances &m_faceInstances;
3231 SelectableVertex *m_vertex;
3233 void select_vertex(bool select)
3235 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3237 m_faceInstances[faceVertex.getFace()].select_vertex(faceVertex.getVertex(), select);
3238 faceVertex = next_vertex(m_vertex->m_faces, faceVertex);
3239 } while (faceVertex.getFace() != m_vertex->m_faceVertex.getFace());
3242 bool selected_vertex() const
3244 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3246 if (!m_faceInstances[faceVertex.getFace()].selected_vertex(faceVertex.getVertex())) {
3249 faceVertex = next_vertex(m_vertex->m_faces, faceVertex);
3250 } while (faceVertex.getFace() != m_vertex->m_faceVertex.getFace());
3255 VertexInstance(FaceInstances &faceInstances, SelectableVertex &vertex)
3256 : m_faceInstances(faceInstances), m_vertex(&vertex)
3260 VertexInstance &operator=(const VertexInstance &other)
3262 m_vertex = other.m_vertex;
3266 void setSelected(bool select)
3268 select_vertex(select);
3271 bool isSelected() const
3273 return selected_vertex();
3276 void testSelect(Selector &selector, SelectionTest &test)
3278 SelectionIntersection best;
3279 m_vertex->testSelect(test, best);
3281 Selector_add(selector, *this, best);
3286 class BrushInstanceVisitor {
3288 virtual void visit(FaceInstance &face) const = 0;
3291 class BrushInstance :
3292 public BrushObserver,
3293 public scene::Instance,
3296 public SelectionTestable,
3297 public ComponentSelectionTestable,
3298 public ComponentEditable,
3299 public ComponentSnappable,
3300 public PlaneSelectable,
3301 public LightCullable {
3303 InstanceTypeCastTable m_casts;
3307 InstanceStaticCast<BrushInstance, Selectable>::install(m_casts);
3308 InstanceContainedCast<BrushInstance, Bounded>::install(m_casts);
3309 InstanceContainedCast<BrushInstance, Cullable>::install(m_casts);
3310 InstanceStaticCast<BrushInstance, Renderable>::install(m_casts);
3311 InstanceStaticCast<BrushInstance, SelectionTestable>::install(m_casts);
3312 InstanceStaticCast<BrushInstance, ComponentSelectionTestable>::install(m_casts);
3313 InstanceStaticCast<BrushInstance, ComponentEditable>::install(m_casts);
3314 InstanceStaticCast<BrushInstance, ComponentSnappable>::install(m_casts);
3315 InstanceStaticCast<BrushInstance, PlaneSelectable>::install(m_casts);
3316 InstanceIdentityCast<BrushInstance>::install(m_casts);
3317 InstanceContainedCast<BrushInstance, Transformable>::install(m_casts);
3320 InstanceTypeCastTable &get()
3329 FaceInstances m_faceInstances;
3331 typedef std::vector<EdgeInstance> EdgeInstances;
3332 EdgeInstances m_edgeInstances;
3333 typedef std::vector<VertexInstance> VertexInstances;
3334 VertexInstances m_vertexInstances;
3336 ObservedSelectable m_selectable;
3338 mutable RenderableWireframe m_render_wireframe;
3339 mutable RenderablePointVector m_render_selected;
3340 mutable AABB m_aabb_component;
3341 mutable Array<PointVertex> m_faceCentroidPointsCulled;
3342 RenderablePointArray m_render_faces_wireframe;
3343 mutable bool m_viewChanged; // requires re-evaluation of view-dependent cached data
3345 BrushClipPlane m_clipPlane;
3347 static Shader *m_state_selpoint;
3349 const LightList *m_lightList;
3351 TransformModifier m_transform;
3353 BrushInstance(const BrushInstance &other); // NOT COPYABLE
3354 BrushInstance &operator=(const BrushInstance &other); // NOT ASSIGNABLE
3356 static Counter *m_counter;
3358 typedef LazyStatic<TypeCasts> StaticTypeCasts;
3360 void lightsChanged()
3362 m_lightList->lightsChanged();
3365 typedef MemberCaller<BrushInstance, void(), &BrushInstance::lightsChanged> LightsChangedCaller;
3367 STRING_CONSTANT(Name, "BrushInstance");
3369 BrushInstance(const scene::Path &path, scene::Instance *parent, Brush &brush) :
3370 Instance(path, parent, this, StaticTypeCasts::instance().get()),
3372 m_selectable(SelectedChangedCaller(*this)),
3373 m_render_selected(GL_POINTS),
3374 m_render_faces_wireframe(m_faceCentroidPointsCulled, GL_POINTS),
3375 m_viewChanged(false),
3376 m_transform(Brush::TransformChangedCaller(m_brush), ApplyTransformCaller(*this))
3378 m_brush.instanceAttach(Instance::path());
3379 m_brush.attach(*this);
3380 m_counter->increment();
3382 m_lightList = &GlobalShaderCache().attach(*this);
3383 m_brush.m_lightsChanged = LightsChangedCaller(*this); ///\todo Make this work with instancing.
3385 Instance::setTransformChangedCallback(LightsChangedCaller(*this));
3390 Instance::setTransformChangedCallback(Callback<void()>());
3392 m_brush.m_lightsChanged = Callback<void()>();
3393 GlobalShaderCache().detach(*this);
3395 m_counter->decrement();
3396 m_brush.detach(*this);
3397 m_brush.instanceDetach(Instance::path());
3405 const Brush &getBrush() const
3410 Bounded &get(NullType<Bounded>)
3415 Cullable &get(NullType<Cullable>)
3420 Transformable &get(NullType<Transformable>)
3425 void selectedChanged(const Selectable &selectable)
3427 GlobalSelectionSystem().getObserver(SelectionSystem::ePrimitive)(selectable);
3428 GlobalSelectionSystem().onSelectedChanged(*this, selectable);
3430 Instance::selectedChanged();
3433 typedef MemberCaller<BrushInstance, void(
3434 const Selectable &), &BrushInstance::selectedChanged> SelectedChangedCaller;
3436 void selectedChangedComponent(const Selectable &selectable)
3438 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
3439 GlobalSelectionSystem().onComponentSelection(*this, selectable);
3442 typedef MemberCaller<BrushInstance, void(
3443 const Selectable &), &BrushInstance::selectedChangedComponent> SelectedChangedComponentCaller;
3445 const BrushInstanceVisitor &forEachFaceInstance(const BrushInstanceVisitor &visitor)
3447 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3453 static void constructStatic()
3455 m_state_selpoint = GlobalShaderCache().capture("$SELPOINT");
3458 static void destroyStatic()
3460 GlobalShaderCache().release("$SELPOINT");
3465 m_faceInstances.clear();
3468 void reserve(std::size_t size)
3470 m_faceInstances.reserve(size);
3473 void push_back(Face &face)
3475 m_faceInstances.push_back(FaceInstance(face, SelectedChangedComponentCaller(*this)));
3480 ASSERT_MESSAGE(!m_faceInstances.empty(), "erasing invalid element");
3481 m_faceInstances.pop_back();
3484 void erase(std::size_t index)
3486 ASSERT_MESSAGE(index < m_faceInstances.size(), "erasing invalid element");
3487 m_faceInstances.erase(m_faceInstances.begin() + index);
3490 void connectivityChanged()
3492 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3493 (*i).connectivityChanged();
3499 m_edgeInstances.clear();
3502 void edge_push_back(SelectableEdge &edge)
3504 m_edgeInstances.push_back(EdgeInstance(m_faceInstances, edge));
3509 m_vertexInstances.clear();
3512 void vertex_push_back(SelectableVertex &vertex)
3514 m_vertexInstances.push_back(VertexInstance(m_faceInstances, vertex));
3517 void DEBUG_verify() const
3519 ASSERT_MESSAGE(m_faceInstances.size() == m_brush.DEBUG_size(), "FATAL: mismatch");
3522 bool isSelected() const
3524 return m_selectable.isSelected();
3527 void setSelected(bool select)
3529 m_selectable.setSelected(select);
3532 void update_selected() const
3534 m_render_selected.clear();
3535 for (FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3536 if ((*i).getFace().contributes()) {
3537 (*i).iterate_selected(m_render_selected);
3542 void evaluateViewDependent(const VolumeTest &volume, const Matrix4 &localToWorld) const
3544 if (m_viewChanged) {
3545 m_viewChanged = false;
3547 bool faces_visible[c_brush_maxFaces];
3549 bool *j = faces_visible;
3550 for (FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i, ++j) {
3551 *j = (*i).intersectVolume(volume, localToWorld);
3555 m_brush.update_wireframe(m_render_wireframe, faces_visible);
3556 m_brush.update_faces_wireframe(m_faceCentroidPointsCulled, faces_visible);
3560 void renderComponentsSelected(Renderer &renderer, const VolumeTest &volume, const Matrix4 &localToWorld) const
3562 m_brush.evaluateBRep();
3565 if (!m_render_selected.empty()) {
3566 renderer.Highlight(Renderer::ePrimitive, false);
3567 renderer.SetState(m_state_selpoint, Renderer::eWireframeOnly);
3568 renderer.SetState(m_state_selpoint, Renderer::eFullMaterials);
3569 renderer.addRenderable(m_render_selected, localToWorld);
3573 void renderComponents(Renderer &renderer, const VolumeTest &volume) const
3575 m_brush.evaluateBRep();
3577 const Matrix4 &localToWorld = Instance::localToWorld();
3579 renderer.SetState(m_brush.m_state_point, Renderer::eWireframeOnly);
3580 renderer.SetState(m_brush.m_state_point, Renderer::eFullMaterials);
3582 if (volume.fill() && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace) {
3583 evaluateViewDependent(volume, localToWorld);
3584 renderer.addRenderable(m_render_faces_wireframe, localToWorld);
3586 m_brush.renderComponents(GlobalSelectionSystem().ComponentMode(), renderer, volume, localToWorld);
3590 void renderClipPlane(Renderer &renderer, const VolumeTest &volume) const
3592 if (GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip && isSelected()) {
3593 m_clipPlane.render(renderer, volume, localToWorld());
3597 void renderCommon(Renderer &renderer, const VolumeTest &volume) const
3599 bool componentMode = GlobalSelectionSystem().Mode() == SelectionSystem::eComponent;
3601 if (componentMode && isSelected()) {
3602 renderComponents(renderer, volume);
3605 if (parentSelected()) {
3606 if (!componentMode) {
3607 renderer.Highlight(Renderer::eFace);
3609 renderer.Highlight(Renderer::ePrimitive);
3613 void renderSolid(Renderer &renderer, const VolumeTest &volume, const Matrix4 &localToWorld) const
3615 //renderCommon(renderer, volume);
3617 m_lightList->evaluateLights();
3619 for (FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3620 renderer.setLights((*i).m_lights);
3621 (*i).render(renderer, volume, localToWorld);
3624 renderComponentsSelected(renderer, volume, localToWorld);
3627 void renderWireframe(Renderer &renderer, const VolumeTest &volume, const Matrix4 &localToWorld) const
3629 //renderCommon(renderer, volume);
3631 evaluateViewDependent(volume, localToWorld);
3633 if (m_render_wireframe.m_size != 0) {
3634 renderer.addRenderable(m_render_wireframe, localToWorld);
3637 renderComponentsSelected(renderer, volume, localToWorld);
3640 void renderSolid(Renderer &renderer, const VolumeTest &volume) const
3642 m_brush.evaluateBRep();
3644 renderClipPlane(renderer, volume);
3646 renderSolid(renderer, volume, localToWorld());
3649 void renderWireframe(Renderer &renderer, const VolumeTest &volume) const
3651 m_brush.evaluateBRep();
3653 renderClipPlane(renderer, volume);
3655 renderWireframe(renderer, volume, localToWorld());
3658 void viewChanged() const
3660 m_viewChanged = true;
3663 void testSelect(Selector &selector, SelectionTest &test)
3665 test.BeginMesh(localToWorld());
3667 SelectionIntersection best;
3668 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3669 (*i).testSelect(test, best);
3672 selector.addIntersection(best);
3676 bool isSelectedComponents() const
3678 for (FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3679 if ((*i).selectedComponents()) {
3686 void setSelectedComponents(bool select, SelectionSystem::EComponentMode mode)
3688 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3689 (*i).setSelected(mode, select);
3693 void testSelectComponents(Selector &selector, SelectionTest &test, SelectionSystem::EComponentMode mode)
3695 test.BeginMesh(localToWorld());
3698 case SelectionSystem::eVertex: {
3699 for (VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i) {
3700 (*i).testSelect(selector, test);
3704 case SelectionSystem::eEdge: {
3705 for (EdgeInstances::iterator i = m_edgeInstances.begin(); i != m_edgeInstances.end(); ++i) {
3706 (*i).testSelect(selector, test);
3710 case SelectionSystem::eFace: {
3711 if (test.getVolume().fill()) {
3712 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3713 (*i).testSelect(selector, test);
3716 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3717 (*i).testSelect_centroid(selector, test);
3727 void selectPlanes(Selector &selector, SelectionTest &test, const PlaneCallback &selectedPlaneCallback)
3729 test.BeginMesh(localToWorld());
3731 PlanePointer brushPlanes[c_brush_maxFaces];
3732 PlanesIterator j = brushPlanes;
3734 for (Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i) {
3735 *j++ = &(*i)->plane3();
3738 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3739 (*i).selectPlane(selector, Line(test.getNear(), test.getFar()), brushPlanes, j, selectedPlaneCallback);
3743 void selectReversedPlanes(Selector &selector, const SelectedPlanes &selectedPlanes)
3745 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3746 (*i).selectReversedPlane(selector, selectedPlanes);
3751 void transformComponents(const Matrix4 &matrix)
3753 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3754 (*i).transformComponents(matrix);
3758 const AABB &getSelectedComponentsBounds() const
3760 m_aabb_component = AABB();
3762 for (FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3763 (*i).iterate_selected(m_aabb_component);
3766 return m_aabb_component;
3769 void snapComponents(float snap)
3771 for (FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3772 (*i).snapComponents(snap);
3776 void evaluateTransform()
3778 Matrix4 matrix(m_transform.calculateTransform());
3779 //globalOutputStream() << "matrix: " << matrix << "\n";
3781 if (m_transform.getType() == TRANSFORM_PRIMITIVE) {
3782 m_brush.transform(matrix);
3784 transformComponents(matrix);
3788 void applyTransform()
3790 m_brush.revertTransform();
3791 evaluateTransform();
3792 m_brush.freezeTransform();
3795 typedef MemberCaller<BrushInstance, void(), &BrushInstance::applyTransform> ApplyTransformCaller;
3797 void setClipPlane(const Plane3 &plane)
3799 m_clipPlane.setPlane(m_brush, plane);
3802 bool testLight(const RendererLight &light) const
3804 return light.testAABB(worldAABB());
3807 void insertLight(const RendererLight &light)
3809 const Matrix4 &localToWorld = Instance::localToWorld();
3810 for (FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3811 Face_addLight(*i, localToWorld, light);
3817 for (FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i) {
3818 (*i).m_lights.clear();
3823 inline BrushInstance *Instance_getBrush(scene::Instance &instance)
3825 return InstanceTypeCast<BrushInstance>::cast(instance);
3829 template<typename Functor>
3830 class BrushSelectedVisitor : public SelectionSystem::Visitor {
3831 const Functor &m_functor;
3833 BrushSelectedVisitor(const Functor &functor) : m_functor(functor)
3837 void visit(scene::Instance &instance) const
3839 BrushInstance *brush = Instance_getBrush(instance);
3846 template<typename Functor>
3847 inline const Functor &Scene_forEachSelectedBrush(const Functor &functor)
3849 GlobalSelectionSystem().foreachSelected(BrushSelectedVisitor<Functor>(functor));
3853 template<typename Functor>
3854 class BrushVisibleSelectedVisitor : public SelectionSystem::Visitor {
3855 const Functor &m_functor;
3857 BrushVisibleSelectedVisitor(const Functor &functor) : m_functor(functor)
3861 void visit(scene::Instance &instance) const
3863 BrushInstance *brush = Instance_getBrush(instance);
3865 && instance.path().top().get().visible()) {
3871 template<typename Functor>
3872 inline const Functor &Scene_forEachVisibleSelectedBrush(const Functor &functor)
3874 GlobalSelectionSystem().foreachSelected(BrushVisibleSelectedVisitor<Functor>(functor));
3878 class BrushForEachFace {
3879 const BrushInstanceVisitor &m_visitor;
3881 BrushForEachFace(const BrushInstanceVisitor &visitor) : m_visitor(visitor)
3885 void operator()(BrushInstance &brush) const
3887 brush.forEachFaceInstance(m_visitor);
3891 template<class Functor>
3892 class FaceInstanceVisitFace : public BrushInstanceVisitor {
3893 const Functor &functor;
3895 FaceInstanceVisitFace(const Functor &functor)
3900 void visit(FaceInstance &face) const
3902 functor(face.getFace());
3906 template<typename Functor>
3907 inline const Functor &Brush_forEachFace(BrushInstance &brush, const Functor &functor)
3909 brush.forEachFaceInstance(FaceInstanceVisitFace<Functor>(functor));
3913 template<class Functor>
3914 class FaceVisitAll : public BrushVisitor {
3915 const Functor &functor;
3917 FaceVisitAll(const Functor &functor)
3922 void visit(Face &face) const
3928 template<typename Functor>
3929 inline const Functor &Brush_forEachFace(const Brush &brush, const Functor &functor)
3931 brush.forEachFace(FaceVisitAll<Functor>(functor));
3935 template<typename Functor>
3936 inline const Functor &Brush_forEachFace(Brush &brush, const Functor &functor)
3938 brush.forEachFace(FaceVisitAll<Functor>(functor));
3942 template<class Functor>
3943 class FaceInstanceVisitAll : public BrushInstanceVisitor {
3944 const Functor &functor;
3946 FaceInstanceVisitAll(const Functor &functor)
3951 void visit(FaceInstance &face) const
3957 template<typename Functor>
3958 inline const Functor &Brush_ForEachFaceInstance(BrushInstance &brush, const Functor &functor)
3960 brush.forEachFaceInstance(FaceInstanceVisitAll<Functor>(functor));
3964 template<typename Functor>
3965 inline const Functor &Scene_forEachBrush(scene::Graph &graph, const Functor &functor)
3967 graph.traverse(InstanceWalker<InstanceApply<BrushInstance, Functor> >(functor));
3971 template<typename Type, typename Functor>
3972 class InstanceIfVisible : public Functor {
3974 InstanceIfVisible(const Functor &functor) : Functor(functor)
3978 void operator()(scene::Instance &instance)
3980 if (instance.path().top().get().visible()) {
3981 Functor::operator()(instance);
3986 template<typename Functor>
3987 class BrushVisibleWalker : public scene::Graph::Walker {
3988 const Functor &m_functor;
3990 BrushVisibleWalker(const Functor &functor) : m_functor(functor)
3994 bool pre(const scene::Path &path, scene::Instance &instance) const
3996 if (path.top().get().visible()) {
3997 BrushInstance *brush = Instance_getBrush(instance);
4006 template<typename Functor>
4007 inline const Functor &Scene_forEachVisibleBrush(scene::Graph &graph, const Functor &functor)
4009 graph.traverse(BrushVisibleWalker<Functor>(functor));
4013 template<typename Functor>
4014 inline const Functor &Scene_ForEachBrush_ForEachFace(scene::Graph &graph, const Functor &functor)
4016 Scene_forEachBrush(graph, BrushForEachFace(FaceInstanceVisitFace<Functor>(functor)));
4021 template<typename Functor>
4022 inline const Functor &Scene_ForEachBrush_ForEachFaceInstance(scene::Graph &graph, const Functor &functor)
4024 Scene_forEachBrush(graph, BrushForEachFace(FaceInstanceVisitAll<Functor>(functor)));
4028 template<typename Functor>
4029 inline const Functor &Scene_ForEachSelectedBrush_ForEachFace(scene::Graph &graph, const Functor &functor)
4031 Scene_forEachSelectedBrush(BrushForEachFace(FaceInstanceVisitFace<Functor>(functor)));
4035 template<typename Functor>
4036 inline const Functor &Scene_ForEachSelectedBrush_ForEachFaceInstance(scene::Graph &graph, const Functor &functor)
4038 Scene_forEachSelectedBrush(BrushForEachFace(FaceInstanceVisitAll<Functor>(functor)));
4042 template<typename Functor>
4043 class FaceVisitorWrapper {
4044 const Functor &functor;
4046 FaceVisitorWrapper(const Functor &functor) : functor(functor)
4050 void operator()(FaceInstance &faceInstance) const
4052 functor(faceInstance.getFace());
4056 template<typename Functor>
4057 inline const Functor &Scene_ForEachSelectedBrushFace(scene::Graph &graph, const Functor &functor)
4059 g_SelectedFaceInstances.foreach(FaceVisitorWrapper<Functor>(functor));