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 typedef ReferencePair<FaceShaderObserver> FaceShaderObserverPair;
317 class ContentsFlagsValue
320 ContentsFlagsValue(){
322 ContentsFlagsValue( int surfaceFlags, int contentFlags, int value, bool specified ) :
323 m_surfaceFlags( surfaceFlags ),
324 m_contentFlags( contentFlags ),
326 m_specified( specified ){
334 inline void ContentsFlagsValue_assignMasked( ContentsFlagsValue& flags, const ContentsFlagsValue& other ){
335 bool detail = bitfield_enabled( flags.m_contentFlags, BRUSH_DETAIL_MASK );
338 flags.m_contentFlags = bitfield_enable( flags.m_contentFlags, BRUSH_DETAIL_MASK );
342 flags.m_contentFlags = bitfield_disable( flags.m_contentFlags, BRUSH_DETAIL_MASK );
347 class FaceShader : public ModuleObserver
353 CopiedString m_shader;
354 ContentsFlagsValue m_flags;
356 SavedState( const FaceShader& faceShader ){
357 m_shader = faceShader.getShader();
358 m_flags = faceShader.m_flags;
361 void exportState( FaceShader& faceShader ) const {
362 faceShader.setShader( m_shader.c_str() );
363 faceShader.setFlags( m_flags );
367 CopiedString m_shader;
369 ContentsFlagsValue m_flags;
370 FaceShaderObserverPair m_observers;
374 FaceShader( const char* shader, const ContentsFlagsValue& flags = ContentsFlagsValue( 0, 0, 0, false ) ) :
378 m_instanced( false ),
385 // copy-construction not supported
386 FaceShader( const FaceShader& other );
388 void instanceAttach(){
390 m_state->incrementUsed();
392 void instanceDetach(){
393 m_state->decrementUsed();
397 void captureShader(){
398 ASSERT_MESSAGE( m_state == 0, "shader cannot be captured" );
399 brush_check_shader( m_shader.c_str() );
400 m_state = GlobalShaderCache().capture( m_shader.c_str() );
401 m_state->attach( *this );
403 void releaseShader(){
404 ASSERT_MESSAGE( m_state != 0, "shader cannot be released" );
405 m_state->detach( *this );
406 GlobalShaderCache().release( m_shader.c_str() );
411 ASSERT_MESSAGE( !m_realised, "FaceTexdef::realise: already realised" );
413 m_observers.forEach([](FaceShaderObserver &observer) {
414 observer.realiseShader();
418 ASSERT_MESSAGE( m_realised, "FaceTexdef::unrealise: already unrealised" );
419 m_observers.forEach([](FaceShaderObserver &observer) {
420 observer.unrealiseShader();
425 void attach( FaceShaderObserver& observer ){
426 m_observers.attach( observer );
428 observer.realiseShader();
432 void detach( FaceShaderObserver& observer ){
434 observer.unrealiseShader();
436 m_observers.detach( observer );
439 const char* getShader() const {
440 return m_shader.c_str();
442 void setShader( const char* name ){
444 m_state->decrementUsed();
450 m_state->incrementUsed();
453 ContentsFlagsValue getFlags() const {
454 ASSERT_MESSAGE( m_realised, "FaceShader::getFlags: flags not valid when unrealised" );
455 if ( !m_flags.m_specified ) {
456 return ContentsFlagsValue(
457 m_state->getTexture().surfaceFlags,
458 m_state->getTexture().contentFlags,
459 m_state->getTexture().value,
465 void setFlags( const ContentsFlagsValue& flags ){
466 ASSERT_MESSAGE( m_realised, "FaceShader::setFlags: flags not valid when unrealised" );
467 ContentsFlagsValue_assignMasked( m_flags, flags );
470 Shader* state() const {
474 std::size_t width() const {
476 return m_state->getTexture().width;
480 std::size_t height() const {
482 return m_state->getTexture().height;
486 unsigned int shaderFlags() const {
488 return m_state->getFlags();
497 class FaceTexdef : public FaceShaderObserver
500 FaceTexdef( const FaceTexdef& other );
502 FaceTexdef& operator=( const FaceTexdef& other );
507 TextureProjection m_projection;
509 SavedState( const FaceTexdef& faceTexdef ){
510 m_projection = faceTexdef.m_projection;
513 void exportState( FaceTexdef& faceTexdef ) const {
514 Texdef_Assign( faceTexdef.m_projection, m_projection );
518 FaceShader& m_shader;
519 TextureProjection m_projection;
520 bool m_projectionInitialised;
525 const TextureProjection& projection,
526 bool projectionInitialised = true
529 m_projection( projection ),
530 m_projectionInitialised( projectionInitialised ),
531 m_scaleApplied( false ){
532 m_shader.attach( *this );
535 m_shader.detach( *this );
539 ASSERT_MESSAGE( !m_scaleApplied, "texture scale aready added" );
540 m_scaleApplied = true;
541 m_projection.m_brushprimit_texdef.addScale( m_shader.width(), m_shader.height() );
544 ASSERT_MESSAGE( m_scaleApplied, "texture scale aready removed" );
545 m_scaleApplied = false;
546 m_projection.m_brushprimit_texdef.removeScale( m_shader.width(), m_shader.height() );
549 void realiseShader(){
550 if ( m_projectionInitialised && !m_scaleApplied ) {
554 void unrealiseShader(){
555 if ( m_projectionInitialised && m_scaleApplied ) {
560 void setTexdef( const TextureProjection& projection ){
562 Texdef_Assign( m_projection, projection );
566 void shift( float s, float t ){
567 ASSERT_MESSAGE( texdef_sane( m_projection.m_texdef ), "FaceTexdef::shift: bad texdef" );
569 Texdef_Shift( m_projection, s, t );
573 void scale( float s, float t ){
575 Texdef_Scale( m_projection, s, t );
579 void rotate( float angle ){
581 Texdef_Rotate( m_projection, angle );
585 void fit( const Vector3& normal, const Winding& winding, float s_repeat, float t_repeat ){
586 Texdef_FitTexture( m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat );
589 void emitTextureCoordinates( Winding& winding, const Vector3& normal, const Matrix4& localToWorld ){
590 Texdef_EmitTextureCoordinates( m_projection, m_shader.width(), m_shader.height(), winding, normal, localToWorld );
593 void transform( const Plane3& plane, const Matrix4& matrix ){
595 Texdef_transformLocked( m_projection, m_shader.width(), m_shader.height(), plane, matrix );
599 TextureProjection normalised() const {
600 brushprimit_texdef_t tmp( m_projection.m_brushprimit_texdef );
601 tmp.removeScale( m_shader.width(), m_shader.height() );
602 return TextureProjection( m_projection.m_texdef, tmp, m_projection.m_basis_s, m_projection.m_basis_t );
604 void setBasis( const Vector3& normal ){
606 Normal_GetTransform( normal, basis );
607 m_projection.m_basis_s = Vector3( basis.xx(), basis.yx(), basis.zx() );
608 m_projection.m_basis_t = Vector3( -basis.xy(), -basis.yy(), -basis.zy() );
612 inline void planepts_print( const PlanePoints& planePoints, TextOutputStream& ostream ){
613 ostream << "( " << planePoints[0][0] << " " << planePoints[0][1] << " " << planePoints[0][2] << " ) "
614 << "( " << planePoints[1][0] << " " << planePoints[1][1] << " " << planePoints[1][2] << " ) "
615 << "( " << planePoints[2][0] << " " << planePoints[2][1] << " " << planePoints[2][2] << " )";
619 inline Plane3 Plane3_applyTranslation( const Plane3& plane, const Vector3& translation ){
620 Plane3 tmp( plane3_translated( Plane3( plane.normal(), -plane.dist() ), translation ) );
621 return Plane3( tmp.normal(), -tmp.dist() );
624 inline Plane3 Plane3_applyTransform( const Plane3& plane, const Matrix4& matrix ){
625 Plane3 tmp( plane3_transformed( Plane3( plane.normal(), -plane.dist() ), matrix ) );
626 return Plane3( tmp.normal(), -tmp.dist() );
631 PlanePoints m_planepts;
632 Plane3 m_planeCached;
635 Vector3 m_funcStaticOrigin;
637 static EBrushType m_type;
639 static bool isDoom3Plane(){
640 return FacePlane::m_type == eBrushTypeDoom3 || FacePlane::m_type == eBrushTypeQuake4;
646 PlanePoints m_planepts;
649 SavedState( const FacePlane& facePlane ){
650 if ( facePlane.isDoom3Plane() ) {
651 m_plane = facePlane.m_plane;
655 planepts_assign( m_planepts, facePlane.planePoints() );
659 void exportState( FacePlane& facePlane ) const {
660 if ( facePlane.isDoom3Plane() ) {
661 facePlane.m_plane = m_plane;
662 facePlane.updateTranslated();
666 planepts_assign( facePlane.planePoints(), m_planepts );
667 facePlane.MakePlane();
672 FacePlane() : m_funcStaticOrigin( 0, 0, 0 ){
674 FacePlane( const FacePlane& other ) : m_funcStaticOrigin( 0, 0, 0 ){
675 if ( !isDoom3Plane() ) {
676 planepts_assign( m_planepts, other.m_planepts );
681 m_plane = other.m_plane;
687 if ( !isDoom3Plane() ) {
689 if ( check_plane_is_integer( m_planepts ) ) {
690 globalErrorStream() << "non-integer planepts: ";
691 planepts_print( m_planepts, globalErrorStream() );
692 globalErrorStream() << "\n";
695 m_planeCached = plane3_for_points( m_planepts );
700 if ( !isDoom3Plane() ) {
701 vector3_swap( m_planepts[0], m_planepts[2] );
706 m_planeCached = plane3_flipped( m_plane );
710 void transform( const Matrix4& matrix, bool mirror ){
711 if ( !isDoom3Plane() ) {
714 bool off = check_plane_is_integer( planePoints() );
717 matrix4_transform_point( matrix, m_planepts[0] );
718 matrix4_transform_point( matrix, m_planepts[1] );
719 matrix4_transform_point( matrix, m_planepts[2] );
726 if ( check_plane_is_integer( planePoints() ) ) {
728 globalErrorStream() << "caused by transform\n";
736 m_planeCached = Plane3_applyTransform( m_planeCached, matrix );
740 void offset( float offset ){
741 if ( !isDoom3Plane() ) {
742 Vector3 move( vector3_scaled( m_planeCached.normal(), -offset ) );
744 vector3_subtract( m_planepts[0], move );
745 vector3_subtract( m_planepts[1], move );
746 vector3_subtract( m_planepts[2], move );
752 m_planeCached.d += offset;
757 void updateTranslated(){
758 m_planeCached = Plane3_applyTranslation( m_plane, m_funcStaticOrigin );
761 m_plane = Plane3_applyTranslation( m_planeCached, vector3_negated( m_funcStaticOrigin ) );
765 PlanePoints& planePoints(){
768 const PlanePoints& planePoints() const {
771 const Plane3& plane3() const {
772 return m_planeCached;
774 void setDoom3Plane( const Plane3& plane ){
778 const Plane3& getDoom3Plane() const {
782 void copy( const FacePlane& other ){
783 if ( !isDoom3Plane() ) {
784 planepts_assign( m_planepts, other.m_planepts );
789 m_planeCached = other.m_plane;
793 void copy( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
794 if ( !isDoom3Plane() ) {
802 m_planeCached = plane3_for_points( p2, p1, p0 );
808 inline void Winding_testSelect( Winding& winding, SelectionTest& test, SelectionIntersection& best ){
809 test.TestPolygon( VertexPointer( reinterpret_cast<VertexPointer::pointer>( &winding.points.data()->vertex ), sizeof( WindingVertex ) ), winding.numpoints, best );
812 const double GRID_MIN = 0.125;
814 inline double quantiseInteger( double f ){
815 return float_to_integer( f );
818 inline double quantiseFloating( double f ){
819 return float_snapped( f, 1.f / ( 1 << 16 ) );
822 typedef double ( *QuantiseFunc )( double f );
829 virtual bool filter( const Face& face ) const = 0;
832 bool face_filtered( Face& face );
833 void add_face_filter( FaceFilter& filter, int mask, bool invert = false );
835 void Brush_addTextureChangedCallback( const SignalHandler& callback );
836 void Brush_textureChanged();
839 extern bool g_brush_texturelock_enabled;
844 virtual void planeChanged() = 0;
845 virtual void connectivityChanged() = 0;
846 virtual void shaderChanged() = 0;
847 virtual void evaluateTransform() = 0;
851 public OpenGLRenderable,
854 public FaceShaderObserver
856 std::size_t m_refcount;
858 class SavedState : public UndoMemento
861 FacePlane::SavedState m_planeState;
862 FaceTexdef::SavedState m_texdefState;
863 FaceShader::SavedState m_shaderState;
865 SavedState( const Face& face ) : m_planeState( face.getPlane() ), m_texdefState( face.getTexdef() ), m_shaderState( face.getShader() ){
868 void exportState( Face& face ) const {
869 m_planeState.exportState( face.getPlane() );
870 m_shaderState.exportState( face.getShader() );
871 m_texdefState.exportState( face.getTexdef() );
880 static QuantiseFunc m_quantise;
881 static EBrushType m_type;
883 PlanePoints m_move_planepts;
884 PlanePoints m_move_planeptsTransformed;
887 FacePlane m_planeTransformed;
890 TextureProjection m_texdefTransformed;
896 FaceObserver* m_observer;
897 UndoObserver* m_undoable_observer;
900 // assignment not supported
901 Face& operator=( const Face& other );
902 // copy-construction not supported
903 Face( const Face& other );
907 Face( FaceObserver* observer ) :
909 m_shader( texdef_name_default() ),
910 m_texdef( m_shader, TextureProjection(), false ),
912 m_observer( observer ),
913 m_undoable_observer( 0 ),
915 m_shader.attach( *this );
916 m_plane.copy( Vector3( 0, 0, 0 ), Vector3( 64, 0, 0 ), Vector3( 0, 64, 0 ) );
917 m_texdef.setBasis( m_plane.plane3().normal() );
925 const TextureProjection& projection,
926 FaceObserver* observer
930 m_texdef( m_shader, projection ),
931 m_observer( observer ),
932 m_undoable_observer( 0 ),
934 m_shader.attach( *this );
935 m_plane.copy( p0, p1, p2 );
936 m_texdef.setBasis( m_plane.plane3().normal() );
940 Face( const Face& other, FaceObserver* observer ) :
942 m_shader( other.m_shader.getShader(), other.m_shader.m_flags ),
943 m_texdef( m_shader, other.getTexdef().normalised() ),
944 m_observer( observer ),
945 m_undoable_observer( 0 ),
947 m_shader.attach( *this );
948 m_plane.copy( other.m_plane );
949 planepts_assign( m_move_planepts, other.m_move_planepts );
950 m_texdef.setBasis( m_plane.plane3().normal() );
955 m_shader.detach( *this );
960 m_observer->planeChanged();
963 void realiseShader(){
964 m_observer->shaderChanged();
966 void unrealiseShader(){
969 void instanceAttach( MapFile* map ){
970 m_shader.instanceAttach();
972 m_undoable_observer = GlobalUndoSystem().observer( this );
973 GlobalFilterSystem().registerFilterable( *this );
975 void instanceDetach( MapFile* map ){
976 GlobalFilterSystem().unregisterFilterable( *this );
977 m_undoable_observer = 0;
978 GlobalUndoSystem().release( this );
980 m_shader.instanceDetach();
983 void render( RenderStateFlags state ) const {
984 Winding_Draw( m_winding, m_planeTransformed.plane3().normal(), state );
987 void updateFiltered(){
988 m_filtered = face_filtered( *this );
990 bool isFiltered() const {
998 if ( m_undoable_observer != 0 ) {
999 m_undoable_observer->save( this );
1004 UndoMemento* exportState() const {
1005 return new SavedState( *this );
1007 void importState( const UndoMemento* data ){
1010 static_cast<const SavedState*>( data )->exportState( *this );
1013 m_observer->connectivityChanged();
1015 m_observer->shaderChanged();
1023 if ( --m_refcount == 0 ) {
1033 bool intersectVolume( const VolumeTest& volume, const Matrix4& localToWorld ) const {
1034 return volume.TestPlane( Plane3( plane3().normal(), -plane3().dist() ), localToWorld );
1037 void render( Renderer& renderer, const Matrix4& localToWorld ) const {
1038 renderer.SetState( m_shader.state(), Renderer::eFullMaterials );
1039 renderer.addRenderable( *this, localToWorld );
1042 void transform( const Matrix4& matrix, bool mirror ){
1043 if ( g_brush_texturelock_enabled ) {
1044 Texdef_transformLocked( m_texdefTransformed, m_shader.width(), m_shader.height(), m_plane.plane3(), matrix );
1047 m_planeTransformed.transform( matrix, mirror );
1050 ASSERT_MESSAGE( projectionaxis_for_normal( normal ) == projectionaxis_for_normal( plane3().normal() ), "bleh" );
1052 m_observer->planeChanged();
1054 if ( g_brush_texturelock_enabled ) {
1055 Brush_textureChanged();
1059 void assign_planepts( const PlanePoints planepts ){
1060 m_planeTransformed.copy( planepts[0], planepts[1], planepts[2] );
1061 m_observer->planeChanged();
1064 /// \brief Reverts the transformable state of the brush to identity.
1065 void revertTransform(){
1066 m_planeTransformed = m_plane;
1067 planepts_assign( m_move_planeptsTransformed, m_move_planepts );
1068 m_texdefTransformed = m_texdef.m_projection;
1070 void freezeTransform(){
1072 m_plane = m_planeTransformed;
1073 planepts_assign( m_move_planepts, m_move_planeptsTransformed );
1074 m_texdef.m_projection = m_texdefTransformed;
1077 void update_move_planepts_vertex( std::size_t index, PlanePoints planePoints ){
1078 std::size_t numpoints = getWinding().numpoints;
1079 ASSERT_MESSAGE( index < numpoints, "update_move_planepts_vertex: invalid index" );
1081 std::size_t opposite = Winding_Opposite( getWinding(), index );
1082 std::size_t adjacent = Winding_wrap( getWinding(), opposite + numpoints - 1 );
1083 planePoints[0] = getWinding()[opposite].vertex;
1084 planePoints[1] = getWinding()[index].vertex;
1085 planePoints[2] = getWinding()[adjacent].vertex;
1086 // winding points are very inaccurate, so they must be quantised before using them to generate the face-plane
1087 planepts_quantise( planePoints, GRID_MIN );
1090 void snapto( float snap ){
1091 if ( contributes() ) {
1093 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane before snap to grid" );
1094 planepts_snap( m_plane.planePoints(), snap );
1095 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane after snap to grid" );
1097 PlanePoints planePoints;
1098 update_move_planepts_vertex( 0, planePoints );
1099 vector3_snap( planePoints[0], snap );
1100 vector3_snap( planePoints[1], snap );
1101 vector3_snap( planePoints[2], snap );
1102 assign_planepts( planePoints );
1105 SceneChangeNotify();
1106 if ( !plane3_valid( m_plane.plane3() ) ) {
1107 globalErrorStream() << "WARNING: invalid plane after snap to grid\n";
1112 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1113 Winding_testSelect( m_winding, test, best );
1116 void testSelect_centroid( SelectionTest& test, SelectionIntersection& best ){
1117 test.TestPoint( m_centroid, best );
1120 void shaderChanged(){
1121 EmitTextureCoordinates();
1122 Brush_textureChanged();
1123 m_observer->shaderChanged();
1126 SceneChangeNotify();
1129 const char* GetShader() const {
1130 return m_shader.getShader();
1132 void SetShader( const char* name ){
1134 m_shader.setShader( name );
1138 void revertTexdef(){
1139 m_texdefTransformed = m_texdef.m_projection;
1141 void texdefChanged(){
1143 EmitTextureCoordinates();
1144 Brush_textureChanged();
1147 void GetTexdef( TextureProjection& projection ) const {
1148 projection = m_texdef.normalised();
1150 void SetTexdef( const TextureProjection& projection ){
1152 m_texdef.setTexdef( projection );
1156 void GetFlags( ContentsFlagsValue& flags ) const {
1157 flags = m_shader.getFlags();
1159 void SetFlags( const ContentsFlagsValue& flags ){
1161 m_shader.setFlags( flags );
1162 m_observer->shaderChanged();
1166 void ShiftTexdef( float s, float t ){
1168 m_texdef.shift( s, t );
1172 void ScaleTexdef( float s, float t ){
1174 m_texdef.scale( s, t );
1178 void RotateTexdef( float angle ){
1180 m_texdef.rotate( angle );
1184 void FitTexture( float s_repeat, float t_repeat ){
1186 m_texdef.fit( m_plane.plane3().normal(), m_winding, s_repeat, t_repeat );
1190 void EmitTextureCoordinates(){
1191 Texdef_EmitTextureCoordinates( m_texdefTransformed, m_shader.width(), m_shader.height(), m_winding, plane3().normal(), g_matrix4_identity );
1195 const Vector3& centroid() const {
1199 void construct_centroid(){
1200 Winding_Centroid( m_winding, plane3(), m_centroid );
1203 const Winding& getWinding() const {
1206 Winding& getWinding(){
1210 const Plane3& plane3() const {
1211 m_observer->evaluateTransform();
1212 return m_planeTransformed.plane3();
1214 FacePlane& getPlane(){
1217 const FacePlane& getPlane() const {
1220 FaceTexdef& getTexdef(){
1223 const FaceTexdef& getTexdef() const {
1226 FaceShader& getShader(){
1229 const FaceShader& getShader() const {
1233 bool isDetail() const {
1234 return ( m_shader.m_flags.m_contentFlags & BRUSH_DETAIL_MASK ) != 0;
1236 void setDetail( bool detail ){
1238 if ( detail && !isDetail() ) {
1239 m_shader.m_flags.m_contentFlags |= BRUSH_DETAIL_MASK;
1241 else if ( !detail && isDetail() ) {
1242 m_shader.m_flags.m_contentFlags &= ~BRUSH_DETAIL_MASK;
1244 m_observer->shaderChanged();
1247 bool contributes() const {
1248 return m_winding.numpoints > 2;
1250 bool is_bounded() const {
1251 for ( Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i )
1253 if ( ( *i ).adjacent == c_brush_maxFaces ) {
1265 std::size_t m_vertex;
1268 FaceVertexId( std::size_t face, std::size_t vertex )
1269 : m_face( face ), m_vertex( vertex ){
1272 std::size_t getFace() const {
1275 std::size_t getVertex() const {
1280 typedef std::size_t faceIndex_t;
1282 struct EdgeRenderIndices
1288 : first( 0 ), second( 0 ){
1290 EdgeRenderIndices( const RenderIndex _first, const RenderIndex _second )
1291 : first( _first ), second( _second ){
1301 : first( c_brush_maxFaces ), second( c_brush_maxFaces ){
1303 EdgeFaces( const faceIndex_t _first, const faceIndex_t _second )
1304 : first( _first ), second( _second ){
1308 class RenderableWireframe : public OpenGLRenderable
1311 void render( RenderStateFlags state ) const {
1313 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices->colour );
1314 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices->vertex );
1315 glDrawElements( GL_LINES, GLsizei( m_size << 1 ), RenderIndexTypeID, m_faceVertex.data() );
1317 glBegin( GL_LINES );
1318 for ( std::size_t i = 0; i < m_size; ++i )
1320 glVertex3fv( &m_vertices[m_faceVertex[i].first].vertex.x );
1321 glVertex3fv( &m_vertices[m_faceVertex[i].second].vertex.x );
1327 Array<EdgeRenderIndices> m_faceVertex;
1329 const PointVertex* m_vertices;
1333 typedef std::vector<Brush*> brush_vector_t;
1338 virtual bool filter( const Brush& brush ) const = 0;
1341 bool brush_filtered( Brush& brush );
1342 void add_brush_filter( BrushFilter& filter, int mask, bool invert = false );
1345 /// \brief Returns true if 'self' takes priority when building brush b-rep.
1346 inline bool plane3_inside( const Plane3& self, const Plane3& other, bool selfIsLater ){
1347 if ( vector3_equal_epsilon( self.normal(), other.normal(), 0.001 ) ) {
1348 // same plane? prefer the one with smaller index
1349 if ( self.dist() == other.dist() ) {
1352 return self.dist() < other.dist();
1357 typedef SmartPointer<Face> FaceSmartPointer;
1358 typedef std::vector<FaceSmartPointer> Faces;
1360 /// \brief Returns the unique-id of the edge adjacent to \p faceVertex in the edge-pair for the set of \p faces.
1361 inline FaceVertexId next_edge( const Faces& faces, FaceVertexId faceVertex ){
1362 std::size_t adjacent_face = faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent;
1363 std::size_t adjacent_vertex = Winding_FindAdjacent( faces[adjacent_face]->getWinding(), faceVertex.getFace() );
1365 ASSERT_MESSAGE( adjacent_vertex != c_brush_maxFaces, "connectivity data invalid" );
1366 if ( adjacent_vertex == c_brush_maxFaces ) {
1370 return FaceVertexId( adjacent_face, adjacent_vertex );
1373 /// \brief Returns the unique-id of the vertex adjacent to \p faceVertex in the vertex-ring for the set of \p faces.
1374 inline FaceVertexId next_vertex( const Faces& faces, FaceVertexId faceVertex ){
1375 FaceVertexId nextEdge = next_edge( faces, faceVertex );
1376 return FaceVertexId( nextEdge.getFace(), Winding_next( faces[nextEdge.getFace()]->getWinding(), nextEdge.getVertex() ) );
1379 class SelectableEdge
1381 Vector3 getEdge() const {
1382 const Winding& winding = getFace().getWinding();
1383 return vector3_mid( winding[m_faceVertex.getVertex()].vertex, winding[Winding_next( winding, m_faceVertex.getVertex() )].vertex );
1388 FaceVertexId m_faceVertex;
1390 SelectableEdge( Faces& faces, FaceVertexId faceVertex )
1391 : m_faces( faces ), m_faceVertex( faceVertex ){
1393 SelectableEdge& operator=( const SelectableEdge& other ){
1394 m_faceVertex = other.m_faceVertex;
1398 Face& getFace() const {
1399 return *m_faces[m_faceVertex.getFace()];
1402 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1403 test.TestPoint( getEdge(), best );
1407 class SelectableVertex
1409 Vector3 getVertex() const {
1410 return getFace().getWinding()[m_faceVertex.getVertex()].vertex;
1415 FaceVertexId m_faceVertex;
1417 SelectableVertex( Faces& faces, FaceVertexId faceVertex )
1418 : m_faces( faces ), m_faceVertex( faceVertex ){
1420 SelectableVertex& operator=( const SelectableVertex& other ){
1421 m_faceVertex = other.m_faceVertex;
1425 Face& getFace() const {
1426 return *m_faces[m_faceVertex.getFace()];
1429 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1430 test.TestPoint( getVertex(), best );
1437 virtual void reserve( std::size_t size ) = 0;
1438 virtual void clear() = 0;
1439 virtual void push_back( Face& face ) = 0;
1440 virtual void pop_back() = 0;
1441 virtual void erase( std::size_t index ) = 0;
1442 virtual void connectivityChanged() = 0;
1444 virtual void edge_clear() = 0;
1445 virtual void edge_push_back( SelectableEdge& edge ) = 0;
1447 virtual void vertex_clear() = 0;
1448 virtual void vertex_push_back( SelectableVertex& vertex ) = 0;
1450 virtual void DEBUG_verify() const = 0;
1456 virtual void visit( Face& face ) const = 0;
1460 public TransformNode,
1465 public FaceObserver,
1471 scene::Node* m_node;
1472 typedef UniqueSet<BrushObserver*> Observers;
1473 Observers m_observers;
1474 UndoObserver* m_undoable_observer;
1481 // cached data compiled from state
1482 Array<PointVertex> m_faceCentroidPoints;
1483 RenderablePointArray m_render_faces;
1485 Array<PointVertex> m_uniqueVertexPoints;
1486 typedef std::vector<SelectableVertex> SelectableVertices;
1487 SelectableVertices m_select_vertices;
1488 RenderablePointArray m_render_vertices;
1490 Array<PointVertex> m_uniqueEdgePoints;
1491 typedef std::vector<SelectableEdge> SelectableEdges;
1492 SelectableEdges m_select_edges;
1493 RenderablePointArray m_render_edges;
1495 Array<EdgeRenderIndices> m_edge_indices;
1496 Array<EdgeFaces> m_edge_faces;
1501 Callback<void()> m_evaluateTransform;
1502 Callback<void()> m_boundsChanged;
1504 mutable bool m_planeChanged; // b-rep evaluation required
1505 mutable bool m_transformChanged; // transform evaluation required
1509 STRING_CONSTANT( Name, "Brush" );
1511 Callback<void()> m_lightsChanged;
1514 static Shader* m_state_point;
1517 static EBrushType m_type;
1518 static double m_maxWorldCoord;
1520 Brush( scene::Node& node, const Callback<void()>& evaluateTransform, const Callback<void()>& boundsChanged ) :
1522 m_undoable_observer( 0 ),
1524 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1525 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1526 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1527 m_evaluateTransform( evaluateTransform ),
1528 m_boundsChanged( boundsChanged ),
1529 m_planeChanged( false ),
1530 m_transformChanged( false ){
1533 Brush( const Brush& other, scene::Node& node, const Callback<void()>& evaluateTransform, const Callback<void()>& boundsChanged ) :
1535 m_undoable_observer( 0 ),
1537 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1538 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1539 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1540 m_evaluateTransform( evaluateTransform ),
1541 m_boundsChanged( boundsChanged ),
1542 m_planeChanged( false ),
1543 m_transformChanged( false ){
1546 Brush( const Brush& other ) :
1547 TransformNode( other ),
1552 FaceObserver( other ),
1553 Filterable( other ),
1555 BrushDoom3( other ),
1557 m_undoable_observer( 0 ),
1559 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1560 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1561 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1562 m_planeChanged( false ),
1563 m_transformChanged( false ){
1567 ASSERT_MESSAGE( m_observers.empty(), "Brush::~Brush: observers still attached" );
1570 // assignment not supported
1571 Brush& operator=( const Brush& other );
1573 void setDoom3GroupOrigin( const Vector3& origin ){
1574 //globalOutputStream() << "func_static origin before: " << m_funcStaticOrigin << " after: " << origin << "\n";
1575 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1577 ( *i )->getPlane().m_funcStaticOrigin = origin;
1578 ( *i )->getPlane().updateTranslated();
1579 ( *i )->planeChanged();
1584 void attach( BrushObserver& observer ){
1585 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1587 observer.push_back( *( *i ) );
1590 for ( SelectableEdges::iterator i = m_select_edges.begin(); i != m_select_edges.end(); ++i )
1592 observer.edge_push_back( *i );
1595 for ( SelectableVertices::iterator i = m_select_vertices.begin(); i != m_select_vertices.end(); ++i )
1597 observer.vertex_push_back( *i );
1600 m_observers.insert( &observer );
1602 void detach( BrushObserver& observer ){
1603 m_observers.erase( &observer );
1606 void forEachFace( const BrushVisitor& visitor ) const {
1607 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1609 visitor.visit( *( *i ) );
1613 void forEachFace_instanceAttach( MapFile* map ) const {
1614 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1616 ( *i )->instanceAttach( map );
1619 void forEachFace_instanceDetach( MapFile* map ) const {
1620 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1622 ( *i )->instanceDetach( map );
1626 InstanceCounter m_instanceCounter;
1627 void instanceAttach( const scene::Path& path ){
1628 if ( ++m_instanceCounter.m_count == 1 ) {
1629 m_map = path_find_mapfile( path.begin(), path.end() );
1630 m_undoable_observer = GlobalUndoSystem().observer( this );
1631 GlobalFilterSystem().registerFilterable( *this );
1632 forEachFace_instanceAttach( m_map );
1636 ASSERT_MESSAGE( path_find_mapfile( path.begin(), path.end() ) == m_map, "node is instanced across more than one file" );
1639 void instanceDetach( const scene::Path& path ){
1640 if ( --m_instanceCounter.m_count == 0 ) {
1641 forEachFace_instanceDetach( m_map );
1642 GlobalFilterSystem().unregisterFilterable( *this );
1644 m_undoable_observer = 0;
1645 GlobalUndoSystem().release( this );
1650 const char* name() const {
1653 void attach( const NameCallback& callback ){
1655 void detach( const NameCallback& callback ){
1659 void updateFiltered(){
1660 if ( m_node != 0 ) {
1661 if ( brush_filtered( *this ) ) {
1662 m_node->enable( scene::Node::eFiltered );
1666 m_node->disable( scene::Node::eFiltered );
1672 void planeChanged(){
1673 m_planeChanged = true;
1677 void shaderChanged(){
1682 void evaluateBRep() const {
1683 if ( m_planeChanged ) {
1684 m_planeChanged = false;
1685 const_cast<Brush*>( this )->buildBRep();
1689 void transformChanged(){
1690 m_transformChanged = true;
1693 typedef MemberCaller<Brush, void(), &Brush::transformChanged> TransformChangedCaller;
1695 void evaluateTransform(){
1696 if ( m_transformChanged ) {
1697 m_transformChanged = false;
1699 m_evaluateTransform();
1702 const Matrix4& localToParent() const {
1703 return g_matrix4_identity;
1708 const AABB& localAABB() const {
1710 return m_aabb_local;
1713 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
1714 return test.TestAABB( m_aabb_local, localToWorld );
1717 void renderComponents( SelectionSystem::EComponentMode mode, Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
1720 case SelectionSystem::eVertex:
1721 renderer.addRenderable( m_render_vertices, localToWorld );
1723 case SelectionSystem::eEdge:
1724 renderer.addRenderable( m_render_edges, localToWorld );
1726 case SelectionSystem::eFace:
1727 renderer.addRenderable( m_render_faces, localToWorld );
1734 void transform( const Matrix4& matrix ){
1735 bool mirror = matrix4_handedness( matrix ) == MATRIX4_LEFTHANDED;
1737 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1739 ( *i )->transform( matrix, mirror );
1742 void snapto( float snap ){
1743 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1745 ( *i )->snapto( snap );
1748 void revertTransform(){
1749 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1751 ( *i )->revertTransform();
1754 void freezeTransform(){
1755 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1757 ( *i )->freezeTransform();
1761 /// \brief Returns the absolute index of the \p faceVertex.
1762 std::size_t absoluteIndex( FaceVertexId faceVertex ){
1763 std::size_t index = 0;
1764 for ( std::size_t i = 0; i < faceVertex.getFace(); ++i )
1766 index += m_faces[i]->getWinding().numpoints;
1768 return index + faceVertex.getVertex();
1771 void appendFaces( const Faces& other ){
1773 for ( Faces::const_iterator i = other.begin(); i != other.end(); ++i )
1779 /// \brief The undo memento for a brush stores only the list of face references - the faces are not copied.
1780 class BrushUndoMemento : public UndoMemento
1783 BrushUndoMemento( const Faces& faces ) : m_faces( faces ){
1796 if ( m_undoable_observer != 0 ) {
1797 m_undoable_observer->save( this );
1801 UndoMemento* exportState() const {
1802 return new BrushUndoMemento( m_faces );
1805 void importState( const UndoMemento* state ){
1807 appendFaces( static_cast<const BrushUndoMemento*>( state )->m_faces );
1810 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1812 ( *i )->DEBUG_verify();
1817 return !m_faces.empty() && m_faces.front()->isDetail();
1820 /// \brief Appends a copy of \p face to the end of the face list.
1821 Face* addFace( const Face& face ){
1822 if ( m_faces.size() == c_brush_maxFaces ) {
1826 push_back( FaceSmartPointer( new Face( face, this ) ) );
1827 m_faces.back()->setDetail( isDetail() );
1829 return m_faces.back();
1832 /// \brief Appends a new face constructed from the parameters to the end of the face list.
1833 Face* addPlane( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection ){
1834 if ( m_faces.size() == c_brush_maxFaces ) {
1838 push_back( FaceSmartPointer( new Face( p0, p1, p2, shader, projection, this ) ) );
1839 m_faces.back()->setDetail( isDetail() );
1841 return m_faces.back();
1844 static void constructStatic( EBrushType type ){
1846 Face::m_type = type;
1847 FacePlane::m_type = type;
1849 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_QUAKE;
1850 if ( m_type == eBrushTypeQuake3BP || m_type == eBrushTypeDoom3 || m_type == eBrushTypeQuake4 ) {
1851 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_BRUSHPRIMITIVES;
1852 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
1854 else if ( m_type == eBrushTypeHalfLife ) {
1855 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_HALFLIFE;
1856 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
1859 Face::m_quantise = ( m_type == eBrushTypeQuake ) ? quantiseInteger : quantiseFloating;
1861 m_state_point = GlobalShaderCache().capture( "$POINT" );
1863 static void destroyStatic(){
1864 GlobalShaderCache().release( "$POINT" );
1867 std::size_t DEBUG_size(){
1868 return m_faces.size();
1871 typedef Faces::const_iterator const_iterator;
1873 const_iterator begin() const {
1874 return m_faces.begin();
1876 const_iterator end() const {
1877 return m_faces.end();
1881 return m_faces.back();
1883 const Face* back() const {
1884 return m_faces.back();
1886 void reserve( std::size_t count ){
1887 m_faces.reserve( count );
1888 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1890 ( *i )->reserve( count );
1893 void push_back( Faces::value_type face ){
1894 m_faces.push_back( face );
1895 if ( m_instanceCounter.m_count != 0 ) {
1896 m_faces.back()->instanceAttach( m_map );
1898 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1900 ( *i )->push_back( *face );
1901 ( *i )->DEBUG_verify();
1905 if ( m_instanceCounter.m_count != 0 ) {
1906 m_faces.back()->instanceDetach( m_map );
1909 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1912 ( *i )->DEBUG_verify();
1915 void erase( std::size_t index ){
1916 if ( m_instanceCounter.m_count != 0 ) {
1917 m_faces[index]->instanceDetach( m_map );
1919 m_faces.erase( m_faces.begin() + index );
1920 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1922 ( *i )->erase( index );
1923 ( *i )->DEBUG_verify();
1926 void connectivityChanged(){
1927 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1929 ( *i )->connectivityChanged();
1936 if ( m_instanceCounter.m_count != 0 ) {
1937 forEachFace_instanceDetach( m_map );
1940 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1943 ( *i )->DEBUG_verify();
1946 std::size_t size() const {
1947 return m_faces.size();
1949 bool empty() const {
1950 return m_faces.empty();
1953 /// \brief Returns true if any face of the brush contributes to the final B-Rep.
1954 bool hasContributingFaces() const {
1955 for ( const_iterator i = begin(); i != end(); ++i )
1957 if ( ( *i )->contributes() ) {
1964 /// \brief Removes faces that do not contribute to the brush. This is useful for cleaning up after CSG operations on the brush.
1965 /// 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.
1966 void removeEmptyFaces(){
1971 while ( i < m_faces.size() )
1973 if ( !m_faces[i]->contributes() ) {
1985 /// \brief Constructs \p winding from the intersection of \p plane with the other planes of the brush.
1986 void windingForClipPlane( Winding& winding, const Plane3& plane ) const {
1987 FixedWinding buffer[2];
1990 // get a poly that covers an effectively infinite area
1991 Winding_createInfinite( buffer[swap], plane, m_maxWorldCoord + 1 );
1993 // chop the poly by all of the other faces
1995 for ( std::size_t i = 0; i < m_faces.size(); ++i )
1997 const Face& clip = *m_faces[i];
1999 if ( plane3_equal( clip.plane3(), plane )
2000 || !plane3_valid( clip.plane3() ) || !plane_unique( i )
2001 || plane3_opposing( plane, clip.plane3() ) ) {
2005 buffer[!swap].clear();
2007 #if BRUSH_CONNECTIVITY_DEBUG
2008 globalOutputStream() << "clip vs face: " << i << "\n";
2012 // flip the plane, because we want to keep the back side
2013 Plane3 clipPlane( vector3_negated( clip.plane3().normal() ), -clip.plane3().dist() );
2014 Winding_Clip( buffer[swap], plane, clipPlane, i, buffer[!swap] );
2017 #if BRUSH_CONNECTIVITY_DEBUG
2018 for ( FixedWinding::Points::iterator k = buffer[!swap].points.begin(), j = buffer[!swap].points.end() - 1; k != buffer[!swap].points.end(); j = k, ++k )
2020 if ( vector3_length_squared( vector3_subtracted( ( *k ).vertex, ( *j ).vertex ) ) < 1 ) {
2021 globalOutputStream() << "v: " << std::distance( buffer[!swap].points.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2026 //ASSERT_MESSAGE(buffer[!swap].numpoints != 1, "created single-point winding");
2032 Winding_forFixedWinding( winding, buffer[swap] );
2034 #if BRUSH_CONNECTIVITY_DEBUG
2035 Winding_printConnectivity( winding );
2037 for ( Winding::iterator i = winding.begin(), j = winding.end() - 1; i != winding.end(); j = i, ++i )
2039 if ( vector3_length_squared( vector3_subtracted( ( *i ).vertex, ( *j ).vertex ) ) < 1 ) {
2040 globalOutputStream() << "v: " << std::distance( winding.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2046 void update_wireframe( RenderableWireframe& wire, const bool* faces_visible ) const {
2047 wire.m_faceVertex.resize( m_edge_indices.size() );
2048 wire.m_vertices = m_uniqueVertexPoints.data();
2050 for ( std::size_t i = 0; i < m_edge_faces.size(); ++i )
2052 if ( faces_visible[m_edge_faces[i].first]
2053 || faces_visible[m_edge_faces[i].second] ) {
2054 wire.m_faceVertex[wire.m_size++] = m_edge_indices[i];
2060 void update_faces_wireframe( Array<PointVertex>& wire, const bool* faces_visible ) const {
2061 std::size_t count = 0;
2062 for ( std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i )
2064 if ( faces_visible[i] ) {
2069 wire.resize( count );
2070 Array<PointVertex>::iterator p = wire.begin();
2071 for ( std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i )
2073 if ( faces_visible[i] ) {
2074 *p++ = m_faceCentroidPoints[i];
2079 /// \brief Makes this brush a deep-copy of the \p other.
2080 void copy( const Brush& other ){
2081 for ( Faces::const_iterator i = other.m_faces.begin(); i != other.m_faces.end(); ++i )
2089 void edge_push_back( FaceVertexId faceVertex ){
2090 m_select_edges.push_back( SelectableEdge( m_faces, faceVertex ) );
2091 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2093 ( *i )->edge_push_back( m_select_edges.back() );
2097 m_select_edges.clear();
2098 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2100 ( *i )->edge_clear();
2103 void vertex_push_back( FaceVertexId faceVertex ){
2104 m_select_vertices.push_back( SelectableVertex( m_faces, faceVertex ) );
2105 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2107 ( *i )->vertex_push_back( m_select_vertices.back() );
2110 void vertex_clear(){
2111 m_select_vertices.clear();
2112 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2114 ( *i )->vertex_clear();
2118 /// \brief Returns true if the face identified by \p index is preceded by another plane that takes priority over it.
2119 bool plane_unique( std::size_t index ) const {
2121 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2123 if ( index != i && !plane3_inside( m_faces[index]->plane3(), m_faces[i]->plane3(), index < i ) ) {
2130 /// \brief Removes edges that are smaller than the tolerance used when generating brush windings.
2131 void removeDegenerateEdges(){
2132 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2134 Winding& winding = m_faces[i]->getWinding();
2135 for ( Winding::iterator j = winding.begin(); j != winding.end(); )
2137 std::size_t index = std::distance( winding.begin(), j );
2138 std::size_t next = Winding_next( winding, index );
2139 if ( Edge_isDegenerate( winding[index].vertex, winding[next].vertex ) ) {
2140 #if BRUSH_DEGENERATE_DEBUG
2141 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate edge adjacent to " << winding[index].adjacent << "\n";
2143 Winding& other = m_faces[winding[index].adjacent]->getWinding();
2144 std::size_t adjacent = Winding_FindAdjacent( other, i );
2145 if ( adjacent != c_brush_maxFaces ) {
2146 other.erase( other.begin() + adjacent );
2158 /// \brief Invalidates faces that have only two vertices in their winding, while preserving edge-connectivity information.
2159 void removeDegenerateFaces(){
2160 // save adjacency info for degenerate faces
2161 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2163 Winding& degen = m_faces[i]->getWinding();
2165 if ( degen.numpoints == 2 ) {
2166 #if BRUSH_DEGENERATE_DEBUG
2167 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate winding adjacent to " << degen[0].adjacent << ", " << degen[1].adjacent << "\n";
2169 // this is an "edge" face, where the plane touches the edge of the brush
2171 Winding& winding = m_faces[degen[0].adjacent]->getWinding();
2172 std::size_t index = Winding_FindAdjacent( winding, i );
2173 if ( index != c_brush_maxFaces ) {
2174 #if BRUSH_DEGENERATE_DEBUG
2175 globalOutputStream() << "Brush::buildWindings: face " << degen[0].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[1].adjacent << "\n";
2177 winding[index].adjacent = degen[1].adjacent;
2182 Winding& winding = m_faces[degen[1].adjacent]->getWinding();
2183 std::size_t index = Winding_FindAdjacent( winding, i );
2184 if ( index != c_brush_maxFaces ) {
2185 #if BRUSH_DEGENERATE_DEBUG
2186 globalOutputStream() << "Brush::buildWindings: face " << degen[1].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[0].adjacent << "\n";
2188 winding[index].adjacent = degen[0].adjacent;
2197 /// \brief Removes edges that have the same adjacent-face as their immediate neighbour.
2198 void removeDuplicateEdges(){
2199 // verify face connectivity graph
2200 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2202 //if(m_faces[i]->contributes())
2204 Winding& winding = m_faces[i]->getWinding();
2205 for ( std::size_t j = 0; j != winding.numpoints; )
2207 std::size_t next = Winding_next( winding, j );
2208 if ( winding[j].adjacent == winding[next].adjacent ) {
2209 #if BRUSH_DEGENERATE_DEBUG
2210 globalOutputStream() << "Brush::buildWindings: face " << i << ": removed duplicate edge adjacent to face " << winding[j].adjacent << "\n";
2212 winding.erase( winding.begin() + next );
2223 /// \brief Removes edges that do not have a matching pair in their adjacent-face.
2224 void verifyConnectivityGraph(){
2225 // verify face connectivity graph
2226 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2228 //if(m_faces[i]->contributes())
2230 Winding& winding = m_faces[i]->getWinding();
2231 for ( Winding::iterator j = winding.begin(); j != winding.end(); )
2233 #if BRUSH_CONNECTIVITY_DEBUG
2234 globalOutputStream() << "Brush::buildWindings: face " << i << ": adjacent to face " << ( *j ).adjacent << "\n";
2236 // remove unidirectional graph edges
2237 if ( ( *j ).adjacent == c_brush_maxFaces
2238 || Winding_FindAdjacent( m_faces[( *j ).adjacent]->getWinding(), i ) == c_brush_maxFaces ) {
2239 #if BRUSH_CONNECTIVITY_DEBUG
2240 globalOutputStream() << "Brush::buildWindings: face " << i << ": removing unidirectional connectivity graph edge adjacent to face " << ( *j ).adjacent << "\n";
2253 /// \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.
2255 for ( const_iterator i = begin(); i != end(); ++i )
2257 if ( !( *i )->is_bounded() ) {
2264 /// \brief Constructs the polygon windings for each face of the brush. Also updates the brush bounding-box and face texture-coordinates.
2265 bool buildWindings(){
2268 m_aabb_local = AABB();
2270 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2272 Face& f = *m_faces[i];
2274 if ( !plane3_valid( f.plane3() ) || !plane_unique( i ) ) {
2275 f.getWinding().resize( 0 );
2279 #if BRUSH_CONNECTIVITY_DEBUG
2280 globalOutputStream() << "face: " << i << "\n";
2282 windingForClipPlane( f.getWinding(), f.plane3() );
2284 // update brush bounds
2285 const Winding& winding = f.getWinding();
2286 for ( Winding::const_iterator i = winding.begin(); i != winding.end(); ++i )
2288 aabb_extend_by_point_safe( m_aabb_local, ( *i ).vertex );
2291 // update texture coordinates
2292 f.EmitTextureCoordinates();
2297 bool degenerate = !isBounded();
2299 if ( !degenerate ) {
2300 // clean up connectivity information.
2301 // these cleanups must be applied in a specific order.
2302 removeDegenerateEdges();
2303 removeDegenerateFaces();
2304 removeDuplicateEdges();
2305 verifyConnectivityGraph();
2311 /// \brief Constructs the face windings and updates anything that depends on them.
2319 class FaceInstanceSet
2321 typedef SelectionList<FaceInstance> FaceInstances;
2322 FaceInstances m_faceInstances;
2324 void insert( FaceInstance& faceInstance ){
2325 m_faceInstances.append( faceInstance );
2327 void erase( FaceInstance& faceInstance ){
2328 m_faceInstances.erase( faceInstance );
2331 template<typename Functor>
2332 void foreach( Functor functor ){
2333 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
2339 bool empty() const {
2340 return m_faceInstances.empty();
2342 FaceInstance& last() const {
2343 return m_faceInstances.back();
2347 extern FaceInstanceSet g_SelectedFaceInstances;
2349 typedef std::list<std::size_t> VertexSelection;
2351 inline VertexSelection::iterator VertexSelection_find( VertexSelection& self, std::size_t value ){
2352 return std::find( self.begin(), self.end(), value );
2355 inline VertexSelection::const_iterator VertexSelection_find( const VertexSelection& self, std::size_t value ){
2356 return std::find( self.begin(), self.end(), value );
2359 inline VertexSelection::iterator VertexSelection_insert( VertexSelection& self, std::size_t value ){
2360 VertexSelection::iterator i = VertexSelection_find( self, value );
2361 if ( i == self.end() ) {
2362 self.push_back( value );
2363 return --self.end();
2367 inline void VertexSelection_erase( VertexSelection& self, std::size_t value ){
2368 VertexSelection::iterator i = VertexSelection_find( self, value );
2369 if ( i != self.end() ) {
2374 inline bool triangle_reversed( std::size_t x, std::size_t y, std::size_t z ){
2375 return !( ( x < y && y < z ) || ( z < x && x < y ) || ( y < z && z < x ) );
2377 template<typename Element>
2378 inline Vector3 triangle_cross( const BasicVector3<Element>& x, const BasicVector3<Element> y, const BasicVector3<Element>& z ){
2379 return vector3_cross( y - x, z - x );
2381 template<typename Element>
2382 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 ){
2383 return vector3_dot( triangle_cross( x1, y1, z1 ), triangle_cross( x2, y2, z2 ) ) > 0;
2387 typedef const Plane3* PlanePointer;
2388 typedef PlanePointer* PlanesIterator;
2390 class VectorLightList : public LightList
2392 typedef std::vector<const RendererLight*> Lights;
2395 void addLight( const RendererLight& light ){
2396 m_lights.push_back( &light );
2401 void evaluateLights() const {
2403 void lightsChanged() const {
2405 void forEachLight( const RendererLightCallback& callback ) const {
2406 for ( Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i )
2408 callback( *( *i ) );
2416 ObservedSelectable m_selectable;
2417 ObservedSelectable m_selectableVertices;
2418 ObservedSelectable m_selectableEdges;
2419 SelectionChangeCallback m_selectionChanged;
2421 VertexSelection m_vertexSelection;
2422 VertexSelection m_edgeSelection;
2425 mutable VectorLightList m_lights;
2427 FaceInstance( Face& face, const SelectionChangeCallback& observer ) :
2429 m_selectable( SelectedChangedCaller( *this ) ),
2430 m_selectableVertices( observer ),
2431 m_selectableEdges( observer ),
2432 m_selectionChanged( observer ){
2434 FaceInstance( const FaceInstance& other ) :
2435 m_face( other.m_face ),
2436 m_selectable( SelectedChangedCaller( *this ) ),
2437 m_selectableVertices( other.m_selectableVertices ),
2438 m_selectableEdges( other.m_selectableEdges ),
2439 m_selectionChanged( other.m_selectionChanged ){
2441 FaceInstance& operator=( const FaceInstance& other ){
2442 m_face = other.m_face;
2449 const Face& getFace() const {
2453 void selectedChanged( const Selectable& selectable ){
2454 if ( selectable.isSelected() ) {
2455 g_SelectedFaceInstances.insert( *this );
2459 g_SelectedFaceInstances.erase( *this );
2461 m_selectionChanged( selectable );
2463 typedef MemberCaller<FaceInstance, void(const Selectable&), &FaceInstance::selectedChanged> SelectedChangedCaller;
2465 bool selectedVertices() const {
2466 return !m_vertexSelection.empty();
2468 bool selectedEdges() const {
2469 return !m_edgeSelection.empty();
2471 bool isSelected() const {
2472 return m_selectable.isSelected();
2475 bool selectedComponents() const {
2476 return selectedVertices() || selectedEdges() || isSelected();
2478 bool selectedComponents( SelectionSystem::EComponentMode mode ) const {
2481 case SelectionSystem::eVertex:
2482 return selectedVertices();
2483 case SelectionSystem::eEdge:
2484 return selectedEdges();
2485 case SelectionSystem::eFace:
2486 return isSelected();
2491 void setSelected( SelectionSystem::EComponentMode mode, bool select ){
2494 case SelectionSystem::eFace:
2495 m_selectable.setSelected( select );
2497 case SelectionSystem::eVertex:
2498 ASSERT_MESSAGE( !select, "select-all not supported" );
2500 m_vertexSelection.clear();
2501 m_selectableVertices.setSelected( false );
2503 case SelectionSystem::eEdge:
2504 ASSERT_MESSAGE( !select, "select-all not supported" );
2506 m_edgeSelection.clear();
2507 m_selectableEdges.setSelected( false );
2514 template<typename Functor>
2515 void SelectedVertices_foreach( Functor functor ) const {
2516 for ( VertexSelection::const_iterator i = m_vertexSelection.begin(); i != m_vertexSelection.end(); ++i )
2518 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *i );
2519 if ( index != c_brush_maxFaces ) {
2520 functor( getFace().getWinding()[index].vertex );
2524 template<typename Functor>
2525 void SelectedEdges_foreach( Functor functor ) const {
2526 for ( VertexSelection::const_iterator i = m_edgeSelection.begin(); i != m_edgeSelection.end(); ++i )
2528 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *i );
2529 if ( index != c_brush_maxFaces ) {
2530 const Winding& winding = getFace().getWinding();
2531 std::size_t adjacent = Winding_next( winding, index );
2532 functor( vector3_mid( winding[index].vertex, winding[adjacent].vertex ) );
2536 template<typename Functor>
2537 void SelectedFaces_foreach( Functor functor ) const {
2538 if ( isSelected() ) {
2539 functor( centroid() );
2543 template<typename Functor>
2544 void SelectedComponents_foreach( Functor functor ) const {
2545 SelectedVertices_foreach( functor );
2546 SelectedEdges_foreach( functor );
2547 SelectedFaces_foreach( functor );
2550 void iterate_selected( AABB& aabb ) const {
2551 SelectedComponents_foreach([&](const Vector3 &point) {
2552 aabb_extend_by_point_safe(aabb, point);
2556 void iterate_selected( RenderablePointVector& points ) const {
2557 SelectedComponents_foreach([&](const Vector3 &point) {
2558 const Colour4b colour_selected(0, 0, 255, 255);
2559 points.push_back(pointvertex_for_windingpoint(point, colour_selected));
2563 bool intersectVolume( const VolumeTest& volume, const Matrix4& localToWorld ) const {
2564 return m_face->intersectVolume( volume, localToWorld );
2567 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
2568 if ( !m_face->isFiltered() && m_face->contributes() && intersectVolume( volume, localToWorld ) ) {
2569 renderer.PushState();
2570 if ( selectedComponents() ) {
2571 renderer.Highlight( Renderer::eFace );
2573 m_face->render( renderer, localToWorld );
2574 renderer.PopState();
2578 void testSelect( SelectionTest& test, SelectionIntersection& best ){
2579 if ( !m_face->isFiltered() ) {
2580 m_face->testSelect( test, best );
2583 void testSelect( Selector& selector, SelectionTest& test ){
2584 SelectionIntersection best;
2585 testSelect( test, best );
2586 if ( best.valid() ) {
2587 Selector_add( selector, m_selectable, best );
2590 void testSelect_centroid( Selector& selector, SelectionTest& test ){
2591 if ( m_face->contributes() && !m_face->isFiltered() ) {
2592 SelectionIntersection best;
2593 m_face->testSelect_centroid( test, best );
2594 if ( best.valid() ) {
2595 Selector_add( selector, m_selectable, best );
2600 void selectPlane( Selector& selector, const Line& line, PlanesIterator first, PlanesIterator last, const PlaneCallback& selectedPlaneCallback ){
2601 for ( Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i )
2603 Vector3 v( vector3_subtracted( line_closest_point( line, ( *i ).vertex ), ( *i ).vertex ) );
2604 double dot = vector3_dot( getFace().plane3().normal(), v );
2610 Selector_add( selector, m_selectable );
2612 selectedPlaneCallback( getFace().plane3() );
2614 void selectReversedPlane( Selector& selector, const SelectedPlanes& selectedPlanes ){
2615 if ( selectedPlanes.contains( plane3_flipped( getFace().plane3() ) ) ) {
2616 Selector_add( selector, m_selectable );
2620 void transformComponents( const Matrix4& matrix ){
2621 if ( isSelected() ) {
2622 m_face->transform( matrix, false );
2624 if ( selectedVertices() ) {
2625 if ( m_vertexSelection.size() == 1 ) {
2626 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2627 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2629 else if ( m_vertexSelection.size() == 2 ) {
2630 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2631 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2632 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2634 else if ( m_vertexSelection.size() >= 3 ) {
2635 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2636 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2637 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2638 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2641 if ( selectedEdges() ) {
2642 if ( m_edgeSelection.size() == 1 ) {
2643 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2644 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2645 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2647 else if ( m_edgeSelection.size() >= 2 ) {
2648 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
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 );
2656 void snapto( float snap ){
2657 m_face->snapto( snap );
2660 void snapComponents( float snap ){
2661 if ( isSelected() ) {
2664 if ( selectedVertices() ) {
2665 vector3_snap( m_face->m_move_planepts[0], snap );
2666 vector3_snap( m_face->m_move_planepts[1], snap );
2667 vector3_snap( m_face->m_move_planepts[2], snap );
2668 m_face->assign_planepts( m_face->m_move_planepts );
2669 planepts_assign( m_face->m_move_planeptsTransformed, m_face->m_move_planepts );
2670 m_face->freezeTransform();
2672 if ( selectedEdges() ) {
2673 vector3_snap( m_face->m_move_planepts[0], snap );
2674 vector3_snap( m_face->m_move_planepts[1], snap );
2675 vector3_snap( m_face->m_move_planepts[2], snap );
2676 m_face->assign_planepts( m_face->m_move_planepts );
2677 planepts_assign( m_face->m_move_planeptsTransformed, m_face->m_move_planepts );
2678 m_face->freezeTransform();
2681 void update_move_planepts_vertex( std::size_t index ){
2682 m_face->update_move_planepts_vertex( index, m_face->m_move_planepts );
2684 void update_move_planepts_vertex2( std::size_t index, std::size_t other ){
2685 const std::size_t numpoints = m_face->getWinding().numpoints;
2686 ASSERT_MESSAGE( index < numpoints, "select_vertex: invalid index" );
2688 const std::size_t opposite = Winding_Opposite( m_face->getWinding(), index, other );
2690 if ( triangle_reversed( index, other, opposite ) ) {
2691 std::swap( index, other );
2695 triangles_same_winding(
2696 m_face->getWinding()[opposite].vertex,
2697 m_face->getWinding()[index].vertex,
2698 m_face->getWinding()[other].vertex,
2699 m_face->getWinding()[0].vertex,
2700 m_face->getWinding()[1].vertex,
2701 m_face->getWinding()[2].vertex
2703 "update_move_planepts_vertex2: error"
2706 m_face->m_move_planepts[0] = m_face->getWinding()[opposite].vertex;
2707 m_face->m_move_planepts[1] = m_face->getWinding()[index].vertex;
2708 m_face->m_move_planepts[2] = m_face->getWinding()[other].vertex;
2709 planepts_quantise( m_face->m_move_planepts, GRID_MIN ); // winding points are very inaccurate
2711 void update_selection_vertex(){
2712 if ( m_vertexSelection.size() == 0 ) {
2713 m_selectableVertices.setSelected( false );
2717 m_selectableVertices.setSelected( true );
2719 if ( m_vertexSelection.size() == 1 ) {
2720 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_vertexSelection.begin() );
2722 if ( index != c_brush_maxFaces ) {
2723 update_move_planepts_vertex( index );
2726 else if ( m_vertexSelection.size() == 2 ) {
2727 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_vertexSelection.begin() );
2728 std::size_t other = Winding_FindAdjacent( getFace().getWinding(), *( ++m_vertexSelection.begin() ) );
2730 if ( index != c_brush_maxFaces
2731 && other != c_brush_maxFaces ) {
2732 update_move_planepts_vertex2( index, other );
2737 void select_vertex( std::size_t index, bool select ){
2739 VertexSelection_insert( m_vertexSelection, getFace().getWinding()[index].adjacent );
2743 VertexSelection_erase( m_vertexSelection, getFace().getWinding()[index].adjacent );
2746 SceneChangeNotify();
2747 update_selection_vertex();
2750 bool selected_vertex( std::size_t index ) const {
2751 return VertexSelection_find( m_vertexSelection, getFace().getWinding()[index].adjacent ) != m_vertexSelection.end();
2754 void update_move_planepts_edge( std::size_t index ){
2755 std::size_t numpoints = m_face->getWinding().numpoints;
2756 ASSERT_MESSAGE( index < numpoints, "select_edge: invalid index" );
2758 std::size_t adjacent = Winding_next( m_face->getWinding(), index );
2759 std::size_t opposite = Winding_Opposite( m_face->getWinding(), index );
2760 m_face->m_move_planepts[0] = m_face->getWinding()[index].vertex;
2761 m_face->m_move_planepts[1] = m_face->getWinding()[adjacent].vertex;
2762 m_face->m_move_planepts[2] = m_face->getWinding()[opposite].vertex;
2763 planepts_quantise( m_face->m_move_planepts, GRID_MIN ); // winding points are very inaccurate
2765 void update_selection_edge(){
2766 if ( m_edgeSelection.size() == 0 ) {
2767 m_selectableEdges.setSelected( false );
2771 m_selectableEdges.setSelected( true );
2773 if ( m_edgeSelection.size() == 1 ) {
2774 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_edgeSelection.begin() );
2776 if ( index != c_brush_maxFaces ) {
2777 update_move_planepts_edge( index );
2782 void select_edge( std::size_t index, bool select ){
2784 VertexSelection_insert( m_edgeSelection, getFace().getWinding()[index].adjacent );
2788 VertexSelection_erase( m_edgeSelection, getFace().getWinding()[index].adjacent );
2791 SceneChangeNotify();
2792 update_selection_edge();
2795 bool selected_edge( std::size_t index ) const {
2796 return VertexSelection_find( m_edgeSelection, getFace().getWinding()[index].adjacent ) != m_edgeSelection.end();
2799 const Vector3& centroid() const {
2800 return m_face->centroid();
2803 void connectivityChanged(){
2804 // This occurs when a face is added or removed.
2805 // The current vertex and edge selections no longer valid and must be cleared.
2806 m_vertexSelection.clear();
2807 m_selectableVertices.setSelected( false );
2808 m_edgeSelection.clear();
2809 m_selectableEdges.setSelected( false );
2813 class BrushClipPlane : public OpenGLRenderable
2817 static Shader* m_state;
2819 static void constructStatic(){
2820 m_state = GlobalShaderCache().capture( "$CLIPPER_OVERLAY" );
2822 static void destroyStatic(){
2823 GlobalShaderCache().release( "$CLIPPER_OVERLAY" );
2826 void setPlane( const Brush& brush, const Plane3& plane ){
2828 if ( plane3_valid( m_plane ) ) {
2829 brush.windingForClipPlane( m_winding, m_plane );
2833 m_winding.resize( 0 );
2837 void render( RenderStateFlags state ) const {
2838 if ( ( state & RENDER_FILL ) != 0 ) {
2839 Winding_Draw( m_winding, m_plane.normal(), state );
2843 Winding_DrawWireframe( m_winding );
2845 // also draw a line indicating the direction of the cut
2846 Vector3 lineverts[2];
2847 Winding_Centroid( m_winding, m_plane, lineverts[0] );
2848 lineverts[1] = vector3_added( lineverts[0], vector3_scaled( m_plane.normal(), Brush::m_maxWorldCoord * 4 ) );
2850 glVertexPointer( 3, GL_FLOAT, sizeof( Vector3 ), &lineverts[0] );
2851 glDrawArrays( GL_LINES, 0, GLsizei( 2 ) );
2855 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
2856 renderer.SetState( m_state, Renderer::eWireframeOnly );
2857 renderer.SetState( m_state, Renderer::eFullMaterials );
2858 renderer.addRenderable( *this, localToWorld );
2862 inline void Face_addLight( const FaceInstance& face, const Matrix4& localToWorld, const RendererLight& light ){
2863 const Plane3& facePlane = face.getFace().plane3();
2864 const Vector3& origin = light.aabb().origin;
2865 Plane3 tmp( plane3_transformed( Plane3( facePlane.normal(), -facePlane.dist() ), localToWorld ) );
2866 if ( !plane3_test_point( tmp, origin )
2867 || !plane3_test_point( tmp, vector3_added( origin, light.offset() ) ) ) {
2868 face.m_lights.addLight( light );
2874 typedef std::vector<FaceInstance> FaceInstances;
2876 class EdgeInstance : public Selectable
2878 FaceInstances& m_faceInstances;
2879 SelectableEdge* m_edge;
2881 void select_edge( bool select ){
2882 FaceVertexId faceVertex = m_edge->m_faceVertex;
2883 m_faceInstances[faceVertex.getFace()].select_edge( faceVertex.getVertex(), select );
2884 faceVertex = next_edge( m_edge->m_faces, faceVertex );
2885 m_faceInstances[faceVertex.getFace()].select_edge( faceVertex.getVertex(), select );
2887 bool selected_edge() const {
2888 FaceVertexId faceVertex = m_edge->m_faceVertex;
2889 if ( !m_faceInstances[faceVertex.getFace()].selected_edge( faceVertex.getVertex() ) ) {
2892 faceVertex = next_edge( m_edge->m_faces, faceVertex );
2893 if ( !m_faceInstances[faceVertex.getFace()].selected_edge( faceVertex.getVertex() ) ) {
2901 EdgeInstance( FaceInstances& faceInstances, SelectableEdge& edge )
2902 : m_faceInstances( faceInstances ), m_edge( &edge ){
2904 EdgeInstance& operator=( const EdgeInstance& other ){
2905 m_edge = other.m_edge;
2909 void setSelected( bool select ){
2910 select_edge( select );
2912 bool isSelected() const {
2913 return selected_edge();
2917 void testSelect( Selector& selector, SelectionTest& test ){
2918 SelectionIntersection best;
2919 m_edge->testSelect( test, best );
2920 if ( best.valid() ) {
2921 Selector_add( selector, *this, best );
2926 class VertexInstance : public Selectable
2928 FaceInstances& m_faceInstances;
2929 SelectableVertex* m_vertex;
2931 void select_vertex( bool select ){
2932 FaceVertexId faceVertex = m_vertex->m_faceVertex;
2935 m_faceInstances[faceVertex.getFace()].select_vertex( faceVertex.getVertex(), select );
2936 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
2938 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
2940 bool selected_vertex() const {
2941 FaceVertexId faceVertex = m_vertex->m_faceVertex;
2944 if ( !m_faceInstances[faceVertex.getFace()].selected_vertex( faceVertex.getVertex() ) ) {
2947 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
2949 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
2954 VertexInstance( FaceInstances& faceInstances, SelectableVertex& vertex )
2955 : m_faceInstances( faceInstances ), m_vertex( &vertex ){
2957 VertexInstance& operator=( const VertexInstance& other ){
2958 m_vertex = other.m_vertex;
2962 void setSelected( bool select ){
2963 select_vertex( select );
2965 bool isSelected() const {
2966 return selected_vertex();
2969 void testSelect( Selector& selector, SelectionTest& test ){
2970 SelectionIntersection best;
2971 m_vertex->testSelect( test, best );
2972 if ( best.valid() ) {
2973 Selector_add( selector, *this, best );
2978 class BrushInstanceVisitor
2981 virtual void visit( FaceInstance& face ) const = 0;
2984 class BrushInstance :
2985 public BrushObserver,
2986 public scene::Instance,
2989 public SelectionTestable,
2990 public ComponentSelectionTestable,
2991 public ComponentEditable,
2992 public ComponentSnappable,
2993 public PlaneSelectable,
2994 public LightCullable
2998 InstanceTypeCastTable m_casts;
3001 InstanceStaticCast<BrushInstance, Selectable>::install( m_casts );
3002 InstanceContainedCast<BrushInstance, Bounded>::install( m_casts );
3003 InstanceContainedCast<BrushInstance, Cullable>::install( m_casts );
3004 InstanceStaticCast<BrushInstance, Renderable>::install( m_casts );
3005 InstanceStaticCast<BrushInstance, SelectionTestable>::install( m_casts );
3006 InstanceStaticCast<BrushInstance, ComponentSelectionTestable>::install( m_casts );
3007 InstanceStaticCast<BrushInstance, ComponentEditable>::install( m_casts );
3008 InstanceStaticCast<BrushInstance, ComponentSnappable>::install( m_casts );
3009 InstanceStaticCast<BrushInstance, PlaneSelectable>::install( m_casts );
3010 InstanceIdentityCast<BrushInstance>::install( m_casts );
3011 InstanceContainedCast<BrushInstance, Transformable>::install( m_casts );
3013 InstanceTypeCastTable& get(){
3021 FaceInstances m_faceInstances;
3023 typedef std::vector<EdgeInstance> EdgeInstances;
3024 EdgeInstances m_edgeInstances;
3025 typedef std::vector<VertexInstance> VertexInstances;
3026 VertexInstances m_vertexInstances;
3028 ObservedSelectable m_selectable;
3030 mutable RenderableWireframe m_render_wireframe;
3031 mutable RenderablePointVector m_render_selected;
3032 mutable AABB m_aabb_component;
3033 mutable Array<PointVertex> m_faceCentroidPointsCulled;
3034 RenderablePointArray m_render_faces_wireframe;
3035 mutable bool m_viewChanged; // requires re-evaluation of view-dependent cached data
3037 BrushClipPlane m_clipPlane;
3039 static Shader* m_state_selpoint;
3041 const LightList* m_lightList;
3043 TransformModifier m_transform;
3045 BrushInstance( const BrushInstance& other ); // NOT COPYABLE
3046 BrushInstance& operator=( const BrushInstance& other ); // NOT ASSIGNABLE
3048 static Counter* m_counter;
3050 typedef LazyStatic<TypeCasts> StaticTypeCasts;
3052 void lightsChanged(){
3053 m_lightList->lightsChanged();
3055 typedef MemberCaller<BrushInstance, void(), &BrushInstance::lightsChanged> LightsChangedCaller;
3057 STRING_CONSTANT( Name, "BrushInstance" );
3059 BrushInstance( const scene::Path& path, scene::Instance* parent, Brush& brush ) :
3060 Instance( path, parent, this, StaticTypeCasts::instance().get() ),
3062 m_selectable( SelectedChangedCaller( *this ) ),
3063 m_render_selected( GL_POINTS ),
3064 m_render_faces_wireframe( m_faceCentroidPointsCulled, GL_POINTS ),
3065 m_viewChanged( false ),
3066 m_transform( Brush::TransformChangedCaller( m_brush ), ApplyTransformCaller( *this ) ){
3067 m_brush.instanceAttach( Instance::path() );
3068 m_brush.attach( *this );
3069 m_counter->increment();
3071 m_lightList = &GlobalShaderCache().attach( *this );
3072 m_brush.m_lightsChanged = LightsChangedCaller( *this ); ///\todo Make this work with instancing.
3074 Instance::setTransformChangedCallback( LightsChangedCaller( *this ) );
3077 Instance::setTransformChangedCallback( Callback<void()>() );
3079 m_brush.m_lightsChanged = Callback<void()>();
3080 GlobalShaderCache().detach( *this );
3082 m_counter->decrement();
3083 m_brush.detach( *this );
3084 m_brush.instanceDetach( Instance::path() );
3090 const Brush& getBrush() const {
3094 Bounded& get( NullType<Bounded>){
3097 Cullable& get( NullType<Cullable>){
3100 Transformable& get( NullType<Transformable>){
3104 void selectedChanged( const Selectable& selectable ){
3105 GlobalSelectionSystem().getObserver ( SelectionSystem::ePrimitive )( selectable );
3106 GlobalSelectionSystem().onSelectedChanged( *this, selectable );
3108 Instance::selectedChanged();
3110 typedef MemberCaller<BrushInstance, void(const Selectable&), &BrushInstance::selectedChanged> SelectedChangedCaller;
3112 void selectedChangedComponent( const Selectable& selectable ){
3113 GlobalSelectionSystem().getObserver ( SelectionSystem::eComponent )( selectable );
3114 GlobalSelectionSystem().onComponentSelection( *this, selectable );
3116 typedef MemberCaller<BrushInstance, void(const Selectable&), &BrushInstance::selectedChangedComponent> SelectedChangedComponentCaller;
3118 const BrushInstanceVisitor& forEachFaceInstance( const BrushInstanceVisitor& visitor ){
3119 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3121 visitor.visit( *i );
3126 static void constructStatic(){
3127 m_state_selpoint = GlobalShaderCache().capture( "$SELPOINT" );
3129 static void destroyStatic(){
3130 GlobalShaderCache().release( "$SELPOINT" );
3134 m_faceInstances.clear();
3136 void reserve( std::size_t size ){
3137 m_faceInstances.reserve( size );
3140 void push_back( Face& face ){
3141 m_faceInstances.push_back( FaceInstance( face, SelectedChangedComponentCaller( *this ) ) );
3144 ASSERT_MESSAGE( !m_faceInstances.empty(), "erasing invalid element" );
3145 m_faceInstances.pop_back();
3147 void erase( std::size_t index ){
3148 ASSERT_MESSAGE( index < m_faceInstances.size(), "erasing invalid element" );
3149 m_faceInstances.erase( m_faceInstances.begin() + index );
3151 void connectivityChanged(){
3152 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3154 ( *i ).connectivityChanged();
3159 m_edgeInstances.clear();
3161 void edge_push_back( SelectableEdge& edge ){
3162 m_edgeInstances.push_back( EdgeInstance( m_faceInstances, edge ) );
3165 void vertex_clear(){
3166 m_vertexInstances.clear();
3168 void vertex_push_back( SelectableVertex& vertex ){
3169 m_vertexInstances.push_back( VertexInstance( m_faceInstances, vertex ) );
3172 void DEBUG_verify() const {
3173 ASSERT_MESSAGE( m_faceInstances.size() == m_brush.DEBUG_size(), "FATAL: mismatch" );
3176 bool isSelected() const {
3177 return m_selectable.isSelected();
3179 void setSelected( bool select ){
3180 m_selectable.setSelected( select );
3183 void update_selected() const {
3184 m_render_selected.clear();
3185 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3187 if ( ( *i ).getFace().contributes() ) {
3188 ( *i ).iterate_selected( m_render_selected );
3193 void evaluateViewDependent( const VolumeTest& volume, const Matrix4& localToWorld ) const {
3194 if ( m_viewChanged ) {
3195 m_viewChanged = false;
3197 bool faces_visible[c_brush_maxFaces];
3199 bool* j = faces_visible;
3200 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i, ++j )
3202 *j = ( *i ).intersectVolume( volume, localToWorld );
3206 m_brush.update_wireframe( m_render_wireframe, faces_visible );
3207 m_brush.update_faces_wireframe( m_faceCentroidPointsCulled, faces_visible );
3211 void renderComponentsSelected( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3212 m_brush.evaluateBRep();
3215 if ( !m_render_selected.empty() ) {
3216 renderer.Highlight( Renderer::ePrimitive, false );
3217 renderer.SetState( m_state_selpoint, Renderer::eWireframeOnly );
3218 renderer.SetState( m_state_selpoint, Renderer::eFullMaterials );
3219 renderer.addRenderable( m_render_selected, localToWorld );
3223 void renderComponents( Renderer& renderer, const VolumeTest& volume ) const {
3224 m_brush.evaluateBRep();
3226 const Matrix4& localToWorld = Instance::localToWorld();
3228 renderer.SetState( m_brush.m_state_point, Renderer::eWireframeOnly );
3229 renderer.SetState( m_brush.m_state_point, Renderer::eFullMaterials );
3231 if ( volume.fill() && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace ) {
3232 evaluateViewDependent( volume, localToWorld );
3233 renderer.addRenderable( m_render_faces_wireframe, localToWorld );
3237 m_brush.renderComponents( GlobalSelectionSystem().ComponentMode(), renderer, volume, localToWorld );
3241 void renderClipPlane( Renderer& renderer, const VolumeTest& volume ) const {
3242 if ( GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip && isSelected() ) {
3243 m_clipPlane.render( renderer, volume, localToWorld() );
3247 void renderCommon( Renderer& renderer, const VolumeTest& volume ) const {
3248 bool componentMode = GlobalSelectionSystem().Mode() == SelectionSystem::eComponent;
3250 if ( componentMode && isSelected() ) {
3251 renderComponents( renderer, volume );
3254 if ( parentSelected() ) {
3255 if ( !componentMode ) {
3256 renderer.Highlight( Renderer::eFace );
3258 renderer.Highlight( Renderer::ePrimitive );
3262 void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3263 //renderCommon(renderer, volume);
3265 m_lightList->evaluateLights();
3267 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3269 renderer.setLights( ( *i ).m_lights );
3270 ( *i ).render( renderer, volume, localToWorld );
3273 renderComponentsSelected( renderer, volume, localToWorld );
3276 void renderWireframe( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3277 //renderCommon(renderer, volume);
3279 evaluateViewDependent( volume, localToWorld );
3281 if ( m_render_wireframe.m_size != 0 ) {
3282 renderer.addRenderable( m_render_wireframe, localToWorld );
3285 renderComponentsSelected( renderer, volume, localToWorld );
3288 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3289 m_brush.evaluateBRep();
3291 renderClipPlane( renderer, volume );
3293 renderSolid( renderer, volume, localToWorld() );
3296 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3297 m_brush.evaluateBRep();
3299 renderClipPlane( renderer, volume );
3301 renderWireframe( renderer, volume, localToWorld() );
3304 void viewChanged() const {
3305 m_viewChanged = true;
3308 void testSelect( Selector& selector, SelectionTest& test ){
3309 test.BeginMesh( localToWorld() );
3311 SelectionIntersection best;
3312 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3314 ( *i ).testSelect( test, best );
3316 if ( best.valid() ) {
3317 selector.addIntersection( best );
3321 bool isSelectedComponents() const {
3322 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3324 if ( ( *i ).selectedComponents() ) {
3330 void setSelectedComponents( bool select, SelectionSystem::EComponentMode mode ){
3331 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3333 ( *i ).setSelected( mode, select );
3336 void testSelectComponents( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode ){
3337 test.BeginMesh( localToWorld() );
3341 case SelectionSystem::eVertex:
3343 for ( VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i )
3345 ( *i ).testSelect( selector, test );
3349 case SelectionSystem::eEdge:
3351 for ( EdgeInstances::iterator i = m_edgeInstances.begin(); i != m_edgeInstances.end(); ++i )
3353 ( *i ).testSelect( selector, test );
3357 case SelectionSystem::eFace:
3359 if ( test.getVolume().fill() ) {
3360 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3362 ( *i ).testSelect( selector, test );
3367 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3369 ( *i ).testSelect_centroid( selector, test );
3379 void selectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
3380 test.BeginMesh( localToWorld() );
3382 PlanePointer brushPlanes[c_brush_maxFaces];
3383 PlanesIterator j = brushPlanes;
3385 for ( Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i )
3387 *j++ = &( *i )->plane3();
3390 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3392 ( *i ).selectPlane( selector, Line( test.getNear(), test.getFar() ), brushPlanes, j, selectedPlaneCallback );
3395 void selectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes ){
3396 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3398 ( *i ).selectReversedPlane( selector, selectedPlanes );
3403 void transformComponents( const Matrix4& matrix ){
3404 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3406 ( *i ).transformComponents( matrix );
3409 const AABB& getSelectedComponentsBounds() const {
3410 m_aabb_component = AABB();
3412 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3414 ( *i ).iterate_selected( m_aabb_component );
3417 return m_aabb_component;
3420 void snapComponents( float snap ){
3421 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3423 ( *i ).snapComponents( snap );
3426 void evaluateTransform(){
3427 Matrix4 matrix( m_transform.calculateTransform() );
3428 //globalOutputStream() << "matrix: " << matrix << "\n";
3430 if ( m_transform.getType() == TRANSFORM_PRIMITIVE ) {
3431 m_brush.transform( matrix );
3435 transformComponents( matrix );
3438 void applyTransform(){
3439 m_brush.revertTransform();
3440 evaluateTransform();
3441 m_brush.freezeTransform();
3443 typedef MemberCaller<BrushInstance, void(), &BrushInstance::applyTransform> ApplyTransformCaller;
3445 void setClipPlane( const Plane3& plane ){
3446 m_clipPlane.setPlane( m_brush, plane );
3449 bool testLight( const RendererLight& light ) const {
3450 return light.testAABB( worldAABB() );
3452 void insertLight( const RendererLight& light ){
3453 const Matrix4& localToWorld = Instance::localToWorld();
3454 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3456 Face_addLight( *i, localToWorld, light );
3460 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3462 ( *i ).m_lights.clear();
3467 inline BrushInstance* Instance_getBrush( scene::Instance& instance ){
3468 return InstanceTypeCast<BrushInstance>::cast( instance );
3472 template<typename Functor>
3473 class BrushSelectedVisitor : public SelectionSystem::Visitor
3475 const Functor& m_functor;
3477 BrushSelectedVisitor( const Functor& functor ) : m_functor( functor ){
3479 void visit( scene::Instance& instance ) const {
3480 BrushInstance* brush = Instance_getBrush( instance );
3482 m_functor( *brush );
3487 template<typename Functor>
3488 inline const Functor& Scene_forEachSelectedBrush( const Functor& functor ){
3489 GlobalSelectionSystem().foreachSelected( BrushSelectedVisitor<Functor>( functor ) );
3493 template<typename Functor>
3494 class BrushVisibleSelectedVisitor : public SelectionSystem::Visitor
3496 const Functor& m_functor;
3498 BrushVisibleSelectedVisitor( const Functor& functor ) : m_functor( functor ){
3500 void visit( scene::Instance& instance ) const {
3501 BrushInstance* brush = Instance_getBrush( instance );
3503 && instance.path().top().get().visible() ) {
3504 m_functor( *brush );
3509 template<typename Functor>
3510 inline const Functor& Scene_forEachVisibleSelectedBrush( const Functor& functor ){
3511 GlobalSelectionSystem().foreachSelected( BrushVisibleSelectedVisitor<Functor>( functor ) );
3515 class BrushForEachFace
3517 const BrushInstanceVisitor& m_visitor;
3519 BrushForEachFace( const BrushInstanceVisitor& visitor ) : m_visitor( visitor ){
3521 void operator()( BrushInstance& brush ) const {
3522 brush.forEachFaceInstance( m_visitor );
3526 template<class Functor>
3527 class FaceInstanceVisitFace : public BrushInstanceVisitor
3529 const Functor& functor;
3531 FaceInstanceVisitFace( const Functor& functor )
3532 : functor( functor ){
3534 void visit( FaceInstance& face ) const {
3535 functor( face.getFace() );
3539 template<typename Functor>
3540 inline const Functor& Brush_forEachFace( BrushInstance& brush, const Functor& functor ){
3541 brush.forEachFaceInstance( FaceInstanceVisitFace<Functor>( functor ) );
3545 template<class Functor>
3546 class FaceVisitAll : public BrushVisitor
3548 const Functor& functor;
3550 FaceVisitAll( const Functor& functor )
3551 : functor( functor ){
3553 void visit( Face& face ) const {
3558 template<typename Functor>
3559 inline const Functor& Brush_forEachFace( const Brush& brush, const Functor& functor ){
3560 brush.forEachFace( FaceVisitAll<Functor>( functor ) );
3564 template<typename Functor>
3565 inline const Functor& Brush_forEachFace( Brush& brush, const Functor& functor ){
3566 brush.forEachFace( FaceVisitAll<Functor>( functor ) );
3570 template<class Functor>
3571 class FaceInstanceVisitAll : public BrushInstanceVisitor
3573 const Functor& functor;
3575 FaceInstanceVisitAll( const Functor& functor )
3576 : functor( functor ){
3578 void visit( FaceInstance& face ) const {
3583 template<typename Functor>
3584 inline const Functor& Brush_ForEachFaceInstance( BrushInstance& brush, const Functor& functor ){
3585 brush.forEachFaceInstance( FaceInstanceVisitAll<Functor>( functor ) );
3589 template<typename Functor>
3590 inline const Functor& Scene_forEachBrush( scene::Graph& graph, const Functor& functor ){
3591 graph.traverse( InstanceWalker< InstanceApply<BrushInstance, Functor> >( functor ) );
3595 template<typename Type, typename Functor>
3596 class InstanceIfVisible : public Functor
3599 InstanceIfVisible( const Functor& functor ) : Functor( functor ){
3601 void operator()( scene::Instance& instance ){
3602 if ( instance.path().top().get().visible() ) {
3603 Functor::operator()( instance );
3608 template<typename Functor>
3609 class BrushVisibleWalker : public scene::Graph::Walker
3611 const Functor& m_functor;
3613 BrushVisibleWalker( const Functor& functor ) : m_functor( functor ){
3615 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3616 if ( path.top().get().visible() ) {
3617 BrushInstance* brush = Instance_getBrush( instance );
3619 m_functor( *brush );
3626 template<typename Functor>
3627 inline const Functor& Scene_forEachVisibleBrush( scene::Graph& graph, const Functor& functor ){
3628 graph.traverse( BrushVisibleWalker<Functor>( functor ) );
3632 template<typename Functor>
3633 inline const Functor& Scene_ForEachBrush_ForEachFace( scene::Graph& graph, const Functor& functor ){
3634 Scene_forEachBrush( graph, BrushForEachFace( FaceInstanceVisitFace<Functor>( functor ) ) );
3639 template<typename Functor>
3640 inline const Functor& Scene_ForEachBrush_ForEachFaceInstance( scene::Graph& graph, const Functor& functor ){
3641 Scene_forEachBrush( graph, BrushForEachFace( FaceInstanceVisitAll<Functor>( functor ) ) );
3645 template<typename Functor>
3646 inline const Functor& Scene_ForEachSelectedBrush_ForEachFace( scene::Graph& graph, const Functor& functor ){
3647 Scene_forEachSelectedBrush( BrushForEachFace( FaceInstanceVisitFace<Functor>( functor ) ) );
3651 template<typename Functor>
3652 inline const Functor& Scene_ForEachSelectedBrush_ForEachFaceInstance( scene::Graph& graph, const Functor& functor ){
3653 Scene_forEachSelectedBrush( BrushForEachFace( FaceInstanceVisitAll<Functor>( functor ) ) );
3657 template<typename Functor>
3658 class FaceVisitorWrapper
3660 const Functor& functor;
3662 FaceVisitorWrapper( const Functor& functor ) : functor( functor ){
3665 void operator()( FaceInstance& faceInstance ) const {
3666 functor( faceInstance.getFace() );
3670 template<typename Functor>
3671 inline const Functor& Scene_ForEachSelectedBrushFace( scene::Graph& graph, const Functor& functor ){
3672 g_SelectedFaceInstances.foreach( FaceVisitorWrapper<Functor>( functor ) );