2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_BRUSH_H )
23 #define INCLUDED_BRUSH_H
26 /// \brief The brush primitive.
28 /// A collection of planes that define a convex polyhedron.
29 /// The Boundary-Representation of this primitive is a manifold polygonal mesh.
30 /// Each face polygon is represented by a list of vertices in a \c Winding.
31 /// Each vertex is associated with another face that is adjacent to the edge
32 /// formed by itself and the next vertex in the winding. This information can
33 /// be used to find edge-pairs and vertex-rings.
36 #include "debugging/debugging.h"
40 #include "iselection.h"
47 #include "moduleobserver.h"
52 #include "renderable.h"
53 #include "selectable.h"
57 #include "math/frustum.h"
58 #include "selectionlib.h"
60 #include "texturelib.h"
61 #include "container/container.h"
62 #include "generic/bitfield.h"
63 #include "signal/signalfwd.h"
66 #include "brush_primit.h"
68 const unsigned int BRUSH_DETAIL_FLAG = 27;
69 const unsigned int BRUSH_DETAIL_MASK = ( 1 << BRUSH_DETAIL_FLAG );
83 #define BRUSH_CONNECTIVITY_DEBUG 0
84 #define BRUSH_DEGENERATE_DEBUG 0
86 template<typename TextOuputStreamType>
87 inline TextOuputStreamType& ostream_write( TextOuputStreamType& ostream, const Matrix4& m ){
88 return ostream << "(" << m[0] << " " << m[1] << " " << m[2] << " " << m[3] << ", "
89 << m[4] << " " << m[5] << " " << m[6] << " " << m[7] << ", "
90 << m[8] << " " << m[9] << " " << m[10] << " " << m[11] << ", "
91 << m[12] << " " << m[13] << " " << m[14] << " " << m[15] << ")";
94 inline void print_vector3( const Vector3& v ){
95 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )\n";
98 inline void print_3x3( const Matrix4& m ){
99 globalOutputStream() << "( " << m.xx() << " " << m.xy() << " " << m.xz() << " ) "
100 << "( " << m.yx() << " " << m.yy() << " " << m.yz() << " ) "
101 << "( " << m.zx() << " " << m.zy() << " " << m.zz() << " )\n";
105 inline bool texdef_sane( const texdef_t& texdef ){
106 return fabs( texdef.shift[0] ) < ( 1 << 16 )
107 && fabs( texdef.shift[1] ) < ( 1 << 16 );
110 inline void Winding_DrawWireframe( const Winding& winding ){
111 glVertexPointer( 3, GL_FLOAT, sizeof( WindingVertex ), &winding.points.data()->vertex );
112 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( winding.numpoints ) );
115 inline void Winding_Draw( const Winding& winding, const Vector3& normal, RenderStateFlags state ){
116 glVertexPointer( 3, GL_FLOAT, sizeof( WindingVertex ), &winding.points.data()->vertex );
118 if ( ( state & RENDER_BUMP ) != 0 ) {
119 Vector3 normals[c_brush_maxFaces];
120 typedef Vector3* Vector3Iter;
121 for ( Vector3Iter i = normals, end = normals + winding.numpoints; i != end; ++i )
125 if ( GlobalShaderCache().useShaderLanguage() ) {
126 glNormalPointer( GL_FLOAT, sizeof( Vector3 ), normals );
127 glVertexAttribPointerARB( c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->texcoord );
128 glVertexAttribPointerARB( c_attr_Tangent, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->tangent );
129 glVertexAttribPointerARB( c_attr_Binormal, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->bitangent );
133 glVertexAttribPointerARB( 11, 3, GL_FLOAT, 0, sizeof( Vector3 ), normals );
134 glVertexAttribPointerARB( 8, 2, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->texcoord );
135 glVertexAttribPointerARB( 9, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->tangent );
136 glVertexAttribPointerARB( 10, 3, GL_FLOAT, 0, sizeof( WindingVertex ), &winding.points.data()->bitangent );
141 if ( state & RENDER_LIGHTING ) {
142 Vector3 normals[c_brush_maxFaces];
143 typedef Vector3* Vector3Iter;
144 for ( Vector3Iter i = normals, last = normals + winding.numpoints; i != last; ++i )
148 glNormalPointer( GL_FLOAT, sizeof( Vector3 ), normals );
151 if ( state & RENDER_TEXTURE ) {
152 glTexCoordPointer( 2, GL_FLOAT, sizeof( WindingVertex ), &winding.points.data()->texcoord );
156 if ( state & RENDER_FILL ) {
157 glDrawArrays( GL_TRIANGLE_FAN, 0, GLsizei( winding.numpoints ) );
161 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( winding.numpoints ) );
164 glDrawArrays( GL_POLYGON, 0, GLsizei( winding.numpoints ) );
168 const Winding& winding = winding;
170 if ( state & RENDER_FILL ) {
171 glBegin( GL_POLYGON );
175 glBegin( GL_LINE_LOOP );
178 if ( state & RENDER_LIGHTING ) {
179 glNormal3fv( normal );
182 for ( int i = 0; i < winding.numpoints; ++i )
184 if ( state & RENDER_TEXTURE ) {
185 glTexCoord2fv( &winding.points[i][3] );
187 glVertex3fv( winding.points[i] );
194 #include "shaderlib.h"
196 typedef DoubleVector3 PlanePoints[3];
198 inline bool planepts_equal( const PlanePoints planepts, const PlanePoints other ){
199 return planepts[0] == other[0] && planepts[1] == other[1] && planepts[2] == other[2];
202 inline void planepts_assign( PlanePoints planepts, const PlanePoints other ){
203 planepts[0] = other[0];
204 planepts[1] = other[1];
205 planepts[2] = other[2];
208 inline void planepts_quantise( PlanePoints planepts, double snap ){
209 vector3_snap( planepts[0], snap );
210 vector3_snap( planepts[1], snap );
211 vector3_snap( planepts[2], snap );
214 inline float vector3_max_component( const Vector3& vec3 ){
215 return std::max( fabsf( vec3[0] ), std::max( fabsf( vec3[1] ), fabsf( vec3[2] ) ) );
218 inline void edge_snap( Vector3& edge, double snap ){
219 float scale = static_cast<float>( ceil( fabs( snap / vector3_max_component( edge ) ) ) );
220 if ( scale > 0.0f ) {
221 vector3_scale( edge, scale );
223 vector3_snap( edge, snap );
226 inline void planepts_snap( PlanePoints planepts, double snap ){
227 Vector3 edge01( vector3_subtracted( planepts[1], planepts[0] ) );
228 Vector3 edge12( vector3_subtracted( planepts[2], planepts[1] ) );
229 Vector3 edge20( vector3_subtracted( planepts[0], planepts[2] ) );
231 double length_squared_01 = vector3_dot( edge01, edge01 );
232 double length_squared_12 = vector3_dot( edge12, edge12 );
233 double length_squared_20 = vector3_dot( edge20, edge20 );
235 vector3_snap( planepts[0], snap );
237 if ( length_squared_01 < length_squared_12 ) {
238 if ( length_squared_12 < length_squared_20 ) {
239 edge_snap( edge01, snap );
240 edge_snap( edge12, snap );
241 planepts[1] = vector3_added( planepts[0], edge01 );
242 planepts[2] = vector3_added( planepts[1], edge12 );
246 edge_snap( edge20, snap );
247 edge_snap( edge01, snap );
248 planepts[1] = vector3_added( planepts[0], edge20 );
249 planepts[2] = vector3_added( planepts[1], edge01 );
254 if ( length_squared_01 < length_squared_20 ) {
255 edge_snap( edge01, snap );
256 edge_snap( edge12, snap );
257 planepts[1] = vector3_added( planepts[0], edge01 );
258 planepts[2] = vector3_added( planepts[1], edge12 );
262 edge_snap( edge12, snap );
263 edge_snap( edge20, snap );
264 planepts[1] = vector3_added( planepts[0], edge12 );
265 planepts[2] = vector3_added( planepts[1], edge20 );
270 inline PointVertex pointvertex_for_planept( const DoubleVector3& point, const Colour4b& colour ){
273 static_cast<float>( point.x() ),
274 static_cast<float>( point.y() ),
275 static_cast<float>( point.z() )
281 inline PointVertex pointvertex_for_windingpoint( const Vector3& point, const Colour4b& colour ){
283 vertex3f_for_vector3( point ),
288 inline bool check_plane_is_integer( const PlanePoints& planePoints ){
289 return !float_is_integer( planePoints[0][0] )
290 || !float_is_integer( planePoints[0][1] )
291 || !float_is_integer( planePoints[0][2] )
292 || !float_is_integer( planePoints[1][0] )
293 || !float_is_integer( planePoints[1][1] )
294 || !float_is_integer( planePoints[1][2] )
295 || !float_is_integer( planePoints[2][0] )
296 || !float_is_integer( planePoints[2][1] )
297 || !float_is_integer( planePoints[2][2] );
300 inline void brush_check_shader( const char* name ){
301 if ( !shader_valid( name ) ) {
302 globalErrorStream() << "brush face has invalid texture name: '" << name << "'\n";
306 class FaceShaderObserver
309 virtual void realiseShader() = 0;
310 virtual void unrealiseShader() = 0;
313 typedef ReferencePair<FaceShaderObserver> FaceShaderObserverPair;
316 class ContentsFlagsValue
319 ContentsFlagsValue(){
322 ContentsFlagsValue( int surfaceFlags, int contentFlags, int value, bool specified ) :
323 m_surfaceFlags( surfaceFlags ),
324 m_contentFlags( contentFlags ),
326 m_specified( specified ){
335 inline void ContentsFlagsValue_assignMasked( ContentsFlagsValue& flags, const ContentsFlagsValue& other ){
336 bool detail = bitfield_enabled( flags.m_contentFlags, BRUSH_DETAIL_MASK );
339 flags.m_contentFlags = bitfield_enable( flags.m_contentFlags, BRUSH_DETAIL_MASK );
343 flags.m_contentFlags = bitfield_disable( flags.m_contentFlags, BRUSH_DETAIL_MASK );
348 class FaceShader : public ModuleObserver
354 CopiedString m_shader;
355 ContentsFlagsValue m_flags;
357 SavedState( const FaceShader& faceShader ){
358 m_shader = faceShader.getShader();
359 m_flags = faceShader.m_flags;
362 void exportState( FaceShader& faceShader ) const {
363 faceShader.setShader( m_shader.c_str() );
364 //faceShader.setFlags( m_flags );
365 faceShader.m_flags = m_flags;
369 CopiedString m_shader;
371 ContentsFlagsValue m_flags;
372 FaceShaderObserverPair m_observers;
376 FaceShader( const char* shader, const ContentsFlagsValue& flags = ContentsFlagsValue( 0, 0, 0, false ) ) :
380 m_instanced( false ),
389 // copy-construction not supported
390 FaceShader( const FaceShader& other );
392 void instanceAttach(){
394 m_state->incrementUsed();
397 void instanceDetach(){
398 m_state->decrementUsed();
402 void captureShader(){
403 ASSERT_MESSAGE( m_state == 0, "shader cannot be captured" );
404 brush_check_shader( m_shader.c_str() );
405 m_state = GlobalShaderCache().capture( m_shader.c_str() );
406 m_state->attach( *this );
409 void releaseShader(){
410 ASSERT_MESSAGE( m_state != 0, "shader cannot be released" );
411 m_state->detach( *this );
412 GlobalShaderCache().release( m_shader.c_str() );
417 ASSERT_MESSAGE( !m_realised, "FaceTexdef::realise: already realised" );
419 m_observers.forEach([](FaceShaderObserver &observer) {
420 observer.realiseShader();
425 ASSERT_MESSAGE( m_realised, "FaceTexdef::unrealise: already unrealised" );
426 m_observers.forEach([](FaceShaderObserver &observer) {
427 observer.unrealiseShader();
432 void attach( FaceShaderObserver& observer ){
433 m_observers.attach( observer );
435 observer.realiseShader();
439 void detach( FaceShaderObserver& observer ){
441 observer.unrealiseShader();
443 m_observers.detach( observer );
446 const char* getShader() const {
447 return m_shader.c_str();
449 void setShader( const char* name ){
451 m_state->decrementUsed();
457 m_state->incrementUsed();
461 ContentsFlagsValue getFlags() const {
462 ASSERT_MESSAGE( m_realised, "FaceShader::getFlags: flags not valid when unrealised" );
463 if ( !m_flags.m_specified ) {
464 return ContentsFlagsValue(
465 m_state->getTexture().surfaceFlags,
466 m_state->getTexture().contentFlags,
467 m_state->getTexture().value,
474 void setFlags( const ContentsFlagsValue& flags ){
475 ASSERT_MESSAGE( m_realised, "FaceShader::setFlags: flags not valid when unrealised" );
476 ContentsFlagsValue_assignMasked( m_flags, flags );
479 Shader* state() const {
483 std::size_t width() const {
485 return m_state->getTexture().width;
490 std::size_t height() const {
492 return m_state->getTexture().height;
497 unsigned int shaderFlags() const {
499 return m_state->getFlags();
506 class FaceTexdef : public FaceShaderObserver
509 FaceTexdef( const FaceTexdef& other );
512 FaceTexdef& operator=( const FaceTexdef& other );
518 TextureProjection m_projection;
520 SavedState( const FaceTexdef& faceTexdef ){
521 m_projection = faceTexdef.m_projection;
524 void exportState( FaceTexdef& faceTexdef ) const {
525 Texdef_Assign( faceTexdef.m_projection, m_projection );
529 FaceShader& m_shader;
530 TextureProjection m_projection;
531 bool m_projectionInitialised;
536 const TextureProjection& projection,
537 bool projectionInitialised = true
540 m_projection( projection ),
541 m_projectionInitialised( projectionInitialised ),
542 m_scaleApplied( false ){
543 m_shader.attach( *this );
547 m_shader.detach( *this );
551 ASSERT_MESSAGE( !m_scaleApplied, "texture scale aready added" );
552 m_scaleApplied = true;
553 m_projection.m_brushprimit_texdef.addScale( m_shader.width(), m_shader.height() );
557 ASSERT_MESSAGE( m_scaleApplied, "texture scale aready removed" );
558 m_scaleApplied = false;
559 m_projection.m_brushprimit_texdef.removeScale( m_shader.width(), m_shader.height() );
562 void realiseShader(){
563 if ( m_projectionInitialised && !m_scaleApplied ) {
568 void unrealiseShader(){
569 if ( m_projectionInitialised && m_scaleApplied ) {
574 void setTexdef( const TextureProjection& projection ){
576 Texdef_Assign( m_projection, projection );
580 void shift( float s, float t ){
581 ASSERT_MESSAGE( texdef_sane( m_projection.m_texdef ), "FaceTexdef::shift: bad texdef" );
583 Texdef_Shift( m_projection, s, t );
587 void scale( float s, float t ){
589 Texdef_Scale( m_projection, s, t );
593 void rotate( float angle ){
595 Texdef_Rotate( m_projection, angle );
599 void fit( const Vector3& normal, const Winding& winding, float s_repeat, float t_repeat ){
600 Texdef_FitTexture( m_projection, m_shader.width(), m_shader.height(), normal, winding, s_repeat, t_repeat );
603 void emitTextureCoordinates( Winding& winding, const Vector3& normal, const Matrix4& localToWorld ){
604 Texdef_EmitTextureCoordinates( m_projection, m_shader.width(), m_shader.height(), winding, normal, localToWorld );
607 void transform( const Plane3& plane, const Matrix4& matrix ){
609 Texdef_transformLocked( m_projection, m_shader.width(), m_shader.height(), plane, matrix );
613 TextureProjection normalised() const {
614 brushprimit_texdef_t tmp( m_projection.m_brushprimit_texdef );
615 tmp.removeScale( m_shader.width(), m_shader.height() );
616 return TextureProjection( m_projection.m_texdef, tmp, m_projection.m_basis_s, m_projection.m_basis_t );
619 void setBasis( const Vector3& normal ){
621 Normal_GetTransform( normal, basis );
622 m_projection.m_basis_s = Vector3( basis.xx(), basis.yx(), basis.zx() );
623 m_projection.m_basis_t = Vector3( -basis.xy(), -basis.yy(), -basis.zy() );
627 inline void planepts_print( const PlanePoints& planePoints, TextOutputStream& ostream ){
628 ostream << "( " << planePoints[0][0] << " " << planePoints[0][1] << " " << planePoints[0][2] << " ) "
629 << "( " << planePoints[1][0] << " " << planePoints[1][1] << " " << planePoints[1][2] << " ) "
630 << "( " << planePoints[2][0] << " " << planePoints[2][1] << " " << planePoints[2][2] << " )";
634 inline Plane3 Plane3_applyTranslation( const Plane3& plane, const Vector3& translation ){
635 Plane3 tmp( plane3_translated( Plane3( plane.normal(), -plane.dist() ), translation ) );
636 return Plane3( tmp.normal(), -tmp.dist() );
639 inline Plane3 Plane3_applyTransform( const Plane3& plane, const Matrix4& matrix ){
640 Plane3 tmp( plane3_transformed( Plane3( plane.normal(), -plane.dist() ), matrix ) );
641 return Plane3( tmp.normal(), -tmp.dist() );
646 PlanePoints m_planepts;
647 Plane3 m_planeCached;
650 Vector3 m_funcStaticOrigin;
652 static EBrushType m_type;
654 static bool isDoom3Plane(){
655 return FacePlane::m_type == eBrushTypeDoom3 || FacePlane::m_type == eBrushTypeQuake4;
658 FacePlane& operator=(const FacePlane&) = default;
663 PlanePoints m_planepts;
666 SavedState( const FacePlane& facePlane ){
667 if ( facePlane.isDoom3Plane() ) {
668 m_plane = facePlane.m_plane;
672 planepts_assign( m_planepts, facePlane.planePoints() );
676 void exportState( FacePlane& facePlane ) const {
677 if ( facePlane.isDoom3Plane() ) {
678 facePlane.m_plane = m_plane;
679 facePlane.updateTranslated();
683 planepts_assign( facePlane.planePoints(), m_planepts );
684 facePlane.MakePlane();
689 FacePlane() : m_funcStaticOrigin( 0, 0, 0 ){
692 FacePlane( const FacePlane& other ) : m_funcStaticOrigin( 0, 0, 0 ){
693 if ( !isDoom3Plane() ) {
694 planepts_assign( m_planepts, other.m_planepts );
699 m_plane = other.m_plane;
705 if ( !isDoom3Plane() ) {
707 if ( check_plane_is_integer( m_planepts ) ) {
708 globalErrorStream() << "non-integer planepts: ";
709 planepts_print( m_planepts, globalErrorStream() );
710 globalErrorStream() << "\n";
713 m_planeCached = plane3_for_points( m_planepts );
718 if ( !isDoom3Plane() ) {
719 vector3_swap( m_planepts[0], m_planepts[2] );
724 m_planeCached = plane3_flipped( m_plane );
729 void transform( const Matrix4& matrix, bool mirror ){
730 if ( !isDoom3Plane() ) {
733 bool off = check_plane_is_integer( planePoints() );
736 matrix4_transform_point( matrix, m_planepts[0] );
737 matrix4_transform_point( matrix, m_planepts[1] );
738 matrix4_transform_point( matrix, m_planepts[2] );
745 if ( check_plane_is_integer( planePoints() ) ) {
747 globalErrorStream() << "caused by transform\n";
755 m_planeCached = Plane3_applyTransform( m_planeCached, matrix );
760 void offset( float offset ){
761 if ( !isDoom3Plane() ) {
762 Vector3 move( vector3_scaled( m_planeCached.normal(), -offset ) );
764 vector3_subtract( m_planepts[0], move );
765 vector3_subtract( m_planepts[1], move );
766 vector3_subtract( m_planepts[2], move );
772 m_planeCached.d += offset;
777 void updateTranslated(){
778 m_planeCached = Plane3_applyTranslation( m_plane, m_funcStaticOrigin );
782 m_plane = Plane3_applyTranslation( m_planeCached, vector3_negated( m_funcStaticOrigin ) );
786 PlanePoints& planePoints(){
790 const PlanePoints& planePoints() const {
794 const Plane3& plane3() const {
795 return m_planeCached;
798 void setDoom3Plane( const Plane3& plane ){
803 const Plane3& getDoom3Plane() const {
807 void copy( const FacePlane& other ){
808 if ( !isDoom3Plane() ) {
809 planepts_assign( m_planepts, other.m_planepts );
814 m_planeCached = other.m_plane;
819 void copy( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
820 if ( !isDoom3Plane() ) {
828 m_planeCached = plane3_for_points( p2, p1, p0 );
834 inline void Winding_testSelect( Winding& winding, SelectionTest& test, SelectionIntersection& best ){
835 test.TestPolygon( VertexPointer( reinterpret_cast<VertexPointer::pointer>( &winding.points.data()->vertex ), sizeof( WindingVertex ) ), winding.numpoints, best );
838 const double GRID_MIN = 0.125;
840 inline double quantiseInteger( double f ){
841 return float_to_integer( f );
844 inline double quantiseFloating( double f ){
845 return float_snapped( f, 1.f / ( 1 << 16 ) );
848 typedef double ( *QuantiseFunc )( double f );
855 virtual bool filter( const Face& face ) const = 0;
858 bool face_filtered( Face& face );
860 void add_face_filter( FaceFilter& filter, int mask, bool invert = false );
862 void Brush_addTextureChangedCallback( const SignalHandler& callback );
864 void Brush_textureChanged();
867 extern bool g_brush_texturelock_enabled;
872 virtual void planeChanged() = 0;
873 virtual void connectivityChanged() = 0;
874 virtual void shaderChanged() = 0;
875 virtual void evaluateTransform() = 0;
879 public OpenGLRenderable,
882 public FaceShaderObserver
884 std::size_t m_refcount;
886 class SavedState : public UndoMemento
889 FacePlane::SavedState m_planeState;
890 FaceTexdef::SavedState m_texdefState;
891 FaceShader::SavedState m_shaderState;
893 SavedState( const Face& face ) : m_planeState( face.getPlane() ), m_texdefState( face.getTexdef() ), m_shaderState( face.getShader() ){
896 void exportState( Face& face ) const {
897 m_planeState.exportState( face.getPlane() );
898 m_shaderState.exportState( face.getShader() );
899 m_texdefState.exportState( face.getTexdef() );
908 static QuantiseFunc m_quantise;
909 static EBrushType m_type;
911 PlanePoints m_move_planepts;
912 PlanePoints m_move_planeptsTransformed;
915 FacePlane m_planeTransformed;
918 TextureProjection m_texdefTransformed;
924 FaceObserver* m_observer;
925 UndoObserver* m_undoable_observer;
928 // assignment not supported
929 Face& operator=( const Face& other );
931 // copy-construction not supported
932 Face( const Face& other );
936 Face( FaceObserver* observer ) :
938 m_shader( texdef_name_default() ),
939 m_texdef( m_shader, TextureProjection(), false ),
941 m_observer( observer ),
942 m_undoable_observer( 0 ),
944 m_shader.attach( *this );
945 m_plane.copy( Vector3( 0, 0, 0 ), Vector3( 64, 0, 0 ), Vector3( 0, 64, 0 ) );
946 m_texdef.setBasis( m_plane.plane3().normal() );
955 const TextureProjection& projection,
956 FaceObserver* observer
960 m_texdef( m_shader, projection ),
961 m_observer( observer ),
962 m_undoable_observer( 0 ),
964 m_shader.attach( *this );
965 m_plane.copy( p0, p1, p2 );
966 m_texdef.setBasis( m_plane.plane3().normal() );
971 Face( const Face& other, FaceObserver* observer ) :
973 m_shader( other.m_shader.getShader(), other.m_shader.m_flags ),
974 m_texdef( m_shader, other.getTexdef().normalised() ),
975 m_observer( observer ),
976 m_undoable_observer( 0 ),
978 m_shader.attach( *this );
979 m_plane.copy( other.m_plane );
980 planepts_assign( m_move_planepts, other.m_move_planepts );
981 m_texdef.setBasis( m_plane.plane3().normal() );
987 m_shader.detach( *this );
992 m_observer->planeChanged();
995 void realiseShader(){
996 m_observer->shaderChanged();
999 void unrealiseShader(){
1002 void instanceAttach( MapFile* map ){
1003 m_shader.instanceAttach();
1005 m_undoable_observer = GlobalUndoSystem().observer( this );
1006 GlobalFilterSystem().registerFilterable( *this );
1008 void instanceDetach( MapFile* map ){
1009 GlobalFilterSystem().unregisterFilterable( *this );
1010 m_undoable_observer = 0;
1011 GlobalUndoSystem().release( this );
1013 m_shader.instanceDetach();
1016 void render( RenderStateFlags state ) const {
1017 Winding_Draw( m_winding, m_planeTransformed.plane3().normal(), state );
1020 void updateFiltered(){
1021 m_filtered = face_filtered( *this );
1024 bool isFiltered() const {
1032 if ( m_undoable_observer != 0 ) {
1033 m_undoable_observer->save( this );
1038 UndoMemento* exportState() const {
1039 return new SavedState( *this );
1042 void importState( const UndoMemento* data ){
1045 static_cast<const SavedState*>( data )->exportState( *this );
1048 m_observer->connectivityChanged();
1050 m_observer->shaderChanged();
1059 if ( --m_refcount == 0 ) {
1069 bool intersectVolume( const VolumeTest& volume, const Matrix4& localToWorld ) const {
1070 return volume.TestPlane( Plane3( plane3().normal(), -plane3().dist() ), localToWorld );
1073 void render( Renderer& renderer, const Matrix4& localToWorld ) const {
1074 renderer.SetState( m_shader.state(), Renderer::eFullMaterials );
1075 renderer.addRenderable( *this, localToWorld );
1078 void transform( const Matrix4& matrix, bool mirror ){
1079 if ( g_brush_texturelock_enabled ) {
1080 Texdef_transformLocked( m_texdefTransformed, m_shader.width(), m_shader.height(), m_plane.plane3(), matrix );
1083 m_planeTransformed.transform( matrix, mirror );
1086 ASSERT_MESSAGE( projectionaxis_for_normal( normal ) == projectionaxis_for_normal( plane3().normal() ), "bleh" );
1088 m_observer->planeChanged();
1090 if ( g_brush_texturelock_enabled ) {
1091 Brush_textureChanged();
1095 void assign_planepts( const PlanePoints planepts ){
1096 m_planeTransformed.copy( planepts[0], planepts[1], planepts[2] );
1097 m_observer->planeChanged();
1100 /// \brief Reverts the transformable state of the brush to identity.
1101 void revertTransform(){
1102 m_planeTransformed = m_plane;
1103 planepts_assign( m_move_planeptsTransformed, m_move_planepts );
1104 m_texdefTransformed = m_texdef.m_projection;
1107 void freezeTransform(){
1109 m_plane = m_planeTransformed;
1110 planepts_assign( m_move_planepts, m_move_planeptsTransformed );
1111 m_texdef.m_projection = m_texdefTransformed;
1114 void update_move_planepts_vertex( std::size_t index, PlanePoints planePoints ){
1115 std::size_t numpoints = getWinding().numpoints;
1116 ASSERT_MESSAGE( index < numpoints, "update_move_planepts_vertex: invalid index" );
1118 std::size_t opposite = Winding_Opposite( getWinding(), index );
1119 std::size_t adjacent = Winding_wrap( getWinding(), opposite + numpoints - 1 );
1120 planePoints[0] = getWinding()[opposite].vertex;
1121 planePoints[1] = getWinding()[index].vertex;
1122 planePoints[2] = getWinding()[adjacent].vertex;
1123 // winding points are very inaccurate, so they must be quantised before using them to generate the face-plane
1124 planepts_quantise( planePoints, GRID_MIN );
1127 void snapto( float snap ){
1128 if ( contributes() ) {
1130 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane before snap to grid" );
1131 planepts_snap( m_plane.planePoints(), snap );
1132 ASSERT_MESSAGE( plane3_valid( m_plane.plane3() ), "invalid plane after snap to grid" );
1134 PlanePoints planePoints;
1135 update_move_planepts_vertex( 0, planePoints );
1136 vector3_snap( planePoints[0], snap );
1137 vector3_snap( planePoints[1], snap );
1138 vector3_snap( planePoints[2], snap );
1139 assign_planepts( planePoints );
1142 SceneChangeNotify();
1143 if ( !plane3_valid( m_plane.plane3() ) ) {
1144 globalErrorStream() << "WARNING: invalid plane after snap to grid\n";
1149 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1150 Winding_testSelect( m_winding, test, best );
1153 void testSelect_centroid( SelectionTest& test, SelectionIntersection& best ){
1154 test.TestPoint( m_centroid, best );
1157 void shaderChanged(){
1158 EmitTextureCoordinates();
1159 Brush_textureChanged();
1160 m_observer->shaderChanged();
1163 SceneChangeNotify();
1166 const char* GetShader() const {
1167 return m_shader.getShader();
1170 void SetShader( const char* name ){
1172 m_shader.setShader( name );
1176 void revertTexdef(){
1177 m_texdefTransformed = m_texdef.m_projection;
1180 void texdefChanged(){
1182 EmitTextureCoordinates();
1183 Brush_textureChanged();
1186 void GetTexdef( TextureProjection& projection ) const {
1187 projection = m_texdef.normalised();
1190 void SetTexdef( const TextureProjection& projection ){
1192 m_texdef.setTexdef( projection );
1196 void GetFlags( ContentsFlagsValue& flags ) const {
1197 flags = m_shader.getFlags();
1200 void SetFlags( const ContentsFlagsValue& flags ){
1202 m_shader.setFlags( flags );
1203 m_observer->shaderChanged();
1207 void ShiftTexdef( float s, float t ){
1209 m_texdef.shift( s, t );
1213 void ScaleTexdef( float s, float t ){
1215 m_texdef.scale( s, t );
1219 void RotateTexdef( float angle ){
1221 m_texdef.rotate( angle );
1225 void FitTexture( float s_repeat, float t_repeat ){
1227 m_texdef.fit( m_plane.plane3().normal(), m_winding, s_repeat, t_repeat );
1231 void EmitTextureCoordinates(){
1232 Texdef_EmitTextureCoordinates( m_texdefTransformed, m_shader.width(), m_shader.height(), m_winding, plane3().normal(), g_matrix4_identity );
1236 const Vector3& centroid() const {
1240 void construct_centroid(){
1241 Winding_Centroid( m_winding, plane3(), m_centroid );
1244 const Winding& getWinding() const {
1248 Winding& getWinding(){
1252 const Plane3& plane3() const {
1253 m_observer->evaluateTransform();
1254 return m_planeTransformed.plane3();
1257 FacePlane& getPlane(){
1261 const FacePlane& getPlane() const {
1265 FaceTexdef& getTexdef(){
1269 const FaceTexdef& getTexdef() const {
1273 FaceShader& getShader(){
1277 const FaceShader& getShader() const {
1281 bool isDetail() const {
1282 return ( m_shader.m_flags.m_contentFlags & BRUSH_DETAIL_MASK ) != 0;
1285 void setDetail( bool detail ){
1287 if ( detail && !isDetail() ) {
1288 m_shader.m_flags.m_contentFlags |= BRUSH_DETAIL_MASK;
1290 else if ( !detail && isDetail() ) {
1291 m_shader.m_flags.m_contentFlags &= ~BRUSH_DETAIL_MASK;
1293 m_observer->shaderChanged();
1296 bool contributes() const {
1297 return m_winding.numpoints > 2;
1300 bool is_bounded() const {
1301 for ( Winding::const_iterator i = m_winding.begin(); i != m_winding.end(); ++i )
1303 if ( ( *i ).adjacent == c_brush_maxFaces ) {
1315 std::size_t m_vertex;
1318 FaceVertexId( std::size_t face, std::size_t vertex )
1319 : m_face( face ), m_vertex( vertex ){
1322 std::size_t getFace() const {
1326 std::size_t getVertex() const {
1331 typedef std::size_t faceIndex_t;
1333 struct EdgeRenderIndices
1339 : first( 0 ), second( 0 ){
1342 EdgeRenderIndices( const RenderIndex _first, const RenderIndex _second )
1343 : first( _first ), second( _second ){
1353 : first( c_brush_maxFaces ), second( c_brush_maxFaces ){
1356 EdgeFaces( const faceIndex_t _first, const faceIndex_t _second )
1357 : first( _first ), second( _second ){
1361 class RenderableWireframe : public OpenGLRenderable
1364 void render( RenderStateFlags state ) const {
1366 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices->colour );
1367 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices->vertex );
1368 glDrawElements( GL_LINES, GLsizei( m_size << 1 ), RenderIndexTypeID, m_faceVertex.data() );
1370 glBegin( GL_LINES );
1371 for ( std::size_t i = 0; i < m_size; ++i )
1373 glVertex3fv( &m_vertices[m_faceVertex[i].first].vertex.x );
1374 glVertex3fv( &m_vertices[m_faceVertex[i].second].vertex.x );
1380 Array<EdgeRenderIndices> m_faceVertex;
1382 const PointVertex* m_vertices;
1387 typedef std::vector<Brush*> brush_vector_t;
1392 virtual bool filter( const Brush& brush ) const = 0;
1395 bool brush_filtered( Brush& brush );
1397 void add_brush_filter( BrushFilter& filter, int mask, bool invert = false );
1400 /// \brief Returns true if 'self' takes priority when building brush b-rep.
1401 inline bool plane3_inside( const Plane3& self, const Plane3& other, bool selfIsLater ){
1402 if ( vector3_equal_epsilon( self.normal(), other.normal(), 0.001 ) ) {
1403 // same plane? prefer the one with smaller index
1404 if ( self.dist() == other.dist() ) {
1407 return self.dist() < other.dist();
1412 typedef std::vector<std::shared_ptr<Face>> Faces;
1414 /// \brief Returns the unique-id of the edge adjacent to \p faceVertex in the edge-pair for the set of \p faces.
1415 inline FaceVertexId next_edge( const Faces& faces, FaceVertexId faceVertex ){
1416 std::size_t adjacent_face = faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent;
1417 std::size_t adjacent_vertex = Winding_FindAdjacent( faces[adjacent_face]->getWinding(), faceVertex.getFace() );
1419 ASSERT_MESSAGE( adjacent_vertex != c_brush_maxFaces, "connectivity data invalid" );
1420 if ( adjacent_vertex == c_brush_maxFaces ) {
1424 return FaceVertexId( adjacent_face, adjacent_vertex );
1427 /// \brief Returns the unique-id of the vertex adjacent to \p faceVertex in the vertex-ring for the set of \p faces.
1428 inline FaceVertexId next_vertex( const Faces& faces, FaceVertexId faceVertex ){
1429 FaceVertexId nextEdge = next_edge( faces, faceVertex );
1430 return FaceVertexId( nextEdge.getFace(), Winding_next( faces[nextEdge.getFace()]->getWinding(), nextEdge.getVertex() ) );
1433 class SelectableEdge
1435 Vector3 getEdge() const {
1436 const Winding& winding = getFace().getWinding();
1437 return vector3_mid( winding[m_faceVertex.getVertex()].vertex, winding[Winding_next( winding, m_faceVertex.getVertex() )].vertex );
1442 FaceVertexId m_faceVertex;
1444 SelectableEdge( Faces& faces, FaceVertexId faceVertex )
1445 : m_faces( faces ), m_faceVertex( faceVertex ){
1448 SelectableEdge& operator=( const SelectableEdge& other ){
1449 m_faceVertex = other.m_faceVertex;
1453 Face& getFace() const {
1454 return *m_faces[m_faceVertex.getFace()];
1457 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1458 test.TestPoint( getEdge(), best );
1462 class SelectableVertex
1464 Vector3 getVertex() const {
1465 return getFace().getWinding()[m_faceVertex.getVertex()].vertex;
1470 FaceVertexId m_faceVertex;
1472 SelectableVertex( Faces& faces, FaceVertexId faceVertex )
1473 : m_faces( faces ), m_faceVertex( faceVertex ){
1476 SelectableVertex& operator=( const SelectableVertex& other ){
1477 m_faceVertex = other.m_faceVertex;
1481 Face& getFace() const {
1482 return *m_faces[m_faceVertex.getFace()];
1485 void testSelect( SelectionTest& test, SelectionIntersection& best ){
1486 test.TestPoint( getVertex(), best );
1493 virtual void reserve( std::size_t size ) = 0;
1494 virtual void clear() = 0;
1495 virtual void push_back( Face& face ) = 0;
1496 virtual void pop_back() = 0;
1497 virtual void erase( std::size_t index ) = 0;
1498 virtual void connectivityChanged() = 0;
1499 virtual void edge_clear() = 0;
1500 virtual void edge_push_back( SelectableEdge& edge ) = 0;
1501 virtual void vertex_clear() = 0;
1502 virtual void vertex_push_back( SelectableVertex& vertex ) = 0;
1503 virtual void DEBUG_verify() const = 0;
1509 virtual void visit( Face& face ) const = 0;
1513 public TransformNode,
1518 public FaceObserver,
1524 scene::Node* m_node;
1525 typedef UniqueSet<BrushObserver*> Observers;
1526 Observers m_observers;
1527 UndoObserver* m_undoable_observer;
1534 // cached data compiled from state
1535 Array<PointVertex> m_faceCentroidPoints;
1536 RenderablePointArray m_render_faces;
1538 Array<PointVertex> m_uniqueVertexPoints;
1539 typedef std::vector<SelectableVertex> SelectableVertices;
1540 SelectableVertices m_select_vertices;
1541 RenderablePointArray m_render_vertices;
1543 Array<PointVertex> m_uniqueEdgePoints;
1544 typedef std::vector<SelectableEdge> SelectableEdges;
1545 SelectableEdges m_select_edges;
1546 RenderablePointArray m_render_edges;
1548 Array<EdgeRenderIndices> m_edge_indices;
1549 Array<EdgeFaces> m_edge_faces;
1554 Callback<void()> m_evaluateTransform;
1555 Callback<void()> m_boundsChanged;
1557 mutable bool m_planeChanged; // b-rep evaluation required
1558 mutable bool m_transformChanged; // transform evaluation required
1562 STRING_CONSTANT( Name, "Brush" );
1564 Callback<void()> m_lightsChanged;
1567 static Shader* m_state_point;
1570 static EBrushType m_type;
1571 static double m_maxWorldCoord;
1573 Brush( scene::Node& node, const Callback<void()>& evaluateTransform, const Callback<void()>& boundsChanged ) :
1575 m_undoable_observer( 0 ),
1577 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1578 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1579 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1580 m_evaluateTransform( evaluateTransform ),
1581 m_boundsChanged( boundsChanged ),
1582 m_planeChanged( false ),
1583 m_transformChanged( false ){
1586 Brush( const Brush& other, scene::Node& node, const Callback<void()>& evaluateTransform, const Callback<void()>& boundsChanged ) :
1588 m_undoable_observer( 0 ),
1590 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1591 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1592 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1593 m_evaluateTransform( evaluateTransform ),
1594 m_boundsChanged( boundsChanged ),
1595 m_planeChanged( false ),
1596 m_transformChanged( false ){
1600 Brush( const Brush& other ) :
1601 TransformNode( other ),
1606 FaceObserver( other ),
1607 Filterable( other ),
1609 BrushDoom3( other ),
1611 m_undoable_observer( 0 ),
1613 m_render_faces( m_faceCentroidPoints, GL_POINTS ),
1614 m_render_vertices( m_uniqueVertexPoints, GL_POINTS ),
1615 m_render_edges( m_uniqueEdgePoints, GL_POINTS ),
1616 m_planeChanged( false ),
1617 m_transformChanged( false ){
1622 ASSERT_MESSAGE( m_observers.empty(), "Brush::~Brush: observers still attached" );
1625 // assignment not supported
1626 Brush& operator=( const Brush& other );
1628 void setDoom3GroupOrigin( const Vector3& origin ){
1629 //globalOutputStream() << "func_static origin before: " << m_funcStaticOrigin << " after: " << origin << "\n";
1630 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1632 ( *i )->getPlane().m_funcStaticOrigin = origin;
1633 ( *i )->getPlane().updateTranslated();
1634 ( *i )->planeChanged();
1639 void attach( BrushObserver& observer ){
1640 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1642 observer.push_back( *( *i ) );
1645 for ( SelectableEdges::iterator i = m_select_edges.begin(); i != m_select_edges.end(); ++i )
1647 observer.edge_push_back( *i );
1650 for ( SelectableVertices::iterator i = m_select_vertices.begin(); i != m_select_vertices.end(); ++i )
1652 observer.vertex_push_back( *i );
1655 m_observers.insert( &observer );
1658 void detach( BrushObserver& observer ){
1659 m_observers.erase( &observer );
1662 void forEachFace( const BrushVisitor& visitor ) const {
1663 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1665 visitor.visit( *( *i ) );
1669 void forEachFace_instanceAttach( MapFile* map ) const {
1670 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1672 ( *i )->instanceAttach( map );
1676 void forEachFace_instanceDetach( MapFile* map ) const {
1677 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1679 ( *i )->instanceDetach( map );
1683 InstanceCounter m_instanceCounter;
1685 void instanceAttach( const scene::Path& path ){
1686 if ( ++m_instanceCounter.m_count == 1 ) {
1687 m_map = path_find_mapfile( path.begin(), path.end() );
1688 m_undoable_observer = GlobalUndoSystem().observer( this );
1689 GlobalFilterSystem().registerFilterable( *this );
1690 forEachFace_instanceAttach( m_map );
1694 ASSERT_MESSAGE( path_find_mapfile( path.begin(), path.end() ) == m_map, "node is instanced across more than one file" );
1698 void instanceDetach( const scene::Path& path ){
1699 if ( --m_instanceCounter.m_count == 0 ) {
1700 forEachFace_instanceDetach( m_map );
1701 GlobalFilterSystem().unregisterFilterable( *this );
1703 m_undoable_observer = 0;
1704 GlobalUndoSystem().release( this );
1709 const char* name() const {
1713 void attach( const NameCallback& callback ){
1716 void detach( const NameCallback& callback ){
1720 void updateFiltered(){
1721 if ( m_node != 0 ) {
1722 if ( brush_filtered( *this ) ) {
1723 m_node->enable( scene::Node::eFiltered );
1727 m_node->disable( scene::Node::eFiltered );
1733 void planeChanged(){
1734 m_planeChanged = true;
1739 void shaderChanged(){
1744 void evaluateBRep() const {
1745 if ( m_planeChanged ) {
1746 m_planeChanged = false;
1747 const_cast<Brush*>( this )->buildBRep();
1751 void transformChanged(){
1752 m_transformChanged = true;
1756 typedef MemberCaller<Brush, void(), &Brush::transformChanged> TransformChangedCaller;
1758 void evaluateTransform(){
1759 if ( m_transformChanged ) {
1760 m_transformChanged = false;
1762 m_evaluateTransform();
1766 const Matrix4& localToParent() const {
1767 return g_matrix4_identity;
1774 const AABB& localAABB() const {
1776 return m_aabb_local;
1779 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
1780 return test.TestAABB( m_aabb_local, localToWorld );
1783 void renderComponents( SelectionSystem::EComponentMode mode, Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
1786 case SelectionSystem::eVertex:
1787 renderer.addRenderable( m_render_vertices, localToWorld );
1789 case SelectionSystem::eEdge:
1790 renderer.addRenderable( m_render_edges, localToWorld );
1792 case SelectionSystem::eFace:
1793 renderer.addRenderable( m_render_faces, localToWorld );
1800 void transform( const Matrix4& matrix ){
1801 bool mirror = matrix4_handedness( matrix ) == MATRIX4_LEFTHANDED;
1803 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1805 ( *i )->transform( matrix, mirror );
1809 void snapto( float snap ){
1810 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1812 ( *i )->snapto( snap );
1816 void revertTransform(){
1817 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1819 ( *i )->revertTransform();
1823 void freezeTransform(){
1824 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
1826 ( *i )->freezeTransform();
1830 /// \brief Returns the absolute index of the \p faceVertex.
1831 std::size_t absoluteIndex( FaceVertexId faceVertex ){
1832 std::size_t index = 0;
1833 for ( std::size_t i = 0; i < faceVertex.getFace(); ++i )
1835 index += m_faces[i]->getWinding().numpoints;
1837 return index + faceVertex.getVertex();
1840 void appendFaces( const Faces& other ){
1842 for ( Faces::const_iterator i = other.begin(); i != other.end(); ++i )
1848 /// \brief The undo memento for a brush stores only the list of face references - the faces are not copied.
1849 class BrushUndoMemento : public UndoMemento
1852 BrushUndoMemento( const Faces& faces ) : m_faces( faces ){
1866 if ( m_undoable_observer != 0 ) {
1867 m_undoable_observer->save( this );
1871 UndoMemento* exportState() const {
1872 return new BrushUndoMemento( m_faces );
1875 void importState( const UndoMemento* state ){
1877 appendFaces( static_cast<const BrushUndoMemento*>( state )->m_faces );
1880 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1882 ( *i )->DEBUG_verify();
1887 return !m_faces.empty() && m_faces.front()->isDetail();
1890 /// \brief Appends a copy of \p face to the end of the face list.
1891 std::shared_ptr<Face> addFace( const Face& face ){
1892 if ( m_faces.size() == c_brush_maxFaces ) {
1896 push_back( std::make_shared<Face>( face, this ) );
1897 m_faces.back()->setDetail( isDetail() );
1899 return m_faces.back();
1902 /// \brief Appends a new face constructed from the parameters to the end of the face list.
1903 std::shared_ptr<Face> addPlane( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection ){
1904 if ( m_faces.size() == c_brush_maxFaces ) {
1908 push_back( std::make_shared<Face>( p0, p1, p2, shader, projection, this ) );
1909 m_faces.back()->setDetail( isDetail() );
1911 return m_faces.back();
1914 static void constructStatic( EBrushType type ){
1916 Face::m_type = type;
1917 FacePlane::m_type = type;
1919 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_QUAKE;
1920 if ( m_type == eBrushTypeQuake3BP || m_type == eBrushTypeDoom3 || m_type == eBrushTypeQuake4 ) {
1921 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_BRUSHPRIMITIVES;
1922 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
1924 else if ( m_type == eBrushTypeHalfLife ) {
1925 g_bp_globals.m_texdefTypeId = TEXDEFTYPEID_HALFLIFE;
1926 // g_brush_texturelock_enabled = true; // bad idea, this overrides user setting
1929 Face::m_quantise = ( m_type == eBrushTypeQuake ) ? quantiseInteger : quantiseFloating;
1931 m_state_point = GlobalShaderCache().capture( "$POINT" );
1934 static void destroyStatic(){
1935 GlobalShaderCache().release( "$POINT" );
1938 std::size_t DEBUG_size(){
1939 return m_faces.size();
1942 typedef Faces::const_iterator const_iterator;
1944 const_iterator begin() const {
1945 return m_faces.begin();
1948 const_iterator end() const {
1949 return m_faces.end();
1952 std::shared_ptr<Face> back(){
1953 return m_faces.back();
1956 const std::shared_ptr<Face> back() const {
1957 return m_faces.back();
1960 void reserve( std::size_t count ){
1961 m_faces.reserve( count );
1962 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1964 ( *i )->reserve( count );
1968 void push_back( Faces::value_type face ){
1969 m_faces.push_back( face );
1970 if ( m_instanceCounter.m_count != 0 ) {
1971 m_faces.back()->instanceAttach( m_map );
1973 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1975 ( *i )->push_back( *face );
1976 ( *i )->DEBUG_verify();
1981 if ( m_instanceCounter.m_count != 0 ) {
1982 m_faces.back()->instanceDetach( m_map );
1985 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1988 ( *i )->DEBUG_verify();
1992 void erase( std::size_t index ){
1993 if ( m_instanceCounter.m_count != 0 ) {
1994 m_faces[index]->instanceDetach( m_map );
1996 m_faces.erase( m_faces.begin() + index );
1997 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
1999 ( *i )->erase( index );
2000 ( *i )->DEBUG_verify();
2004 void connectivityChanged(){
2005 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2007 ( *i )->connectivityChanged();
2014 if ( m_instanceCounter.m_count != 0 ) {
2015 forEachFace_instanceDetach( m_map );
2018 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2021 ( *i )->DEBUG_verify();
2025 std::size_t size() const {
2026 return m_faces.size();
2029 bool empty() const {
2030 return m_faces.empty();
2033 /// \brief Returns true if any face of the brush contributes to the final B-Rep.
2034 bool hasContributingFaces() const {
2035 for ( const_iterator i = begin(); i != end(); ++i )
2037 if ( ( *i )->contributes() ) {
2044 /// \brief Removes faces that do not contribute to the brush. This is useful for cleaning up after CSG operations on the brush.
2045 /// 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.
2046 void removeEmptyFaces(){
2051 while ( i < m_faces.size() )
2053 if ( !m_faces[i]->contributes() ) {
2065 /// \brief Constructs \p winding from the intersection of \p plane with the other planes of the brush.
2066 void windingForClipPlane( Winding& winding, const Plane3& plane ) const {
2067 FixedWinding buffer[2];
2070 // get a poly that covers an effectively infinite area
2071 Winding_createInfinite( buffer[swap], plane, m_maxWorldCoord + 1 );
2073 // chop the poly by all of the other faces
2075 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2077 const Face& clip = *m_faces[i];
2079 if ( plane3_equal( clip.plane3(), plane )
2080 || !plane3_valid( clip.plane3() ) || !plane_unique( i )
2081 || plane3_opposing( plane, clip.plane3() ) ) {
2085 if( buffer[swap].points.empty() ){
2086 //globalErrorStream() << "windingForClipPlane: about to feed empty winding\n";
2090 buffer[!swap].clear();
2092 #if BRUSH_CONNECTIVITY_DEBUG
2093 globalOutputStream() << "clip vs face: " << i << "\n";
2097 // flip the plane, because we want to keep the back side
2098 Plane3 clipPlane( vector3_negated( clip.plane3().normal() ), -clip.plane3().dist() );
2099 Winding_Clip( buffer[swap], plane, clipPlane, i, buffer[!swap] );
2102 #if BRUSH_CONNECTIVITY_DEBUG
2103 for ( FixedWinding::Points::iterator k = buffer[!swap].points.begin(), j = buffer[!swap].points.end() - 1; k != buffer[!swap].points.end(); j = k, ++k )
2105 if ( vector3_length_squared( vector3_subtracted( ( *k ).vertex, ( *j ).vertex ) ) < 1 ) {
2106 globalOutputStream() << "v: " << std::distance( buffer[!swap].points.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2111 //ASSERT_MESSAGE(buffer[!swap].numpoints != 1, "created single-point winding");
2117 Winding_forFixedWinding( winding, buffer[swap] );
2119 #if BRUSH_CONNECTIVITY_DEBUG
2120 Winding_printConnectivity( winding );
2122 for ( Winding::iterator i = winding.begin(), j = winding.end() - 1; i != winding.end(); j = i, ++i )
2124 if ( vector3_length_squared( vector3_subtracted( ( *i ).vertex, ( *j ).vertex ) ) < 1 ) {
2125 globalOutputStream() << "v: " << std::distance( winding.begin(), j ) << " tiny edge adjacent to face " << ( *j ).adjacent << "\n";
2131 void update_wireframe( RenderableWireframe& wire, const bool* faces_visible ) const {
2132 wire.m_faceVertex.resize( m_edge_indices.size() );
2133 wire.m_vertices = m_uniqueVertexPoints.data();
2135 for ( std::size_t i = 0; i < m_edge_faces.size(); ++i )
2137 if ( faces_visible[m_edge_faces[i].first]
2138 || faces_visible[m_edge_faces[i].second] ) {
2139 wire.m_faceVertex[wire.m_size++] = m_edge_indices[i];
2145 void update_faces_wireframe( Array<PointVertex>& wire, const bool* faces_visible ) const {
2146 std::size_t count = 0;
2147 for ( std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i )
2149 if ( faces_visible[i] ) {
2154 wire.resize( count );
2155 Array<PointVertex>::iterator p = wire.begin();
2156 for ( std::size_t i = 0; i < m_faceCentroidPoints.size(); ++i )
2158 if ( faces_visible[i] ) {
2159 *p++ = m_faceCentroidPoints[i];
2164 /// \brief Makes this brush a deep-copy of the \p other.
2165 void copy( const Brush& other ){
2166 for ( Faces::const_iterator i = other.m_faces.begin(); i != other.m_faces.end(); ++i )
2174 void edge_push_back( FaceVertexId faceVertex ){
2175 m_select_edges.push_back( SelectableEdge( m_faces, faceVertex ) );
2176 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2178 ( *i )->edge_push_back( m_select_edges.back() );
2183 m_select_edges.clear();
2184 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2186 ( *i )->edge_clear();
2190 void vertex_push_back( FaceVertexId faceVertex ){
2191 m_select_vertices.push_back( SelectableVertex( m_faces, faceVertex ) );
2192 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2194 ( *i )->vertex_push_back( m_select_vertices.back() );
2198 void vertex_clear(){
2199 m_select_vertices.clear();
2200 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
2202 ( *i )->vertex_clear();
2206 /// \brief Returns true if the face identified by \p index is preceded by another plane that takes priority over it.
2207 bool plane_unique( std::size_t index ) const {
2209 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2211 if ( index != i && !plane3_inside( m_faces[index]->plane3(), m_faces[i]->plane3(), index < i ) ) {
2218 /// \brief Removes edges that are smaller than the tolerance used when generating brush windings.
2219 void removeDegenerateEdges(){
2220 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2222 Winding& winding = m_faces[i]->getWinding();
2223 for ( Winding::iterator j = winding.begin(); j != winding.end(); )
2225 std::size_t index = std::distance( winding.begin(), j );
2226 std::size_t next = Winding_next( winding, index );
2227 if ( Edge_isDegenerate( winding[index].vertex, winding[next].vertex ) ) {
2228 #if BRUSH_DEGENERATE_DEBUG
2229 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate edge adjacent to " << winding[index].adjacent << "\n";
2231 Winding& other = m_faces[winding[index].adjacent]->getWinding();
2232 std::size_t adjacent = Winding_FindAdjacent( other, i );
2233 if ( adjacent != c_brush_maxFaces ) {
2234 other.erase( other.begin() + adjacent );
2246 /// \brief Invalidates faces that have only two vertices in their winding, while preserving edge-connectivity information.
2247 void removeDegenerateFaces(){
2248 // save adjacency info for degenerate faces
2249 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2251 Winding& degen = m_faces[i]->getWinding();
2253 if ( degen.numpoints == 2 ) {
2254 #if BRUSH_DEGENERATE_DEBUG
2255 globalOutputStream() << "Brush::buildWindings: face " << i << ": degenerate winding adjacent to " << degen[0].adjacent << ", " << degen[1].adjacent << "\n";
2257 // this is an "edge" face, where the plane touches the edge of the brush
2259 Winding& winding = m_faces[degen[0].adjacent]->getWinding();
2260 std::size_t index = Winding_FindAdjacent( winding, i );
2261 if ( index != c_brush_maxFaces ) {
2262 #if BRUSH_DEGENERATE_DEBUG
2263 globalOutputStream() << "Brush::buildWindings: face " << degen[0].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[1].adjacent << "\n";
2265 winding[index].adjacent = degen[1].adjacent;
2270 Winding& winding = m_faces[degen[1].adjacent]->getWinding();
2271 std::size_t index = Winding_FindAdjacent( winding, i );
2272 if ( index != c_brush_maxFaces ) {
2273 #if BRUSH_DEGENERATE_DEBUG
2274 globalOutputStream() << "Brush::buildWindings: face " << degen[1].adjacent << ": remapping adjacent " << winding[index].adjacent << " to " << degen[0].adjacent << "\n";
2276 winding[index].adjacent = degen[0].adjacent;
2285 /// \brief Removes edges that have the same adjacent-face as their immediate neighbour.
2286 void removeDuplicateEdges(){
2287 // verify face connectivity graph
2288 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2290 //if(m_faces[i]->contributes())
2292 Winding& winding = m_faces[i]->getWinding();
2293 for ( std::size_t j = 0; j != winding.numpoints; )
2295 std::size_t next = Winding_next( winding, j );
2296 if ( winding[j].adjacent == winding[next].adjacent ) {
2297 #if BRUSH_DEGENERATE_DEBUG
2298 globalOutputStream() << "Brush::buildWindings: face " << i << ": removed duplicate edge adjacent to face " << winding[j].adjacent << "\n";
2300 winding.erase( winding.begin() + next );
2311 /// \brief Removes edges that do not have a matching pair in their adjacent-face.
2312 void verifyConnectivityGraph(){
2313 // verify face connectivity graph
2314 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2316 //if(m_faces[i]->contributes())
2318 Winding& winding = m_faces[i]->getWinding();
2319 for ( Winding::iterator j = winding.begin(); j != winding.end(); )
2321 #if BRUSH_CONNECTIVITY_DEBUG
2322 globalOutputStream() << "Brush::buildWindings: face " << i << ": adjacent to face " << ( *j ).adjacent << "\n";
2324 // remove unidirectional graph edges
2325 if ( ( *j ).adjacent == c_brush_maxFaces
2326 || Winding_FindAdjacent( m_faces[( *j ).adjacent]->getWinding(), i ) == c_brush_maxFaces ) {
2327 #if BRUSH_CONNECTIVITY_DEBUG
2328 globalOutputStream() << "Brush::buildWindings: face " << i << ": removing unidirectional connectivity graph edge adjacent to face " << ( *j ).adjacent << "\n";
2341 /// \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.
2343 for ( const_iterator i = begin(); i != end(); ++i )
2345 if ( !( *i )->is_bounded() ) {
2352 /// \brief Constructs the polygon windings for each face of the brush. Also updates the brush bounding-box and face texture-coordinates.
2353 bool buildWindings(){
2356 m_aabb_local = AABB();
2358 for ( std::size_t i = 0; i < m_faces.size(); ++i )
2360 Face& f = *m_faces[i];
2362 if ( !plane3_valid( f.plane3() ) || !plane_unique( i ) ) {
2363 f.getWinding().resize( 0 );
2367 #if BRUSH_CONNECTIVITY_DEBUG
2368 globalOutputStream() << "face: " << i << "\n";
2370 windingForClipPlane( f.getWinding(), f.plane3() );
2372 // update brush bounds
2373 const Winding& winding = f.getWinding();
2374 for ( Winding::const_iterator i = winding.begin(); i != winding.end(); ++i )
2376 aabb_extend_by_point_safe( m_aabb_local, ( *i ).vertex );
2379 // update texture coordinates
2380 f.EmitTextureCoordinates();
2385 bool degenerate = !isBounded();
2387 if ( !degenerate ) {
2388 // clean up connectivity information.
2389 // these cleanups must be applied in a specific order.
2390 removeDegenerateEdges();
2391 removeDegenerateFaces();
2392 removeDuplicateEdges();
2393 verifyConnectivityGraph();
2399 /// \brief Constructs the face windings and updates anything that depends on them.
2406 class FaceInstanceSet
2408 typedef SelectionList<FaceInstance> FaceInstances;
2409 FaceInstances m_faceInstances;
2411 void insert( FaceInstance& faceInstance ){
2412 m_faceInstances.append( faceInstance );
2415 void erase( FaceInstance& faceInstance ){
2416 m_faceInstances.erase( faceInstance );
2419 template<typename Functor>
2420 void foreach( Functor functor ){
2421 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
2427 bool empty() const {
2428 return m_faceInstances.empty();
2431 FaceInstance& last() const {
2432 return m_faceInstances.back();
2436 extern FaceInstanceSet g_SelectedFaceInstances;
2438 typedef std::list<std::size_t> VertexSelection;
2440 inline VertexSelection::iterator VertexSelection_find( VertexSelection& self, std::size_t value ){
2441 return std::find( self.begin(), self.end(), value );
2444 inline VertexSelection::const_iterator VertexSelection_find( const VertexSelection& self, std::size_t value ){
2445 return std::find( self.begin(), self.end(), value );
2448 inline VertexSelection::iterator VertexSelection_insert( VertexSelection& self, std::size_t value ){
2449 VertexSelection::iterator i = VertexSelection_find( self, value );
2450 if ( i == self.end() ) {
2451 self.push_back( value );
2452 return --self.end();
2457 inline void VertexSelection_erase( VertexSelection& self, std::size_t value ){
2458 VertexSelection::iterator i = VertexSelection_find( self, value );
2459 if ( i != self.end() ) {
2464 inline bool triangle_reversed( std::size_t x, std::size_t y, std::size_t z ){
2465 return !( ( x < y && y < z ) || ( z < x && x < y ) || ( y < z && z < x ) );
2468 template<typename Element>
2469 inline Vector3 triangle_cross( const BasicVector3<Element>& x, const BasicVector3<Element> y, const BasicVector3<Element>& z ){
2470 return vector3_cross( y - x, z - x );
2473 template<typename Element>
2474 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 ){
2475 return vector3_dot( triangle_cross( x1, y1, z1 ), triangle_cross( x2, y2, z2 ) ) > 0;
2479 class VectorLightList : public LightList
2481 typedef std::vector<const RendererLight*> Lights;
2484 void addLight( const RendererLight& light ){
2485 m_lights.push_back( &light );
2492 void evaluateLights() const {
2495 void lightsChanged() const {
2498 void forEachLight( const RendererLightCallback& callback ) const {
2499 for ( Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i )
2501 callback( *( *i ) );
2506 class BoolSelector : public Selector
2509 SelectionIntersection m_intersection;
2510 Selectable* m_selectable;
2512 BoolSelector() : m_selected( false ){
2515 void pushSelectable( Selectable& selectable ){
2516 m_intersection = SelectionIntersection();
2517 m_selectable = &selectable;
2519 void popSelectable(){
2520 if ( m_intersection.valid() ) {
2523 m_intersection = SelectionIntersection();
2525 void addIntersection( const SelectionIntersection& intersection ){
2526 assign_if_closer( m_intersection, intersection );
2537 ObservedSelectable m_selectable;
2538 ObservedSelectable m_selectableVertices;
2539 ObservedSelectable m_selectableEdges;
2540 SelectionChangeCallback m_selectionChanged;
2542 VertexSelection m_vertexSelection;
2543 VertexSelection m_edgeSelection;
2546 mutable VectorLightList m_lights;
2548 FaceInstance( Face& face, const SelectionChangeCallback& observer ) :
2550 m_selectable( SelectedChangedCaller( *this ) ),
2551 m_selectableVertices( observer ),
2552 m_selectableEdges( observer ),
2553 m_selectionChanged( observer ){
2556 FaceInstance( const FaceInstance& other ) :
2557 m_face( other.m_face ),
2558 m_selectable( SelectedChangedCaller( *this ) ),
2559 m_selectableVertices( other.m_selectableVertices ),
2560 m_selectableEdges( other.m_selectableEdges ),
2561 m_selectionChanged( other.m_selectionChanged ){
2564 FaceInstance& operator=( const FaceInstance& other ){
2565 m_face = other.m_face;
2573 const Face& getFace() const {
2577 void selectedChanged( const Selectable& selectable ){
2578 if ( selectable.isSelected() ) {
2579 g_SelectedFaceInstances.insert( *this );
2583 g_SelectedFaceInstances.erase( *this );
2585 m_selectionChanged( selectable );
2588 typedef MemberCaller<FaceInstance, void(const Selectable&), &FaceInstance::selectedChanged> SelectedChangedCaller;
2590 bool selectedVertices() const {
2591 return !m_vertexSelection.empty();
2594 bool selectedEdges() const {
2595 return !m_edgeSelection.empty();
2598 bool isSelected() const {
2599 return m_selectable.isSelected();
2602 bool selectedComponents() const {
2603 return selectedVertices() || selectedEdges() || isSelected();
2606 bool selectedComponents( SelectionSystem::EComponentMode mode ) const {
2609 case SelectionSystem::eVertex:
2610 return selectedVertices();
2611 case SelectionSystem::eEdge:
2612 return selectedEdges();
2613 case SelectionSystem::eFace:
2614 return isSelected();
2620 void setSelected( SelectionSystem::EComponentMode mode, bool select ){
2623 case SelectionSystem::eFace:
2624 m_selectable.setSelected( select );
2626 case SelectionSystem::eVertex:
2627 ASSERT_MESSAGE( !select, "select-all not supported" );
2629 m_vertexSelection.clear();
2630 m_selectableVertices.setSelected( false );
2632 case SelectionSystem::eEdge:
2633 ASSERT_MESSAGE( !select, "select-all not supported" );
2635 m_edgeSelection.clear();
2636 m_selectableEdges.setSelected( false );
2643 template<typename Functor>
2644 void SelectedVertices_foreach( Functor functor ) const {
2645 for ( VertexSelection::const_iterator i = m_vertexSelection.begin(); i != m_vertexSelection.end(); ++i )
2647 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *i );
2648 if ( index != c_brush_maxFaces ) {
2649 functor( getFace().getWinding()[index].vertex );
2654 template<typename Functor>
2655 void SelectedEdges_foreach( Functor functor ) const {
2656 for ( VertexSelection::const_iterator i = m_edgeSelection.begin(); i != m_edgeSelection.end(); ++i )
2658 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *i );
2659 if ( index != c_brush_maxFaces ) {
2660 const Winding& winding = getFace().getWinding();
2661 std::size_t adjacent = Winding_next( winding, index );
2662 functor( vector3_mid( winding[index].vertex, winding[adjacent].vertex ) );
2667 template<typename Functor>
2668 void SelectedFaces_foreach( Functor functor ) const {
2669 if ( isSelected() ) {
2670 functor( centroid() );
2674 template<typename Functor>
2675 void SelectedComponents_foreach( Functor functor ) const {
2676 SelectedVertices_foreach( functor );
2677 SelectedEdges_foreach( functor );
2678 SelectedFaces_foreach( functor );
2681 void iterate_selected( AABB& aabb ) const {
2682 SelectedComponents_foreach([&](const Vector3 &point) {
2683 aabb_extend_by_point_safe(aabb, point);
2687 void iterate_selected( RenderablePointVector& points ) const {
2688 SelectedComponents_foreach([&](const Vector3 &point) {
2689 const Colour4b colour_selected(0, 0, 255, 255);
2690 points.push_back(pointvertex_for_windingpoint(point, colour_selected));
2694 bool intersectVolume( const VolumeTest& volume, const Matrix4& localToWorld ) const {
2695 return m_face->intersectVolume( volume, localToWorld );
2698 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
2699 if ( !m_face->isFiltered() && m_face->contributes() && intersectVolume( volume, localToWorld ) ) {
2700 renderer.PushState();
2701 if ( selectedComponents() ) {
2702 renderer.Highlight( Renderer::eFace );
2704 m_face->render( renderer, localToWorld );
2705 renderer.PopState();
2709 void testSelect( SelectionTest& test, SelectionIntersection& best ){
2710 if ( !m_face->isFiltered() ) {
2711 m_face->testSelect( test, best );
2715 void testSelect( Selector& selector, SelectionTest& test ){
2716 SelectionIntersection best;
2717 testSelect( test, best );
2718 if ( best.valid() ) {
2719 Selector_add( selector, m_selectable, best );
2723 void testSelect_centroid( Selector& selector, SelectionTest& test ){
2724 if ( m_face->contributes() && !m_face->isFiltered() ) {
2725 SelectionIntersection best;
2726 m_face->testSelect_centroid( test, best );
2727 if ( best.valid() ) {
2728 Selector_add( selector, m_selectable, best );
2733 void selectPlane( Selector& selector, const Line& line, const PlaneCallback& selectedPlaneCallback ){
2734 for ( Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i )
2736 Vector3 v( vector3_subtracted( line_closest_point( line, ( *i ).vertex ), ( *i ).vertex ) );
2737 double dot = vector3_dot( getFace().plane3().normal(), v );
2738 //globalOutputStream() << dot << "\n";
2739 //epsilon to prevent perpendicular faces pickup
2740 if ( dot <= 0.005 ) {
2745 Selector_add( selector, m_selectable );
2747 selectedPlaneCallback( getFace().plane3() );
2750 void selectReversedPlane( Selector& selector, const SelectedPlanes& selectedPlanes ){
2751 if ( selectedPlanes.contains( plane3_flipped( getFace().plane3() ) ) ) {
2752 Selector_add( selector, m_selectable );
2756 bool trySelectPlane( const Line& line ){
2757 for ( Winding::const_iterator i = getFace().getWinding().begin(); i != getFace().getWinding().end(); ++i ){
2758 Vector3 v( vector3_subtracted( line_closest_point( line, ( *i ).vertex ), ( *i ).vertex ) );
2759 double dot = vector3_dot( getFace().plane3().normal(), v );
2760 //epsilon to prevent perpendicular faces pickup
2761 if ( dot <= 0.005 ) {
2768 void transformComponents( const Matrix4& matrix ){
2769 if ( isSelected() ) {
2770 m_face->transform( matrix, false );
2772 if ( selectedVertices() ) {
2773 if ( m_vertexSelection.size() == 1 ) {
2774 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2775 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2777 else if ( m_vertexSelection.size() == 2 ) {
2778 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2779 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2780 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2782 else if ( m_vertexSelection.size() >= 3 ) {
2783 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2784 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2785 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2786 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2789 if ( selectedEdges() ) {
2790 if ( m_edgeSelection.size() == 1 ) {
2791 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2792 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2793 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2795 else if ( m_edgeSelection.size() >= 2 ) {
2796 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[0] );
2797 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[1] );
2798 matrix4_transform_point( matrix, m_face->m_move_planeptsTransformed[2] );
2799 m_face->assign_planepts( m_face->m_move_planeptsTransformed );
2804 void snapto( float snap ){
2805 m_face->snapto( snap );
2808 void snapComponents( float snap ){
2809 if ( isSelected() ) {
2812 if ( selectedVertices() ) {
2813 vector3_snap( m_face->m_move_planepts[0], snap );
2814 vector3_snap( m_face->m_move_planepts[1], snap );
2815 vector3_snap( m_face->m_move_planepts[2], snap );
2816 m_face->assign_planepts( m_face->m_move_planepts );
2817 planepts_assign( m_face->m_move_planeptsTransformed, m_face->m_move_planepts );
2818 m_face->freezeTransform();
2820 if ( selectedEdges() ) {
2821 vector3_snap( m_face->m_move_planepts[0], snap );
2822 vector3_snap( m_face->m_move_planepts[1], snap );
2823 vector3_snap( m_face->m_move_planepts[2], snap );
2824 m_face->assign_planepts( m_face->m_move_planepts );
2825 planepts_assign( m_face->m_move_planeptsTransformed, m_face->m_move_planepts );
2826 m_face->freezeTransform();
2830 void update_move_planepts_vertex( std::size_t index ){
2831 m_face->update_move_planepts_vertex( index, m_face->m_move_planepts );
2834 void update_move_planepts_vertex2( std::size_t index, std::size_t other ){
2835 const std::size_t numpoints = m_face->getWinding().numpoints;
2836 ASSERT_MESSAGE( index < numpoints, "select_vertex: invalid index" );
2838 const std::size_t opposite = Winding_Opposite( m_face->getWinding(), index, other );
2840 if ( triangle_reversed( index, other, opposite ) ) {
2841 std::swap( index, other );
2845 triangles_same_winding(
2846 m_face->getWinding()[opposite].vertex,
2847 m_face->getWinding()[index].vertex,
2848 m_face->getWinding()[other].vertex,
2849 m_face->getWinding()[0].vertex,
2850 m_face->getWinding()[1].vertex,
2851 m_face->getWinding()[2].vertex
2853 "update_move_planepts_vertex2: error"
2856 m_face->m_move_planepts[0] = m_face->getWinding()[opposite].vertex;
2857 m_face->m_move_planepts[1] = m_face->getWinding()[index].vertex;
2858 m_face->m_move_planepts[2] = m_face->getWinding()[other].vertex;
2859 planepts_quantise( m_face->m_move_planepts, GRID_MIN ); // winding points are very inaccurate
2862 void update_selection_vertex(){
2863 if ( m_vertexSelection.size() == 0 ) {
2864 m_selectableVertices.setSelected( false );
2868 m_selectableVertices.setSelected( true );
2870 if ( m_vertexSelection.size() == 1 ) {
2871 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_vertexSelection.begin() );
2873 if ( index != c_brush_maxFaces ) {
2874 update_move_planepts_vertex( index );
2877 else if ( m_vertexSelection.size() == 2 ) {
2878 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_vertexSelection.begin() );
2879 std::size_t other = Winding_FindAdjacent( getFace().getWinding(), *( ++m_vertexSelection.begin() ) );
2881 if ( index != c_brush_maxFaces
2882 && other != c_brush_maxFaces ) {
2883 update_move_planepts_vertex2( index, other );
2889 void select_vertex( std::size_t index, bool select ){
2891 VertexSelection_insert( m_vertexSelection, getFace().getWinding()[index].adjacent );
2895 VertexSelection_erase( m_vertexSelection, getFace().getWinding()[index].adjacent );
2898 SceneChangeNotify();
2899 update_selection_vertex();
2902 bool selected_vertex( std::size_t index ) const {
2903 return VertexSelection_find( m_vertexSelection, getFace().getWinding()[index].adjacent ) != m_vertexSelection.end();
2906 void update_move_planepts_edge( std::size_t index ){
2907 std::size_t numpoints = m_face->getWinding().numpoints;
2908 ASSERT_MESSAGE( index < numpoints, "select_edge: invalid index" );
2910 std::size_t adjacent = Winding_next( m_face->getWinding(), index );
2911 std::size_t opposite = Winding_Opposite( m_face->getWinding(), index );
2912 m_face->m_move_planepts[0] = m_face->getWinding()[index].vertex;
2913 m_face->m_move_planepts[1] = m_face->getWinding()[adjacent].vertex;
2914 m_face->m_move_planepts[2] = m_face->getWinding()[opposite].vertex;
2915 planepts_quantise( m_face->m_move_planepts, GRID_MIN ); // winding points are very inaccurate
2918 void update_selection_edge(){
2919 if ( m_edgeSelection.size() == 0 ) {
2920 m_selectableEdges.setSelected( false );
2924 m_selectableEdges.setSelected( true );
2926 if ( m_edgeSelection.size() == 1 ) {
2927 std::size_t index = Winding_FindAdjacent( getFace().getWinding(), *m_edgeSelection.begin() );
2929 if ( index != c_brush_maxFaces ) {
2930 update_move_planepts_edge( index );
2936 void select_edge( std::size_t index, bool select ){
2938 VertexSelection_insert( m_edgeSelection, getFace().getWinding()[index].adjacent );
2942 VertexSelection_erase( m_edgeSelection, getFace().getWinding()[index].adjacent );
2945 SceneChangeNotify();
2946 update_selection_edge();
2949 bool selected_edge( std::size_t index ) const {
2950 return VertexSelection_find( m_edgeSelection, getFace().getWinding()[index].adjacent ) != m_edgeSelection.end();
2953 const Vector3& centroid() const {
2954 return m_face->centroid();
2957 void connectivityChanged(){
2958 // This occurs when a face is added or removed.
2959 // The current vertex and edge selections no longer valid and must be cleared.
2960 m_vertexSelection.clear();
2961 m_selectableVertices.setSelected( false );
2962 m_edgeSelection.clear();
2963 m_selectableEdges.setSelected( false );
2967 class BrushClipPlane : public OpenGLRenderable
2971 static Shader* m_state;
2973 static void constructStatic(){
2974 m_state = GlobalShaderCache().capture( "$CLIPPER_OVERLAY" );
2977 static void destroyStatic(){
2978 GlobalShaderCache().release( "$CLIPPER_OVERLAY" );
2981 void setPlane( const Brush& brush, const Plane3& plane ){
2983 if ( plane3_valid( m_plane ) ) {
2984 brush.windingForClipPlane( m_winding, m_plane );
2988 m_winding.resize( 0 );
2992 void render( RenderStateFlags state ) const {
2993 if ( ( state & RENDER_FILL ) != 0 ) {
2994 Winding_Draw( m_winding, m_plane.normal(), state );
2998 Winding_DrawWireframe( m_winding );
3000 // also draw a line indicating the direction of the cut
3001 Vector3 lineverts[2];
3002 Winding_Centroid( m_winding, m_plane, lineverts[0] );
3003 lineverts[1] = vector3_added( lineverts[0], vector3_scaled( m_plane.normal(), Brush::m_maxWorldCoord * 4 ) );
3005 glVertexPointer( 3, GL_FLOAT, sizeof( Vector3 ), &lineverts[0] );
3006 glDrawArrays( GL_LINES, 0, GLsizei( 2 ) );
3010 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3011 renderer.SetState( m_state, Renderer::eWireframeOnly );
3012 renderer.SetState( m_state, Renderer::eFullMaterials );
3013 renderer.addRenderable( *this, localToWorld );
3017 inline void Face_addLight( const FaceInstance& face, const Matrix4& localToWorld, const RendererLight& light ){
3018 const Plane3& facePlane = face.getFace().plane3();
3019 const Vector3& origin = light.aabb().origin;
3020 Plane3 tmp( plane3_transformed( Plane3( facePlane.normal(), -facePlane.dist() ), localToWorld ) );
3021 if ( !plane3_test_point( tmp, origin )
3022 || !plane3_test_point( tmp, vector3_added( origin, light.offset() ) ) ) {
3023 face.m_lights.addLight( light );
3028 typedef std::vector<FaceInstance> FaceInstances;
3030 class EdgeInstance : public Selectable
3032 FaceInstances& m_faceInstances;
3033 SelectableEdge* m_edge;
3035 void select_edge( bool select ){
3036 FaceVertexId faceVertex = m_edge->m_faceVertex;
3037 m_faceInstances[faceVertex.getFace()].select_edge( faceVertex.getVertex(), select );
3038 faceVertex = next_edge( m_edge->m_faces, faceVertex );
3039 m_faceInstances[faceVertex.getFace()].select_edge( faceVertex.getVertex(), select );
3042 bool selected_edge() const {
3043 FaceVertexId faceVertex = m_edge->m_faceVertex;
3044 if ( !m_faceInstances[faceVertex.getFace()].selected_edge( faceVertex.getVertex() ) ) {
3047 faceVertex = next_edge( m_edge->m_faces, faceVertex );
3048 if ( !m_faceInstances[faceVertex.getFace()].selected_edge( faceVertex.getVertex() ) ) {
3056 EdgeInstance( FaceInstances& faceInstances, SelectableEdge& edge )
3057 : m_faceInstances( faceInstances ), m_edge( &edge ){
3059 EdgeInstance& operator=( const EdgeInstance& other ){
3060 m_edge = other.m_edge;
3064 void setSelected( bool select ){
3065 select_edge( select );
3068 bool isSelected() const {
3069 return selected_edge();
3073 void testSelect( Selector& selector, SelectionTest& test ){
3074 SelectionIntersection best;
3075 m_edge->testSelect( test, best );
3076 if ( best.valid() ) {
3077 Selector_add( selector, *this, best );
3082 class VertexInstance : public Selectable
3084 FaceInstances& m_faceInstances;
3085 SelectableVertex* m_vertex;
3087 void select_vertex( bool select ){
3088 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3091 m_faceInstances[faceVertex.getFace()].select_vertex( faceVertex.getVertex(), select );
3092 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
3094 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
3097 bool selected_vertex() const {
3098 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3101 if ( !m_faceInstances[faceVertex.getFace()].selected_vertex( faceVertex.getVertex() ) ) {
3104 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
3106 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
3111 VertexInstance( FaceInstances& faceInstances, SelectableVertex& vertex )
3112 : m_faceInstances( faceInstances ), m_vertex( &vertex ){
3114 VertexInstance& operator=( const VertexInstance& other ){
3115 m_vertex = other.m_vertex;
3119 void setSelected( bool select ){
3120 select_vertex( select );
3123 bool isSelected() const {
3124 return selected_vertex();
3127 void testSelect( Selector& selector, SelectionTest& test ){
3128 SelectionIntersection best;
3129 m_vertex->testSelect( test, best );
3130 if ( best.valid() ) {
3131 Selector_add( selector, *this, best );
3135 void selectVerticesOnPlanes( SelectionTest& test ){
3136 Line line( test.getNear(), test.getFar() );
3137 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3140 if( m_faceInstances[faceVertex.getFace()].trySelectPlane( line ) ){
3141 //m_faceInstances[faceVertex.getFace()].select_vertex( faceVertex.getVertex(), true );
3142 setSelected( true );
3144 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
3146 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
3149 void selectVerticesOnTestedFaces( SelectionTest& test ){
3150 FaceVertexId faceVertex = m_vertex->m_faceVertex;
3153 BoolSelector selector;
3154 m_faceInstances[faceVertex.getFace()].testSelect( selector, test );
3155 if( selector.isSelected() ){
3156 setSelected( true );
3158 faceVertex = next_vertex( m_vertex->m_faces, faceVertex );
3160 while ( faceVertex.getFace() != m_vertex->m_faceVertex.getFace() );
3164 class BrushInstanceVisitor
3167 virtual void visit( FaceInstance& face ) const = 0;
3170 class BrushInstance :
3171 public BrushObserver,
3172 public scene::Instance,
3175 public SelectionTestable,
3176 public ComponentSelectionTestable,
3177 public ComponentEditable,
3178 public ComponentSnappable,
3179 public PlaneSelectable,
3180 public LightCullable
3184 InstanceTypeCastTable m_casts;
3187 InstanceStaticCast<BrushInstance, Selectable>::install( m_casts );
3188 InstanceContainedCast<BrushInstance, Bounded>::install( m_casts );
3189 InstanceContainedCast<BrushInstance, Cullable>::install( m_casts );
3190 InstanceStaticCast<BrushInstance, Renderable>::install( m_casts );
3191 InstanceStaticCast<BrushInstance, SelectionTestable>::install( m_casts );
3192 InstanceStaticCast<BrushInstance, ComponentSelectionTestable>::install( m_casts );
3193 InstanceStaticCast<BrushInstance, ComponentEditable>::install( m_casts );
3194 InstanceStaticCast<BrushInstance, ComponentSnappable>::install( m_casts );
3195 InstanceStaticCast<BrushInstance, PlaneSelectable>::install( m_casts );
3196 InstanceIdentityCast<BrushInstance>::install( m_casts );
3197 InstanceContainedCast<BrushInstance, Transformable>::install( m_casts );
3200 InstanceTypeCastTable& get(){
3208 FaceInstances m_faceInstances;
3210 typedef std::vector<EdgeInstance> EdgeInstances;
3211 EdgeInstances m_edgeInstances;
3212 typedef std::vector<VertexInstance> VertexInstances;
3213 VertexInstances m_vertexInstances;
3215 ObservedSelectable m_selectable;
3217 mutable RenderableWireframe m_render_wireframe;
3218 mutable RenderablePointVector m_render_selected;
3219 mutable AABB m_aabb_component;
3220 mutable Array<PointVertex> m_faceCentroidPointsCulled;
3221 RenderablePointArray m_render_faces_wireframe;
3222 mutable bool m_viewChanged; // requires re-evaluation of view-dependent cached data
3224 BrushClipPlane m_clipPlane;
3226 static Shader* m_state_selpoint;
3228 const LightList* m_lightList;
3230 TransformModifier m_transform;
3232 BrushInstance( const BrushInstance& other ); // NOT COPYABLE
3233 BrushInstance& operator=( const BrushInstance& other ); // NOT ASSIGNABLE
3236 static Counter* m_counter;
3238 typedef LazyStatic<TypeCasts> StaticTypeCasts;
3240 void lightsChanged(){
3241 m_lightList->lightsChanged();
3244 typedef MemberCaller<BrushInstance, void(), &BrushInstance::lightsChanged> LightsChangedCaller;
3246 STRING_CONSTANT( Name, "BrushInstance" );
3248 BrushInstance( const scene::Path& path, scene::Instance* parent, Brush& brush ) :
3249 Instance( path, parent, this, StaticTypeCasts::instance().get() ),
3251 m_selectable( SelectedChangedCaller( *this ) ),
3252 m_render_selected( GL_POINTS ),
3253 m_render_faces_wireframe( m_faceCentroidPointsCulled, GL_POINTS ),
3254 m_viewChanged( false ),
3255 m_transform( Brush::TransformChangedCaller( m_brush ), ApplyTransformCaller( *this ) ){
3256 m_brush.instanceAttach( Instance::path() );
3257 m_brush.attach( *this );
3258 m_counter->increment();
3260 m_lightList = &GlobalShaderCache().attach( *this );
3261 m_brush.m_lightsChanged = LightsChangedCaller( *this ); ///\todo Make this work with instancing.
3263 Instance::setTransformChangedCallback( LightsChangedCaller( *this ) );
3267 Instance::setTransformChangedCallback( Callback<void()>() );
3269 m_brush.m_lightsChanged = Callback<void()>();
3270 GlobalShaderCache().detach( *this );
3272 m_counter->decrement();
3273 m_brush.detach( *this );
3274 m_brush.instanceDetach( Instance::path() );
3280 const Brush& getBrush() const {
3284 Bounded& get( NullType<Bounded>){
3288 Cullable& get( NullType<Cullable>){
3292 Transformable& get( NullType<Transformable>){
3296 void selectedChanged( const Selectable& selectable ){
3297 GlobalSelectionSystem().getObserver ( SelectionSystem::ePrimitive )( selectable );
3298 GlobalSelectionSystem().onSelectedChanged( *this, selectable );
3300 Instance::selectedChanged();
3302 typedef MemberCaller<BrushInstance, void(const Selectable&), &BrushInstance::selectedChanged> SelectedChangedCaller;
3304 void selectedChangedComponent( const Selectable& selectable ){
3305 GlobalSelectionSystem().getObserver ( SelectionSystem::eComponent )( selectable );
3306 GlobalSelectionSystem().onComponentSelection( *this, selectable );
3308 typedef MemberCaller<BrushInstance, void(const Selectable&), &BrushInstance::selectedChangedComponent> SelectedChangedComponentCaller;
3310 const BrushInstanceVisitor& forEachFaceInstance( const BrushInstanceVisitor& visitor ){
3311 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3313 visitor.visit( *i );
3318 static void constructStatic(){
3319 m_state_selpoint = GlobalShaderCache().capture( "$SELPOINT" );
3322 static void destroyStatic(){
3323 GlobalShaderCache().release( "$SELPOINT" );
3327 m_faceInstances.clear();
3330 void reserve( std::size_t size ){
3331 m_faceInstances.reserve( size );
3334 void push_back( Face& face ){
3335 m_faceInstances.push_back( FaceInstance( face, SelectedChangedComponentCaller( *this ) ) );
3339 ASSERT_MESSAGE( !m_faceInstances.empty(), "erasing invalid element" );
3340 m_faceInstances.pop_back();
3343 void erase( std::size_t index ){
3344 ASSERT_MESSAGE( index < m_faceInstances.size(), "erasing invalid element" );
3345 m_faceInstances.erase( m_faceInstances.begin() + index );
3348 void connectivityChanged(){
3349 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3351 ( *i ).connectivityChanged();
3356 m_edgeInstances.clear();
3359 void edge_push_back( SelectableEdge& edge ){
3360 m_edgeInstances.push_back( EdgeInstance( m_faceInstances, edge ) );
3363 void vertex_clear(){
3364 m_vertexInstances.clear();
3367 void vertex_push_back( SelectableVertex& vertex ){
3368 m_vertexInstances.push_back( VertexInstance( m_faceInstances, vertex ) );
3371 void DEBUG_verify() const {
3372 ASSERT_MESSAGE( m_faceInstances.size() == m_brush.DEBUG_size(), "FATAL: mismatch" );
3375 bool isSelected() const {
3376 return m_selectable.isSelected();
3379 void setSelected( bool select ){
3380 m_selectable.setSelected( select );
3381 if ( !select && parent() ){
3382 Selectable* sel_parent = Instance_getSelectable( *parent() );
3383 if ( sel_parent && sel_parent->isSelected() )
3384 sel_parent->setSelected( false );
3388 void update_selected() const {
3389 m_render_selected.clear();
3390 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3392 if ( ( *i ).getFace().contributes() ) {
3393 ( *i ).iterate_selected( m_render_selected );
3398 void evaluateViewDependent( const VolumeTest& volume, const Matrix4& localToWorld ) const {
3399 if ( m_viewChanged ) {
3400 m_viewChanged = false;
3402 bool faces_visible[c_brush_maxFaces];
3404 bool* j = faces_visible;
3405 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i, ++j )
3407 *j = ( *i ).intersectVolume( volume, localToWorld );
3411 m_brush.update_wireframe( m_render_wireframe, faces_visible );
3412 m_brush.update_faces_wireframe( m_faceCentroidPointsCulled, faces_visible );
3416 void renderComponentsSelected( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3417 m_brush.evaluateBRep();
3420 if ( !m_render_selected.empty() ) {
3421 renderer.Highlight( Renderer::ePrimitive, false );
3422 renderer.SetState( m_state_selpoint, Renderer::eWireframeOnly );
3423 renderer.SetState( m_state_selpoint, Renderer::eFullMaterials );
3424 renderer.addRenderable( m_render_selected, localToWorld );
3428 void renderComponents( Renderer& renderer, const VolumeTest& volume ) const {
3429 m_brush.evaluateBRep();
3431 const Matrix4& localToWorld = Instance::localToWorld();
3433 renderer.SetState( m_brush.m_state_point, Renderer::eWireframeOnly );
3434 renderer.SetState( m_brush.m_state_point, Renderer::eFullMaterials );
3436 if ( volume.fill() && GlobalSelectionSystem().ComponentMode() == SelectionSystem::eFace ) {
3437 evaluateViewDependent( volume, localToWorld );
3438 renderer.addRenderable( m_render_faces_wireframe, localToWorld );
3442 m_brush.renderComponents( GlobalSelectionSystem().ComponentMode(), renderer, volume, localToWorld );
3446 void renderClipPlane( Renderer& renderer, const VolumeTest& volume ) const {
3447 if ( GlobalSelectionSystem().ManipulatorMode() == SelectionSystem::eClip && isSelected() ) {
3448 m_clipPlane.render( renderer, volume, localToWorld() );
3452 void renderCommon( Renderer& renderer, const VolumeTest& volume ) const {
3453 bool componentMode = GlobalSelectionSystem().Mode() == SelectionSystem::eComponent;
3455 if ( componentMode && isSelected() ) {
3456 renderComponents( renderer, volume );
3459 if ( parentSelected() ) {
3460 if ( !componentMode ) {
3461 renderer.Highlight( Renderer::eFace );
3463 renderer.Highlight( Renderer::ePrimitive );
3467 void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3468 //renderCommon(renderer, volume);
3470 m_lightList->evaluateLights();
3472 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3474 renderer.setLights( ( *i ).m_lights );
3475 ( *i ).render( renderer, volume, localToWorld );
3478 renderComponentsSelected( renderer, volume, localToWorld );
3481 void renderWireframe( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
3482 //renderCommon(renderer, volume);
3484 evaluateViewDependent( volume, localToWorld );
3486 if ( m_render_wireframe.m_size != 0 ) {
3487 renderer.addRenderable( m_render_wireframe, localToWorld );
3490 renderComponentsSelected( renderer, volume, localToWorld );
3493 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3494 m_brush.evaluateBRep();
3496 renderClipPlane( renderer, volume );
3498 renderSolid( renderer, volume, localToWorld() );
3501 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3502 m_brush.evaluateBRep();
3504 renderClipPlane( renderer, volume );
3506 renderWireframe( renderer, volume, localToWorld() );
3509 void viewChanged() const {
3510 m_viewChanged = true;
3513 void testSelect( Selector& selector, SelectionTest& test ){
3514 test.BeginMesh( localToWorld() );
3516 SelectionIntersection best;
3517 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3519 ( *i ).testSelect( test, best );
3521 if ( best.valid() ) {
3522 selector.addIntersection( best );
3526 bool isSelectedComponents() const {
3527 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3529 if ( ( *i ).selectedComponents() ) {
3536 void setSelectedComponents( bool select, SelectionSystem::EComponentMode mode ){
3537 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3539 ( *i ).setSelected( mode, select );
3543 void testSelectComponents( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode ){
3544 test.BeginMesh( localToWorld() );
3548 case SelectionSystem::eVertex:
3550 for ( VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i )
3552 ( *i ).testSelect( selector, test );
3556 case SelectionSystem::eEdge:
3558 for ( EdgeInstances::iterator i = m_edgeInstances.begin(); i != m_edgeInstances.end(); ++i )
3560 ( *i ).testSelect( selector, test );
3564 case SelectionSystem::eFace:
3566 if ( test.getVolume().fill() ) {
3567 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3569 ( *i ).testSelect( selector, test );
3574 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3576 ( *i ).testSelect_centroid( selector, test );
3586 void invertComponentSelection( SelectionSystem::EComponentMode mode ){
3589 case SelectionSystem::eVertex:
3591 for ( VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i )
3593 ( *i ).setSelected( !( *i ).isSelected() );
3597 case SelectionSystem::eEdge:
3599 for ( EdgeInstances::iterator i = m_edgeInstances.begin(); i != m_edgeInstances.end(); ++i )
3601 ( *i ).setSelected( !( *i ).isSelected() );
3605 case SelectionSystem::eFace:
3607 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3609 if( !( *i ).getFace().isFiltered() )
3610 ( *i ).setSelected( mode, !( *i ).isSelected() );
3619 void selectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
3620 test.BeginMesh( localToWorld() );
3622 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3624 ( *i ).selectPlane( selector, Line( test.getNear(), test.getFar() ), selectedPlaneCallback );
3628 void selectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes ){
3629 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3631 ( *i ).selectReversedPlane( selector, selectedPlanes );
3636 void selectVerticesOnPlanes( SelectionTest& test ){
3637 test.BeginMesh( localToWorld() );
3639 for ( VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i ){
3640 ( *i ).selectVerticesOnPlanes( test );
3645 void selectVerticesOnTestedFaces( SelectionTest& test ){
3646 test.BeginMesh( localToWorld() );
3648 for ( VertexInstances::iterator i = m_vertexInstances.begin(); i != m_vertexInstances.end(); ++i ){
3649 ( *i ).selectVerticesOnTestedFaces( test );
3654 void transformComponents( const Matrix4& matrix ){
3655 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3657 ( *i ).transformComponents( matrix );
3661 const AABB& getSelectedComponentsBounds() const {
3662 m_aabb_component = AABB();
3664 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3666 ( *i ).iterate_selected( m_aabb_component );
3669 return m_aabb_component;
3672 void snapComponents( float snap ){
3673 for ( FaceInstances::iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3675 ( *i ).snapComponents( snap );
3679 void evaluateTransform(){
3680 Matrix4 matrix( m_transform.calculateTransform() );
3681 //globalOutputStream() << "matrix: " << matrix << "\n";
3683 if ( m_transform.getType() == TRANSFORM_PRIMITIVE ) {
3684 m_brush.transform( matrix );
3688 transformComponents( matrix );
3692 void applyTransform(){
3693 m_brush.revertTransform();
3694 evaluateTransform();
3695 m_brush.freezeTransform();
3698 typedef MemberCaller<BrushInstance, void(), &BrushInstance::applyTransform> ApplyTransformCaller;
3700 void setClipPlane( const Plane3& plane ){
3701 m_clipPlane.setPlane( m_brush, plane );
3704 bool testLight( const RendererLight& light ) const {
3705 return light.testAABB( worldAABB() );
3708 void insertLight( const RendererLight& light ){
3709 const Matrix4& localToWorld = Instance::localToWorld();
3710 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3712 Face_addLight( *i, localToWorld, light );
3717 for ( FaceInstances::const_iterator i = m_faceInstances.begin(); i != m_faceInstances.end(); ++i )
3719 ( *i ).m_lights.clear();
3724 inline BrushInstance* Instance_getBrush( scene::Instance& instance ){
3725 return InstanceTypeCast<BrushInstance>::cast( instance );
3729 template<typename Functor>
3730 class BrushSelectedVisitor : public SelectionSystem::Visitor
3732 const Functor& m_functor;
3734 BrushSelectedVisitor( const Functor& functor ) : m_functor( functor ){
3737 void visit( scene::Instance& instance ) const {
3738 BrushInstance* brush = Instance_getBrush( instance );
3740 m_functor( *brush );
3745 template<typename Functor>
3746 inline const Functor& Scene_forEachSelectedBrush( const Functor& functor ){
3747 GlobalSelectionSystem().foreachSelected( BrushSelectedVisitor<Functor>( functor ) );
3751 template<typename Functor>
3752 class BrushVisibleSelectedVisitor : public SelectionSystem::Visitor
3754 const Functor& m_functor;
3756 BrushVisibleSelectedVisitor( const Functor& functor ) : m_functor( functor ){
3759 void visit( scene::Instance& instance ) const {
3760 BrushInstance* brush = Instance_getBrush( instance );
3762 && instance.path().top().get().visible() ) {
3763 m_functor( *brush );
3768 template<typename Functor>
3769 inline const Functor& Scene_forEachVisibleSelectedBrush( const Functor& functor ){
3770 GlobalSelectionSystem().foreachSelected( BrushVisibleSelectedVisitor<Functor>( functor ) );
3774 class BrushForEachFace
3776 const BrushInstanceVisitor& m_visitor;
3778 BrushForEachFace( const BrushInstanceVisitor& visitor ) : m_visitor( visitor ){
3781 void operator()( BrushInstance& brush ) const {
3782 brush.forEachFaceInstance( m_visitor );
3786 template<class Functor>
3787 class FaceInstanceVisitFace : public BrushInstanceVisitor
3789 const Functor& functor;
3791 FaceInstanceVisitFace( const Functor& functor )
3792 : functor( functor ){
3795 void visit( FaceInstance& face ) const {
3796 functor( face.getFace() );
3800 template<typename Functor>
3801 inline const Functor& Brush_forEachFace( BrushInstance& brush, const Functor& functor ){
3802 brush.forEachFaceInstance( FaceInstanceVisitFace<Functor>( functor ) );
3806 template<class Functor>
3807 class FaceVisitAll : public BrushVisitor
3809 const Functor& functor;
3811 FaceVisitAll( const Functor& functor )
3812 : functor( functor ){
3815 void visit( Face& face ) const {
3820 template<typename Functor>
3821 inline const Functor& Brush_forEachFace( const Brush& brush, const Functor& functor ){
3822 brush.forEachFace( FaceVisitAll<Functor>( functor ) );
3826 template<typename Functor>
3827 inline const Functor& Brush_forEachFace( Brush& brush, const Functor& functor ){
3828 brush.forEachFace( FaceVisitAll<Functor>( functor ) );
3832 template<class Functor>
3833 class FaceInstanceVisitAll : public BrushInstanceVisitor
3835 const Functor& functor;
3837 FaceInstanceVisitAll( const Functor& functor )
3838 : functor( functor ){
3841 void visit( FaceInstance& face ) const {
3846 template<typename Functor>
3847 inline const Functor& Brush_ForEachFaceInstance( BrushInstance& brush, const Functor& functor ){
3848 brush.forEachFaceInstance( FaceInstanceVisitAll<Functor>( functor ) );
3852 template<typename Functor>
3853 inline const Functor& Scene_forEachBrush( scene::Graph& graph, const Functor& functor ){
3854 graph.traverse( InstanceWalker< InstanceApply<BrushInstance, Functor> >( functor ) );
3858 template<typename Type, typename Functor>
3859 class InstanceIfVisible : public Functor
3862 InstanceIfVisible( const Functor& functor ) : Functor( functor ){
3865 void operator()( scene::Instance& instance ){
3866 if ( instance.path().top().get().visible() ) {
3867 Functor::operator()( instance );
3872 template<typename Functor>
3873 class BrushVisibleWalker : public scene::Graph::Walker
3875 const Functor& m_functor;
3877 BrushVisibleWalker( const Functor& functor ) : m_functor( functor ){
3880 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3881 if ( path.top().get().visible() ) {
3882 BrushInstance* brush = Instance_getBrush( instance );
3884 m_functor( *brush );
3891 template<typename Functor>
3892 inline const Functor& Scene_forEachVisibleBrush( scene::Graph& graph, const Functor& functor ){
3893 graph.traverse( BrushVisibleWalker<Functor>( functor ) );
3897 template<typename Functor>
3898 inline const Functor& Scene_ForEachBrush_ForEachFace( scene::Graph& graph, const Functor& functor ){
3899 Scene_forEachBrush( graph, BrushForEachFace( FaceInstanceVisitFace<Functor>( functor ) ) );
3904 template<typename Functor>
3905 inline const Functor& Scene_ForEachBrush_ForEachFaceInstance( scene::Graph& graph, const Functor& functor ){
3906 Scene_forEachBrush( graph, BrushForEachFace( FaceInstanceVisitAll<Functor>( functor ) ) );
3910 template<typename Functor>
3911 inline const Functor& Scene_ForEachSelectedBrush_ForEachFace( scene::Graph& graph, const Functor& functor ){
3912 Scene_forEachSelectedBrush( BrushForEachFace( FaceInstanceVisitFace<Functor>( functor ) ) );
3916 template<typename Functor>
3917 inline const Functor& Scene_ForEachSelectedBrush_ForEachFaceInstance( scene::Graph& graph, const Functor& functor ){
3918 Scene_forEachSelectedBrush( BrushForEachFace( FaceInstanceVisitAll<Functor>( functor ) ) );
3922 template<typename Functor>
3923 class FaceVisitorWrapper
3925 const Functor& functor;
3927 FaceVisitorWrapper( const Functor& functor ) : functor( functor ){
3930 void operator()( FaceInstance& faceInstance ) const {
3931 functor( faceInstance.getFace() );
3935 template<typename Functor>
3936 inline const Functor& Scene_ForEachSelectedBrushFace( scene::Graph& graph, const Functor& functor ){
3937 g_SelectedFaceInstances.foreach( FaceVisitorWrapper<Functor>( functor ) );