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 );
83 #define BRUSH_CONNECTIVITY_DEBUG 0
84 #define BRUSH_DEGENERATE_DEBUG 0
86 template<typename TextOuputStreamType>
87 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 ){
95 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )\n";
98 inline void print_3x3( const Matrix4& m ){
99 globalOutputStream() << "( " << m.xx() << " " << m.xy() << " " << m.xz() << " ) "
100 << "( " << m.yx() << " " << m.yy() << " " << m.yz() << " ) "
101 << "( " << m.zx() << " " << m.zy() << " " << m.zz() << " )\n";
105 inline bool texdef_sane( const texdef_t& texdef ){
106 return fabs( texdef.shift[0] ) < ( 1 << 16 )
107 && fabs( texdef.shift[1] ) < ( 1 << 16 );
110 inline void Winding_DrawWireframe( const Winding& winding ){
111 glVertexPointer( 3, GL_FLOAT, sizeof( WindingVertex ), &winding.points.data()->vertex );
112 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( winding.numpoints ) );
115 inline void Winding_Draw( const Winding& winding, const Vector3& normal, RenderStateFlags state ){
116 glVertexPointer( 3, GL_FLOAT, sizeof( WindingVertex ), &winding.points.data()->vertex );
118 if ( ( state & RENDER_BUMP ) != 0 ) {
119 Vector3 normals[c_brush_maxFaces];
120 typedef Vector3* Vector3Iter;
121 for ( Vector3Iter i = normals, end = normals + winding.numpoints; i != end; ++i )
125 if ( GlobalShaderCache().useShaderLanguage() ) {
126 glNormalPointer( GL_FLOAT, sizeof( Vector3 ), normals );
127 glVertexAttribPointerARB( c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->texcoord );
128 glVertexAttribPointerARB( c_attr_Tangent, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->tangent );
129 glVertexAttribPointerARB( c_attr_Binormal, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->bitangent );
133 glVertexAttribPointerARB( 11, 3, GL_FLOAT, 0, sizeof( Vector3 ), normals );
134 glVertexAttribPointerARB( 8, 2, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->texcoord );
135 glVertexAttribPointerARB( 9, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->tangent );
136 glVertexAttribPointerARB( 10, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->bitangent );
141 if ( state & RENDER_LIGHTING ) {
142 Vector3 normals[c_brush_maxFaces];
143 typedef Vector3* Vector3Iter;
144 for ( Vector3Iter i = normals, last = normals + winding.numpoints; i != last; ++i )
148 glNormalPointer( GL_FLOAT, sizeof( Vector3 ), normals );
151 if ( state & RENDER_TEXTURE ) {
152 glTexCoordPointer( 2, GL_FLOAT, sizeof( WindingVertex ), &winding.points.data()->texcoord );
156 if ( state & RENDER_FILL ) {
157 glDrawArrays( GL_TRIANGLE_FAN, 0, GLsizei( winding.numpoints ) );
161 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( winding.numpoints ) );
164 glDrawArrays( GL_POLYGON, 0, GLsizei( winding.numpoints ) );
168 const Winding& winding = winding;
170 if ( state & RENDER_FILL ) {
171 glBegin( GL_POLYGON );
175 glBegin( GL_LINE_LOOP );
178 if ( state & RENDER_LIGHTING ) {
179 glNormal3fv( normal );
182 for ( int i = 0; i < winding.numpoints; ++i )
184 if ( state & RENDER_TEXTURE ) {
185 glTexCoord2fv( &winding.points[i][3] );
187 glVertex3fv( winding.points[i] );
194 #include "shaderlib.h"
196 typedef DoubleVector3 PlanePoints[3];
198 inline bool planepts_equal( const PlanePoints planepts, const PlanePoints other ){
199 return planepts[0] == other[0] && planepts[1] == other[1] && planepts[2] == other[2];
202 inline void planepts_assign( PlanePoints planepts, const PlanePoints other ){
203 planepts[0] = other[0];
204 planepts[1] = other[1];
205 planepts[2] = other[2];
208 inline void planepts_quantise( PlanePoints planepts, double snap ){
209 vector3_snap( planepts[0], snap );
210 vector3_snap( planepts[1], snap );
211 vector3_snap( planepts[2], snap );
214 inline float vector3_max_component( const Vector3& vec3 ){
215 return std::max( fabsf( vec3[0] ), std::max( fabsf( vec3[1] ), fabsf( vec3[2] ) ) );
218 inline void edge_snap( Vector3& edge, double snap ){
219 float scale = static_cast<float>( ceil( fabs( snap / vector3_max_component( edge ) ) ) );
220 if ( scale > 0.0f ) {
221 vector3_scale( edge, scale );
223 vector3_snap( edge, snap );
226 inline void planepts_snap( PlanePoints planepts, double snap ){
227 Vector3 edge01( vector3_subtracted( planepts[1], planepts[0] ) );
228 Vector3 edge12( vector3_subtracted( planepts[2], planepts[1] ) );
229 Vector3 edge20( vector3_subtracted( planepts[0], planepts[2] ) );
231 double length_squared_01 = vector3_dot( edge01, edge01 );
232 double length_squared_12 = vector3_dot( edge12, edge12 );
233 double length_squared_20 = vector3_dot( edge20, edge20 );
235 vector3_snap( planepts[0], snap );
237 if ( length_squared_01 < length_squared_12 ) {
238 if ( length_squared_12 < length_squared_20 ) {
239 edge_snap( edge01, snap );
240 edge_snap( edge12, snap );
241 planepts[1] = vector3_added( planepts[0], edge01 );
242 planepts[2] = vector3_added( planepts[1], edge12 );
246 edge_snap( edge20, snap );
247 edge_snap( edge01, snap );
248 planepts[1] = vector3_added( planepts[0], edge20 );
249 planepts[2] = vector3_added( planepts[1], edge01 );
254 if ( length_squared_01 < length_squared_20 ) {
255 edge_snap( edge01, snap );
256 edge_snap( edge12, snap );
257 planepts[1] = vector3_added( planepts[0], edge01 );
258 planepts[2] = vector3_added( planepts[1], edge12 );
262 edge_snap( edge12, snap );
263 edge_snap( edge20, snap );
264 planepts[1] = vector3_added( planepts[0], edge12 );
265 planepts[2] = vector3_added( planepts[1], edge20 );
270 inline PointVertex pointvertex_for_planept( const DoubleVector3& point, const Colour4b& colour ){
273 static_cast<float>( point.x() ),
274 static_cast<float>( point.y() ),
275 static_cast<float>( point.z() )
281 inline PointVertex pointvertex_for_windingpoint( const Vector3& point, const Colour4b& colour ){
283 vertex3f_for_vector3( point ),
288 inline bool check_plane_is_integer( const PlanePoints& planePoints ){
289 return !float_is_integer( planePoints[0][0] )
290 || !float_is_integer( planePoints[0][1] )
291 || !float_is_integer( planePoints[0][2] )
292 || !float_is_integer( planePoints[1][0] )
293 || !float_is_integer( planePoints[1][1] )
294 || !float_is_integer( planePoints[1][2] )
295 || !float_is_integer( planePoints[2][0] )
296 || !float_is_integer( planePoints[2][1] )
297 || !float_is_integer( planePoints[2][2] );
300 inline void brush_check_shader( const char* name ){
301 if ( !shader_valid( name ) ) {
302 globalErrorStream() << "brush face has invalid texture name: '" << name << "'\n";
306 class FaceShaderObserver
309 virtual void realiseShader() = 0;
310 virtual void unrealiseShader() = 0;
313 typedef ReferencePair<FaceShaderObserver> FaceShaderObserverPair;
316 class ContentsFlagsValue
319 ContentsFlagsValue(){
322 ContentsFlagsValue( int surfaceFlags, int contentFlags, int value, bool specified ) :
323 m_surfaceFlags( surfaceFlags ),
324 m_contentFlags( contentFlags ),
326 m_specified( specified ){
335 inline void ContentsFlagsValue_assignMasked( ContentsFlagsValue& flags, const ContentsFlagsValue& other ){
336 bool detail = bitfield_enabled( flags.m_contentFlags, BRUSH_DETAIL_MASK );
339 flags.m_contentFlags = bitfield_enable( flags.m_contentFlags, BRUSH_DETAIL_MASK );
343 flags.m_contentFlags = bitfield_disable( flags.m_contentFlags, BRUSH_DETAIL_MASK );
348 class FaceShader : public ModuleObserver
354 CopiedString m_shader;
355 ContentsFlagsValue m_flags;
357 SavedState( const FaceShader& faceShader ){
358 m_shader = faceShader.getShader();
359 m_flags = faceShader.m_flags;
362 void exportState( FaceShader& faceShader ) const {
363 faceShader.setShader( m_shader.c_str() );
364 faceShader.m_flags = m_flags;
368 CopiedString m_shader;
370 ContentsFlagsValue m_flags;
371 FaceShaderObserverPair m_observers;
375 FaceShader( const char* shader, const ContentsFlagsValue& flags = ContentsFlagsValue( 0, 0, 0, false ) ) :
379 m_instanced( false ),
388 // copy-construction not supported
389 FaceShader( const FaceShader& other );
391 void instanceAttach(){
393 m_state->incrementUsed();
396 void instanceDetach(){
397 m_state->decrementUsed();
401 void captureShader(){
402 ASSERT_MESSAGE( m_state == 0, "shader cannot be captured" );
403 brush_check_shader( m_shader.c_str() );
404 m_state = GlobalShaderCache().capture( m_shader.c_str() );
405 m_state->attach( *this );
408 void releaseShader(){
409 ASSERT_MESSAGE( m_state != 0, "shader cannot be released" );
410 m_state->detach( *this );
411 GlobalShaderCache().release( m_shader.c_str() );
416 ASSERT_MESSAGE( !m_realised, "FaceTexdef::realise: already realised" );
418 m_observers.forEach([](FaceShaderObserver &observer) {
419 observer.realiseShader();
424 ASSERT_MESSAGE( m_realised, "FaceTexdef::unrealise: already unrealised" );
425 m_observers.forEach([](FaceShaderObserver &observer) {
426 observer.unrealiseShader();
431 void attach( FaceShaderObserver& observer ){
432 m_observers.attach( observer );
434 observer.realiseShader();
438 void detach( FaceShaderObserver& observer ){
440 observer.unrealiseShader();
442 m_observers.detach( observer );
445 const char* getShader() const {
446 return m_shader.c_str();
448 void setShader( const char* name ){
450 m_state->decrementUsed();
456 m_state->incrementUsed();
460 ContentsFlagsValue getFlags() const {
461 ASSERT_MESSAGE( m_realised, "FaceShader::getFlags: flags not valid when unrealised" );
462 if ( !m_flags.m_specified ) {
463 return ContentsFlagsValue(
464 m_state->getTexture().surfaceFlags,
465 m_state->getTexture().contentFlags,
466 m_state->getTexture().value,
473 void setFlags( const ContentsFlagsValue& flags ){
474 ASSERT_MESSAGE( m_realised, "FaceShader::setFlags: flags not valid when unrealised" );
475 ContentsFlagsValue_assignMasked( m_flags, flags );
478 Shader* state() const {
482 std::size_t width() const {
484 return m_state->getTexture().width;
489 std::size_t height() const {
491 return m_state->getTexture().height;
496 unsigned int shaderFlags() const {
498 return m_state->getFlags();
505 class FaceTexdef : public FaceShaderObserver
508 FaceTexdef( const FaceTexdef& other );
511 FaceTexdef& operator=( const FaceTexdef& other );
517 TextureProjection m_projection;
519 SavedState( const FaceTexdef& faceTexdef ){
520 m_projection = faceTexdef.m_projection;
523 void exportState( FaceTexdef& faceTexdef ) const {
524 Texdef_Assign( faceTexdef.m_projection, m_projection );
528 FaceShader& m_shader;
529 TextureProjection m_projection;
530 bool m_projectionInitialised;
535 const TextureProjection& projection,
536 bool projectionInitialised = true
539 m_projection( projection ),
540 m_projectionInitialised( projectionInitialised ),
541 m_scaleApplied( false ){
542 m_shader.attach( *this );
546 m_shader.detach( *this );
550 ASSERT_MESSAGE( !m_scaleApplied, "texture scale aready added" );
551 m_scaleApplied = true;
552 m_projection.m_brushprimit_texdef.addScale( m_shader.width(), m_shader.height() );
556 ASSERT_MESSAGE( m_scaleApplied, "texture scale aready removed" );
557 m_scaleApplied = false;
558 m_projection.m_brushprimit_texdef.removeScale( m_shader.width(), m_shader.height() );
561 void realiseShader(){
562 if ( m_projectionInitialised && !m_scaleApplied ) {
567 void unrealiseShader(){
568 if ( m_projectionInitialised && m_scaleApplied ) {
573 void setTexdef( const TextureProjection& projection ){
575 Texdef_Assign( m_projection, projection );
579 void shift( float s, float t ){
580 ASSERT_MESSAGE( texdef_sane( m_projection.m_texdef ), "FaceTexdef::shift: bad texdef" );
582 Texdef_Shift( m_projection, s, t );
586 void scale( float s, float t ){
588 Texdef_Scale( m_projection, s, t );
592 void rotate( float angle ){
594 Texdef_Rotate( m_projection, angle );
598 void fit( const Vector3& normal, const Winding& winding, float s_repeat, float t_repeat ){
599 Texdef_FitTexture( m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat );
602 void fitW( const Vector3& normal, const Winding& winding, float s_repeat, float t_repeat ){
603 Texdef_FitTextureW( m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat );
606 void fitH( const Vector3& normal, const Winding& winding, float s_repeat, float t_repeat ){
607 Texdef_FitTextureH( m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat );
610 void emitTextureCoordinates( Winding& winding, const Vector3& normal, const Matrix4& localToWorld ){
611 Texdef_EmitTextureCoordinates( m_projection, m_shader.width(), m_shader.height(), winding, normal, localToWorld );
614 void transform( const Plane3& plane, const Matrix4& matrix ){
616 Texdef_transformLocked( m_projection, m_shader.width(), m_shader.height(), plane, matrix );
620 TextureProjection normalised() const {
621 brushprimit_texdef_t tmp( m_projection.m_brushprimit_texdef );
622 tmp.removeScale( m_shader.width(), m_shader.height() );
623 return TextureProjection( m_projection.m_texdef, tmp, m_projection.m_basis_s, m_projection.m_basis_t );
626 void setBasis( const Vector3& normal ){
628 Normal_GetTransform( normal, basis );
629 m_projection.m_basis_s = Vector3( basis.xx(), basis.yx(), basis.zx() );
630 m_projection.m_basis_t = Vector3( -basis.xy(), -basis.yy(), -basis.zy() );
634 inline void planepts_print( const PlanePoints& planePoints, TextOutputStream& ostream ){
635 ostream << "( " << planePoints[0][0] << " " << planePoints[0][1] << " " << planePoints[0][2] << " ) "
636 << "( " << planePoints[1][0] << " " << planePoints[1][1] << " " << planePoints[1][2] << " ) "
637 << "( " << planePoints[2][0] << " " << planePoints[2][1] << " " << planePoints[2][2] << " )";
641 inline Plane3 Plane3_applyTranslation( const Plane3& plane, const Vector3& translation ){
642 Plane3 tmp( plane3_translated( Plane3( plane.normal(), -plane.dist() ), translation ) );
643 return Plane3( tmp.normal(), -tmp.dist() );
646 inline Plane3 Plane3_applyTransform( const Plane3& plane, const Matrix4& matrix ){
647 Plane3 tmp( plane3_transformed( Plane3( plane.normal(), -plane.dist() ), matrix ) );
648 return Plane3( tmp.normal(), -tmp.dist() );
653 PlanePoints m_planepts;
654 Plane3 m_planeCached;
657 Vector3 m_funcStaticOrigin;
659 static EBrushType m_type;
661 static bool isDoom3Plane(){
662 return FacePlane::m_type == eBrushTypeDoom3 || FacePlane::m_type == eBrushTypeQuake4;
665 FacePlane& operator=(const FacePlane&) = default;
670 PlanePoints m_planepts;
673 SavedState( const FacePlane& facePlane ){
674 if ( facePlane.isDoom3Plane() ) {
675 m_plane = facePlane.m_plane;
679 planepts_assign( m_planepts, facePlane.planePoints() );
683 void exportState( FacePlane& facePlane ) const {
684 if ( facePlane.isDoom3Plane() ) {
685 facePlane.m_plane = m_plane;
686 facePlane.updateTranslated();
690 planepts_assign( facePlane.planePoints(), m_planepts );
691 facePlane.MakePlane();
696 FacePlane() : m_funcStaticOrigin( 0, 0, 0 ){
699 FacePlane( const FacePlane& other ) : m_funcStaticOrigin( 0, 0, 0 ){
700 if ( !isDoom3Plane() ) {
701 planepts_assign( m_planepts, other.m_planepts );
706 m_plane = other.m_plane;
712 if ( !isDoom3Plane() ) {
714 if ( check_plane_is_integer( m_planepts ) ) {
715 globalErrorStream() << "non-integer planepts: ";
716 planepts_print( m_planepts, globalErrorStream() );
717 globalErrorStream() << "\n";
720 m_planeCached = plane3_for_points( m_planepts );
725 if ( !isDoom3Plane() ) {
726 vector3_swap( m_planepts[0], m_planepts[2] );
731 m_planeCached = plane3_flipped( m_plane );
736 void transform( const Matrix4& matrix, bool mirror ){
737 if ( !isDoom3Plane() ) {
740 bool off = check_plane_is_integer( planePoints() );
743 matrix4_transform_point( matrix, m_planepts[0] );
744 matrix4_transform_point( matrix, m_planepts[1] );
745 matrix4_transform_point( matrix, m_planepts[2] );
752 if ( check_plane_is_integer( planePoints() ) ) {
754 globalErrorStream() << "caused by transform\n";
762 m_planeCached = Plane3_applyTransform( m_planeCached, matrix );
767 void offset( float offset ){
768 if ( !isDoom3Plane() ) {
769 Vector3 move( vector3_scaled( m_planeCached.normal(), -offset ) );
771 vector3_subtract( m_planepts[0], move );
772 vector3_subtract( m_planepts[1], move );
773 vector3_subtract( m_planepts[2], move );
779 m_planeCached.d += offset;
784 void updateTranslated(){
785 m_planeCached = Plane3_applyTranslation( m_plane, m_funcStaticOrigin );
789 m_plane = Plane3_applyTranslation( m_planeCached, vector3_negated( m_funcStaticOrigin ) );
793 PlanePoints& planePoints(){
797 const PlanePoints& planePoints() const {
801 const Plane3& plane3() const {
802 return m_planeCached;
805 void setDoom3Plane( const Plane3& plane ){
810 const Plane3& getDoom3Plane() const {
814 void copy( const FacePlane& other ){
815 if ( !isDoom3Plane() ) {
816 planepts_assign( m_planepts, other.m_planepts );
821 m_planeCached = other.m_plane;
826 void copy( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
827 if ( !isDoom3Plane() ) {
835 m_planeCached = plane3_for_points( p2, p1, p0 );
841 inline void Winding_testSelect( Winding& winding, SelectionTest& test, SelectionIntersection& best ){
842 test.TestPolygon( VertexPointer( reinterpret_cast<VertexPointer::pointer>( &winding.points.data()->vertex ), sizeof( WindingVertex ) ), winding.numpoints, best );
845 const double GRID_MIN = 0.125;
847 inline double quantiseInteger( double f ){
848 return float_to_integer( f );
851 inline double quantiseFloating( double f ){
852 return float_snapped( f, 1.f / ( 1 << 16 ) );
855 typedef double ( *QuantiseFunc )( double f );
862 virtual bool filter( const Face& face ) const = 0;
865 bool face_filtered( Face& face );
867 void add_face_filter( FaceFilter& filter, int mask, bool invert = false );
869 void Brush_addTextureChangedCallback( const SignalHandler& callback );
871 void Brush_textureChanged();
874 extern bool g_brush_texturelock_enabled;
879 virtual void planeChanged() = 0;
880 virtual void connectivityChanged() = 0;
881 virtual void shaderChanged() = 0;
882 virtual void evaluateTransform() = 0;
886 public OpenGLRenderable,
889 public FaceShaderObserver
891 std::size_t m_refcount;
893 class SavedState : public UndoMemento
896 FacePlane::SavedState m_planeState;
897 FaceTexdef::SavedState m_texdefState;
898 FaceShader::SavedState m_shaderState;
900 SavedState( const Face& face ) : m_planeState( face.getPlane() ), m_texdefState( face.getTexdef() ), m_shaderState( face.getShader() ){
903 void exportState( Face& face ) const {
904 m_planeState.exportState( face.getPlane() );
905 m_shaderState.exportState( face.getShader() );
906 m_texdefState.exportState( face.getTexdef() );
915 static QuantiseFunc m_quantise;
916 static EBrushType m_type;
918 PlanePoints m_move_planepts;
919 PlanePoints m_move_planeptsTransformed;
922 FacePlane m_planeTransformed;
925 TextureProjection m_texdefTransformed;
931 FaceObserver* m_observer;
932 UndoObserver* m_undoable_observer;
935 // assignment not supported
936 Face& operator=( const Face& other );
938 // copy-construction not supported
939 Face( const Face& other );
943 Face( FaceObserver* observer ) :
945 m_shader( texdef_name_default() ),
946 m_texdef( m_shader, TextureProjection(), false ),
948 m_observer( observer ),
949 m_undoable_observer( 0 ),
951 m_shader.attach( *this );
952 m_plane.copy( Vector3( 0, 0, 0 ), Vector3( 64, 0, 0 ), Vector3( 0, 64, 0 ) );
953 m_texdef.setBasis( m_plane.plane3().normal() );
962 const TextureProjection& projection,
963 FaceObserver* observer
967 m_texdef( m_shader, projection ),
968 m_observer( observer ),
969 m_undoable_observer( 0 ),
971 m_shader.attach( *this );
972 m_plane.copy( p0, p1, p2 );
973 m_texdef.setBasis( m_plane.plane3().normal() );
978 Face( const Face& other, FaceObserver* observer ) :
980 m_shader( other.m_shader.getShader(), other.m_shader.m_flags ),
981 m_texdef( m_shader, other.getTexdef().normalised() ),
982 m_observer( observer ),
983 m_undoable_observer( 0 ),
985 m_shader.attach( *this );
986 m_plane.copy( other.m_plane );
987 planepts_assign( m_move_planepts, other.m_move_planepts );
988 m_texdef.setBasis( m_plane.plane3().normal() );
994 m_shader.detach( *this );
999 m_observer->planeChanged();
1002 void realiseShader(){
1003 m_observer->shaderChanged();
1006 void unrealiseShader(){
1009 void instanceAttach( MapFile* map ){
1010 m_shader.instanceAttach();
1012 m_undoable_observer = GlobalUndoSystem().observer( this );
1013 GlobalFilterSystem().registerFilterable( *this );
1015 void instanceDetach( MapFile* map ){
1016 GlobalFilterSystem().unregisterFilterable( *this );
1017 m_undoable_observer = 0;
1018 GlobalUndoSystem().release( this );
1020 m_shader.instanceDetach();
1023 void render( RenderStateFlags state ) const {
1024 Winding_Draw( m_winding, m_planeTransformed.plane3().normal(), state );
1027 void updateFiltered(){
1028 m_filtered = face_filtered( *this );
1031 bool isFiltered() const {
1039 if ( m_undoable_observer != 0 ) {
1040 m_undoable_observer->save( this );
1045 UndoMemento* exportState() const {
1046 return new SavedState( *this );
1049 void importState( const UndoMemento* data ){
1052 static_cast<const SavedState*>( data )->exportState( *this );
1055 m_observer->connectivityChanged();
1057 m_observer->shaderChanged();
1066 if ( --m_refcount == 0 ) {
1076 bool intersectVolume( const VolumeTest& volume, const Matrix4& localToWorld ) const {
1077 return volume.TestPlane( Plane3( plane3().normal(), -plane3().dist() ), localToWorld );
1080 void render( Renderer& renderer, const Matrix4& localToWorld ) const {
1081 renderer.SetState( m_shader.state(), Renderer::eFullMaterials );
1082 renderer.addRenderable( *this, localToWorld );
1085 void transform( const Matrix4& matrix, bool mirror ){
1086 if ( g_brush_texturelock_enabled ) {
1087 Texdef_transformLocked( m_texdefTransformed, m_shader.width(), m_shader.height(), m_plane.plane3(), matrix );
1090 m_planeTransformed.transform( matrix, mirror );
1093 ASSERT_MESSAGE( projectionaxis_for_normal( normal ) == projectionaxis_for_normal( plane3().normal() ), "bleh" );
1095 m_observer->planeChanged();
1097 if ( g_brush_texturelock_enabled ) {
1098 Brush_textureChanged();
1102 void assign_planepts( const PlanePoints planepts ){
1103 m_planeTransformed.copy( planepts[0], planepts[1], planepts[2] );
1104 m_observer->planeChanged();
1107 /// \brief Reverts the transformable state of the brush to identity.
1108 void revertTransform(){
1109 m_planeTransformed = m_plane;
1110 planepts_assign( m_move_planeptsTransformed, m_move_planepts );
1111 m_texdefTransformed = m_texdef.m_projection;
1114 void freezeTransform(){
1116 m_plane = m_planeTransformed;
1117 planepts_assign( m_move_planepts, m_move_planeptsTransformed );
1118 m_texdef.m_projection = m_texdefTransformed;
1121 void update_move_planepts_vertex( std::size_t index, PlanePoints planePoints ){
1122 std::size_t numpoints = getWinding().numpoints;
1123 ASSERT_MESSAGE( index < numpoints, "update_move_planepts_vertex: invalid index" );
1125 std::size_t opposite = Winding_Opposite( getWinding(), index );
1126 std::size_t adjacent = Winding_wrap( getWinding(), opposite + numpoints - 1 );
1127 planePoints[0] = getWinding()[opposite].vertex;
1128 planePoints[1] = getWinding()[index].vertex;
1129 planePoints[2] = getWinding()[adjacent].vertex;
1130 // winding points are very inaccurate, so they must be quantised before using them to generate the face-plane
1131 planepts_quantise( planePoints, GRID_MIN );
1134 void snapto( float snap ){
1135 if ( contributes() ) {
1137 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane before snap to grid" );
1138 planepts_snap( m_plane.planePoints(), snap );
1139 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane after snap to grid" );
1141 PlanePoints planePoints;
1142 update_move_planepts_vertex( 0, planePoints );
1143 vector3_snap( planePoints[0], snap );
1144 vector3_snap( planePoints[1], snap );
1145 vector3_snap( planePoints[2], snap );
1146 assign_planepts( planePoints );
1149 SceneChangeNotify();
1150 if ( !plane3_valid( m_plane.plane3() ) ) {
1151 globalErrorStream() << "WARNING: invalid plane after snap to grid\n";
1156 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1157 Winding_testSelect( m_winding, test, best );
1160 void testSelect_centroid( SelectionTest& test, SelectionIntersection& best ){
1161 test.TestPoint( m_centroid, best );
1164 void shaderChanged(){
1165 EmitTextureCoordinates();
1166 Brush_textureChanged();
1167 m_observer->shaderChanged();
1170 SceneChangeNotify();
1173 const char* GetShader() const {
1174 return m_shader.getShader();
1177 void SetShader( const char* name ){
1179 m_shader.setShader( name );
1183 void revertTexdef(){
1184 m_texdefTransformed = m_texdef.m_projection;
1187 void texdefChanged(){
1189 EmitTextureCoordinates();
1190 Brush_textureChanged();
1193 void GetTexdef( TextureProjection& projection ) const {
1194 projection = m_texdef.normalised();
1197 void SetTexdef( const TextureProjection& projection ){
1199 m_texdef.setTexdef( projection );
1203 void GetFlags( ContentsFlagsValue& flags ) const {
1204 flags = m_shader.getFlags();
1207 void SetFlags( const ContentsFlagsValue& flags ){
1209 m_shader.setFlags( flags );
1210 m_observer->shaderChanged();
1214 void ShiftTexdef( float s, float t ){
1216 m_texdef.shift( s, t );
1220 void ScaleTexdef( float s, float t ){
1222 m_texdef.scale( s, t );
1226 void RotateTexdef( float angle ){
1228 m_texdef.rotate( angle );
1232 void FitTexture( float s_repeat, float t_repeat ){
1234 m_texdef.fit( m_plane.plane3().normal(), m_winding, s_repeat, t_repeat );
1238 void FitTextureW( float s_repeat, float t_repeat ){
1240 m_texdef.fitW( m_plane.plane3().normal(), m_winding, s_repeat, t_repeat );
1244 void FitTextureH( float s_repeat, float t_repeat ){
1246 m_texdef.fitH( m_plane.plane3().normal(), m_winding, s_repeat, t_repeat );
1250 void EmitTextureCoordinates(){
1251 Texdef_EmitTextureCoordinates( m_texdefTransformed, m_shader.width(), m_shader.height(), m_winding, plane3().normal(), g_matrix4_identity );
1255 const Vector3& centroid() const {
1259 void construct_centroid(){
1260 Winding_Centroid( m_winding, plane3(), m_centroid );
1263 const Winding& getWinding() const {
1267 Winding& getWinding(){
1271 const Plane3& plane3() const {
1272 m_observer->evaluateTransform();
1273 return m_planeTransformed.plane3();
1276 FacePlane& getPlane(){
1280 const FacePlane& getPlane() const {
1284 FaceTexdef& getTexdef(){
1288 const FaceTexdef& getTexdef() const {
1292 FaceShader& getShader(){
1296 const FaceShader& getShader() const {
1300 bool isDetail() const {
1301 return ( m_shader.m_flags.m_contentFlags & BRUSH_DETAIL_MASK ) != 0;
1304 void setDetail( bool detail ){
1306 if ( detail && !isDetail() ) {
1307 m_shader.m_flags.m_contentFlags |= BRUSH_DETAIL_MASK;
1309 else if ( !detail && isDetail() ) {
1310 m_shader.m_flags.m_contentFlags &= ~BRUSH_DETAIL_MASK;
1312 m_observer->shaderChanged();
1315 bool contributes() const {
1316 return m_winding.numpoints > 2;
1319 bool is_bounded() const {
1320 for ( Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i )
1322 if ( ( *i ).adjacent == c_brush_maxFaces ) {
1334 std::size_t m_vertex;
1337 FaceVertexId( std::size_t face, std::size_t vertex )
1338 : m_face( face ), m_vertex( vertex ){
1341 std::size_t getFace() const {
1345 std::size_t getVertex() const {
1350 typedef std::size_t faceIndex_t;
1352 struct EdgeRenderIndices
1358 : first( 0 ), second( 0 ){
1361 EdgeRenderIndices( const RenderIndex _first, const RenderIndex _second )
1362 : first( _first ), second( _second ){
1372 : first( c_brush_maxFaces ), second( c_brush_maxFaces ){
1375 EdgeFaces( const faceIndex_t _first, const faceIndex_t _second )
1376 : first( _first ), second( _second ){
1380 class RenderableWireframe : public OpenGLRenderable
1383 void render( RenderStateFlags state ) const {
1385 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices->colour );
1386 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices->vertex );
1387 glDrawElements( GL_LINES, GLsizei( m_size << 1 ), RenderIndexTypeID, m_faceVertex.data() );
1389 glBegin( GL_LINES );
1390 for ( std::size_t i = 0; i < m_size; ++i )
1392 glVertex3fv( &m_vertices[m_faceVertex[i].first].vertex.x );
1393 glVertex3fv( &m_vertices[m_faceVertex[i].second].vertex.x );
1399 Array<EdgeRenderIndices> m_faceVertex;
1401 const PointVertex* m_vertices;
1406 typedef std::vector<Brush*> brush_vector_t;
1411 virtual bool filter( const Brush& brush ) const = 0;
1414 bool brush_filtered( Brush& brush );
1416 void add_brush_filter( BrushFilter& filter, int mask, bool invert = false );
1419 /// \brief Returns true if 'self' takes priority when building brush b-rep.
1420 inline bool plane3_inside( const Plane3& self, const Plane3& other, bool selfIsLater ){
1421 if ( vector3_equal_epsilon( self.normal(), other.normal(), 0.001 ) ) {
1422 // same plane? prefer the one with smaller index
1423 if ( self.dist() == other.dist() ) {
1426 return self.dist() < other.dist();
1431 typedef std::vector<std::shared_ptr<Face>> Faces;
1433 /// \brief Returns the unique-id of the edge adjacent to \p faceVertex in the edge-pair for the set of \p faces.
1434 inline FaceVertexId next_edge( const Faces& faces, FaceVertexId faceVertex ){
1435 std::size_t adjacent_face = faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent;
1436 std::size_t adjacent_vertex = Winding_FindAdjacent( faces[adjacent_face]->getWinding(), faceVertex.getFace() );
1438 ASSERT_MESSAGE( adjacent_vertex != c_brush_maxFaces, "connectivity data invalid" );
1439 if ( adjacent_vertex == c_brush_maxFaces ) {
1443 return FaceVertexId( adjacent_face, adjacent_vertex );
1446 /// \brief Returns the unique-id of the vertex adjacent to \p faceVertex in the vertex-ring for the set of \p faces.
1447 inline FaceVertexId next_vertex( const Faces& faces, FaceVertexId faceVertex ){
1448 FaceVertexId nextEdge = next_edge( faces, faceVertex );
1449 return FaceVertexId( nextEdge.getFace(), Winding_next( faces[nextEdge.getFace()]->getWinding(), nextEdge.getVertex() ) );
1452 class SelectableEdge
1454 Vector3 getEdge() const {
1455 const Winding& winding = getFace().getWinding();
1456 return vector3_mid( winding[m_faceVertex.getVertex()].vertex, winding[Winding_next( winding, m_faceVertex.getVertex() )].vertex );
1461 FaceVertexId m_faceVertex;
1463 SelectableEdge( Faces& faces, FaceVertexId faceVertex )
1464 : m_faces( faces ), m_faceVertex( faceVertex ){
1467 SelectableEdge& operator=( const SelectableEdge& other ){
1468 m_faceVertex = other.m_faceVertex;
1472 Face& getFace() const {
1473 return *m_faces[m_faceVertex.getFace()];
1476 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1477 test.TestPoint( getEdge(), best );
1481 class SelectableVertex
1483 Vector3 getVertex() const {
1484 return getFace().getWinding()[m_faceVertex.getVertex()].vertex;
1489 FaceVertexId m_faceVertex;
1491 SelectableVertex( Faces& faces, FaceVertexId faceVertex )
1492 : m_faces( faces ), m_faceVertex( faceVertex ){
1495 SelectableVertex& operator=( const SelectableVertex& other ){
1496 m_faceVertex = other.m_faceVertex;
1500 Face& getFace() const {
1501 return *m_faces[m_faceVertex.getFace()];
1504 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1505 test.TestPoint( getVertex(), best );
1512 virtual void reserve( std::size_t size ) = 0;
1513 virtual void clear() = 0;
1514 virtual void push_back( Face& face ) = 0;
1515 virtual void pop_back() = 0;
1516 virtual void erase( std::size_t index ) = 0;
1517 virtual void connectivityChanged() = 0;
1518 virtual void edge_clear() = 0;
1519 virtual void edge_push_back( SelectableEdge& edge ) = 0;
1520 virtual void vertex_clear() = 0;
1521 virtual void vertex_push_back( SelectableVertex& vertex ) = 0;
1522 virtual void DEBUG_verify() const = 0;
1528 virtual void visit( Face& face ) const = 0;
1532 public TransformNode,
1537 public FaceObserver,
1543 scene::Node* m_node;
1544 typedef UniqueSet<BrushObserver*> Observers;
1545 Observers m_observers;
1546 UndoObserver* m_undoable_observer;
1553 // cached data compiled from state
1554 Array<PointVertex> m_faceCentroidPoints;
1555 RenderablePointArray m_render_faces;
1557 Array<PointVertex> m_uniqueVertexPoints;
1558 typedef std::vector<SelectableVertex> SelectableVertices;
1559 SelectableVertices m_select_vertices;
1560 RenderablePointArray m_render_vertices;
1562 Array<PointVertex> m_uniqueEdgePoints;
1563 typedef std::vector<SelectableEdge> SelectableEdges;
1564 SelectableEdges m_select_edges;
1565 RenderablePointArray m_render_edges;
1567 Array<EdgeRenderIndices> m_edge_indices;
1568 Array<EdgeFaces> m_edge_faces;
1573 Callback<void()> m_evaluateTransform;
1574 Callback<void()> m_boundsChanged;
1576 mutable bool m_planeChanged; // b-rep evaluation required
1577 mutable bool m_transformChanged; // transform evaluation required
1581 STRING_CONSTANT( Name, "Brush" );
1583 Callback<void()> m_lightsChanged;
1586 static Shader* m_state_point;
1589 static EBrushType m_type;
1590 static double m_maxWorldCoord;
1592 Brush( scene::Node& node, const Callback<void()>& evaluateTransform, const Callback<void()>& boundsChanged ) :
1594 m_undoable_observer( 0 ),
1596 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1597 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1598 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1599 m_evaluateTransform( evaluateTransform ),
1600 m_boundsChanged( boundsChanged ),
1601 m_planeChanged( false ),
1602 m_transformChanged( false ){
1605 Brush( const Brush& other, scene::Node& node, const Callback<void()>& evaluateTransform, const Callback<void()>& boundsChanged ) :
1607 m_undoable_observer( 0 ),
1609 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1610 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1611 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1612 m_evaluateTransform( evaluateTransform ),
1613 m_boundsChanged( boundsChanged ),
1614 m_planeChanged( false ),
1615 m_transformChanged( false ){
1619 Brush( const Brush& other ) :
1620 TransformNode( other ),
1625 FaceObserver( other ),
1626 Filterable( other ),
1628 BrushDoom3( other ),
1630 m_undoable_observer( 0 ),
1632 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1633 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1634 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1635 m_planeChanged( false ),
1636 m_transformChanged( false ){
1641 ASSERT_MESSAGE( m_observers.empty(), "Brush::~Brush: observers still attached" );
1644 // assignment not supported
1645 Brush& operator=( const Brush& other );
1647 void setDoom3GroupOrigin( const Vector3& origin ){
1648 //globalOutputStream() << "func_static origin before: " << m_funcStaticOrigin << " after: " << origin << "\n";
1649 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1651 ( *i )->getPlane().m_funcStaticOrigin = origin;
1652 ( *i )->getPlane().updateTranslated();
1653 ( *i )->planeChanged();
1658 void attach( BrushObserver& observer ){
1659 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1661 observer.push_back( *( *i ) );
1664 for ( SelectableEdges::iterator i = m_select_edges.begin(); i != m_select_edges.end(); ++i )
1666 observer.edge_push_back( *i );
1669 for ( SelectableVertices::iterator i = m_select_vertices.begin(); i != m_select_vertices.end(); ++i )
1671 observer.vertex_push_back( *i );
1674 m_observers.insert( &observer );
1677 void detach( BrushObserver& observer ){
1678 m_observers.erase( &observer );
1681 void forEachFace( const BrushVisitor& visitor ) const {
1682 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1684 visitor.visit( *( *i ) );
1688 void forEachFace_instanceAttach( MapFile* map ) const {
1689 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1691 ( *i )->instanceAttach( map );
1695 void forEachFace_instanceDetach( MapFile* map ) const {
1696 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1698 ( *i )->instanceDetach( map );
1702 InstanceCounter m_instanceCounter;
1704 void instanceAttach( const scene::Path& path ){
1705 if ( ++m_instanceCounter.m_count == 1 ) {
1706 m_map = path_find_mapfile( path.begin(), path.end() );
1707 m_undoable_observer = GlobalUndoSystem().observer( this );
1708 GlobalFilterSystem().registerFilterable( *this );
1709 forEachFace_instanceAttach( m_map );
1713 ASSERT_MESSAGE( path_find_mapfile( path.begin(), path.end() ) == m_map, "node is instanced across more than one file" );
1717 void instanceDetach( const scene::Path& path ){
1718 if ( --m_instanceCounter.m_count == 0 ) {
1719 forEachFace_instanceDetach( m_map );
1720 GlobalFilterSystem().unregisterFilterable( *this );
1722 m_undoable_observer = 0;
1723 GlobalUndoSystem().release( this );
1728 const char* name() const {
1732 void attach( const NameCallback& callback ){
1735 void detach( const NameCallback& callback ){
1739 void updateFiltered(){
1740 if ( m_node != 0 ) {
1741 if ( brush_filtered( *this ) ) {
1742 m_node->enable( scene::Node::eFiltered );
1746 m_node->disable( scene::Node::eFiltered );
1752 void planeChanged(){
1753 m_planeChanged = true;
1758 void shaderChanged(){
1763 void evaluateBRep() const {
1764 if ( m_planeChanged ) {
1765 m_planeChanged = false;
1766 const_cast<Brush*>( this )->buildBRep();
1770 void transformChanged(){
1771 m_transformChanged = true;
1775 typedef MemberCaller<Brush, void(), &Brush::transformChanged> TransformChangedCaller;
1777 void evaluateTransform(){
1778 if ( m_transformChanged ) {
1779 m_transformChanged = false;
1781 m_evaluateTransform();
1785 const Matrix4& localToParent() const {
1786 return g_matrix4_identity;
1793 const AABB& localAABB() const {
1795 return m_aabb_local;
1798 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
1799 return test.TestAABB( m_aabb_local, localToWorld );
1802 void renderComponents( SelectionSystem::EComponentMode mode, Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
1805 case SelectionSystem::eVertex:
1806 renderer.addRenderable( m_render_vertices, localToWorld );
1808 case SelectionSystem::eEdge:
1809 renderer.addRenderable( m_render_edges, localToWorld );
1811 case SelectionSystem::eFace:
1812 renderer.addRenderable( m_render_faces, localToWorld );
1819 void transform( const Matrix4& matrix ){
1820 bool mirror = matrix4_handedness( matrix ) == MATRIX4_LEFTHANDED;
1822 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1824 ( *i )->transform( matrix, mirror );
1828 void snapto( float snap ){
1829 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1831 ( *i )->snapto( snap );
1835 void revertTransform(){
1836 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1838 ( *i )->revertTransform();
1842 void freezeTransform(){
1843 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1845 ( *i )->freezeTransform();
1849 /// \brief Returns the absolute index of the \p faceVertex.
1850 std::size_t absoluteIndex( FaceVertexId faceVertex ){
1851 std::size_t index = 0;
1852 for ( std::size_t i = 0; i < faceVertex.getFace(); ++i )
1854 index += m_faces[i]->getWinding().numpoints;
1856 return index + faceVertex.getVertex();
1859 void appendFaces( const Faces& other ){
1861 for ( Faces::const_iterator i = other.begin(); i != other.end(); ++i )
1867 /// \brief The undo memento for a brush stores only the list of face references - the faces are not copied.
1868 class BrushUndoMemento : public UndoMemento
1871 BrushUndoMemento( const Faces& faces ) : m_faces( faces ){
1885 if ( m_undoable_observer != 0 ) {
1886 m_undoable_observer->save( this );
1890 UndoMemento* exportState() const {
1891 return new BrushUndoMemento( m_faces );
1894 void importState( const UndoMemento* state ){
1896 appendFaces( static_cast<const BrushUndoMemento*>( state )->m_faces );
1899 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1901 ( *i )->DEBUG_verify();
1906 return !m_faces.empty() && m_faces.front()->isDetail();
1909 /// \brief Appends a copy of \p face to the end of the face list.
1910 std::shared_ptr<Face> addFace( const Face& face ){
1911 if ( m_faces.size() == c_brush_maxFaces ) {
1915 push_back( std::make_shared<Face>( face, this ) );
1916 m_faces.back()->setDetail( isDetail() );
1918 return m_faces.back();
1921 /// \brief Appends a new face constructed from the parameters to the end of the face list.
1922 std::shared_ptr<Face> addPlane( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection ){
1923 if ( m_faces.size() == c_brush_maxFaces ) {
1927 push_back( std::make_shared<Face>( p0, p1, p2, shader, projection, this ) );
1928 m_faces.back()->setDetail( isDetail() );
1930 return m_faces.back();
1933 static void constructStatic( EBrushType type ){
1935 Face::m_type = type;
1936 FacePlane::m_type = type;
1938 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_QUAKE;
1939 if ( m_type == eBrushTypeQuake3BP || m_type == eBrushTypeDoom3 || m_type == eBrushTypeQuake4 ) {
1940 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_BRUSHPRIMITIVES;
1941 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
1943 else if ( m_type == eBrushTypeHalfLife ) {
1944 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_HALFLIFE;
1945 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
1948 Face::m_quantise = ( m_type == eBrushTypeQuake ) ? quantiseInteger : quantiseFloating;
1950 m_state_point = GlobalShaderCache().capture( "$POINT" );
1953 static void destroyStatic(){
1954 GlobalShaderCache().release( "$POINT" );
1957 std::size_t DEBUG_size(){
1958 return m_faces.size();
1961 typedef Faces::const_iterator const_iterator;
1963 const_iterator begin() const {
1964 return m_faces.begin();
1967 const_iterator end() const {
1968 return m_faces.end();
1971 std::shared_ptr<Face> back(){
1972 return m_faces.back();
1975 const std::shared_ptr<Face> back() const {
1976 return m_faces.back();
1979 void reserve( std::size_t count ){
1980 m_faces.reserve( count );
1981 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1983 ( *i )->reserve( count );
1987 void push_back( Faces::value_type face ){
1988 m_faces.push_back( face );
1989 if ( m_instanceCounter.m_count != 0 ) {
1990 m_faces.back()->instanceAttach( m_map );
1992 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1994 ( *i )->push_back( *face );
1995 ( *i )->DEBUG_verify();
2000 if ( m_instanceCounter.m_count != 0 ) {
2001 m_faces.back()->instanceDetach( m_map );
2004 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2007 ( *i )->DEBUG_verify();
2011 void erase( std::size_t index ){
2012 if ( m_instanceCounter.m_count != 0 ) {
2013 m_faces[index]->instanceDetach( m_map );
2015 m_faces.erase( m_faces.begin() + index );
2016 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2018 ( *i )->erase( index );
2019 ( *i )->DEBUG_verify();
2023 void connectivityChanged(){
2024 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2026 ( *i )->connectivityChanged();
2033 if ( m_instanceCounter.m_count != 0 ) {
2034 forEachFace_instanceDetach( m_map );
2037 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2040 ( *i )->DEBUG_verify();
2044 std::size_t size() const {
2045 return m_faces.size();
2048 bool empty() const {
2049 return m_faces.empty();
2052 /// \brief Returns true if any face of the brush contributes to the final B-Rep.
2053 bool hasContributingFaces() const {
2054 for ( const_iterator i = begin(); i != end(); ++i )
2056 if ( ( *i )->contributes() ) {
2063 /// \brief Removes faces that do not contribute to the brush. This is useful for cleaning up after CSG operations on the brush.
2064 /// 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.
2065 void removeEmptyFaces(){
2070 while ( i < m_faces.size() )
2072 if ( !m_faces[i]->contributes() ) {
2084 /// \brief Constructs \p winding from the intersection of \p plane with the other planes of the brush.
2085 void windingForClipPlane( Winding& winding, const Plane3& plane ) const {
2086 FixedWinding buffer[2];
2089 // get a poly that covers an effectively infinite area
2090 Winding_createInfinite( buffer[swap], plane, m_maxWorldCoord + 1 );
2092 // chop the poly by all of the other faces
2094 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2096 const Face& clip = *m_faces[i];
2098 if ( plane3_equal( clip.plane3(), plane )
2099 || !plane3_valid( clip.plane3() ) || !plane_unique( i )
2100 || plane3_opposing( plane, clip.plane3() ) ) {
2104 buffer[!swap].clear();
2106 #if BRUSH_CONNECTIVITY_DEBUG
2107 globalOutputStream() << "clip vs face: " << i << "\n";
2111 // flip the plane, because we want to keep the back side
2112 Plane3 clipPlane( vector3_negated( clip.plane3().normal() ), -clip.plane3().dist() );
2113 Winding_Clip( buffer[swap], plane, clipPlane, i, buffer[!swap] );
2116 #if BRUSH_CONNECTIVITY_DEBUG
2117 for ( FixedWinding::Points::iterator k = buffer[!swap].points.begin(), j = buffer[!swap].points.end() - 1; k != buffer[!swap].points.end(); j = k, ++k )
2119 if ( vector3_length_squared( vector3_subtracted( ( *k ).vertex, ( *j ).vertex ) ) < 1 ) {
2120 globalOutputStream() << "v: " << std::distance( buffer[!swap].points.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2125 //ASSERT_MESSAGE(buffer[!swap].numpoints != 1, "created single-point winding");
2131 Winding_forFixedWinding( winding, buffer[swap] );
2133 #if BRUSH_CONNECTIVITY_DEBUG
2134 Winding_printConnectivity( winding );
2136 for ( Winding::iterator i = winding.begin(), j = winding.end() - 1; i != winding.end(); j = i, ++i )
2138 if ( vector3_length_squared( vector3_subtracted( ( *i ).vertex, ( *j ).vertex ) ) < 1 ) {
2139 globalOutputStream() << "v: " << std::distance( winding.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2145 void update_wireframe( RenderableWireframe& wire, const bool* faces_visible ) const {
2146 wire.m_faceVertex.resize( m_edge_indices.size() );
2147 wire.m_vertices = m_uniqueVertexPoints.data();
2149 for ( std::size_t i = 0; i < m_edge_faces.size(); ++i )
2151 if ( faces_visible[m_edge_faces[i].first]
2152 || faces_visible[m_edge_faces[i].second] ) {
2153 wire.m_faceVertex[wire.m_size++] = m_edge_indices[i];
2159 void update_faces_wireframe( Array<PointVertex>& wire, const bool* faces_visible ) const {
2160 std::size_t count = 0;
2161 for ( std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i )
2163 if ( faces_visible[i] ) {
2168 wire.resize( count );
2169 Array<PointVertex>::iterator p = wire.begin();
2170 for ( std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i )
2172 if ( faces_visible[i] ) {
2173 *p++ = m_faceCentroidPoints[i];
2178 /// \brief Makes this brush a deep-copy of the \p other.
2179 void copy( const Brush& other ){
2180 for ( Faces::const_iterator i = other.m_faces.begin(); i != other.m_faces.end(); ++i )
2188 void edge_push_back( FaceVertexId faceVertex ){
2189 m_select_edges.push_back( SelectableEdge( m_faces, faceVertex ) );
2190 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2192 ( *i )->edge_push_back( m_select_edges.back() );
2197 m_select_edges.clear();
2198 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2200 ( *i )->edge_clear();
2204 void vertex_push_back( FaceVertexId faceVertex ){
2205 m_select_vertices.push_back( SelectableVertex( m_faces, faceVertex ) );
2206 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2208 ( *i )->vertex_push_back( m_select_vertices.back() );
2212 void vertex_clear(){
2213 m_select_vertices.clear();
2214 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2216 ( *i )->vertex_clear();
2220 /// \brief Returns true if the face identified by \p index is preceded by another plane that takes priority over it.
2221 bool plane_unique( std::size_t index ) const {
2223 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2225 if ( index != i && !plane3_inside( m_faces[index]->plane3(), m_faces[i]->plane3(), index < i ) ) {
2232 /// \brief Removes edges that are smaller than the tolerance used when generating brush windings.
2233 void removeDegenerateEdges(){
2234 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2236 Winding& winding = m_faces[i]->getWinding();
2237 for ( Winding::iterator j = winding.begin(); j != winding.end(); )
2239 std::size_t index = std::distance( winding.begin(), j );
2240 std::size_t next = Winding_next( winding, index );
2241 if ( Edge_isDegenerate( winding[index].vertex, winding[next].vertex ) ) {
2242 #if BRUSH_DEGENERATE_DEBUG
2243 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate edge adjacent to " << winding[index].adjacent << "\n";
2245 Winding& other = m_faces[winding[index].adjacent]->getWinding();
2246 std::size_t adjacent = Winding_FindAdjacent( other, i );
2247 if ( adjacent != c_brush_maxFaces ) {
2248 other.erase( other.begin() + adjacent );
2260 /// \brief Invalidates faces that have only two vertices in their winding, while preserving edge-connectivity information.
2261 void removeDegenerateFaces(){
2262 // save adjacency info for degenerate faces
2263 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2265 Winding& degen = m_faces[i]->getWinding();
2267 if ( degen.numpoints == 2 ) {
2268 #if BRUSH_DEGENERATE_DEBUG
2269 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate winding adjacent to " << degen[0].adjacent << ", " << degen[1].adjacent << "\n";
2271 // this is an "edge" face, where the plane touches the edge of the brush
2273 Winding& winding = m_faces[degen[0].adjacent]->getWinding();
2274 std::size_t index = Winding_FindAdjacent( winding, i );
2275 if ( index != c_brush_maxFaces ) {
2276 #if BRUSH_DEGENERATE_DEBUG
2277 globalOutputStream() << "Brush::buildWindings: face " << degen[0].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[1].adjacent << "\n";
2279 winding[index].adjacent = degen[1].adjacent;
2284 Winding& winding = m_faces[degen[1].adjacent]->getWinding();
2285 std::size_t index = Winding_FindAdjacent( winding, i );
2286 if ( index != c_brush_maxFaces ) {
2287 #if BRUSH_DEGENERATE_DEBUG
2288 globalOutputStream() << "Brush::buildWindings: face " << degen[1].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[0].adjacent << "\n";
2290 winding[index].adjacent = degen[0].adjacent;
2299 /// \brief Removes edges that have the same adjacent-face as their immediate neighbour.
2300 void removeDuplicateEdges(){
2301 // verify face connectivity graph
2302 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2304 //if(m_faces[i]->contributes())
2306 Winding& winding = m_faces[i]->getWinding();
2307 for ( std::size_t j = 0; j != winding.numpoints; )
2309 std::size_t next = Winding_next( winding, j );
2310 if ( winding[j].adjacent == winding[next].adjacent ) {
2311 #if BRUSH_DEGENERATE_DEBUG
2312 globalOutputStream() << "Brush::buildWindings: face " << i << ": removed duplicate edge adjacent to face " << winding[j].adjacent << "\n";
2314 winding.erase( winding.begin() + next );
2325 /// \brief Removes edges that do not have a matching pair in their adjacent-face.
2326 void verifyConnectivityGraph(){
2327 // verify face connectivity graph
2328 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2330 //if(m_faces[i]->contributes())
2332 Winding& winding = m_faces[i]->getWinding();
2333 for ( Winding::iterator j = winding.begin(); j != winding.end(); )
2335 #if BRUSH_CONNECTIVITY_DEBUG
2336 globalOutputStream() << "Brush::buildWindings: face " << i << ": adjacent to face " << ( *j ).adjacent << "\n";
2338 // remove unidirectional graph edges
2339 if ( ( *j ).adjacent == c_brush_maxFaces
2340 || Winding_FindAdjacent( m_faces[( *j ).adjacent]->getWinding(), i ) == c_brush_maxFaces ) {
2341 #if BRUSH_CONNECTIVITY_DEBUG
2342 globalOutputStream() << "Brush::buildWindings: face " << i << ": removing unidirectional connectivity graph edge adjacent to face " << ( *j ).adjacent << "\n";
2355 /// \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.
2357 for ( const_iterator i = begin(); i != end(); ++i )
2359 if ( !( *i )->is_bounded() ) {
2366 /// \brief Constructs the polygon windings for each face of the brush. Also updates the brush bounding-box and face texture-coordinates.
2367 bool buildWindings(){
2370 m_aabb_local = AABB();
2372 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2374 Face& f = *m_faces[i];
2376 if ( !plane3_valid( f.plane3() ) || !plane_unique( i ) ) {
2377 f.getWinding().resize( 0 );
2381 #if BRUSH_CONNECTIVITY_DEBUG
2382 globalOutputStream() << "face: " << i << "\n";
2384 windingForClipPlane( f.getWinding(), f.plane3() );
2386 // update brush bounds
2387 const Winding& winding = f.getWinding();
2388 for ( Winding::const_iterator i = winding.begin(); i != winding.end(); ++i )
2390 aabb_extend_by_point_safe( m_aabb_local, ( *i ).vertex );
2393 // update texture coordinates
2394 f.EmitTextureCoordinates();
2399 bool degenerate = !isBounded();
2401 if ( !degenerate ) {
2402 // clean up connectivity information.
2403 // these cleanups must be applied in a specific order.
2404 removeDegenerateEdges();
2405 removeDegenerateFaces();
2406 removeDuplicateEdges();
2407 verifyConnectivityGraph();
2413 /// \brief Constructs the face windings and updates anything that depends on them.
2420 class FaceInstanceSet
2422 typedef SelectionList<FaceInstance> FaceInstances;
2423 FaceInstances m_faceInstances;
2425 void insert( FaceInstance& faceInstance ){
2426 m_faceInstances.append( faceInstance );
2429 void erase( FaceInstance& faceInstance ){
2430 m_faceInstances.erase( faceInstance );
2433 template<typename Functor>
2434 void foreach( Functor functor ){
2435 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
2441 bool empty() const {
2442 return m_faceInstances.empty();
2445 FaceInstance& last() const {
2446 return m_faceInstances.back();
2450 extern FaceInstanceSet g_SelectedFaceInstances;
2452 typedef std::list<std::size_t> VertexSelection;
2454 inline VertexSelection::iterator VertexSelection_find( VertexSelection& self, std::size_t value ){
2455 return std::find( self.begin(), self.end(), value );
2458 inline VertexSelection::const_iterator VertexSelection_find( const VertexSelection& self, std::size_t value ){
2459 return std::find( self.begin(), self.end(), value );
2462 inline VertexSelection::iterator VertexSelection_insert( VertexSelection& self, std::size_t value ){
2463 VertexSelection::iterator i = VertexSelection_find( self, value );
2464 if ( i == self.end() ) {
2465 self.push_back( value );
2466 return --self.end();
2471 inline void VertexSelection_erase( VertexSelection& self, std::size_t value ){
2472 VertexSelection::iterator i = VertexSelection_find( self, value );
2473 if ( i != self.end() ) {
2478 inline bool triangle_reversed( std::size_t x, std::size_t y, std::size_t z ){
2479 return !( ( x < y && y < z ) || ( z < x && x < y ) || ( y < z && z < x ) );
2482 template<typename Element>
2483 inline Vector3 triangle_cross( const BasicVector3<Element>& x, const BasicVector3<Element> y, const BasicVector3<Element>& z ){
2484 return vector3_cross( y - x, z - x );
2487 template<typename Element>
2488 inline bool triangles_same_winding( const BasicVector3<Element>& x1, const BasicVector3<Element> y1, const BasicVector3<Element>& z1, const BasicVector3<Element>& x2, const BasicVector3<Element> y2, const BasicVector3<Element>& z2 ){
2489 return vector3_dot( triangle_cross( x1, y1, z1 ), triangle_cross( x2, y2, z2 ) ) > 0;
2493 typedef const Plane3* PlanePointer;
2494 typedef PlanePointer* PlanesIterator;
2496 class VectorLightList : public LightList
2498 typedef std::vector<const RendererLight*> Lights;
2501 void addLight( const RendererLight& light ){
2502 m_lights.push_back( &light );
2509 void evaluateLights() const {
2512 void lightsChanged() const {
2515 void forEachLight( const RendererLightCallback& callback ) const {
2516 for ( Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i )
2518 callback( *( *i ) );
2526 ObservedSelectable m_selectable;
2527 ObservedSelectable m_selectableVertices;
2528 ObservedSelectable m_selectableEdges;
2529 SelectionChangeCallback m_selectionChanged;
2531 VertexSelection m_vertexSelection;
2532 VertexSelection m_edgeSelection;
2535 mutable VectorLightList m_lights;
2537 FaceInstance( Face& face, const SelectionChangeCallback& observer ) :
2539 m_selectable( SelectedChangedCaller( *this ) ),
2540 m_selectableVertices( observer ),
2541 m_selectableEdges( observer ),
2542 m_selectionChanged( observer ){
2545 FaceInstance( const FaceInstance& other ) :
2546 m_face( other.m_face ),
2547 m_selectable( SelectedChangedCaller( *this ) ),
2548 m_selectableVertices( other.m_selectableVertices ),
2549 m_selectableEdges( other.m_selectableEdges ),
2550 m_selectionChanged( other.m_selectionChanged ){
2553 FaceInstance& operator=( const FaceInstance& other ){
2554 m_face = other.m_face;
2562 const Face& getFace() const {
2566 void selectedChanged( const Selectable& selectable ){
2567 if ( selectable.isSelected() ) {
2568 g_SelectedFaceInstances.insert( *this );
2572 g_SelectedFaceInstances.erase( *this );
2574 m_selectionChanged( selectable );
2577 typedef MemberCaller<FaceInstance, void(const Selectable&), &FaceInstance::selectedChanged> SelectedChangedCaller;
2579 bool selectedVertices() const {
2580 return !m_vertexSelection.empty();
2583 bool selectedEdges() const {
2584 return !m_edgeSelection.empty();
2587 bool isSelected() const {
2588 return m_selectable.isSelected();
2591 bool selectedComponents() const {
2592 return selectedVertices() || selectedEdges() || isSelected();
2595 bool selectedComponents( SelectionSystem::EComponentMode mode ) const {
2598 case SelectionSystem::eVertex:
2599 return selectedVertices();
2600 case SelectionSystem::eEdge:
2601 return selectedEdges();
2602 case SelectionSystem::eFace:
2603 return isSelected();
2609 void setSelected( SelectionSystem::EComponentMode mode, bool select ){
2612 case SelectionSystem::eFace:
2613 m_selectable.setSelected( select );
2615 case SelectionSystem::eVertex:
2616 ASSERT_MESSAGE( !select, "select-all not supported" );
2618 m_vertexSelection.clear();
2619 m_selectableVertices.setSelected( false );
2621 case SelectionSystem::eEdge:
2622 ASSERT_MESSAGE( !select, "select-all not supported" );
2624 m_edgeSelection.clear();
2625 m_selectableEdges.setSelected( false );
2632 template<typename Functor>
2633 void SelectedVertices_foreach( Functor functor ) const {
2634 for ( VertexSelection::const_iterator i = m_vertexSelection.begin(); i != m_vertexSelection.end(); ++i )
2636 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *i );
2637 if ( index != c_brush_maxFaces ) {
2638 functor( getFace().getWinding()[index].vertex );
2643 template<typename Functor>
2644 void SelectedEdges_foreach( Functor functor ) const {
2645 for ( VertexSelection::const_iterator i = m_edgeSelection.begin(); i != m_edgeSelection.end(); ++i )
2647 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *i );
2648 if ( index != c_brush_maxFaces ) {
2649 const Winding& winding = getFace().getWinding();
2650 std::size_t adjacent = Winding_next( winding, index );
2651 functor( vector3_mid( winding[index].vertex, winding[adjacent].vertex ) );
2656 template<typename Functor>
2657 void SelectedFaces_foreach( Functor functor ) const {
2658 if ( isSelected() ) {
2659 functor( centroid() );
2663 template<typename Functor>
2664 void SelectedComponents_foreach( Functor functor ) const {
2665 SelectedVertices_foreach( functor );
2666 SelectedEdges_foreach( functor );
2667 SelectedFaces_foreach( functor );
2670 void iterate_selected( AABB& aabb ) const {
2671 SelectedComponents_foreach([&](const Vector3 &point) {
2672 aabb_extend_by_point_safe(aabb, point);
2676 void iterate_selected( RenderablePointVector& points ) const {
2677 SelectedComponents_foreach([&](const Vector3 &point) {
2678 const Colour4b colour_selected(0, 0, 255, 255);
2679 points.push_back(pointvertex_for_windingpoint(point, colour_selected));
2683 bool intersectVolume( const VolumeTest& volume, const Matrix4& localToWorld ) const {
2684 return m_face->intersectVolume( volume, localToWorld );
2687 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
2688 if ( !m_face->isFiltered() && m_face->contributes() && intersectVolume( volume, localToWorld ) ) {
2689 renderer.PushState();
2690 if ( selectedComponents() ) {
2691 renderer.Highlight( Renderer::eFace );
2693 m_face->render( renderer, localToWorld );
2694 renderer.PopState();
2698 void testSelect( SelectionTest& test, SelectionIntersection& best ){
2699 if ( !m_face->isFiltered() ) {
2700 m_face->testSelect( test, best );
2704 void testSelect( Selector& selector, SelectionTest& test ){
2705 SelectionIntersection best;
2706 testSelect( test, best );
2707 if ( best.valid() ) {
2708 Selector_add( selector, m_selectable, best );
2712 void testSelect_centroid( Selector& selector, SelectionTest& test ){
2713 if ( m_face->contributes() && !m_face->isFiltered() ) {
2714 SelectionIntersection best;
2715 m_face->testSelect_centroid( test, best );
2716 if ( best.valid() ) {
2717 Selector_add( selector, m_selectable, best );
2722 void selectPlane( Selector& selector, const Line& line, PlanesIterator first, PlanesIterator last, const PlaneCallback& selectedPlaneCallback ){
2723 for ( Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i )
2725 Vector3 v( vector3_subtracted( line_closest_point( line, ( *i ).vertex ), ( *i ).vertex ) );
2726 double dot = vector3_dot( getFace().plane3().normal(), v );
2732 Selector_add( selector, m_selectable );
2734 selectedPlaneCallback( getFace().plane3() );
2737 void selectReversedPlane( Selector& selector, const SelectedPlanes& selectedPlanes ){
2738 if ( selectedPlanes.contains( plane3_flipped( getFace().plane3() ) ) ) {
2739 Selector_add( selector, m_selectable );
2743 void transformComponents( const Matrix4& matrix ){
2744 if ( isSelected() ) {
2745 m_face->transform( matrix, false );
2747 if ( selectedVertices() ) {
2748 if ( m_vertexSelection.size() == 1 ) {
2749 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2750 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2752 else if ( m_vertexSelection.size() == 2 ) {
2753 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2754 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2755 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2757 else if ( m_vertexSelection.size() >= 3 ) {
2758 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2759 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2760 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2761 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2764 if ( selectedEdges() ) {
2765 if ( m_edgeSelection.size() == 1 ) {
2766 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2767 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2768 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2770 else if ( m_edgeSelection.size() >= 2 ) {
2771 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2772 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2773 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2774 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2779 void snapto( float snap ){
2780 m_face->snapto( snap );
2783 void snapComponents( float snap ){
2784 if ( isSelected() ) {
2787 if ( selectedVertices() ) {
2788 vector3_snap( m_face->m_move_planepts[0], snap );
2789 vector3_snap( m_face->m_move_planepts[1], snap );
2790 vector3_snap( m_face->m_move_planepts[2], snap );
2791 m_face->assign_planepts( m_face->m_move_planepts );
2792 planepts_assign( m_face->m_move_planeptsTransformed, m_face->m_move_planepts );
2793 m_face->freezeTransform();
2795 if ( selectedEdges() ) {
2796 vector3_snap( m_face->m_move_planepts[0], snap );
2797 vector3_snap( m_face->m_move_planepts[1], snap );
2798 vector3_snap( m_face->m_move_planepts[2], snap );
2799 m_face->assign_planepts( m_face->m_move_planepts );
2800 planepts_assign( m_face->m_move_planeptsTransformed, m_face->m_move_planepts );
2801 m_face->freezeTransform();
2805 void update_move_planepts_vertex( std::size_t index ){
2806 m_face->update_move_planepts_vertex( index, m_face->m_move_planepts );
2809 void update_move_planepts_vertex2( std::size_t index, std::size_t other ){
2810 const std::size_t numpoints = m_face->getWinding().numpoints;
2811 ASSERT_MESSAGE( index < numpoints, "select_vertex: invalid index" );
2813 const std::size_t opposite = Winding_Opposite( m_face->getWinding(), index, other );
2815 if ( triangle_reversed( index, other, opposite ) ) {
2816 std::swap( index, other );
2820 triangles_same_winding(
2821 m_face->getWinding()[opposite].vertex,
2822 m_face->getWinding()[index].vertex,
2823 m_face->getWinding()[other].vertex,
2824 m_face->getWinding()[0].vertex,
2825 m_face->getWinding()[1].vertex,
2826 m_face->getWinding()[2].vertex
2828 "update_move_planepts_vertex2: error"
2831 m_face->m_move_planepts[0] = m_face->getWinding()[opposite].vertex;
2832 m_face->m_move_planepts[1] = m_face->getWinding()[index].vertex;
2833 m_face->m_move_planepts[2] = m_face->getWinding()[other].vertex;
2834 planepts_quantise( m_face->m_move_planepts, GRID_MIN ); // winding points are very inaccurate
2837 void update_selection_vertex(){
2838 if ( m_vertexSelection.size() == 0 ) {
2839 m_selectableVertices.setSelected( false );
2843 m_selectableVertices.setSelected( true );
2845 if ( m_vertexSelection.size() == 1 ) {
2846 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_vertexSelection.begin() );
2848 if ( index != c_brush_maxFaces ) {
2849 update_move_planepts_vertex( index );
2852 else if ( m_vertexSelection.size() == 2 ) {
2853 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_vertexSelection.begin() );
2854 std::size_t other = Winding_FindAdjacent( getFace().getWinding(), *( ++m_vertexSelection.begin() ) );
2856 if ( index != c_brush_maxFaces
2857 && other != c_brush_maxFaces ) {
2858 update_move_planepts_vertex2( index, other );
2864 void select_vertex( std::size_t index, bool select ){
2866 VertexSelection_insert( m_vertexSelection, getFace().getWinding()[index].adjacent );
2870 VertexSelection_erase( m_vertexSelection, getFace().getWinding()[index].adjacent );
2873 SceneChangeNotify();
2874 update_selection_vertex();
2877 bool selected_vertex( std::size_t index ) const {
2878 return VertexSelection_find( m_vertexSelection, getFace().getWinding()[index].adjacent ) != m_vertexSelection.end();
2881 void update_move_planepts_edge( std::size_t index ){
2882 std::size_t numpoints = m_face->getWinding().numpoints;
2883 ASSERT_MESSAGE( index < numpoints, "select_edge: invalid index" );
2885 std::size_t adjacent = Winding_next( m_face->getWinding(), index );
2886 std::size_t opposite = Winding_Opposite( m_face->getWinding(), index );
2887 m_face->m_move_planepts[0] = m_face->getWinding()[index].vertex;
2888 m_face->m_move_planepts[1] = m_face->getWinding()[adjacent].vertex;
2889 m_face->m_move_planepts[2] = m_face->getWinding()[opposite].vertex;
2890 planepts_quantise( m_face->m_move_planepts, GRID_MIN ); // winding points are very inaccurate
2893 void update_selection_edge(){
2894 if ( m_edgeSelection.size() == 0 ) {
2895 m_selectableEdges.setSelected( false );
2899 m_selectableEdges.setSelected( true );
2901 if ( m_edgeSelection.size() == 1 ) {
2902 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_edgeSelection.begin() );
2904 if ( index != c_brush_maxFaces ) {
2905 update_move_planepts_edge( index );
2911 void select_edge( std::size_t index, bool select ){
2913 VertexSelection_insert( m_edgeSelection, getFace().getWinding()[index].adjacent );
2917 VertexSelection_erase( m_edgeSelection, getFace().getWinding()[index].adjacent );
2920 SceneChangeNotify();
2921 update_selection_edge();
2924 bool selected_edge( std::size_t index ) const {
2925 return VertexSelection_find( m_edgeSelection, getFace().getWinding()[index].adjacent ) != m_edgeSelection.end();
2928 const Vector3& centroid() const {
2929 return m_face->centroid();
2932 void connectivityChanged(){
2933 // This occurs when a face is added or removed.
2934 // The current vertex and edge selections no longer valid and must be cleared.
2935 m_vertexSelection.clear();
2936 m_selectableVertices.setSelected( false );
2937 m_edgeSelection.clear();
2938 m_selectableEdges.setSelected( false );
2942 class BrushClipPlane : public OpenGLRenderable
2946 static Shader* m_state;
2948 static void constructStatic(){
2949 m_state = GlobalShaderCache().capture( "$CLIPPER_OVERLAY" );
2952 static void destroyStatic(){
2953 GlobalShaderCache().release( "$CLIPPER_OVERLAY" );
2956 void setPlane( const Brush& brush, const Plane3& plane ){
2958 if ( plane3_valid( m_plane ) ) {
2959 brush.windingForClipPlane( m_winding, m_plane );
2963 m_winding.resize( 0 );
2967 void render( RenderStateFlags state ) const {
2968 if ( ( state & RENDER_FILL ) != 0 ) {
2969 Winding_Draw( m_winding, m_plane.normal(), state );
2973 Winding_DrawWireframe( m_winding );
2975 // also draw a line indicating the direction of the cut
2976 Vector3 lineverts[2];
2977 Winding_Centroid( m_winding, m_plane, lineverts[0] );
2978 lineverts[1] = vector3_added( lineverts[0], vector3_scaled( m_plane.normal(), Brush::m_maxWorldCoord * 4 ) );
2980 glVertexPointer( 3, GL_FLOAT, sizeof( Vector3 ), &lineverts[0] );
2981 glDrawArrays( GL_LINES, 0, GLsizei( 2 ) );
2985 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
2986 renderer.SetState( m_state, Renderer::eWireframeOnly );
2987 renderer.SetState( m_state, Renderer::eFullMaterials );
2988 renderer.addRenderable( *this, localToWorld );
2992 inline void Face_addLight( const FaceInstance& face, const Matrix4& localToWorld, const RendererLight& light ){
2993 const Plane3& facePlane = face.getFace().plane3();
2994 const Vector3& origin = light.aabb().origin;
2995 Plane3 tmp( plane3_transformed( Plane3( facePlane.normal(), -facePlane.dist() ), localToWorld ) );
2996 if ( !plane3_test_point( tmp, origin )
2997 || !plane3_test_point( tmp, vector3_added( origin, light.offset() ) ) ) {
2998 face.m_lights.addLight( light );
3003 typedef std::vector<FaceInstance> FaceInstances;
3005 class EdgeInstance : public Selectable
3007 FaceInstances& m_faceInstances;
3008 SelectableEdge* m_edge;
3010 void select_edge( bool select ){
3011 FaceVertexId faceVertex = m_edge->m_faceVertex;
3012 m_faceInstances[faceVertex.getFace()].select_edge( faceVertex.getVertex(), select );
3013 faceVertex = next_edge( m_edge->m_faces, faceVertex );
3014 m_faceInstances[faceVertex.getFace()].select_edge( faceVertex.getVertex(), select );
3017 bool selected_edge() const {
3018 FaceVertexId faceVertex = m_edge->m_faceVertex;
3019 if ( !m_faceInstances[faceVertex.getFace()].selected_edge( faceVertex.getVertex() ) ) {
3022 faceVertex = next_edge( m_edge->m_faces, faceVertex );
3023 if ( !m_faceInstances[faceVertex.getFace()].selected_edge( faceVertex.getVertex() ) ) {
3031 EdgeInstance( FaceInstances& faceInstances, SelectableEdge& edge )
3032 : m_faceInstances( faceInstances ), m_edge( &edge ){
3034 EdgeInstance& operator=( const EdgeInstance& other ){
3035 m_edge = other.m_edge;
3039 void setSelected( bool select ){
3040 select_edge( select );
3043 bool isSelected() const {
3044 return selected_edge();
3048 void testSelect( Selector& selector, SelectionTest& test ){
3049 SelectionIntersection best;
3050 m_edge->testSelect( test, best );
3051 if ( best.valid() ) {
3052 Selector_add( selector, *this, best );
3057 class VertexInstance : public Selectable
3059 FaceInstances& m_faceInstances;
3060 SelectableVertex* m_vertex;
3062 void select_vertex( bool select ){
3063 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3066 m_faceInstances[faceVertex.getFace()].select_vertex( faceVertex.getVertex(), select );
3067 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
3069 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
3072 bool selected_vertex() const {
3073 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3076 if ( !m_faceInstances[faceVertex.getFace()].selected_vertex( faceVertex.getVertex() ) ) {
3079 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
3081 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
3086 VertexInstance( FaceInstances& faceInstances, SelectableVertex& vertex )
3087 : m_faceInstances( faceInstances ), m_vertex( &vertex ){
3089 VertexInstance& operator=( const VertexInstance& other ){
3090 m_vertex = other.m_vertex;
3094 void setSelected( bool select ){
3095 select_vertex( select );
3098 bool isSelected() const {
3099 return selected_vertex();
3102 void testSelect( Selector& selector, SelectionTest& test ){
3103 SelectionIntersection best;
3104 m_vertex->testSelect( test, best );
3105 if ( best.valid() ) {
3106 Selector_add( selector, *this, best );
3111 class BrushInstanceVisitor
3114 virtual void visit( FaceInstance& face ) const = 0;
3117 class BrushInstance :
3118 public BrushObserver,
3119 public scene::Instance,
3122 public SelectionTestable,
3123 public ComponentSelectionTestable,
3124 public ComponentEditable,
3125 public ComponentSnappable,
3126 public PlaneSelectable,
3127 public LightCullable
3131 InstanceTypeCastTable m_casts;
3134 InstanceStaticCast<BrushInstance, Selectable>::install( m_casts );
3135 InstanceContainedCast<BrushInstance, Bounded>::install( m_casts );
3136 InstanceContainedCast<BrushInstance, Cullable>::install( m_casts );
3137 InstanceStaticCast<BrushInstance, Renderable>::install( m_casts );
3138 InstanceStaticCast<BrushInstance, SelectionTestable>::install( m_casts );
3139 InstanceStaticCast<BrushInstance, ComponentSelectionTestable>::install( m_casts );
3140 InstanceStaticCast<BrushInstance, ComponentEditable>::install( m_casts );
3141 InstanceStaticCast<BrushInstance, ComponentSnappable>::install( m_casts );
3142 InstanceStaticCast<BrushInstance, PlaneSelectable>::install( m_casts );
3143 InstanceIdentityCast<BrushInstance>::install( m_casts );
3144 InstanceContainedCast<BrushInstance, Transformable>::install( m_casts );
3147 InstanceTypeCastTable& get(){
3155 FaceInstances m_faceInstances;
3157 typedef std::vector<EdgeInstance> EdgeInstances;
3158 EdgeInstances m_edgeInstances;
3159 typedef std::vector<VertexInstance> VertexInstances;
3160 VertexInstances m_vertexInstances;
3162 ObservedSelectable m_selectable;
3164 mutable RenderableWireframe m_render_wireframe;
3165 mutable RenderablePointVector m_render_selected;
3166 mutable AABB m_aabb_component;
3167 mutable Array<PointVertex> m_faceCentroidPointsCulled;
3168 RenderablePointArray m_render_faces_wireframe;
3169 mutable bool m_viewChanged; // requires re-evaluation of view-dependent cached data
3171 BrushClipPlane m_clipPlane;
3173 static Shader* m_state_selpoint;
3175 const LightList* m_lightList;
3177 TransformModifier m_transform;
3179 BrushInstance( const BrushInstance& other ); // NOT COPYABLE
3180 BrushInstance& operator=( const BrushInstance& other ); // NOT ASSIGNABLE
3183 static Counter* m_counter;
3185 typedef LazyStatic<TypeCasts> StaticTypeCasts;
3187 void lightsChanged(){
3188 m_lightList->lightsChanged();
3191 typedef MemberCaller<BrushInstance, void(), &BrushInstance::lightsChanged> LightsChangedCaller;
3193 STRING_CONSTANT( Name, "BrushInstance" );
3195 BrushInstance( const scene::Path& path, scene::Instance* parent, Brush& brush ) :
3196 Instance( path, parent, this, StaticTypeCasts::instance().get() ),
3198 m_selectable( SelectedChangedCaller( *this ) ),
3199 m_render_selected( GL_POINTS ),
3200 m_render_faces_wireframe( m_faceCentroidPointsCulled, GL_POINTS ),
3201 m_viewChanged( false ),
3202 m_transform( Brush::TransformChangedCaller( m_brush ), ApplyTransformCaller( *this ) ){
3203 m_brush.instanceAttach( Instance::path() );
3204 m_brush.attach( *this );
3205 m_counter->increment();
3207 m_lightList = &GlobalShaderCache().attach( *this );
3208 m_brush.m_lightsChanged = LightsChangedCaller( *this ); ///\todo Make this work with instancing.
3210 Instance::setTransformChangedCallback( LightsChangedCaller( *this ) );
3214 Instance::setTransformChangedCallback( Callback<void()>() );
3216 m_brush.m_lightsChanged = Callback<void()>();
3217 GlobalShaderCache().detach( *this );
3219 m_counter->decrement();
3220 m_brush.detach( *this );
3221 m_brush.instanceDetach( Instance::path() );
3227 const Brush& getBrush() const {
3231 Bounded& get( NullType<Bounded>){
3235 Cullable& get( NullType<Cullable>){
3239 Transformable& get( NullType<Transformable>){
3243 void selectedChanged( const Selectable& selectable ){
3244 GlobalSelectionSystem().getObserver ( SelectionSystem::ePrimitive )( selectable );
3245 GlobalSelectionSystem().onSelectedChanged( *this, selectable );
3247 Instance::selectedChanged();
3249 typedef MemberCaller<BrushInstance, void(const Selectable&), &BrushInstance::selectedChanged> SelectedChangedCaller;
3251 void selectedChangedComponent( const Selectable& selectable ){
3252 GlobalSelectionSystem().getObserver ( SelectionSystem::eComponent )( selectable );
3253 GlobalSelectionSystem().onComponentSelection( *this, selectable );
3255 typedef MemberCaller<BrushInstance, void(const Selectable&), &BrushInstance::selectedChangedComponent> SelectedChangedComponentCaller;
3257 const BrushInstanceVisitor& forEachFaceInstance( const BrushInstanceVisitor& visitor ){
3258 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3260 visitor.visit( *i );
3265 static void constructStatic(){
3266 m_state_selpoint = GlobalShaderCache().capture( "$SELPOINT" );
3269 static void destroyStatic(){
3270 GlobalShaderCache().release( "$SELPOINT" );
3274 m_faceInstances.clear();
3277 void reserve( std::size_t size ){
3278 m_faceInstances.reserve( size );
3281 void push_back( Face& face ){
3282 m_faceInstances.push_back( FaceInstance( face, SelectedChangedComponentCaller( *this ) ) );
3286 ASSERT_MESSAGE( !m_faceInstances.empty(), "erasing invalid element" );
3287 m_faceInstances.pop_back();
3290 void erase( std::size_t index ){
3291 ASSERT_MESSAGE( index < m_faceInstances.size(), "erasing invalid element" );
3292 m_faceInstances.erase( m_faceInstances.begin() + index );
3295 void connectivityChanged(){
3296 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3298 ( *i ).connectivityChanged();
3303 m_edgeInstances.clear();
3306 void edge_push_back( SelectableEdge& edge ){
3307 m_edgeInstances.push_back( EdgeInstance( m_faceInstances, edge ) );
3310 void vertex_clear(){
3311 m_vertexInstances.clear();
3314 void vertex_push_back( SelectableVertex& vertex ){
3315 m_vertexInstances.push_back( VertexInstance( m_faceInstances, vertex ) );
3318 void DEBUG_verify() const {
3319 ASSERT_MESSAGE( m_faceInstances.size() == m_brush.DEBUG_size(), "FATAL: mismatch" );
3322 bool isSelected() const {
3323 return m_selectable.isSelected();
3326 void setSelected( bool select ){
3327 m_selectable.setSelected( select );
3330 void update_selected() const {
3331 m_render_selected.clear();
3332 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3334 if ( ( *i ).getFace().contributes() ) {
3335 ( *i ).iterate_selected( m_render_selected );
3340 void evaluateViewDependent( const VolumeTest& volume, const Matrix4& localToWorld ) const {
3341 if ( m_viewChanged ) {
3342 m_viewChanged = false;
3344 bool faces_visible[c_brush_maxFaces];
3346 bool* j = faces_visible;
3347 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i, ++j )
3349 *j = ( *i ).intersectVolume( volume, localToWorld );
3353 m_brush.update_wireframe( m_render_wireframe, faces_visible );
3354 m_brush.update_faces_wireframe( m_faceCentroidPointsCulled, faces_visible );
3358 void renderComponentsSelected( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3359 m_brush.evaluateBRep();
3362 if ( !m_render_selected.empty() ) {
3363 renderer.Highlight( Renderer::ePrimitive, false );
3364 renderer.SetState( m_state_selpoint, Renderer::eWireframeOnly );
3365 renderer.SetState( m_state_selpoint, Renderer::eFullMaterials );
3366 renderer.addRenderable( m_render_selected, localToWorld );
3370 void renderComponents( Renderer& renderer, const VolumeTest& volume ) const {
3371 m_brush.evaluateBRep();
3373 const Matrix4& localToWorld = Instance::localToWorld();
3375 renderer.SetState( m_brush.m_state_point, Renderer::eWireframeOnly );
3376 renderer.SetState( m_brush.m_state_point, Renderer::eFullMaterials );
3378 if ( volume.fill() && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace ) {
3379 evaluateViewDependent( volume, localToWorld );
3380 renderer.addRenderable( m_render_faces_wireframe, localToWorld );
3384 m_brush.renderComponents( GlobalSelectionSystem().ComponentMode(), renderer, volume, localToWorld );
3388 void renderClipPlane( Renderer& renderer, const VolumeTest& volume ) const {
3389 if ( GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip && isSelected() ) {
3390 m_clipPlane.render( renderer, volume, localToWorld() );
3394 void renderCommon( Renderer& renderer, const VolumeTest& volume ) const {
3395 bool componentMode = GlobalSelectionSystem().Mode() == SelectionSystem::eComponent;
3397 if ( componentMode && isSelected() ) {
3398 renderComponents( renderer, volume );
3401 if ( parentSelected() ) {
3402 if ( !componentMode ) {
3403 renderer.Highlight( Renderer::eFace );
3405 renderer.Highlight( Renderer::ePrimitive );
3409 void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3410 //renderCommon(renderer, volume);
3412 m_lightList->evaluateLights();
3414 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3416 renderer.setLights( ( *i ).m_lights );
3417 ( *i ).render( renderer, volume, localToWorld );
3420 renderComponentsSelected( renderer, volume, localToWorld );
3423 void renderWireframe( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3424 //renderCommon(renderer, volume);
3426 evaluateViewDependent( volume, localToWorld );
3428 if ( m_render_wireframe.m_size != 0 ) {
3429 renderer.addRenderable( m_render_wireframe, localToWorld );
3432 renderComponentsSelected( renderer, volume, localToWorld );
3435 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3436 m_brush.evaluateBRep();
3438 renderClipPlane( renderer, volume );
3440 renderSolid( renderer, volume, localToWorld() );
3443 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3444 m_brush.evaluateBRep();
3446 renderClipPlane( renderer, volume );
3448 renderWireframe( renderer, volume, localToWorld() );
3451 void viewChanged() const {
3452 m_viewChanged = true;
3455 void testSelect( Selector& selector, SelectionTest& test ){
3456 test.BeginMesh( localToWorld() );
3458 SelectionIntersection best;
3459 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3461 ( *i ).testSelect( test, best );
3463 if ( best.valid() ) {
3464 selector.addIntersection( best );
3468 bool isSelectedComponents() const {
3469 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3471 if ( ( *i ).selectedComponents() ) {
3478 void setSelectedComponents( bool select, SelectionSystem::EComponentMode mode ){
3479 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3481 ( *i ).setSelected( mode, select );
3485 void testSelectComponents( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode ){
3486 test.BeginMesh( localToWorld() );
3490 case SelectionSystem::eVertex:
3492 for ( VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i )
3494 ( *i ).testSelect( selector, test );
3498 case SelectionSystem::eEdge:
3500 for ( EdgeInstances::iterator i = m_edgeInstances.begin(); i != m_edgeInstances.end(); ++i )
3502 ( *i ).testSelect( selector, test );
3506 case SelectionSystem::eFace:
3508 if ( test.getVolume().fill() ) {
3509 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3511 ( *i ).testSelect( selector, test );
3516 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3518 ( *i ).testSelect_centroid( selector, test );
3528 void selectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
3529 test.BeginMesh( localToWorld() );
3531 PlanePointer brushPlanes[c_brush_maxFaces];
3532 PlanesIterator j = brushPlanes;
3534 for ( Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i )
3536 *j++ = &( *i )->plane3();
3539 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3541 ( *i ).selectPlane( selector, Line( test.getNear(), test.getFar() ), brushPlanes, j, selectedPlaneCallback );
3545 void selectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes ){
3546 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3548 ( *i ).selectReversedPlane( selector, selectedPlanes );
3553 void transformComponents( const Matrix4& matrix ){
3554 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3556 ( *i ).transformComponents( matrix );
3560 const AABB& getSelectedComponentsBounds() const {
3561 m_aabb_component = AABB();
3563 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3565 ( *i ).iterate_selected( m_aabb_component );
3568 return m_aabb_component;
3571 void snapComponents( float snap ){
3572 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3574 ( *i ).snapComponents( snap );
3578 void evaluateTransform(){
3579 Matrix4 matrix( m_transform.calculateTransform() );
3580 //globalOutputStream() << "matrix: " << matrix << "\n";
3582 if ( m_transform.getType() == TRANSFORM_PRIMITIVE ) {
3583 m_brush.transform( matrix );
3587 transformComponents( matrix );
3591 void applyTransform(){
3592 m_brush.revertTransform();
3593 evaluateTransform();
3594 m_brush.freezeTransform();
3597 typedef MemberCaller<BrushInstance, void(), &BrushInstance::applyTransform> ApplyTransformCaller;
3599 void setClipPlane( const Plane3& plane ){
3600 m_clipPlane.setPlane( m_brush, plane );
3603 bool testLight( const RendererLight& light ) const {
3604 return light.testAABB( worldAABB() );
3607 void insertLight( const RendererLight& light ){
3608 const Matrix4& localToWorld = Instance::localToWorld();
3609 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3611 Face_addLight( *i, localToWorld, light );
3616 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3618 ( *i ).m_lights.clear();
3623 inline BrushInstance* Instance_getBrush( scene::Instance& instance ){
3624 return InstanceTypeCast<BrushInstance>::cast( instance );
3628 template<typename Functor>
3629 class BrushSelectedVisitor : public SelectionSystem::Visitor
3631 const Functor& m_functor;
3633 BrushSelectedVisitor( const Functor& functor ) : m_functor( functor ){
3636 void visit( scene::Instance& instance ) const {
3637 BrushInstance* brush = Instance_getBrush( instance );
3639 m_functor( *brush );
3644 template<typename Functor>
3645 inline const Functor& Scene_forEachSelectedBrush( const Functor& functor ){
3646 GlobalSelectionSystem().foreachSelected( BrushSelectedVisitor<Functor>( functor ) );
3650 template<typename Functor>
3651 class BrushVisibleSelectedVisitor : public SelectionSystem::Visitor
3653 const Functor& m_functor;
3655 BrushVisibleSelectedVisitor( const Functor& functor ) : m_functor( functor ){
3658 void visit( scene::Instance& instance ) const {
3659 BrushInstance* brush = Instance_getBrush( instance );
3661 && instance.path().top().get().visible() ) {
3662 m_functor( *brush );
3667 template<typename Functor>
3668 inline const Functor& Scene_forEachVisibleSelectedBrush( const Functor& functor ){
3669 GlobalSelectionSystem().foreachSelected( BrushVisibleSelectedVisitor<Functor>( functor ) );
3673 class BrushForEachFace
3675 const BrushInstanceVisitor& m_visitor;
3677 BrushForEachFace( const BrushInstanceVisitor& visitor ) : m_visitor( visitor ){
3680 void operator()( BrushInstance& brush ) const {
3681 brush.forEachFaceInstance( m_visitor );
3685 template<class Functor>
3686 class FaceInstanceVisitFace : public BrushInstanceVisitor
3688 const Functor& functor;
3690 FaceInstanceVisitFace( const Functor& functor )
3691 : functor( functor ){
3694 void visit( FaceInstance& face ) const {
3695 functor( face.getFace() );
3699 template<typename Functor>
3700 inline const Functor& Brush_forEachFace( BrushInstance& brush, const Functor& functor ){
3701 brush.forEachFaceInstance( FaceInstanceVisitFace<Functor>( functor ) );
3705 template<class Functor>
3706 class FaceVisitAll : public BrushVisitor
3708 const Functor& functor;
3710 FaceVisitAll( const Functor& functor )
3711 : functor( functor ){
3714 void visit( Face& face ) const {
3719 template<typename Functor>
3720 inline const Functor& Brush_forEachFace( const Brush& brush, const Functor& functor ){
3721 brush.forEachFace( FaceVisitAll<Functor>( functor ) );
3725 template<typename Functor>
3726 inline const Functor& Brush_forEachFace( Brush& brush, const Functor& functor ){
3727 brush.forEachFace( FaceVisitAll<Functor>( functor ) );
3731 template<class Functor>
3732 class FaceInstanceVisitAll : public BrushInstanceVisitor
3734 const Functor& functor;
3736 FaceInstanceVisitAll( const Functor& functor )
3737 : functor( functor ){
3740 void visit( FaceInstance& face ) const {
3745 template<typename Functor>
3746 inline const Functor& Brush_ForEachFaceInstance( BrushInstance& brush, const Functor& functor ){
3747 brush.forEachFaceInstance( FaceInstanceVisitAll<Functor>( functor ) );
3751 template<typename Functor>
3752 inline const Functor& Scene_forEachBrush( scene::Graph& graph, const Functor& functor ){
3753 graph.traverse( InstanceWalker< InstanceApply<BrushInstance, Functor> >( functor ) );
3757 template<typename Type, typename Functor>
3758 class InstanceIfVisible : public Functor
3761 InstanceIfVisible( const Functor& functor ) : Functor( functor ){
3764 void operator()( scene::Instance& instance ){
3765 if ( instance.path().top().get().visible() ) {
3766 Functor::operator()( instance );
3771 template<typename Functor>
3772 class BrushVisibleWalker : public scene::Graph::Walker
3774 const Functor& m_functor;
3776 BrushVisibleWalker( const Functor& functor ) : m_functor( functor ){
3779 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3780 if ( path.top().get().visible() ) {
3781 BrushInstance* brush = Instance_getBrush( instance );
3783 m_functor( *brush );
3790 template<typename Functor>
3791 inline const Functor& Scene_forEachVisibleBrush( scene::Graph& graph, const Functor& functor ){
3792 graph.traverse( BrushVisibleWalker<Functor>( functor ) );
3796 template<typename Functor>
3797 inline const Functor& Scene_ForEachBrush_ForEachFace( scene::Graph& graph, const Functor& functor ){
3798 Scene_forEachBrush( graph, BrushForEachFace( FaceInstanceVisitFace<Functor>( functor ) ) );
3803 template<typename Functor>
3804 inline const Functor& Scene_ForEachBrush_ForEachFaceInstance( scene::Graph& graph, const Functor& functor ){
3805 Scene_forEachBrush( graph, BrushForEachFace( FaceInstanceVisitAll<Functor>( functor ) ) );
3809 template<typename Functor>
3810 inline const Functor& Scene_ForEachSelectedBrush_ForEachFace( scene::Graph& graph, const Functor& functor ){
3811 Scene_forEachSelectedBrush( BrushForEachFace( FaceInstanceVisitFace<Functor>( functor ) ) );
3815 template<typename Functor>
3816 inline const Functor& Scene_ForEachSelectedBrush_ForEachFaceInstance( scene::Graph& graph, const Functor& functor ){
3817 Scene_forEachSelectedBrush( BrushForEachFace( FaceInstanceVisitAll<Functor>( functor ) ) );
3821 template<typename Functor>
3822 class FaceVisitorWrapper
3824 const Functor& functor;
3826 FaceVisitorWrapper( const Functor& functor ) : functor( functor ){
3829 void operator()( FaceInstance& faceInstance ) const {
3830 functor( faceInstance.getFace() );
3834 template<typename Functor>
3835 inline const Functor& Scene_ForEachSelectedBrushFace( scene::Graph& graph, const Functor& functor ){
3836 g_SelectedFaceInstances.foreach( FaceVisitorWrapper<Functor>( functor ) );