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";
106 inline bool texdef_sane( const texdef_t& texdef ){
107 return fabs( texdef.shift[0] ) < ( 1 << 16 )
108 && fabs( texdef.shift[1] ) < ( 1 << 16 );
111 inline void Winding_DrawWireframe( const Winding& winding ){
112 glVertexPointer( 3, GL_FLOAT, sizeof( WindingVertex ), &winding.points.data()->vertex );
113 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( winding.numpoints ) );
116 inline void Winding_Draw( const Winding& winding, const Vector3& normal, RenderStateFlags state ){
117 glVertexPointer( 3, GL_FLOAT, sizeof( WindingVertex ), &winding.points.data()->vertex );
119 if ( ( state & RENDER_BUMP ) != 0 ) {
120 Vector3 normals[c_brush_maxFaces];
121 typedef Vector3* Vector3Iter;
122 for ( Vector3Iter i = normals, end = normals + winding.numpoints; i != end; ++i )
126 if ( GlobalShaderCache().useShaderLanguage() ) {
127 glNormalPointer( GL_FLOAT, sizeof( Vector3 ), normals );
128 glVertexAttribPointerARB( c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->texcoord );
129 glVertexAttribPointerARB( c_attr_Tangent, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->tangent );
130 glVertexAttribPointerARB( c_attr_Binormal, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->bitangent );
134 glVertexAttribPointerARB( 11, 3, GL_FLOAT, 0, sizeof( Vector3 ), normals );
135 glVertexAttribPointerARB( 8, 2, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->texcoord );
136 glVertexAttribPointerARB( 9, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->tangent );
137 glVertexAttribPointerARB( 10, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->bitangent );
142 if ( state & RENDER_LIGHTING ) {
143 Vector3 normals[c_brush_maxFaces];
144 typedef Vector3* Vector3Iter;
145 for ( Vector3Iter i = normals, last = normals + winding.numpoints; i != last; ++i )
149 glNormalPointer( GL_FLOAT, sizeof( Vector3 ), normals );
152 if ( state & RENDER_TEXTURE ) {
153 glTexCoordPointer( 2, GL_FLOAT, sizeof( WindingVertex ), &winding.points.data()->texcoord );
157 if ( state & RENDER_FILL ) {
158 glDrawArrays( GL_TRIANGLE_FAN, 0, GLsizei( winding.numpoints ) );
162 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( winding.numpoints ) );
165 glDrawArrays( GL_POLYGON, 0, GLsizei( winding.numpoints ) );
169 const Winding& winding = winding;
171 if ( state & RENDER_FILL ) {
172 glBegin( GL_POLYGON );
176 glBegin( GL_LINE_LOOP );
179 if ( state & RENDER_LIGHTING ) {
180 glNormal3fv( normal );
183 for ( int i = 0; i < winding.numpoints; ++i )
185 if ( state & RENDER_TEXTURE ) {
186 glTexCoord2fv( &winding.points[i][3] );
188 glVertex3fv( winding.points[i] );
195 #include "shaderlib.h"
197 typedef DoubleVector3 PlanePoints[3];
199 inline bool planepts_equal( const PlanePoints planepts, const PlanePoints other ){
200 return planepts[0] == other[0] && planepts[1] == other[1] && planepts[2] == other[2];
203 inline void planepts_assign( PlanePoints planepts, const PlanePoints other ){
204 planepts[0] = other[0];
205 planepts[1] = other[1];
206 planepts[2] = other[2];
209 inline void planepts_quantise( PlanePoints planepts, double snap ){
210 vector3_snap( planepts[0], snap );
211 vector3_snap( planepts[1], snap );
212 vector3_snap( planepts[2], snap );
215 inline float vector3_max_component( const Vector3& vec3 ){
216 return std::max( fabsf( vec3[0] ), std::max( fabsf( vec3[1] ), fabsf( vec3[2] ) ) );
219 inline void edge_snap( Vector3& edge, double snap ){
220 float scale = static_cast<float>( ceil( fabs( snap / vector3_max_component( edge ) ) ) );
221 if ( scale > 0.0f ) {
222 vector3_scale( edge, scale );
224 vector3_snap( edge, snap );
227 inline void planepts_snap( PlanePoints planepts, double snap ){
228 Vector3 edge01( vector3_subtracted( planepts[1], planepts[0] ) );
229 Vector3 edge12( vector3_subtracted( planepts[2], planepts[1] ) );
230 Vector3 edge20( vector3_subtracted( planepts[0], planepts[2] ) );
232 double length_squared_01 = vector3_dot( edge01, edge01 );
233 double length_squared_12 = vector3_dot( edge12, edge12 );
234 double length_squared_20 = vector3_dot( edge20, edge20 );
236 vector3_snap( planepts[0], snap );
238 if ( length_squared_01 < length_squared_12 ) {
239 if ( length_squared_12 < length_squared_20 ) {
240 edge_snap( edge01, snap );
241 edge_snap( edge12, snap );
242 planepts[1] = vector3_added( planepts[0], edge01 );
243 planepts[2] = vector3_added( planepts[1], edge12 );
247 edge_snap( edge20, snap );
248 edge_snap( edge01, snap );
249 planepts[1] = vector3_added( planepts[0], edge20 );
250 planepts[2] = vector3_added( planepts[1], edge01 );
255 if ( length_squared_01 < length_squared_20 ) {
256 edge_snap( edge01, snap );
257 edge_snap( edge12, snap );
258 planepts[1] = vector3_added( planepts[0], edge01 );
259 planepts[2] = vector3_added( planepts[1], edge12 );
263 edge_snap( edge12, snap );
264 edge_snap( edge20, snap );
265 planepts[1] = vector3_added( planepts[0], edge12 );
266 planepts[2] = vector3_added( planepts[1], edge20 );
271 inline PointVertex pointvertex_for_planept( const DoubleVector3& point, const Colour4b& colour ){
274 static_cast<float>( point.x() ),
275 static_cast<float>( point.y() ),
276 static_cast<float>( point.z() )
282 inline PointVertex pointvertex_for_windingpoint( const Vector3& point, const Colour4b& colour ){
284 vertex3f_for_vector3( point ),
289 inline bool check_plane_is_integer( const PlanePoints& planePoints ){
290 return !float_is_integer( planePoints[0][0] )
291 || !float_is_integer( planePoints[0][1] )
292 || !float_is_integer( planePoints[0][2] )
293 || !float_is_integer( planePoints[1][0] )
294 || !float_is_integer( planePoints[1][1] )
295 || !float_is_integer( planePoints[1][2] )
296 || !float_is_integer( planePoints[2][0] )
297 || !float_is_integer( planePoints[2][1] )
298 || !float_is_integer( planePoints[2][2] );
301 inline void brush_check_shader( const char* name ){
302 if ( !shader_valid( name ) ) {
303 globalErrorStream() << "brush face has invalid texture name: '" << name << "'\n";
307 class FaceShaderObserver
310 virtual void realiseShader() = 0;
311 virtual void unrealiseShader() = 0;
314 class FaceShaderObserverRealise
317 void operator()( FaceShaderObserver& observer ) const {
318 observer.realiseShader();
322 class FaceShaderObserverUnrealise
325 void operator()( FaceShaderObserver& observer ) const {
326 observer.unrealiseShader();
330 typedef ReferencePair<FaceShaderObserver> FaceShaderObserverPair;
333 class ContentsFlagsValue
336 ContentsFlagsValue(){
338 ContentsFlagsValue( int surfaceFlags, int contentFlags, int value, bool specified ) :
339 m_surfaceFlags( surfaceFlags ),
340 m_contentFlags( contentFlags ),
342 m_specified( specified ){
350 inline void ContentsFlagsValue_assignMasked( ContentsFlagsValue& flags, const ContentsFlagsValue& other ){
351 bool detail = bitfield_enabled( flags.m_contentFlags, BRUSH_DETAIL_MASK );
354 flags.m_contentFlags = bitfield_enable( flags.m_contentFlags, BRUSH_DETAIL_MASK );
358 flags.m_contentFlags = bitfield_disable( flags.m_contentFlags, BRUSH_DETAIL_MASK );
363 class FaceShader : public ModuleObserver
369 CopiedString m_shader;
370 ContentsFlagsValue m_flags;
372 SavedState( const FaceShader& faceShader ){
373 m_shader = faceShader.getShader();
374 m_flags = faceShader.m_flags;
377 void exportState( FaceShader& faceShader ) const {
378 faceShader.setShader( m_shader.c_str() );
379 faceShader.setFlags( m_flags );
383 CopiedString m_shader;
385 ContentsFlagsValue m_flags;
386 FaceShaderObserverPair m_observers;
390 FaceShader( const char* shader, const ContentsFlagsValue& flags = ContentsFlagsValue( 0, 0, 0, false ) ) :
394 m_instanced( false ),
401 // copy-construction not supported
402 FaceShader( const FaceShader& other );
404 void instanceAttach(){
406 m_state->incrementUsed();
408 void instanceDetach(){
409 m_state->decrementUsed();
413 void captureShader(){
414 ASSERT_MESSAGE( m_state == 0, "shader cannot be captured" );
415 brush_check_shader( m_shader.c_str() );
416 m_state = GlobalShaderCache().capture( m_shader.c_str() );
417 m_state->attach( *this );
419 void releaseShader(){
420 ASSERT_MESSAGE( m_state != 0, "shader cannot be released" );
421 m_state->detach( *this );
422 GlobalShaderCache().release( m_shader.c_str() );
427 ASSERT_MESSAGE( !m_realised, "FaceTexdef::realise: already realised" );
429 m_observers.forEach( FaceShaderObserverRealise() );
432 ASSERT_MESSAGE( m_realised, "FaceTexdef::unrealise: already unrealised" );
433 m_observers.forEach( FaceShaderObserverUnrealise() );
437 void attach( FaceShaderObserver& observer ){
438 m_observers.attach( observer );
440 observer.realiseShader();
444 void detach( FaceShaderObserver& observer ){
446 observer.unrealiseShader();
448 m_observers.detach( observer );
451 const char* getShader() const {
452 return m_shader.c_str();
454 void setShader( const char* name ){
456 m_state->decrementUsed();
462 m_state->incrementUsed();
465 ContentsFlagsValue getFlags() const {
466 ASSERT_MESSAGE( m_realised, "FaceShader::getFlags: flags not valid when unrealised" );
467 if ( !m_flags.m_specified ) {
468 return ContentsFlagsValue(
469 m_state->getTexture().surfaceFlags,
470 m_state->getTexture().contentFlags,
471 m_state->getTexture().value,
477 void setFlags( const ContentsFlagsValue& flags ){
478 ASSERT_MESSAGE( m_realised, "FaceShader::setFlags: flags not valid when unrealised" );
479 ContentsFlagsValue_assignMasked( m_flags, flags );
482 Shader* state() const {
486 std::size_t width() const {
488 return m_state->getTexture().width;
492 std::size_t height() const {
494 return m_state->getTexture().height;
498 unsigned int shaderFlags() const {
500 return m_state->getFlags();
509 class FaceTexdef : public FaceShaderObserver
512 FaceTexdef( const FaceTexdef& other );
514 FaceTexdef& operator=( const FaceTexdef& other );
519 TextureProjection m_projection;
521 SavedState( const FaceTexdef& faceTexdef ){
522 m_projection = faceTexdef.m_projection;
525 void exportState( FaceTexdef& faceTexdef ) const {
526 Texdef_Assign( faceTexdef.m_projection, m_projection );
530 FaceShader& m_shader;
531 TextureProjection m_projection;
532 bool m_projectionInitialised;
537 const TextureProjection& projection,
538 bool projectionInitialised = true
541 m_projection( projection ),
542 m_projectionInitialised( projectionInitialised ),
543 m_scaleApplied( false ){
544 m_shader.attach( *this );
547 m_shader.detach( *this );
551 ASSERT_MESSAGE( !m_scaleApplied, "texture scale aready added" );
552 m_scaleApplied = true;
553 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 ) {
566 void unrealiseShader(){
567 if ( m_projectionInitialised && m_scaleApplied ) {
572 void setTexdef( const TextureProjection& projection ){
574 Texdef_Assign( m_projection, projection );
578 void shift( float s, float t ){
579 ASSERT_MESSAGE( texdef_sane( m_projection.m_texdef ), "FaceTexdef::shift: bad texdef" );
581 Texdef_Shift( m_projection, s, t );
585 void scale( float s, float t ){
587 Texdef_Scale( m_projection, s, t );
591 void rotate( float angle ){
593 Texdef_Rotate( m_projection, angle );
597 void fit( const Vector3& normal, const Winding& winding, float s_repeat, float t_repeat ){
598 Texdef_FitTexture( m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat );
601 void emitTextureCoordinates( Winding& winding, const Vector3& normal, const Matrix4& localToWorld ){
602 Texdef_EmitTextureCoordinates( m_projection, m_shader.width(), m_shader.height(), winding, normal, localToWorld );
605 void transform( const Plane3& plane, const Matrix4& matrix ){
607 Texdef_transformLocked( m_projection, m_shader.width(), m_shader.height(), plane, matrix );
611 TextureProjection normalised() const {
612 brushprimit_texdef_t tmp( m_projection.m_brushprimit_texdef );
613 tmp.removeScale( m_shader.width(), m_shader.height() );
614 return TextureProjection( m_projection.m_texdef, tmp, m_projection.m_basis_s, m_projection.m_basis_t );
616 void setBasis( const Vector3& normal ){
618 Normal_GetTransform( normal, basis );
619 m_projection.m_basis_s = Vector3( basis.xx(), basis.yx(), basis.zx() );
620 m_projection.m_basis_t = Vector3( -basis.xy(), -basis.yy(), -basis.zy() );
624 inline void planepts_print( const PlanePoints& planePoints, TextOutputStream& ostream ){
625 ostream << "( " << planePoints[0][0] << " " << planePoints[0][1] << " " << planePoints[0][2] << " ) "
626 << "( " << planePoints[1][0] << " " << planePoints[1][1] << " " << planePoints[1][2] << " ) "
627 << "( " << planePoints[2][0] << " " << planePoints[2][1] << " " << planePoints[2][2] << " )";
631 inline Plane3 Plane3_applyTranslation( const Plane3& plane, const Vector3& translation ){
632 Plane3 tmp( plane3_translated( Plane3( plane.normal(), -plane.dist() ), translation ) );
633 return Plane3( tmp.normal(), -tmp.dist() );
636 inline Plane3 Plane3_applyTransform( const Plane3& plane, const Matrix4& matrix ){
637 Plane3 tmp( plane3_transformed( Plane3( plane.normal(), -plane.dist() ), matrix ) );
638 return Plane3( tmp.normal(), -tmp.dist() );
643 PlanePoints m_planepts;
644 Plane3 m_planeCached;
647 Vector3 m_funcStaticOrigin;
649 static EBrushType m_type;
651 static bool isDoom3Plane(){
652 return FacePlane::m_type == eBrushTypeDoom3 || FacePlane::m_type == eBrushTypeQuake4;
658 PlanePoints m_planepts;
661 SavedState( const FacePlane& facePlane ){
662 if ( facePlane.isDoom3Plane() ) {
663 m_plane = facePlane.m_plane;
667 planepts_assign( m_planepts, facePlane.planePoints() );
671 void exportState( FacePlane& facePlane ) const {
672 if ( facePlane.isDoom3Plane() ) {
673 facePlane.m_plane = m_plane;
674 facePlane.updateTranslated();
678 planepts_assign( facePlane.planePoints(), m_planepts );
679 facePlane.MakePlane();
684 FacePlane() : m_funcStaticOrigin( 0, 0, 0 ){
686 FacePlane( const FacePlane& other ) : m_funcStaticOrigin( 0, 0, 0 ){
687 if ( !isDoom3Plane() ) {
688 planepts_assign( m_planepts, other.m_planepts );
693 m_plane = other.m_plane;
699 if ( !isDoom3Plane() ) {
701 if ( check_plane_is_integer( m_planepts ) ) {
702 globalErrorStream() << "non-integer planepts: ";
703 planepts_print( m_planepts, globalErrorStream() );
704 globalErrorStream() << "\n";
707 m_planeCached = plane3_for_points( m_planepts );
712 if ( !isDoom3Plane() ) {
713 vector3_swap( m_planepts[0], m_planepts[2] );
718 m_planeCached = plane3_flipped( m_plane );
722 void transform( const Matrix4& matrix, bool mirror ){
723 if ( !isDoom3Plane() ) {
726 bool off = check_plane_is_integer( planePoints() );
729 matrix4_transform_point( matrix, m_planepts[0] );
730 matrix4_transform_point( matrix, m_planepts[1] );
731 matrix4_transform_point( matrix, m_planepts[2] );
738 if ( check_plane_is_integer( planePoints() ) ) {
740 globalErrorStream() << "caused by transform\n";
748 m_planeCached = Plane3_applyTransform( m_planeCached, matrix );
752 void offset( float offset ){
753 if ( !isDoom3Plane() ) {
754 Vector3 move( vector3_scaled( m_planeCached.normal(), -offset ) );
756 vector3_subtract( m_planepts[0], move );
757 vector3_subtract( m_planepts[1], move );
758 vector3_subtract( m_planepts[2], move );
764 m_planeCached.d += offset;
769 void updateTranslated(){
770 m_planeCached = Plane3_applyTranslation( m_plane, m_funcStaticOrigin );
773 m_plane = Plane3_applyTranslation( m_planeCached, vector3_negated( m_funcStaticOrigin ) );
777 PlanePoints& planePoints(){
780 const PlanePoints& planePoints() const {
783 const Plane3& plane3() const {
784 return m_planeCached;
786 void setDoom3Plane( const Plane3& plane ){
790 const Plane3& getDoom3Plane() const {
794 void copy( const FacePlane& other ){
795 if ( !isDoom3Plane() ) {
796 planepts_assign( m_planepts, other.m_planepts );
801 m_planeCached = other.m_plane;
805 void copy( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
806 if ( !isDoom3Plane() ) {
814 m_planeCached = plane3_for_points( p2, p1, p0 );
820 inline void Winding_testSelect( Winding& winding, SelectionTest& test, SelectionIntersection& best ){
821 test.TestPolygon( VertexPointer( reinterpret_cast<VertexPointer::pointer>( &winding.points.data()->vertex ), sizeof( WindingVertex ) ), winding.numpoints, best );
824 const double GRID_MIN = 0.125;
826 inline double quantiseInteger( double f ){
827 return float_to_integer( f );
830 inline double quantiseFloating( double f ){
831 return float_snapped( f, 1.f / ( 1 << 16 ) );
834 typedef double ( *QuantiseFunc )( double f );
841 virtual bool filter( const Face& face ) const = 0;
844 bool face_filtered( Face& face );
845 void add_face_filter( FaceFilter& filter, int mask, bool invert = false );
847 void Brush_addTextureChangedCallback( const SignalHandler& callback );
848 void Brush_textureChanged();
851 extern bool g_brush_texturelock_enabled;
856 virtual void planeChanged() = 0;
857 virtual void connectivityChanged() = 0;
858 virtual void shaderChanged() = 0;
859 virtual void evaluateTransform() = 0;
863 public OpenGLRenderable,
866 public FaceShaderObserver
868 std::size_t m_refcount;
870 class SavedState : public UndoMemento
873 FacePlane::SavedState m_planeState;
874 FaceTexdef::SavedState m_texdefState;
875 FaceShader::SavedState m_shaderState;
877 SavedState( const Face& face ) : m_planeState( face.getPlane() ), m_texdefState( face.getTexdef() ), m_shaderState( face.getShader() ){
880 void exportState( Face& face ) const {
881 m_planeState.exportState( face.getPlane() );
882 m_shaderState.exportState( face.getShader() );
883 m_texdefState.exportState( face.getTexdef() );
892 static QuantiseFunc m_quantise;
893 static EBrushType m_type;
895 PlanePoints m_move_planepts;
896 PlanePoints m_move_planeptsTransformed;
899 FacePlane m_planeTransformed;
902 TextureProjection m_texdefTransformed;
908 FaceObserver* m_observer;
909 UndoObserver* m_undoable_observer;
912 // assignment not supported
913 Face& operator=( const Face& other );
914 // copy-construction not supported
915 Face( const Face& other );
919 Face( FaceObserver* observer ) :
921 m_shader( texdef_name_default() ),
922 m_texdef( m_shader, TextureProjection(), false ),
924 m_observer( observer ),
925 m_undoable_observer( 0 ),
927 m_shader.attach( *this );
928 m_plane.copy( Vector3( 0, 0, 0 ), Vector3( 64, 0, 0 ), Vector3( 0, 64, 0 ) );
929 m_texdef.setBasis( m_plane.plane3().normal() );
937 const TextureProjection& projection,
938 FaceObserver* observer
942 m_texdef( m_shader, projection ),
943 m_observer( observer ),
944 m_undoable_observer( 0 ),
946 m_shader.attach( *this );
947 m_plane.copy( p0, p1, p2 );
948 m_texdef.setBasis( m_plane.plane3().normal() );
952 Face( const Face& other, FaceObserver* observer ) :
954 m_shader( other.m_shader.getShader(), other.m_shader.m_flags ),
955 m_texdef( m_shader, other.getTexdef().normalised() ),
956 m_observer( observer ),
957 m_undoable_observer( 0 ),
959 m_shader.attach( *this );
960 m_plane.copy( other.m_plane );
961 planepts_assign( m_move_planepts, other.m_move_planepts );
962 m_texdef.setBasis( m_plane.plane3().normal() );
967 m_shader.detach( *this );
972 m_observer->planeChanged();
975 void realiseShader(){
976 m_observer->shaderChanged();
978 void unrealiseShader(){
981 void instanceAttach( MapFile* map ){
982 m_shader.instanceAttach();
984 m_undoable_observer = GlobalUndoSystem().observer( this );
985 GlobalFilterSystem().registerFilterable( *this );
987 void instanceDetach( MapFile* map ){
988 GlobalFilterSystem().unregisterFilterable( *this );
989 m_undoable_observer = 0;
990 GlobalUndoSystem().release( this );
992 m_shader.instanceDetach();
995 void render( RenderStateFlags state ) const {
996 Winding_Draw( m_winding, m_planeTransformed.plane3().normal(), state );
999 void updateFiltered(){
1000 m_filtered = face_filtered( *this );
1002 bool isFiltered() const {
1010 if ( m_undoable_observer != 0 ) {
1011 m_undoable_observer->save( this );
1016 UndoMemento* exportState() const {
1017 return new SavedState( *this );
1019 void importState( const UndoMemento* data ){
1022 static_cast<const SavedState*>( data )->exportState( *this );
1025 m_observer->connectivityChanged();
1027 m_observer->shaderChanged();
1035 if ( --m_refcount == 0 ) {
1045 bool intersectVolume( const VolumeTest& volume, const Matrix4& localToWorld ) const {
1046 return volume.TestPlane( Plane3( plane3().normal(), -plane3().dist() ), localToWorld );
1049 void render( Renderer& renderer, const Matrix4& localToWorld ) const {
1050 renderer.SetState( m_shader.state(), Renderer::eFullMaterials );
1051 renderer.addRenderable( *this, localToWorld );
1054 void transform( const Matrix4& matrix, bool mirror ){
1055 if ( g_brush_texturelock_enabled ) {
1056 Texdef_transformLocked( m_texdefTransformed, m_shader.width(), m_shader.height(), m_plane.plane3(), matrix );
1059 m_planeTransformed.transform( matrix, mirror );
1062 ASSERT_MESSAGE( projectionaxis_for_normal( normal ) == projectionaxis_for_normal( plane3().normal() ), "bleh" );
1064 m_observer->planeChanged();
1066 if ( g_brush_texturelock_enabled ) {
1067 Brush_textureChanged();
1071 void assign_planepts( const PlanePoints planepts ){
1072 m_planeTransformed.copy( planepts[0], planepts[1], planepts[2] );
1073 m_observer->planeChanged();
1076 /// \brief Reverts the transformable state of the brush to identity.
1077 void revertTransform(){
1078 m_planeTransformed = m_plane;
1079 planepts_assign( m_move_planeptsTransformed, m_move_planepts );
1080 m_texdefTransformed = m_texdef.m_projection;
1082 void freezeTransform(){
1084 m_plane = m_planeTransformed;
1085 planepts_assign( m_move_planepts, m_move_planeptsTransformed );
1086 m_texdef.m_projection = m_texdefTransformed;
1089 void update_move_planepts_vertex( std::size_t index, PlanePoints planePoints ){
1090 std::size_t numpoints = getWinding().numpoints;
1091 ASSERT_MESSAGE( index < numpoints, "update_move_planepts_vertex: invalid index" );
1093 std::size_t opposite = Winding_Opposite( getWinding(), index );
1094 std::size_t adjacent = Winding_wrap( getWinding(), opposite + numpoints - 1 );
1095 planePoints[0] = getWinding()[opposite].vertex;
1096 planePoints[1] = getWinding()[index].vertex;
1097 planePoints[2] = getWinding()[adjacent].vertex;
1098 // winding points are very inaccurate, so they must be quantised before using them to generate the face-plane
1099 planepts_quantise( planePoints, GRID_MIN );
1102 void snapto( float snap ){
1103 if ( contributes() ) {
1105 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane before snap to grid" );
1106 planepts_snap( m_plane.planePoints(), snap );
1107 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane after snap to grid" );
1109 PlanePoints planePoints;
1110 update_move_planepts_vertex( 0, planePoints );
1111 vector3_snap( planePoints[0], snap );
1112 vector3_snap( planePoints[1], snap );
1113 vector3_snap( planePoints[2], snap );
1114 assign_planepts( planePoints );
1117 SceneChangeNotify();
1118 if ( !plane3_valid( m_plane.plane3() ) ) {
1119 globalErrorStream() << "WARNING: invalid plane after snap to grid\n";
1124 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1125 Winding_testSelect( m_winding, test, best );
1128 void testSelect_centroid( SelectionTest& test, SelectionIntersection& best ){
1129 test.TestPoint( m_centroid, best );
1132 void shaderChanged(){
1133 EmitTextureCoordinates();
1134 Brush_textureChanged();
1135 m_observer->shaderChanged();
1138 SceneChangeNotify();
1141 const char* GetShader() const {
1142 return m_shader.getShader();
1144 void SetShader( const char* name ){
1146 m_shader.setShader( name );
1150 void revertTexdef(){
1151 m_texdefTransformed = m_texdef.m_projection;
1153 void texdefChanged(){
1155 EmitTextureCoordinates();
1156 Brush_textureChanged();
1159 void GetTexdef( TextureProjection& projection ) const {
1160 projection = m_texdef.normalised();
1162 void SetTexdef( const TextureProjection& projection ){
1164 m_texdef.setTexdef( projection );
1168 void GetFlags( ContentsFlagsValue& flags ) const {
1169 flags = m_shader.getFlags();
1171 void SetFlags( const ContentsFlagsValue& flags ){
1173 m_shader.setFlags( flags );
1174 m_observer->shaderChanged();
1178 void ShiftTexdef( float s, float t ){
1180 m_texdef.shift( s, t );
1184 void ScaleTexdef( float s, float t ){
1186 m_texdef.scale( s, t );
1190 void RotateTexdef( float angle ){
1192 m_texdef.rotate( angle );
1196 void FitTexture( float s_repeat, float t_repeat ){
1198 m_texdef.fit( m_plane.plane3().normal(), m_winding, s_repeat, t_repeat );
1202 void EmitTextureCoordinates(){
1203 Texdef_EmitTextureCoordinates( m_texdefTransformed, m_shader.width(), m_shader.height(), m_winding, plane3().normal(), g_matrix4_identity );
1207 const Vector3& centroid() const {
1211 void construct_centroid(){
1212 Winding_Centroid( m_winding, plane3(), m_centroid );
1215 const Winding& getWinding() const {
1218 Winding& getWinding(){
1222 const Plane3& plane3() const {
1223 m_observer->evaluateTransform();
1224 return m_planeTransformed.plane3();
1226 FacePlane& getPlane(){
1229 const FacePlane& getPlane() const {
1232 FaceTexdef& getTexdef(){
1235 const FaceTexdef& getTexdef() const {
1238 FaceShader& getShader(){
1241 const FaceShader& getShader() const {
1245 bool isDetail() const {
1246 return ( m_shader.m_flags.m_contentFlags & BRUSH_DETAIL_MASK ) != 0;
1248 void setDetail( bool detail ){
1250 if ( detail && !isDetail() ) {
1251 m_shader.m_flags.m_contentFlags |= BRUSH_DETAIL_MASK;
1253 else if ( !detail && isDetail() ) {
1254 m_shader.m_flags.m_contentFlags &= ~BRUSH_DETAIL_MASK;
1256 m_observer->shaderChanged();
1259 bool contributes() const {
1260 return m_winding.numpoints > 2;
1262 bool is_bounded() const {
1263 for ( Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i )
1265 if ( ( *i ).adjacent == c_brush_maxFaces ) {
1277 std::size_t m_vertex;
1280 FaceVertexId( std::size_t face, std::size_t vertex )
1281 : m_face( face ), m_vertex( vertex ){
1284 std::size_t getFace() const {
1287 std::size_t getVertex() const {
1292 typedef std::size_t faceIndex_t;
1294 struct EdgeRenderIndices
1300 : first( 0 ), second( 0 ){
1302 EdgeRenderIndices( const RenderIndex _first, const RenderIndex _second )
1303 : first( _first ), second( _second ){
1313 : first( c_brush_maxFaces ), second( c_brush_maxFaces ){
1315 EdgeFaces( const faceIndex_t _first, const faceIndex_t _second )
1316 : first( _first ), second( _second ){
1320 class RenderableWireframe : public OpenGLRenderable
1323 void render( RenderStateFlags state ) const {
1325 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices->colour );
1326 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices->vertex );
1327 glDrawElements( GL_LINES, GLsizei( m_size << 1 ), RenderIndexTypeID, m_faceVertex.data() );
1329 glBegin( GL_LINES );
1330 for ( std::size_t i = 0; i < m_size; ++i )
1332 glVertex3fv( &m_vertices[m_faceVertex[i].first].vertex.x );
1333 glVertex3fv( &m_vertices[m_faceVertex[i].second].vertex.x );
1339 Array<EdgeRenderIndices> m_faceVertex;
1341 const PointVertex* m_vertices;
1345 typedef std::vector<Brush*> brush_vector_t;
1350 virtual bool filter( const Brush& brush ) const = 0;
1353 bool brush_filtered( Brush& brush );
1354 void add_brush_filter( BrushFilter& filter, int mask, bool invert = false );
1357 /// \brief Returns true if 'self' takes priority when building brush b-rep.
1358 inline bool plane3_inside( const Plane3& self, const Plane3& other, bool selfIsLater ){
1359 if ( vector3_equal_epsilon( self.normal(), other.normal(), 0.001 ) ) {
1360 // same plane? prefer the one with smaller index
1361 if ( self.dist() == other.dist() ) {
1364 return self.dist() < other.dist();
1369 typedef SmartPointer<Face> FaceSmartPointer;
1370 typedef std::vector<FaceSmartPointer> Faces;
1372 /// \brief Returns the unique-id of the edge adjacent to \p faceVertex in the edge-pair for the set of \p faces.
1373 inline FaceVertexId next_edge( const Faces& faces, FaceVertexId faceVertex ){
1374 std::size_t adjacent_face = faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent;
1375 std::size_t adjacent_vertex = Winding_FindAdjacent( faces[adjacent_face]->getWinding(), faceVertex.getFace() );
1377 ASSERT_MESSAGE( adjacent_vertex != c_brush_maxFaces, "connectivity data invalid" );
1378 if ( adjacent_vertex == c_brush_maxFaces ) {
1382 return FaceVertexId( adjacent_face, adjacent_vertex );
1385 /// \brief Returns the unique-id of the vertex adjacent to \p faceVertex in the vertex-ring for the set of \p faces.
1386 inline FaceVertexId next_vertex( const Faces& faces, FaceVertexId faceVertex ){
1387 FaceVertexId nextEdge = next_edge( faces, faceVertex );
1388 return FaceVertexId( nextEdge.getFace(), Winding_next( faces[nextEdge.getFace()]->getWinding(), nextEdge.getVertex() ) );
1391 class SelectableEdge
1393 Vector3 getEdge() const {
1394 const Winding& winding = getFace().getWinding();
1395 return vector3_mid( winding[m_faceVertex.getVertex()].vertex, winding[Winding_next( winding, m_faceVertex.getVertex() )].vertex );
1400 FaceVertexId m_faceVertex;
1402 SelectableEdge( Faces& faces, FaceVertexId faceVertex )
1403 : m_faces( faces ), m_faceVertex( faceVertex ){
1405 SelectableEdge& operator=( const SelectableEdge& other ){
1406 m_faceVertex = other.m_faceVertex;
1410 Face& getFace() const {
1411 return *m_faces[m_faceVertex.getFace()];
1414 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1415 test.TestPoint( getEdge(), best );
1419 class SelectableVertex
1421 Vector3 getVertex() const {
1422 return getFace().getWinding()[m_faceVertex.getVertex()].vertex;
1427 FaceVertexId m_faceVertex;
1429 SelectableVertex( Faces& faces, FaceVertexId faceVertex )
1430 : m_faces( faces ), m_faceVertex( faceVertex ){
1432 SelectableVertex& operator=( const SelectableVertex& other ){
1433 m_faceVertex = other.m_faceVertex;
1437 Face& getFace() const {
1438 return *m_faces[m_faceVertex.getFace()];
1441 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1442 test.TestPoint( getVertex(), best );
1449 virtual void reserve( std::size_t size ) = 0;
1450 virtual void clear() = 0;
1451 virtual void push_back( Face& face ) = 0;
1452 virtual void pop_back() = 0;
1453 virtual void erase( std::size_t index ) = 0;
1454 virtual void connectivityChanged() = 0;
1456 virtual void edge_clear() = 0;
1457 virtual void edge_push_back( SelectableEdge& edge ) = 0;
1459 virtual void vertex_clear() = 0;
1460 virtual void vertex_push_back( SelectableVertex& vertex ) = 0;
1462 virtual void DEBUG_verify() const = 0;
1468 virtual void visit( Face& face ) const = 0;
1472 public TransformNode,
1477 public FaceObserver,
1483 scene::Node* m_node;
1484 typedef UniqueSet<BrushObserver*> Observers;
1485 Observers m_observers;
1486 UndoObserver* m_undoable_observer;
1493 // cached data compiled from state
1494 Array<PointVertex> m_faceCentroidPoints;
1495 RenderablePointArray m_render_faces;
1497 Array<PointVertex> m_uniqueVertexPoints;
1498 typedef std::vector<SelectableVertex> SelectableVertices;
1499 SelectableVertices m_select_vertices;
1500 RenderablePointArray m_render_vertices;
1502 Array<PointVertex> m_uniqueEdgePoints;
1503 typedef std::vector<SelectableEdge> SelectableEdges;
1504 SelectableEdges m_select_edges;
1505 RenderablePointArray m_render_edges;
1507 Array<EdgeRenderIndices> m_edge_indices;
1508 Array<EdgeFaces> m_edge_faces;
1513 Callback m_evaluateTransform;
1514 Callback m_boundsChanged;
1516 mutable bool m_planeChanged; // b-rep evaluation required
1517 mutable bool m_transformChanged; // transform evaluation required
1521 STRING_CONSTANT( Name, "Brush" );
1523 Callback m_lightsChanged;
1526 static Shader* m_state_point;
1529 static EBrushType m_type;
1530 static double m_maxWorldCoord;
1532 Brush( scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged ) :
1534 m_undoable_observer( 0 ),
1536 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1537 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1538 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1539 m_evaluateTransform( evaluateTransform ),
1540 m_boundsChanged( boundsChanged ),
1541 m_planeChanged( false ),
1542 m_transformChanged( false ){
1545 Brush( const Brush& other, scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged ) :
1547 m_undoable_observer( 0 ),
1549 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1550 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1551 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1552 m_evaluateTransform( evaluateTransform ),
1553 m_boundsChanged( boundsChanged ),
1554 m_planeChanged( false ),
1555 m_transformChanged( false ){
1558 Brush( const Brush& other ) :
1559 TransformNode( other ),
1564 FaceObserver( other ),
1565 Filterable( other ),
1567 BrushDoom3( other ),
1569 m_undoable_observer( 0 ),
1571 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1572 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1573 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1574 m_planeChanged( false ),
1575 m_transformChanged( false ){
1579 ASSERT_MESSAGE( m_observers.empty(), "Brush::~Brush: observers still attached" );
1582 // assignment not supported
1583 Brush& operator=( const Brush& other );
1585 void setDoom3GroupOrigin( const Vector3& origin ){
1586 //globalOutputStream() << "func_static origin before: " << m_funcStaticOrigin << " after: " << origin << "\n";
1587 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1589 ( *i )->getPlane().m_funcStaticOrigin = origin;
1590 ( *i )->getPlane().updateTranslated();
1591 ( *i )->planeChanged();
1596 void attach( BrushObserver& observer ){
1597 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1599 observer.push_back( *( *i ) );
1602 for ( SelectableEdges::iterator i = m_select_edges.begin(); i != m_select_edges.end(); ++i )
1604 observer.edge_push_back( *i );
1607 for ( SelectableVertices::iterator i = m_select_vertices.begin(); i != m_select_vertices.end(); ++i )
1609 observer.vertex_push_back( *i );
1612 m_observers.insert( &observer );
1614 void detach( BrushObserver& observer ){
1615 m_observers.erase( &observer );
1618 void forEachFace( const BrushVisitor& visitor ) const {
1619 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1621 visitor.visit( *( *i ) );
1625 void forEachFace_instanceAttach( MapFile* map ) const {
1626 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1628 ( *i )->instanceAttach( map );
1631 void forEachFace_instanceDetach( MapFile* map ) const {
1632 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1634 ( *i )->instanceDetach( map );
1638 InstanceCounter m_instanceCounter;
1639 void instanceAttach( const scene::Path& path ){
1640 if ( ++m_instanceCounter.m_count == 1 ) {
1641 m_map = path_find_mapfile( path.begin(), path.end() );
1642 m_undoable_observer = GlobalUndoSystem().observer( this );
1643 GlobalFilterSystem().registerFilterable( *this );
1644 forEachFace_instanceAttach( m_map );
1648 ASSERT_MESSAGE( path_find_mapfile( path.begin(), path.end() ) == m_map, "node is instanced across more than one file" );
1651 void instanceDetach( const scene::Path& path ){
1652 if ( --m_instanceCounter.m_count == 0 ) {
1653 forEachFace_instanceDetach( m_map );
1654 GlobalFilterSystem().unregisterFilterable( *this );
1656 m_undoable_observer = 0;
1657 GlobalUndoSystem().release( this );
1662 const char* name() const {
1665 void attach( const NameCallback& callback ){
1667 void detach( const NameCallback& callback ){
1671 void updateFiltered(){
1672 if ( m_node != 0 ) {
1673 if ( brush_filtered( *this ) ) {
1674 m_node->enable( scene::Node::eFiltered );
1678 m_node->disable( scene::Node::eFiltered );
1684 void planeChanged(){
1685 m_planeChanged = true;
1689 void shaderChanged(){
1694 void evaluateBRep() const {
1695 if ( m_planeChanged ) {
1696 m_planeChanged = false;
1697 const_cast<Brush*>( this )->buildBRep();
1701 void transformChanged(){
1702 m_transformChanged = true;
1705 typedef MemberCaller<Brush, &Brush::transformChanged> TransformChangedCaller;
1707 void evaluateTransform(){
1708 if ( m_transformChanged ) {
1709 m_transformChanged = false;
1711 m_evaluateTransform();
1714 const Matrix4& localToParent() const {
1715 return g_matrix4_identity;
1720 const AABB& localAABB() const {
1722 return m_aabb_local;
1725 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
1726 return test.TestAABB( m_aabb_local, localToWorld );
1729 void renderComponents( SelectionSystem::EComponentMode mode, Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
1732 case SelectionSystem::eVertex:
1733 renderer.addRenderable( m_render_vertices, localToWorld );
1735 case SelectionSystem::eEdge:
1736 renderer.addRenderable( m_render_edges, localToWorld );
1738 case SelectionSystem::eFace:
1739 renderer.addRenderable( m_render_faces, localToWorld );
1746 void transform( const Matrix4& matrix ){
1747 bool mirror = matrix4_handedness( matrix ) == MATRIX4_LEFTHANDED;
1749 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1751 ( *i )->transform( matrix, mirror );
1754 void snapto( float snap ){
1755 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1757 ( *i )->snapto( snap );
1760 void revertTransform(){
1761 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1763 ( *i )->revertTransform();
1766 void freezeTransform(){
1767 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1769 ( *i )->freezeTransform();
1773 /// \brief Returns the absolute index of the \p faceVertex.
1774 std::size_t absoluteIndex( FaceVertexId faceVertex ){
1775 std::size_t index = 0;
1776 for ( std::size_t i = 0; i < faceVertex.getFace(); ++i )
1778 index += m_faces[i]->getWinding().numpoints;
1780 return index + faceVertex.getVertex();
1783 void appendFaces( const Faces& other ){
1785 for ( Faces::const_iterator i = other.begin(); i != other.end(); ++i )
1791 /// \brief The undo memento for a brush stores only the list of face references - the faces are not copied.
1792 class BrushUndoMemento : public UndoMemento
1795 BrushUndoMemento( const Faces& faces ) : m_faces( faces ){
1808 if ( m_undoable_observer != 0 ) {
1809 m_undoable_observer->save( this );
1813 UndoMemento* exportState() const {
1814 return new BrushUndoMemento( m_faces );
1817 void importState( const UndoMemento* state ){
1819 appendFaces( static_cast<const BrushUndoMemento*>( state )->m_faces );
1822 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1824 ( *i )->DEBUG_verify();
1829 return !m_faces.empty() && m_faces.front()->isDetail();
1832 /// \brief Appends a copy of \p face to the end of the face list.
1833 Face* addFace( const Face& face ){
1834 if ( m_faces.size() == c_brush_maxFaces ) {
1838 push_back( FaceSmartPointer( new Face( face, this ) ) );
1839 m_faces.back()->setDetail( isDetail() );
1841 return m_faces.back();
1844 /// \brief Appends a new face constructed from the parameters to the end of the face list.
1845 Face* addPlane( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection ){
1846 if ( m_faces.size() == c_brush_maxFaces ) {
1850 push_back( FaceSmartPointer( new Face( p0, p1, p2, shader, projection, this ) ) );
1851 m_faces.back()->setDetail( isDetail() );
1853 return m_faces.back();
1856 static void constructStatic( EBrushType type ){
1858 Face::m_type = type;
1859 FacePlane::m_type = type;
1861 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_QUAKE;
1862 if ( m_type == eBrushTypeQuake3BP || m_type == eBrushTypeDoom3 || m_type == eBrushTypeQuake4 ) {
1863 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_BRUSHPRIMITIVES;
1864 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
1866 else if ( m_type == eBrushTypeHalfLife ) {
1867 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_HALFLIFE;
1868 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
1871 Face::m_quantise = ( m_type == eBrushTypeQuake ) ? quantiseInteger : quantiseFloating;
1873 m_state_point = GlobalShaderCache().capture( "$POINT" );
1875 static void destroyStatic(){
1876 GlobalShaderCache().release( "$POINT" );
1879 std::size_t DEBUG_size(){
1880 return m_faces.size();
1883 typedef Faces::const_iterator const_iterator;
1885 const_iterator begin() const {
1886 return m_faces.begin();
1888 const_iterator end() const {
1889 return m_faces.end();
1893 return m_faces.back();
1895 const Face* back() const {
1896 return m_faces.back();
1898 void reserve( std::size_t count ){
1899 m_faces.reserve( count );
1900 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1902 ( *i )->reserve( count );
1905 void push_back( Faces::value_type face ){
1906 m_faces.push_back( face );
1907 if ( m_instanceCounter.m_count != 0 ) {
1908 m_faces.back()->instanceAttach( m_map );
1910 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1912 ( *i )->push_back( *face );
1913 ( *i )->DEBUG_verify();
1917 if ( m_instanceCounter.m_count != 0 ) {
1918 m_faces.back()->instanceDetach( m_map );
1921 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1924 ( *i )->DEBUG_verify();
1927 void erase( std::size_t index ){
1928 if ( m_instanceCounter.m_count != 0 ) {
1929 m_faces[index]->instanceDetach( m_map );
1931 m_faces.erase( m_faces.begin() + index );
1932 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1934 ( *i )->erase( index );
1935 ( *i )->DEBUG_verify();
1938 void connectivityChanged(){
1939 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1941 ( *i )->connectivityChanged();
1948 if ( m_instanceCounter.m_count != 0 ) {
1949 forEachFace_instanceDetach( m_map );
1952 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1955 ( *i )->DEBUG_verify();
1958 std::size_t size() const {
1959 return m_faces.size();
1961 bool empty() const {
1962 return m_faces.empty();
1965 /// \brief Returns true if any face of the brush contributes to the final B-Rep.
1966 bool hasContributingFaces() const {
1967 for ( const_iterator i = begin(); i != end(); ++i )
1969 if ( ( *i )->contributes() ) {
1976 /// \brief Removes faces that do not contribute to the brush. This is useful for cleaning up after CSG operations on the brush.
1977 /// 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.
1978 void removeEmptyFaces(){
1983 while ( i < m_faces.size() )
1985 if ( !m_faces[i]->contributes() ) {
1997 /// \brief Constructs \p winding from the intersection of \p plane with the other planes of the brush.
1998 void windingForClipPlane( Winding& winding, const Plane3& plane ) const {
1999 FixedWinding buffer[2];
2002 // get a poly that covers an effectively infinite area
2003 Winding_createInfinite( buffer[swap], plane, m_maxWorldCoord + 1 );
2005 // chop the poly by all of the other faces
2007 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2009 const Face& clip = *m_faces[i];
2011 if ( plane3_equal( clip.plane3(), plane )
2012 || !plane3_valid( clip.plane3() ) || !plane_unique( i )
2013 || plane3_opposing( plane, clip.plane3() ) ) {
2017 buffer[!swap].clear();
2019 #if BRUSH_CONNECTIVITY_DEBUG
2020 globalOutputStream() << "clip vs face: " << i << "\n";
2024 // flip the plane, because we want to keep the back side
2025 Plane3 clipPlane( vector3_negated( clip.plane3().normal() ), -clip.plane3().dist() );
2026 Winding_Clip( buffer[swap], plane, clipPlane, i, buffer[!swap] );
2029 #if BRUSH_CONNECTIVITY_DEBUG
2030 for ( FixedWinding::Points::iterator k = buffer[!swap].points.begin(), j = buffer[!swap].points.end() - 1; k != buffer[!swap].points.end(); j = k, ++k )
2032 if ( vector3_length_squared( vector3_subtracted( ( *k ).vertex, ( *j ).vertex ) ) < 1 ) {
2033 globalOutputStream() << "v: " << std::distance( buffer[!swap].points.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2038 //ASSERT_MESSAGE(buffer[!swap].numpoints != 1, "created single-point winding");
2044 Winding_forFixedWinding( winding, buffer[swap] );
2046 #if BRUSH_CONNECTIVITY_DEBUG
2047 Winding_printConnectivity( winding );
2049 for ( Winding::iterator i = winding.begin(), j = winding.end() - 1; i != winding.end(); j = i, ++i )
2051 if ( vector3_length_squared( vector3_subtracted( ( *i ).vertex, ( *j ).vertex ) ) < 1 ) {
2052 globalOutputStream() << "v: " << std::distance( winding.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2058 void update_wireframe( RenderableWireframe& wire, const bool* faces_visible ) const {
2059 wire.m_faceVertex.resize( m_edge_indices.size() );
2060 wire.m_vertices = m_uniqueVertexPoints.data();
2062 for ( std::size_t i = 0; i < m_edge_faces.size(); ++i )
2064 if ( faces_visible[m_edge_faces[i].first]
2065 || faces_visible[m_edge_faces[i].second] ) {
2066 wire.m_faceVertex[wire.m_size++] = m_edge_indices[i];
2072 void update_faces_wireframe( Array<PointVertex>& wire, const bool* faces_visible ) const {
2073 std::size_t count = 0;
2074 for ( std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i )
2076 if ( faces_visible[i] ) {
2081 wire.resize( count );
2082 Array<PointVertex>::iterator p = wire.begin();
2083 for ( std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i )
2085 if ( faces_visible[i] ) {
2086 *p++ = m_faceCentroidPoints[i];
2091 /// \brief Makes this brush a deep-copy of the \p other.
2092 void copy( const Brush& other ){
2093 for ( Faces::const_iterator i = other.m_faces.begin(); i != other.m_faces.end(); ++i )
2101 void edge_push_back( FaceVertexId faceVertex ){
2102 m_select_edges.push_back( SelectableEdge( m_faces, faceVertex ) );
2103 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2105 ( *i )->edge_push_back( m_select_edges.back() );
2109 m_select_edges.clear();
2110 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2112 ( *i )->edge_clear();
2115 void vertex_push_back( FaceVertexId faceVertex ){
2116 m_select_vertices.push_back( SelectableVertex( m_faces, faceVertex ) );
2117 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2119 ( *i )->vertex_push_back( m_select_vertices.back() );
2122 void vertex_clear(){
2123 m_select_vertices.clear();
2124 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2126 ( *i )->vertex_clear();
2130 /// \brief Returns true if the face identified by \p index is preceded by another plane that takes priority over it.
2131 bool plane_unique( std::size_t index ) const {
2133 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2135 if ( index != i && !plane3_inside( m_faces[index]->plane3(), m_faces[i]->plane3(), index < i ) ) {
2142 /// \brief Removes edges that are smaller than the tolerance used when generating brush windings.
2143 void removeDegenerateEdges(){
2144 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2146 Winding& winding = m_faces[i]->getWinding();
2147 for ( Winding::iterator j = winding.begin(); j != winding.end(); )
2149 std::size_t index = std::distance( winding.begin(), j );
2150 std::size_t next = Winding_next( winding, index );
2151 if ( Edge_isDegenerate( winding[index].vertex, winding[next].vertex ) ) {
2152 #if BRUSH_DEGENERATE_DEBUG
2153 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate edge adjacent to " << winding[index].adjacent << "\n";
2155 Winding& other = m_faces[winding[index].adjacent]->getWinding();
2156 std::size_t adjacent = Winding_FindAdjacent( other, i );
2157 if ( adjacent != c_brush_maxFaces ) {
2158 other.erase( other.begin() + adjacent );
2170 /// \brief Invalidates faces that have only two vertices in their winding, while preserving edge-connectivity information.
2171 void removeDegenerateFaces(){
2172 // save adjacency info for degenerate faces
2173 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2175 Winding& degen = m_faces[i]->getWinding();
2177 if ( degen.numpoints == 2 ) {
2178 #if BRUSH_DEGENERATE_DEBUG
2179 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate winding adjacent to " << degen[0].adjacent << ", " << degen[1].adjacent << "\n";
2181 // this is an "edge" face, where the plane touches the edge of the brush
2183 Winding& winding = m_faces[degen[0].adjacent]->getWinding();
2184 std::size_t index = Winding_FindAdjacent( winding, i );
2185 if ( index != c_brush_maxFaces ) {
2186 #if BRUSH_DEGENERATE_DEBUG
2187 globalOutputStream() << "Brush::buildWindings: face " << degen[0].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[1].adjacent << "\n";
2189 winding[index].adjacent = degen[1].adjacent;
2194 Winding& winding = m_faces[degen[1].adjacent]->getWinding();
2195 std::size_t index = Winding_FindAdjacent( winding, i );
2196 if ( index != c_brush_maxFaces ) {
2197 #if BRUSH_DEGENERATE_DEBUG
2198 globalOutputStream() << "Brush::buildWindings: face " << degen[1].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[0].adjacent << "\n";
2200 winding[index].adjacent = degen[0].adjacent;
2209 /// \brief Removes edges that have the same adjacent-face as their immediate neighbour.
2210 void removeDuplicateEdges(){
2211 // verify face connectivity graph
2212 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2214 //if(m_faces[i]->contributes())
2216 Winding& winding = m_faces[i]->getWinding();
2217 for ( std::size_t j = 0; j != winding.numpoints; )
2219 std::size_t next = Winding_next( winding, j );
2220 if ( winding[j].adjacent == winding[next].adjacent ) {
2221 #if BRUSH_DEGENERATE_DEBUG
2222 globalOutputStream() << "Brush::buildWindings: face " << i << ": removed duplicate edge adjacent to face " << winding[j].adjacent << "\n";
2224 winding.erase( winding.begin() + next );
2235 /// \brief Removes edges that do not have a matching pair in their adjacent-face.
2236 void verifyConnectivityGraph(){
2237 // verify face connectivity graph
2238 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2240 //if(m_faces[i]->contributes())
2242 Winding& winding = m_faces[i]->getWinding();
2243 for ( Winding::iterator j = winding.begin(); j != winding.end(); )
2245 #if BRUSH_CONNECTIVITY_DEBUG
2246 globalOutputStream() << "Brush::buildWindings: face " << i << ": adjacent to face " << ( *j ).adjacent << "\n";
2248 // remove unidirectional graph edges
2249 if ( ( *j ).adjacent == c_brush_maxFaces
2250 || Winding_FindAdjacent( m_faces[( *j ).adjacent]->getWinding(), i ) == c_brush_maxFaces ) {
2251 #if BRUSH_CONNECTIVITY_DEBUG
2252 globalOutputStream() << "Brush::buildWindings: face " << i << ": removing unidirectional connectivity graph edge adjacent to face " << ( *j ).adjacent << "\n";
2265 /// \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.
2267 for ( const_iterator i = begin(); i != end(); ++i )
2269 if ( !( *i )->is_bounded() ) {
2276 /// \brief Constructs the polygon windings for each face of the brush. Also updates the brush bounding-box and face texture-coordinates.
2277 bool buildWindings(){
2280 m_aabb_local = AABB();
2282 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2284 Face& f = *m_faces[i];
2286 if ( !plane3_valid( f.plane3() ) || !plane_unique( i ) ) {
2287 f.getWinding().resize( 0 );
2291 #if BRUSH_CONNECTIVITY_DEBUG
2292 globalOutputStream() << "face: " << i << "\n";
2294 windingForClipPlane( f.getWinding(), f.plane3() );
2296 // update brush bounds
2297 const Winding& winding = f.getWinding();
2298 for ( Winding::const_iterator i = winding.begin(); i != winding.end(); ++i )
2300 aabb_extend_by_point_safe( m_aabb_local, ( *i ).vertex );
2303 // update texture coordinates
2304 f.EmitTextureCoordinates();
2309 bool degenerate = !isBounded();
2311 if ( !degenerate ) {
2312 // clean up connectivity information.
2313 // these cleanups must be applied in a specific order.
2314 removeDegenerateEdges();
2315 removeDegenerateFaces();
2316 removeDuplicateEdges();
2317 verifyConnectivityGraph();
2323 /// \brief Constructs the face windings and updates anything that depends on them.
2331 class FaceInstanceSet
2333 typedef SelectionList<FaceInstance> FaceInstances;
2334 FaceInstances m_faceInstances;
2336 void insert( FaceInstance& faceInstance ){
2337 m_faceInstances.append( faceInstance );
2339 void erase( FaceInstance& faceInstance ){
2340 m_faceInstances.erase( faceInstance );
2343 template<typename Functor>
2344 void foreach( Functor functor ){
2345 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
2351 bool empty() const {
2352 return m_faceInstances.empty();
2354 FaceInstance& last() const {
2355 return m_faceInstances.back();
2359 extern FaceInstanceSet g_SelectedFaceInstances;
2361 typedef std::list<std::size_t> VertexSelection;
2363 inline VertexSelection::iterator VertexSelection_find( VertexSelection& self, std::size_t value ){
2364 return std::find( self.begin(), self.end(), value );
2367 inline VertexSelection::const_iterator VertexSelection_find( const VertexSelection& self, std::size_t value ){
2368 return std::find( self.begin(), self.end(), value );
2371 inline VertexSelection::iterator VertexSelection_insert( VertexSelection& self, std::size_t value ){
2372 VertexSelection::iterator i = VertexSelection_find( self, value );
2373 if ( i == self.end() ) {
2374 self.push_back( value );
2375 return --self.end();
2379 inline void VertexSelection_erase( VertexSelection& self, std::size_t value ){
2380 VertexSelection::iterator i = VertexSelection_find( self, value );
2381 if ( i != self.end() ) {
2386 inline bool triangle_reversed( std::size_t x, std::size_t y, std::size_t z ){
2387 return !( ( x < y && y < z ) || ( z < x && x < y ) || ( y < z && z < x ) );
2389 template<typename Element>
2390 inline Vector3 triangle_cross( const BasicVector3<Element>& x, const BasicVector3<Element> y, const BasicVector3<Element>& z ){
2391 return vector3_cross( y - x, z - x );
2393 template<typename Element>
2394 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 ){
2395 return vector3_dot( triangle_cross( x1, y1, z1 ), triangle_cross( x2, y2, z2 ) ) > 0;
2399 typedef const Plane3* PlanePointer;
2400 typedef PlanePointer* PlanesIterator;
2402 class VectorLightList : public LightList
2404 typedef std::vector<const RendererLight*> Lights;
2407 void addLight( const RendererLight& light ){
2408 m_lights.push_back( &light );
2413 void evaluateLights() const {
2415 void lightsChanged() const {
2417 void forEachLight( const RendererLightCallback& callback ) const {
2418 for ( Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i )
2420 callback( *( *i ) );
2428 ObservedSelectable m_selectable;
2429 ObservedSelectable m_selectableVertices;
2430 ObservedSelectable m_selectableEdges;
2431 SelectionChangeCallback m_selectionChanged;
2433 VertexSelection m_vertexSelection;
2434 VertexSelection m_edgeSelection;
2437 mutable VectorLightList m_lights;
2439 FaceInstance( Face& face, const SelectionChangeCallback& observer ) :
2441 m_selectable( SelectedChangedCaller( *this ) ),
2442 m_selectableVertices( observer ),
2443 m_selectableEdges( observer ),
2444 m_selectionChanged( observer ){
2446 FaceInstance( const FaceInstance& other ) :
2447 m_face( other.m_face ),
2448 m_selectable( SelectedChangedCaller( *this ) ),
2449 m_selectableVertices( other.m_selectableVertices ),
2450 m_selectableEdges( other.m_selectableEdges ),
2451 m_selectionChanged( other.m_selectionChanged ){
2453 FaceInstance& operator=( const FaceInstance& other ){
2454 m_face = other.m_face;
2461 const Face& getFace() const {
2465 void selectedChanged( const Selectable& selectable ){
2466 if ( selectable.isSelected() ) {
2467 g_SelectedFaceInstances.insert( *this );
2471 g_SelectedFaceInstances.erase( *this );
2473 m_selectionChanged( selectable );
2475 typedef MemberCaller1<FaceInstance, const Selectable&, &FaceInstance::selectedChanged> SelectedChangedCaller;
2477 bool selectedVertices() const {
2478 return !m_vertexSelection.empty();
2480 bool selectedEdges() const {
2481 return !m_edgeSelection.empty();
2483 bool isSelected() const {
2484 return m_selectable.isSelected();
2487 bool selectedComponents() const {
2488 return selectedVertices() || selectedEdges() || isSelected();
2490 bool selectedComponents( SelectionSystem::EComponentMode mode ) const {
2493 case SelectionSystem::eVertex:
2494 return selectedVertices();
2495 case SelectionSystem::eEdge:
2496 return selectedEdges();
2497 case SelectionSystem::eFace:
2498 return isSelected();
2503 void setSelected( SelectionSystem::EComponentMode mode, bool select ){
2506 case SelectionSystem::eFace:
2507 m_selectable.setSelected( select );
2509 case SelectionSystem::eVertex:
2510 ASSERT_MESSAGE( !select, "select-all not supported" );
2512 m_vertexSelection.clear();
2513 m_selectableVertices.setSelected( false );
2515 case SelectionSystem::eEdge:
2516 ASSERT_MESSAGE( !select, "select-all not supported" );
2518 m_edgeSelection.clear();
2519 m_selectableEdges.setSelected( false );
2526 template<typename Functor>
2527 void SelectedVertices_foreach( Functor functor ) const {
2528 for ( VertexSelection::const_iterator i = m_vertexSelection.begin(); i != m_vertexSelection.end(); ++i )
2530 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *i );
2531 if ( index != c_brush_maxFaces ) {
2532 functor( getFace().getWinding()[index].vertex );
2536 template<typename Functor>
2537 void SelectedEdges_foreach( Functor functor ) const {
2538 for ( VertexSelection::const_iterator i = m_edgeSelection.begin(); i != m_edgeSelection.end(); ++i )
2540 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *i );
2541 if ( index != c_brush_maxFaces ) {
2542 const Winding& winding = getFace().getWinding();
2543 std::size_t adjacent = Winding_next( winding, index );
2544 functor( vector3_mid( winding[index].vertex, winding[adjacent].vertex ) );
2548 template<typename Functor>
2549 void SelectedFaces_foreach( Functor functor ) const {
2550 if ( isSelected() ) {
2551 functor( centroid() );
2555 template<typename Functor>
2556 void SelectedComponents_foreach( Functor functor ) const {
2557 SelectedVertices_foreach( functor );
2558 SelectedEdges_foreach( functor );
2559 SelectedFaces_foreach( functor );
2562 void iterate_selected( AABB& aabb ) const {
2563 SelectedComponents_foreach( AABBExtendByPoint( aabb ) );
2566 class RenderablePointVectorPushBack
2568 RenderablePointVector& m_points;
2570 RenderablePointVectorPushBack( RenderablePointVector& points ) : m_points( points ){
2572 void operator()( const Vector3& point ) const {
2573 const Colour4b colour_selected( 0, 0, 255, 255 );
2574 m_points.push_back( pointvertex_for_windingpoint( point, colour_selected ) );
2578 void iterate_selected( RenderablePointVector& points ) const {
2579 SelectedComponents_foreach( RenderablePointVectorPushBack( points ) );
2582 bool intersectVolume( const VolumeTest& volume, const Matrix4& localToWorld ) const {
2583 return m_face->intersectVolume( volume, localToWorld );
2586 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
2587 if ( !m_face->isFiltered() && m_face->contributes() && intersectVolume( volume, localToWorld ) ) {
2588 renderer.PushState();
2589 if ( selectedComponents() ) {
2590 renderer.Highlight( Renderer::eFace );
2592 m_face->render( renderer, localToWorld );
2593 renderer.PopState();
2597 void testSelect( SelectionTest& test, SelectionIntersection& best ){
2598 if ( !m_face->isFiltered() ) {
2599 m_face->testSelect( test, best );
2602 void testSelect( Selector& selector, SelectionTest& test ){
2603 SelectionIntersection best;
2604 testSelect( test, best );
2605 if ( best.valid() ) {
2606 Selector_add( selector, m_selectable, best );
2609 void testSelect_centroid( Selector& selector, SelectionTest& test ){
2610 if ( m_face->contributes() && !m_face->isFiltered() ) {
2611 SelectionIntersection best;
2612 m_face->testSelect_centroid( test, best );
2613 if ( best.valid() ) {
2614 Selector_add( selector, m_selectable, best );
2619 void selectPlane( Selector& selector, const Line& line, PlanesIterator first, PlanesIterator last, const PlaneCallback& selectedPlaneCallback ){
2620 for ( Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i )
2622 Vector3 v( vector3_subtracted( line_closest_point( line, ( *i ).vertex ), ( *i ).vertex ) );
2623 double dot = vector3_dot( getFace().plane3().normal(), v );
2629 Selector_add( selector, m_selectable );
2631 selectedPlaneCallback( getFace().plane3() );
2633 void selectReversedPlane( Selector& selector, const SelectedPlanes& selectedPlanes ){
2634 if ( selectedPlanes.contains( plane3_flipped( getFace().plane3() ) ) ) {
2635 Selector_add( selector, m_selectable );
2639 void transformComponents( const Matrix4& matrix ){
2640 if ( isSelected() ) {
2641 m_face->transform( matrix, false );
2643 if ( selectedVertices() ) {
2644 if ( m_vertexSelection.size() == 1 ) {
2645 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2646 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2648 else if ( m_vertexSelection.size() == 2 ) {
2649 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2650 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2651 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2653 else if ( m_vertexSelection.size() >= 3 ) {
2654 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2655 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2656 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2657 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2660 if ( selectedEdges() ) {
2661 if ( m_edgeSelection.size() == 1 ) {
2662 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2663 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2664 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2666 else if ( m_edgeSelection.size() >= 2 ) {
2667 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2668 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2669 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2670 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2675 void snapto( float snap ){
2676 m_face->snapto( snap );
2679 void snapComponents( float snap ){
2680 if ( isSelected() ) {
2683 if ( selectedVertices() ) {
2684 vector3_snap( m_face->m_move_planepts[0], snap );
2685 vector3_snap( m_face->m_move_planepts[1], snap );
2686 vector3_snap( m_face->m_move_planepts[2], snap );
2687 m_face->assign_planepts( m_face->m_move_planepts );
2688 planepts_assign( m_face->m_move_planeptsTransformed, m_face->m_move_planepts );
2689 m_face->freezeTransform();
2691 if ( selectedEdges() ) {
2692 vector3_snap( m_face->m_move_planepts[0], snap );
2693 vector3_snap( m_face->m_move_planepts[1], snap );
2694 vector3_snap( m_face->m_move_planepts[2], snap );
2695 m_face->assign_planepts( m_face->m_move_planepts );
2696 planepts_assign( m_face->m_move_planeptsTransformed, m_face->m_move_planepts );
2697 m_face->freezeTransform();
2700 void update_move_planepts_vertex( std::size_t index ){
2701 m_face->update_move_planepts_vertex( index, m_face->m_move_planepts );
2703 void update_move_planepts_vertex2( std::size_t index, std::size_t other ){
2704 const std::size_t numpoints = m_face->getWinding().numpoints;
2705 ASSERT_MESSAGE( index < numpoints, "select_vertex: invalid index" );
2707 const std::size_t opposite = Winding_Opposite( m_face->getWinding(), index, other );
2709 if ( triangle_reversed( index, other, opposite ) ) {
2710 std::swap( index, other );
2714 triangles_same_winding(
2715 m_face->getWinding()[opposite].vertex,
2716 m_face->getWinding()[index].vertex,
2717 m_face->getWinding()[other].vertex,
2718 m_face->getWinding()[0].vertex,
2719 m_face->getWinding()[1].vertex,
2720 m_face->getWinding()[2].vertex
2722 "update_move_planepts_vertex2: error"
2725 m_face->m_move_planepts[0] = m_face->getWinding()[opposite].vertex;
2726 m_face->m_move_planepts[1] = m_face->getWinding()[index].vertex;
2727 m_face->m_move_planepts[2] = m_face->getWinding()[other].vertex;
2728 planepts_quantise( m_face->m_move_planepts, GRID_MIN ); // winding points are very inaccurate
2730 void update_selection_vertex(){
2731 if ( m_vertexSelection.size() == 0 ) {
2732 m_selectableVertices.setSelected( false );
2736 m_selectableVertices.setSelected( true );
2738 if ( m_vertexSelection.size() == 1 ) {
2739 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_vertexSelection.begin() );
2741 if ( index != c_brush_maxFaces ) {
2742 update_move_planepts_vertex( index );
2745 else if ( m_vertexSelection.size() == 2 ) {
2746 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_vertexSelection.begin() );
2747 std::size_t other = Winding_FindAdjacent( getFace().getWinding(), *( ++m_vertexSelection.begin() ) );
2749 if ( index != c_brush_maxFaces
2750 && other != c_brush_maxFaces ) {
2751 update_move_planepts_vertex2( index, other );
2756 void select_vertex( std::size_t index, bool select ){
2758 VertexSelection_insert( m_vertexSelection, getFace().getWinding()[index].adjacent );
2762 VertexSelection_erase( m_vertexSelection, getFace().getWinding()[index].adjacent );
2765 SceneChangeNotify();
2766 update_selection_vertex();
2769 bool selected_vertex( std::size_t index ) const {
2770 return VertexSelection_find( m_vertexSelection, getFace().getWinding()[index].adjacent ) != m_vertexSelection.end();
2773 void update_move_planepts_edge( std::size_t index ){
2774 std::size_t numpoints = m_face->getWinding().numpoints;
2775 ASSERT_MESSAGE( index < numpoints, "select_edge: invalid index" );
2777 std::size_t adjacent = Winding_next( m_face->getWinding(), index );
2778 std::size_t opposite = Winding_Opposite( m_face->getWinding(), index );
2779 m_face->m_move_planepts[0] = m_face->getWinding()[index].vertex;
2780 m_face->m_move_planepts[1] = m_face->getWinding()[adjacent].vertex;
2781 m_face->m_move_planepts[2] = m_face->getWinding()[opposite].vertex;
2782 planepts_quantise( m_face->m_move_planepts, GRID_MIN ); // winding points are very inaccurate
2784 void update_selection_edge(){
2785 if ( m_edgeSelection.size() == 0 ) {
2786 m_selectableEdges.setSelected( false );
2790 m_selectableEdges.setSelected( true );
2792 if ( m_edgeSelection.size() == 1 ) {
2793 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_edgeSelection.begin() );
2795 if ( index != c_brush_maxFaces ) {
2796 update_move_planepts_edge( index );
2801 void select_edge( std::size_t index, bool select ){
2803 VertexSelection_insert( m_edgeSelection, getFace().getWinding()[index].adjacent );
2807 VertexSelection_erase( m_edgeSelection, getFace().getWinding()[index].adjacent );
2810 SceneChangeNotify();
2811 update_selection_edge();
2814 bool selected_edge( std::size_t index ) const {
2815 return VertexSelection_find( m_edgeSelection, getFace().getWinding()[index].adjacent ) != m_edgeSelection.end();
2818 const Vector3& centroid() const {
2819 return m_face->centroid();
2822 void connectivityChanged(){
2823 // This occurs when a face is added or removed.
2824 // The current vertex and edge selections no longer valid and must be cleared.
2825 m_vertexSelection.clear();
2826 m_selectableVertices.setSelected( false );
2827 m_edgeSelection.clear();
2828 m_selectableEdges.setSelected( false );
2832 class BrushClipPlane : public OpenGLRenderable
2836 static Shader* m_state;
2838 static void constructStatic(){
2839 m_state = GlobalShaderCache().capture( "$CLIPPER_OVERLAY" );
2841 static void destroyStatic(){
2842 GlobalShaderCache().release( "$CLIPPER_OVERLAY" );
2845 void setPlane( const Brush& brush, const Plane3& plane ){
2847 if ( plane3_valid( m_plane ) ) {
2848 brush.windingForClipPlane( m_winding, m_plane );
2852 m_winding.resize( 0 );
2856 void render( RenderStateFlags state ) const {
2857 if ( ( state & RENDER_FILL ) != 0 ) {
2858 Winding_Draw( m_winding, m_plane.normal(), state );
2862 Winding_DrawWireframe( m_winding );
2864 // also draw a line indicating the direction of the cut
2865 Vector3 lineverts[2];
2866 Winding_Centroid( m_winding, m_plane, lineverts[0] );
2867 lineverts[1] = vector3_added( lineverts[0], vector3_scaled( m_plane.normal(), Brush::m_maxWorldCoord * 4 ) );
2869 glVertexPointer( 3, GL_FLOAT, sizeof( Vector3 ), &lineverts[0] );
2870 glDrawArrays( GL_LINES, 0, GLsizei( 2 ) );
2874 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
2875 renderer.SetState( m_state, Renderer::eWireframeOnly );
2876 renderer.SetState( m_state, Renderer::eFullMaterials );
2877 renderer.addRenderable( *this, localToWorld );
2881 inline void Face_addLight( const FaceInstance& face, const Matrix4& localToWorld, const RendererLight& light ){
2882 const Plane3& facePlane = face.getFace().plane3();
2883 const Vector3& origin = light.aabb().origin;
2884 Plane3 tmp( plane3_transformed( Plane3( facePlane.normal(), -facePlane.dist() ), localToWorld ) );
2885 if ( !plane3_test_point( tmp, origin )
2886 || !plane3_test_point( tmp, vector3_added( origin, light.offset() ) ) ) {
2887 face.m_lights.addLight( light );
2893 typedef std::vector<FaceInstance> FaceInstances;
2895 class EdgeInstance : public Selectable
2897 FaceInstances& m_faceInstances;
2898 SelectableEdge* m_edge;
2900 void select_edge( bool select ){
2901 FaceVertexId faceVertex = m_edge->m_faceVertex;
2902 m_faceInstances[faceVertex.getFace()].select_edge( faceVertex.getVertex(), select );
2903 faceVertex = next_edge( m_edge->m_faces, faceVertex );
2904 m_faceInstances[faceVertex.getFace()].select_edge( faceVertex.getVertex(), select );
2906 bool selected_edge() const {
2907 FaceVertexId faceVertex = m_edge->m_faceVertex;
2908 if ( !m_faceInstances[faceVertex.getFace()].selected_edge( faceVertex.getVertex() ) ) {
2911 faceVertex = next_edge( m_edge->m_faces, faceVertex );
2912 if ( !m_faceInstances[faceVertex.getFace()].selected_edge( faceVertex.getVertex() ) ) {
2920 EdgeInstance( FaceInstances& faceInstances, SelectableEdge& edge )
2921 : m_faceInstances( faceInstances ), m_edge( &edge ){
2923 EdgeInstance& operator=( const EdgeInstance& other ){
2924 m_edge = other.m_edge;
2928 void setSelected( bool select ){
2929 select_edge( select );
2931 bool isSelected() const {
2932 return selected_edge();
2936 void testSelect( Selector& selector, SelectionTest& test ){
2937 SelectionIntersection best;
2938 m_edge->testSelect( test, best );
2939 if ( best.valid() ) {
2940 Selector_add( selector, *this, best );
2945 class VertexInstance : public Selectable
2947 FaceInstances& m_faceInstances;
2948 SelectableVertex* m_vertex;
2950 void select_vertex( bool select ){
2951 FaceVertexId faceVertex = m_vertex->m_faceVertex;
2954 m_faceInstances[faceVertex.getFace()].select_vertex( faceVertex.getVertex(), select );
2955 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
2957 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
2959 bool selected_vertex() const {
2960 FaceVertexId faceVertex = m_vertex->m_faceVertex;
2963 if ( !m_faceInstances[faceVertex.getFace()].selected_vertex( faceVertex.getVertex() ) ) {
2966 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
2968 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
2973 VertexInstance( FaceInstances& faceInstances, SelectableVertex& vertex )
2974 : m_faceInstances( faceInstances ), m_vertex( &vertex ){
2976 VertexInstance& operator=( const VertexInstance& other ){
2977 m_vertex = other.m_vertex;
2981 void setSelected( bool select ){
2982 select_vertex( select );
2984 bool isSelected() const {
2985 return selected_vertex();
2988 void testSelect( Selector& selector, SelectionTest& test ){
2989 SelectionIntersection best;
2990 m_vertex->testSelect( test, best );
2991 if ( best.valid() ) {
2992 Selector_add( selector, *this, best );
2997 class BrushInstanceVisitor
3000 virtual void visit( FaceInstance& face ) const = 0;
3003 class BrushInstance :
3004 public BrushObserver,
3005 public scene::Instance,
3008 public SelectionTestable,
3009 public ComponentSelectionTestable,
3010 public ComponentEditable,
3011 public ComponentSnappable,
3012 public PlaneSelectable,
3013 public LightCullable
3017 InstanceTypeCastTable m_casts;
3020 InstanceStaticCast<BrushInstance, Selectable>::install( m_casts );
3021 InstanceContainedCast<BrushInstance, Bounded>::install( m_casts );
3022 InstanceContainedCast<BrushInstance, Cullable>::install( m_casts );
3023 InstanceStaticCast<BrushInstance, Renderable>::install( m_casts );
3024 InstanceStaticCast<BrushInstance, SelectionTestable>::install( m_casts );
3025 InstanceStaticCast<BrushInstance, ComponentSelectionTestable>::install( m_casts );
3026 InstanceStaticCast<BrushInstance, ComponentEditable>::install( m_casts );
3027 InstanceStaticCast<BrushInstance, ComponentSnappable>::install( m_casts );
3028 InstanceStaticCast<BrushInstance, PlaneSelectable>::install( m_casts );
3029 InstanceIdentityCast<BrushInstance>::install( m_casts );
3030 InstanceContainedCast<BrushInstance, Transformable>::install( m_casts );
3032 InstanceTypeCastTable& get(){
3040 FaceInstances m_faceInstances;
3042 typedef std::vector<EdgeInstance> EdgeInstances;
3043 EdgeInstances m_edgeInstances;
3044 typedef std::vector<VertexInstance> VertexInstances;
3045 VertexInstances m_vertexInstances;
3047 ObservedSelectable m_selectable;
3049 mutable RenderableWireframe m_render_wireframe;
3050 mutable RenderablePointVector m_render_selected;
3051 mutable AABB m_aabb_component;
3052 mutable Array<PointVertex> m_faceCentroidPointsCulled;
3053 RenderablePointArray m_render_faces_wireframe;
3054 mutable bool m_viewChanged; // requires re-evaluation of view-dependent cached data
3056 BrushClipPlane m_clipPlane;
3058 static Shader* m_state_selpoint;
3060 const LightList* m_lightList;
3062 TransformModifier m_transform;
3064 BrushInstance( const BrushInstance& other ); // NOT COPYABLE
3065 BrushInstance& operator=( const BrushInstance& other ); // NOT ASSIGNABLE
3067 static Counter* m_counter;
3069 typedef LazyStatic<TypeCasts> StaticTypeCasts;
3071 void lightsChanged(){
3072 m_lightList->lightsChanged();
3074 typedef MemberCaller<BrushInstance, &BrushInstance::lightsChanged> LightsChangedCaller;
3076 STRING_CONSTANT( Name, "BrushInstance" );
3078 BrushInstance( const scene::Path& path, scene::Instance* parent, Brush& brush ) :
3079 Instance( path, parent, this, StaticTypeCasts::instance().get() ),
3081 m_selectable( SelectedChangedCaller( *this ) ),
3082 m_render_selected( GL_POINTS ),
3083 m_render_faces_wireframe( m_faceCentroidPointsCulled, GL_POINTS ),
3084 m_viewChanged( false ),
3085 m_transform( Brush::TransformChangedCaller( m_brush ), ApplyTransformCaller( *this ) ){
3086 m_brush.instanceAttach( Instance::path() );
3087 m_brush.attach( *this );
3088 m_counter->increment();
3090 m_lightList = &GlobalShaderCache().attach( *this );
3091 m_brush.m_lightsChanged = LightsChangedCaller( *this ); ///\todo Make this work with instancing.
3093 Instance::setTransformChangedCallback( LightsChangedCaller( *this ) );
3096 Instance::setTransformChangedCallback( Callback() );
3098 m_brush.m_lightsChanged = Callback();
3099 GlobalShaderCache().detach( *this );
3101 m_counter->decrement();
3102 m_brush.detach( *this );
3103 m_brush.instanceDetach( Instance::path() );
3109 const Brush& getBrush() const {
3113 Bounded& get( NullType<Bounded>){
3116 Cullable& get( NullType<Cullable>){
3119 Transformable& get( NullType<Transformable>){
3123 void selectedChanged( const Selectable& selectable ){
3124 GlobalSelectionSystem().getObserver ( SelectionSystem::ePrimitive )( selectable );
3125 GlobalSelectionSystem().onSelectedChanged( *this, selectable );
3127 Instance::selectedChanged();
3129 typedef MemberCaller1<BrushInstance, const Selectable&, &BrushInstance::selectedChanged> SelectedChangedCaller;
3131 void selectedChangedComponent( const Selectable& selectable ){
3132 GlobalSelectionSystem().getObserver ( SelectionSystem::eComponent )( selectable );
3133 GlobalSelectionSystem().onComponentSelection( *this, selectable );
3135 typedef MemberCaller1<BrushInstance, const Selectable&, &BrushInstance::selectedChangedComponent> SelectedChangedComponentCaller;
3137 const BrushInstanceVisitor& forEachFaceInstance( const BrushInstanceVisitor& visitor ){
3138 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3140 visitor.visit( *i );
3145 static void constructStatic(){
3146 m_state_selpoint = GlobalShaderCache().capture( "$SELPOINT" );
3148 static void destroyStatic(){
3149 GlobalShaderCache().release( "$SELPOINT" );
3153 m_faceInstances.clear();
3155 void reserve( std::size_t size ){
3156 m_faceInstances.reserve( size );
3159 void push_back( Face& face ){
3160 m_faceInstances.push_back( FaceInstance( face, SelectedChangedComponentCaller( *this ) ) );
3163 ASSERT_MESSAGE( !m_faceInstances.empty(), "erasing invalid element" );
3164 m_faceInstances.pop_back();
3166 void erase( std::size_t index ){
3167 ASSERT_MESSAGE( index < m_faceInstances.size(), "erasing invalid element" );
3168 m_faceInstances.erase( m_faceInstances.begin() + index );
3170 void connectivityChanged(){
3171 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3173 ( *i ).connectivityChanged();
3178 m_edgeInstances.clear();
3180 void edge_push_back( SelectableEdge& edge ){
3181 m_edgeInstances.push_back( EdgeInstance( m_faceInstances, edge ) );
3184 void vertex_clear(){
3185 m_vertexInstances.clear();
3187 void vertex_push_back( SelectableVertex& vertex ){
3188 m_vertexInstances.push_back( VertexInstance( m_faceInstances, vertex ) );
3191 void DEBUG_verify() const {
3192 ASSERT_MESSAGE( m_faceInstances.size() == m_brush.DEBUG_size(), "FATAL: mismatch" );
3195 bool isSelected() const {
3196 return m_selectable.isSelected();
3198 void setSelected( bool select ){
3199 m_selectable.setSelected( select );
3202 void update_selected() const {
3203 m_render_selected.clear();
3204 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3206 if ( ( *i ).getFace().contributes() ) {
3207 ( *i ).iterate_selected( m_render_selected );
3212 void evaluateViewDependent( const VolumeTest& volume, const Matrix4& localToWorld ) const {
3213 if ( m_viewChanged ) {
3214 m_viewChanged = false;
3216 bool faces_visible[c_brush_maxFaces];
3218 bool* j = faces_visible;
3219 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i, ++j )
3221 *j = ( *i ).intersectVolume( volume, localToWorld );
3225 m_brush.update_wireframe( m_render_wireframe, faces_visible );
3226 m_brush.update_faces_wireframe( m_faceCentroidPointsCulled, faces_visible );
3230 void renderComponentsSelected( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3231 m_brush.evaluateBRep();
3234 if ( !m_render_selected.empty() ) {
3235 renderer.Highlight( Renderer::ePrimitive, false );
3236 renderer.SetState( m_state_selpoint, Renderer::eWireframeOnly );
3237 renderer.SetState( m_state_selpoint, Renderer::eFullMaterials );
3238 renderer.addRenderable( m_render_selected, localToWorld );
3242 void renderComponents( Renderer& renderer, const VolumeTest& volume ) const {
3243 m_brush.evaluateBRep();
3245 const Matrix4& localToWorld = Instance::localToWorld();
3247 renderer.SetState( m_brush.m_state_point, Renderer::eWireframeOnly );
3248 renderer.SetState( m_brush.m_state_point, Renderer::eFullMaterials );
3250 if ( volume.fill() && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace ) {
3251 evaluateViewDependent( volume, localToWorld );
3252 renderer.addRenderable( m_render_faces_wireframe, localToWorld );
3256 m_brush.renderComponents( GlobalSelectionSystem().ComponentMode(), renderer, volume, localToWorld );
3260 void renderClipPlane( Renderer& renderer, const VolumeTest& volume ) const {
3261 if ( GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip && isSelected() ) {
3262 m_clipPlane.render( renderer, volume, localToWorld() );
3266 void renderCommon( Renderer& renderer, const VolumeTest& volume ) const {
3267 bool componentMode = GlobalSelectionSystem().Mode() == SelectionSystem::eComponent;
3269 if ( componentMode && isSelected() ) {
3270 renderComponents( renderer, volume );
3273 if ( parentSelected() ) {
3274 if ( !componentMode ) {
3275 renderer.Highlight( Renderer::eFace );
3277 renderer.Highlight( Renderer::ePrimitive );
3281 void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3282 //renderCommon(renderer, volume);
3284 m_lightList->evaluateLights();
3286 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3288 renderer.setLights( ( *i ).m_lights );
3289 ( *i ).render( renderer, volume, localToWorld );
3292 renderComponentsSelected( renderer, volume, localToWorld );
3295 void renderWireframe( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3296 //renderCommon(renderer, volume);
3298 evaluateViewDependent( volume, localToWorld );
3300 if ( m_render_wireframe.m_size != 0 ) {
3301 renderer.addRenderable( m_render_wireframe, localToWorld );
3304 renderComponentsSelected( renderer, volume, localToWorld );
3307 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3308 m_brush.evaluateBRep();
3310 renderClipPlane( renderer, volume );
3312 renderSolid( renderer, volume, localToWorld() );
3315 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3316 m_brush.evaluateBRep();
3318 renderClipPlane( renderer, volume );
3320 renderWireframe( renderer, volume, localToWorld() );
3323 void viewChanged() const {
3324 m_viewChanged = true;
3327 void testSelect( Selector& selector, SelectionTest& test ){
3328 test.BeginMesh( localToWorld() );
3330 SelectionIntersection best;
3331 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3333 ( *i ).testSelect( test, best );
3335 if ( best.valid() ) {
3336 selector.addIntersection( best );
3340 bool isSelectedComponents() const {
3341 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3343 if ( ( *i ).selectedComponents() ) {
3349 void setSelectedComponents( bool select, SelectionSystem::EComponentMode mode ){
3350 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3352 ( *i ).setSelected( mode, select );
3355 void testSelectComponents( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode ){
3356 test.BeginMesh( localToWorld() );
3360 case SelectionSystem::eVertex:
3362 for ( VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i )
3364 ( *i ).testSelect( selector, test );
3368 case SelectionSystem::eEdge:
3370 for ( EdgeInstances::iterator i = m_edgeInstances.begin(); i != m_edgeInstances.end(); ++i )
3372 ( *i ).testSelect( selector, test );
3376 case SelectionSystem::eFace:
3378 if ( test.getVolume().fill() ) {
3379 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3381 ( *i ).testSelect( selector, test );
3386 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3388 ( *i ).testSelect_centroid( selector, test );
3398 void selectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
3399 test.BeginMesh( localToWorld() );
3401 PlanePointer brushPlanes[c_brush_maxFaces];
3402 PlanesIterator j = brushPlanes;
3404 for ( Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i )
3406 *j++ = &( *i )->plane3();
3409 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3411 ( *i ).selectPlane( selector, Line( test.getNear(), test.getFar() ), brushPlanes, j, selectedPlaneCallback );
3414 void selectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes ){
3415 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3417 ( *i ).selectReversedPlane( selector, selectedPlanes );
3422 void transformComponents( const Matrix4& matrix ){
3423 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3425 ( *i ).transformComponents( matrix );
3428 const AABB& getSelectedComponentsBounds() const {
3429 m_aabb_component = AABB();
3431 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3433 ( *i ).iterate_selected( m_aabb_component );
3436 return m_aabb_component;
3439 void snapComponents( float snap ){
3440 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3442 ( *i ).snapComponents( snap );
3445 void evaluateTransform(){
3446 Matrix4 matrix( m_transform.calculateTransform() );
3447 //globalOutputStream() << "matrix: " << matrix << "\n";
3449 if ( m_transform.getType() == TRANSFORM_PRIMITIVE ) {
3450 m_brush.transform( matrix );
3454 transformComponents( matrix );
3457 void applyTransform(){
3458 m_brush.revertTransform();
3459 evaluateTransform();
3460 m_brush.freezeTransform();
3462 typedef MemberCaller<BrushInstance, &BrushInstance::applyTransform> ApplyTransformCaller;
3464 void setClipPlane( const Plane3& plane ){
3465 m_clipPlane.setPlane( m_brush, plane );
3468 bool testLight( const RendererLight& light ) const {
3469 return light.testAABB( worldAABB() );
3471 void insertLight( const RendererLight& light ){
3472 const Matrix4& localToWorld = Instance::localToWorld();
3473 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3475 Face_addLight( *i, localToWorld, light );
3479 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3481 ( *i ).m_lights.clear();
3486 inline BrushInstance* Instance_getBrush( scene::Instance& instance ){
3487 return InstanceTypeCast<BrushInstance>::cast( instance );
3491 template<typename Functor>
3492 class BrushSelectedVisitor : public SelectionSystem::Visitor
3494 const Functor& m_functor;
3496 BrushSelectedVisitor( const Functor& functor ) : m_functor( functor ){
3498 void visit( scene::Instance& instance ) const {
3499 BrushInstance* brush = Instance_getBrush( instance );
3501 m_functor( *brush );
3506 template<typename Functor>
3507 inline const Functor& Scene_forEachSelectedBrush( const Functor& functor ){
3508 GlobalSelectionSystem().foreachSelected( BrushSelectedVisitor<Functor>( functor ) );
3512 template<typename Functor>
3513 class BrushVisibleSelectedVisitor : public SelectionSystem::Visitor
3515 const Functor& m_functor;
3517 BrushVisibleSelectedVisitor( const Functor& functor ) : m_functor( functor ){
3519 void visit( scene::Instance& instance ) const {
3520 BrushInstance* brush = Instance_getBrush( instance );
3522 && instance.path().top().get().visible() ) {
3523 m_functor( *brush );
3528 template<typename Functor>
3529 inline const Functor& Scene_forEachVisibleSelectedBrush( const Functor& functor ){
3530 GlobalSelectionSystem().foreachSelected( BrushVisibleSelectedVisitor<Functor>( functor ) );
3534 class BrushForEachFace
3536 const BrushInstanceVisitor& m_visitor;
3538 BrushForEachFace( const BrushInstanceVisitor& visitor ) : m_visitor( visitor ){
3540 void operator()( BrushInstance& brush ) const {
3541 brush.forEachFaceInstance( m_visitor );
3545 template<class Functor>
3546 class FaceInstanceVisitFace : public BrushInstanceVisitor
3548 const Functor& functor;
3550 FaceInstanceVisitFace( const Functor& functor )
3551 : functor( functor ){
3553 void visit( FaceInstance& face ) const {
3554 functor( face.getFace() );
3558 template<typename Functor>
3559 inline const Functor& Brush_forEachFace( BrushInstance& brush, const Functor& functor ){
3560 brush.forEachFaceInstance( FaceInstanceVisitFace<Functor>( functor ) );
3564 template<class Functor>
3565 class FaceVisitAll : public BrushVisitor
3567 const Functor& functor;
3569 FaceVisitAll( const Functor& functor )
3570 : functor( functor ){
3572 void visit( Face& face ) const {
3577 template<typename Functor>
3578 inline const Functor& Brush_forEachFace( const Brush& brush, const Functor& functor ){
3579 brush.forEachFace( FaceVisitAll<Functor>( functor ) );
3583 template<typename Functor>
3584 inline const Functor& Brush_forEachFace( Brush& brush, const Functor& functor ){
3585 brush.forEachFace( FaceVisitAll<Functor>( functor ) );
3589 template<class Functor>
3590 class FaceInstanceVisitAll : public BrushInstanceVisitor
3592 const Functor& functor;
3594 FaceInstanceVisitAll( const Functor& functor )
3595 : functor( functor ){
3597 void visit( FaceInstance& face ) const {
3602 template<typename Functor>
3603 inline const Functor& Brush_ForEachFaceInstance( BrushInstance& brush, const Functor& functor ){
3604 brush.forEachFaceInstance( FaceInstanceVisitAll<Functor>( functor ) );
3608 template<typename Functor>
3609 inline const Functor& Scene_forEachBrush( scene::Graph& graph, const Functor& functor ){
3610 graph.traverse( InstanceWalker< InstanceApply<BrushInstance, Functor> >( functor ) );
3614 template<typename Type, typename Functor>
3615 class InstanceIfVisible : public Functor
3618 InstanceIfVisible( const Functor& functor ) : Functor( functor ){
3620 void operator()( scene::Instance& instance ){
3621 if ( instance.path().top().get().visible() ) {
3622 Functor::operator()( instance );
3627 template<typename Functor>
3628 class BrushVisibleWalker : public scene::Graph::Walker
3630 const Functor& m_functor;
3632 BrushVisibleWalker( const Functor& functor ) : m_functor( functor ){
3634 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3635 if ( path.top().get().visible() ) {
3636 BrushInstance* brush = Instance_getBrush( instance );
3638 m_functor( *brush );
3645 template<typename Functor>
3646 inline const Functor& Scene_forEachVisibleBrush( scene::Graph& graph, const Functor& functor ){
3647 graph.traverse( BrushVisibleWalker<Functor>( functor ) );
3651 template<typename Functor>
3652 inline const Functor& Scene_ForEachBrush_ForEachFace( scene::Graph& graph, const Functor& functor ){
3653 Scene_forEachBrush( graph, BrushForEachFace( FaceInstanceVisitFace<Functor>( functor ) ) );
3658 template<typename Functor>
3659 inline const Functor& Scene_ForEachBrush_ForEachFaceInstance( scene::Graph& graph, const Functor& functor ){
3660 Scene_forEachBrush( graph, BrushForEachFace( FaceInstanceVisitAll<Functor>( functor ) ) );
3664 template<typename Functor>
3665 inline const Functor& Scene_ForEachSelectedBrush_ForEachFace( scene::Graph& graph, const Functor& functor ){
3666 Scene_forEachSelectedBrush( BrushForEachFace( FaceInstanceVisitFace<Functor>( functor ) ) );
3670 template<typename Functor>
3671 inline const Functor& Scene_ForEachSelectedBrush_ForEachFaceInstance( scene::Graph& graph, const Functor& functor ){
3672 Scene_forEachSelectedBrush( BrushForEachFace( FaceInstanceVisitAll<Functor>( functor ) ) );
3676 template<typename Functor>
3677 class FaceVisitorWrapper
3679 const Functor& functor;
3681 FaceVisitorWrapper( const Functor& functor ) : functor( functor ){
3684 void operator()( FaceInstance& faceInstance ) const {
3685 functor( faceInstance.getFace() );
3689 template<typename Functor>
3690 inline const Functor& Scene_ForEachSelectedBrushFace( scene::Graph& graph, const Functor& functor ){
3691 g_SelectedFaceInstances.foreach( FaceVisitorWrapper<Functor>( functor ) );