2 Copyright (C) 2001-2006, William Joseph.
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_PATCH_H )
23 #define INCLUDED_PATCH_H
26 /// \brief The patch primitive.
28 /// A 2-dimensional matrix of vertices that define a quadratic bezier surface.
29 /// The Boundary-Representation of this primitive is a triangle mesh.
30 /// The surface is recursively tesselated until the angle between each triangle
31 /// edge is smaller than a specified tolerance.
39 #include "renderable.h"
41 #include "selectable.h"
43 #include "debugging/debugging.h"
48 #include "math/frustum.h"
49 #include "string/string.h"
50 #include "stream/stringstream.h"
51 #include "stream/textstream.h"
52 #include "xml/xmlelement.h"
54 #include "transformlib.h"
55 #include "instancelib.h"
56 #include "selectionlib.h"
57 #include "traverselib.h"
60 #include "shaderlib.h"
61 #include "generic/callback.h"
62 #include "signal/signalfwd.h"
63 #include "texturelib.h"
65 #include "dragplanes.h"
73 extern int g_PatchSubdivideThreshold;
76 #define MIN_PATCH_WIDTH 3
77 #define MIN_PATCH_HEIGHT 3
79 extern std::size_t MAX_PATCH_WIDTH;
80 extern std::size_t MAX_PATCH_HEIGHT;
82 #define MAX_PATCH_ROWCTRL ( ( ( MAX_PATCH_WIDTH - 1 ) - 1 ) / 2 )
83 #define MAX_PATCH_COLCTRL ( ( ( MAX_PATCH_HEIGHT - 1 ) - 1 ) / 2 )
122 const std::size_t BEZIERCURVETREE_MAX_INDEX = std::size_t( 1 ) << ( std::numeric_limits<std::size_t>::digits - 1 );
124 struct BezierCurveTree
127 BezierCurveTree* left;
128 BezierCurveTree* right;
131 inline bool BezierCurveTree_isLeaf( const BezierCurveTree* node ){
132 return node->left == 0 && node->right == 0;
135 void BezierCurveTree_Delete( BezierCurveTree *pCurve );
138 inline VertexPointer vertexpointer_arbitrarymeshvertex( const ArbitraryMeshVertex* array ){
139 return VertexPointer( VertexPointer::pointer( &array->vertex ), sizeof( ArbitraryMeshVertex ) );
142 typedef PatchControl* PatchControlIter;
143 typedef const PatchControl* PatchControlConstIter;
145 inline void copy_ctrl( PatchControlIter ctrl, PatchControlConstIter begin, PatchControlConstIter end ){
146 std::copy( begin, end, ctrl );
149 const Colour4b colour_corner( 0, 255, 0, 255 );
150 const Colour4b colour_inside( 255, 0, 255, 255 );
157 virtual bool filter( const Patch& patch ) const = 0;
160 bool patch_filtered( Patch& patch );
161 void add_patch_filter( PatchFilter& filter, int mask, bool invert = false );
163 void Patch_addTextureChangedCallback( const SignalHandler& handler );
164 void Patch_textureChanged();
166 inline void BezierCurveTreeArray_deleteAll( Array<BezierCurveTree*>& curveTrees ){
167 for ( Array<BezierCurveTree*>::iterator i = curveTrees.begin(); i != curveTrees.end(); ++i )
169 BezierCurveTree_Delete( *i );
173 inline void PatchControlArray_invert( Array<PatchControl>& ctrl, std::size_t width, std::size_t height ){
174 Array<PatchControl> tmp( width );
176 PatchControlIter from = ctrl.data() + ( width * ( height - 1 ) );
177 PatchControlIter to = ctrl.data();
178 for ( std::size_t h = 0; h != ( ( height - 1 ) >> 1 ); ++h, to += width, from -= width )
180 copy_ctrl( tmp.data(), to, to + width );
181 copy_ctrl( to, from, from + width );
182 copy_ctrl( from, tmp.data(), tmp.data() + width );
186 class PatchTesselation
190 : m_numStrips( 0 ), m_lenStrips( 0 ), m_nArrayWidth( 0 ), m_nArrayHeight( 0 ){
192 Array<ArbitraryMeshVertex> m_vertices;
193 Array<RenderIndex> m_indices;
194 std::size_t m_numStrips;
195 std::size_t m_lenStrips;
197 Array<std::size_t> m_arrayWidth;
198 std::size_t m_nArrayWidth;
199 Array<std::size_t> m_arrayHeight;
200 std::size_t m_nArrayHeight;
202 Array<BezierCurveTree*> m_curveTreeU;
203 Array<BezierCurveTree*> m_curveTreeV;
206 class RenderablePatchWireframe : public OpenGLRenderable
208 PatchTesselation& m_tess;
210 RenderablePatchWireframe( PatchTesselation& tess ) : m_tess( tess ){
212 void render( RenderStateFlags state ) const {
215 glVertexPointer( 3, GL_FLOAT, 0, 0 );
216 glDrawArrays( GL_TRIANGLE_FAN, 0, 0 );
220 glVertexPointer( 3, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->vertex );
221 for ( std::size_t i = 0; i <= m_tess.m_curveTreeV.size(); ++i )
223 glDrawArrays( GL_LINE_STRIP, GLint( n ), GLsizei( m_tess.m_nArrayWidth ) );
225 if ( i == m_tess.m_curveTreeV.size() ) {
229 if ( !BezierCurveTree_isLeaf( m_tess.m_curveTreeV[i] ) ) {
230 glDrawArrays( GL_LINE_STRIP, GLint( m_tess.m_curveTreeV[i]->index ), GLsizei( m_tess.m_nArrayWidth ) );
233 n += ( m_tess.m_arrayHeight[i] * m_tess.m_nArrayWidth );
239 const ArbitraryMeshVertex* p = m_tess.m_vertices.data();
240 std::size_t n = m_tess.m_nArrayWidth * sizeof( ArbitraryMeshVertex );
241 for ( std::size_t i = 0; i <= m_tess.m_curveTreeU.size(); ++i )
243 glVertexPointer( 3, GL_FLOAT, GLsizei( n ), &p->vertex );
244 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_tess.m_nArrayHeight ) );
246 if ( i == m_tess.m_curveTreeU.size() ) {
250 if ( !BezierCurveTree_isLeaf( m_tess.m_curveTreeU[i] ) ) {
251 glVertexPointer( 3, GL_FLOAT, GLsizei( n ), &( m_tess.m_vertices.data() + ( m_tess.m_curveTreeU[i]->index ) )->vertex );
252 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_tess.m_nArrayHeight ) );
255 p += m_tess.m_arrayWidth[i];
261 class RenderablePatchFixedWireframe : public OpenGLRenderable
263 PatchTesselation& m_tess;
265 RenderablePatchFixedWireframe( PatchTesselation& tess ) : m_tess( tess ){
267 void render( RenderStateFlags state ) const {
268 glVertexPointer( 3, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->vertex );
269 const RenderIndex* strip_indices = m_tess.m_indices.data();
270 for ( std::size_t i = 0; i < m_tess.m_numStrips; i++, strip_indices += m_tess.m_lenStrips )
272 glDrawElements( GL_QUAD_STRIP, GLsizei( m_tess.m_lenStrips ), RenderIndexTypeID, strip_indices );
277 class RenderablePatchSolid : public OpenGLRenderable
279 PatchTesselation& m_tess;
281 RenderablePatchSolid( PatchTesselation& tess ) : m_tess( tess ){
283 void RenderNormals() const;
284 void render( RenderStateFlags state ) const {
286 if ( ( state & RENDER_FILL ) == 0 ) {
287 RenderablePatchWireframe( m_tess ).render( state );
292 if ( ( state & RENDER_BUMP ) != 0 ) {
293 if ( GlobalShaderCache().useShaderLanguage() ) {
294 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->normal );
295 glVertexAttribPointerARB( c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->texcoord );
296 glVertexAttribPointerARB( c_attr_Tangent, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->tangent );
297 glVertexAttribPointerARB( c_attr_Binormal, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->bitangent );
301 glVertexAttribPointerARB( 11, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->normal );
302 glVertexAttribPointerARB( 8, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->texcoord );
303 glVertexAttribPointerARB( 9, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->tangent );
304 glVertexAttribPointerARB( 10, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->bitangent );
309 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->normal );
310 glTexCoordPointer( 2, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->texcoord );
312 glVertexPointer( 3, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_tess.m_vertices.data()->vertex );
313 const RenderIndex* strip_indices = m_tess.m_indices.data();
314 for ( std::size_t i = 0; i < m_tess.m_numStrips; i++, strip_indices += m_tess.m_lenStrips )
316 glDrawElements( GL_QUAD_STRIP, GLsizei( m_tess.m_lenStrips ), RenderIndexTypeID, strip_indices );
320 #if defined( _DEBUG )
326 // parametric surface defined by quadratic bezier control curves
330 public TransformNode,
348 xml_state_t( EState state )
351 EState state() const {
354 const char* content() const {
355 return m_content.c_str();
357 std::size_t write( const char* buffer, std::size_t length ){
358 return m_content.write( buffer, length );
362 StringOutputStream m_content;
365 std::vector<xml_state_t> m_xml_state;
367 typedef Array<PatchControl> PatchControlArray;
369 class SavedState : public UndoMemento
375 const PatchControlArray& ctrl,
378 std::size_t subdivisions_x,
379 std::size_t subdivisions_y
385 m_patchDef3( patchDef3 ),
386 m_subdivisions_x( subdivisions_x ),
387 m_subdivisions_y( subdivisions_y ){
394 std::size_t m_width, m_height;
395 CopiedString m_shader;
396 PatchControlArray m_ctrl;
398 std::size_t m_subdivisions_x;
399 std::size_t m_subdivisions_y;
406 virtual void allocate( std::size_t size ) = 0;
410 typedef UniqueSet<Observer*> Observers;
411 Observers m_observers;
415 AABB m_aabb_local; // local bbox
417 CopiedString m_shader;
421 std::size_t m_height;
424 std::size_t m_subdivisions_x;
425 std::size_t m_subdivisions_y;
428 UndoObserver* m_undoable_observer;
431 // dynamically allocated array of control points, size is m_width*m_height
432 PatchControlArray m_ctrl;
433 PatchControlArray m_ctrlTransformed;
435 PatchTesselation m_tess;
436 RenderablePatchSolid m_render_solid;
437 RenderablePatchWireframe m_render_wireframe;
438 RenderablePatchFixedWireframe m_render_wireframe_fixed;
440 static Shader* m_state_ctrl;
441 static Shader* m_state_lattice;
442 VertexBuffer<PointVertex> m_ctrl_vertices;
443 RenderableVertexBuffer m_render_ctrl;
444 IndexBuffer m_lattice_indices;
445 RenderableIndexBuffer m_render_lattice;
449 bool m_transformChanged;
450 Callback m_evaluateTransform;
451 Callback m_boundsChanged;
455 m_width = m_height = 0;
458 m_subdivisions_x = 0;
459 m_subdivisions_y = 0;
464 m_xml_state.push_back( xml_state_t::eDefault );
468 Callback m_lightsChanged;
470 static int m_CycleCapIndex; // = 0;
471 static EPatchType m_type;
473 STRING_CONSTANT( Name, "Patch" );
475 Patch( scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged ) :
477 m_shader( texdef_name_default() ),
479 m_undoable_observer( 0 ),
481 m_render_solid( m_tess ),
482 m_render_wireframe( m_tess ),
483 m_render_wireframe_fixed( m_tess ),
484 m_render_ctrl( GL_POINTS, m_ctrl_vertices ),
485 m_render_lattice( GL_LINES, m_lattice_indices, m_ctrl_vertices ),
486 m_transformChanged( false ),
487 m_evaluateTransform( evaluateTransform ),
488 m_boundsChanged( boundsChanged ){
491 Patch( const Patch& other, scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged ) :
493 m_shader( texdef_name_default() ),
495 m_undoable_observer( 0 ),
497 m_render_solid( m_tess ),
498 m_render_wireframe( m_tess ),
499 m_render_wireframe_fixed( m_tess ),
500 m_render_ctrl( GL_POINTS, m_ctrl_vertices ),
501 m_render_lattice( GL_LINES, m_lattice_indices, m_ctrl_vertices ),
502 m_transformChanged( false ),
503 m_evaluateTransform( evaluateTransform ),
504 m_boundsChanged( boundsChanged ){
507 m_patchDef3 = other.m_patchDef3;
508 m_subdivisions_x = other.m_subdivisions_x;
509 m_subdivisions_y = other.m_subdivisions_y;
510 setDims( other.m_width, other.m_height );
511 copy_ctrl( m_ctrl.data(), other.m_ctrl.data(), other.m_ctrl.data() + ( m_width * m_height ) );
512 SetShader( other.m_shader.c_str() );
513 controlPointsChanged();
516 Patch( const Patch& other ) :
517 XMLImporter( other ),
518 XMLExporter( other ),
519 TransformNode( other ),
527 m_undoable_observer( 0 ),
529 m_render_solid( m_tess ),
530 m_render_wireframe( m_tess ),
531 m_render_wireframe_fixed( m_tess ),
532 m_render_ctrl( GL_POINTS, m_ctrl_vertices ),
533 m_render_lattice( GL_LINES, m_lattice_indices, m_ctrl_vertices ),
534 m_transformChanged( false ),
535 m_evaluateTransform( other.m_evaluateTransform ),
536 m_boundsChanged( other.m_boundsChanged ){
539 m_patchDef3 = other.m_patchDef3;
540 m_subdivisions_x = other.m_subdivisions_x;
541 m_subdivisions_y = other.m_subdivisions_y;
542 setDims( other.m_width, other.m_height );
543 copy_ctrl( m_ctrl.data(), other.m_ctrl.data(), other.m_ctrl.data() + ( m_width * m_height ) );
544 SetShader( other.m_shader.c_str() );
545 controlPointsChanged();
549 BezierCurveTreeArray_deleteAll( m_tess.m_curveTreeU );
550 BezierCurveTreeArray_deleteAll( m_tess.m_curveTreeV );
554 ASSERT_MESSAGE( m_observers.empty(), "Patch::~Patch: observers still attached" );
557 InstanceCounter m_instanceCounter;
558 void instanceAttach( const scene::Path& path ){
559 if ( ++m_instanceCounter.m_count == 1 ) {
560 m_state->incrementUsed();
561 m_map = path_find_mapfile( path.begin(), path.end() );
562 m_undoable_observer = GlobalUndoSystem().observer( this );
563 GlobalFilterSystem().registerFilterable( *this );
567 ASSERT_MESSAGE( path_find_mapfile( path.begin(), path.end() ) == m_map, "node is instanced across more than one file" );
570 void instanceDetach( const scene::Path& path ){
571 if ( --m_instanceCounter.m_count == 0 ) {
573 m_undoable_observer = 0;
574 GlobalUndoSystem().release( this );
575 GlobalFilterSystem().unregisterFilterable( *this );
576 m_state->decrementUsed();
580 const char* name() const {
583 void attach( const NameCallback& callback ){
585 void detach( const NameCallback& callback ){
588 void attach( Observer* observer ){
589 observer->allocate( m_width * m_height );
591 m_observers.insert( observer );
593 void detach( Observer* observer ){
594 m_observers.erase( observer );
597 void updateFiltered(){
599 if ( patch_filtered( *this ) ) {
600 m_node->enable( scene::Node::eFiltered );
604 m_node->disable( scene::Node::eFiltered );
609 void onAllocate( std::size_t size ){
610 for ( Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i )
612 ( *i )->allocate( size );
616 const Matrix4& localToParent() const {
617 return g_matrix4_identity;
619 const AABB& localAABB() const {
622 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
623 return test.TestAABB( m_aabb_local, localToWorld );
625 void render_solid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
626 renderer.SetState( m_state, Renderer::eFullMaterials );
627 renderer.addRenderable( m_render_solid, localToWorld );
629 void render_wireframe( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
630 renderer.SetState( m_state, Renderer::eFullMaterials );
632 renderer.addRenderable( m_render_wireframe_fixed, localToWorld );
636 renderer.addRenderable( m_render_wireframe, localToWorld );
640 void render_component( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
641 renderer.SetState( m_state_lattice, Renderer::eWireframeOnly );
642 renderer.SetState( m_state_lattice, Renderer::eFullMaterials );
643 renderer.addRenderable( m_render_lattice, localToWorld );
645 renderer.SetState( m_state_ctrl, Renderer::eWireframeOnly );
646 renderer.SetState( m_state_ctrl, Renderer::eFullMaterials );
647 renderer.addRenderable( m_render_ctrl, localToWorld );
649 void testSelect( Selector& selector, SelectionTest& test ){
650 SelectionIntersection best;
651 IndexPointer::index_type* pIndex = m_tess.m_indices.data();
652 for ( std::size_t s = 0; s < m_tess.m_numStrips; s++ )
654 test.TestQuadStrip( vertexpointer_arbitrarymeshvertex( m_tess.m_vertices.data() ), IndexPointer( pIndex, m_tess.m_lenStrips ), best );
655 pIndex += m_tess.m_lenStrips;
657 if ( best.valid() ) {
658 selector.addIntersection( best );
661 void transform( const Matrix4& matrix ){
662 for ( PatchControlIter i = m_ctrlTransformed.data(); i != m_ctrlTransformed.data() + m_ctrlTransformed.size(); ++i )
664 matrix4_transform_point( matrix, ( *i ).m_vertex );
667 if ( matrix4_handedness( matrix ) == MATRIX4_LEFTHANDED ) {
668 PatchControlArray_invert( m_ctrlTransformed, m_width, m_height );
672 void transformChanged(){
673 m_transformChanged = true;
677 typedef MemberCaller<Patch, &Patch::transformChanged> TransformChangedCaller;
679 void evaluateTransform(){
680 if ( m_transformChanged ) {
681 m_transformChanged = false;
683 m_evaluateTransform();
687 void revertTransform(){
688 m_ctrlTransformed = m_ctrl;
690 void freezeTransform(){
693 ASSERT_MESSAGE( m_ctrlTransformed.size() == m_ctrl.size(), "Patch::freeze: size mismatch" );
694 std::copy( m_ctrlTransformed.begin(), m_ctrlTransformed.end(), m_ctrl.begin() );
697 void controlPointsChanged(){
702 bool isValid() const;
704 void snapto( float snap ){
707 for ( PatchControlIter i = m_ctrl.data(); i != m_ctrl.data() + m_ctrl.size(); ++i )
709 vector3_snap( ( *i ).m_vertex, snap );
712 controlPointsChanged();
718 void RenderDebug( RenderStateFlags state ) const;
719 void RenderNormals( RenderStateFlags state ) const;
721 void pushElement( const XMLElement& element ){
722 switch ( m_xml_state.back().state() )
724 case xml_state_t::eDefault:
725 ASSERT_MESSAGE( string_equal( element.name(), "patch" ), "parse error" );
726 m_xml_state.push_back( xml_state_t::ePatch );
728 case xml_state_t::ePatch:
729 if ( string_equal( element.name(), "matrix" ) ) {
730 setDims( atoi( element.attribute( "width" ) ), atoi( element.attribute( "height" ) ) );
731 m_xml_state.push_back( xml_state_t::eMatrix );
733 else if ( string_equal( element.name(), "shader" ) ) {
734 m_xml_state.push_back( xml_state_t::eShader );
738 ERROR_MESSAGE( "parse error" );
742 void popElement( const char* name ){
743 switch ( m_xml_state.back().state() )
745 case xml_state_t::eDefault:
746 ERROR_MESSAGE( "parse error" );
748 case xml_state_t::ePatch:
750 case xml_state_t::eMatrix:
752 StringTokeniser content( m_xml_state.back().content() );
754 for ( PatchControlIter i = m_ctrl.data(), end = m_ctrl.data() + m_ctrl.size(); i != end; ++i )
756 ( *i ).m_vertex[0] = string_read_float( content.getToken() );
757 ( *i ).m_vertex[1] = string_read_float( content.getToken() );
758 ( *i ).m_vertex[2] = string_read_float( content.getToken() );
759 ( *i ).m_texcoord[0] = string_read_float( content.getToken() );
760 ( *i ).m_texcoord[1] = string_read_float( content.getToken() );
762 controlPointsChanged();
765 case xml_state_t::eShader:
767 SetShader( m_xml_state.back().content() );
771 ERROR_MESSAGE( "parse error" );
774 ASSERT_MESSAGE( !m_xml_state.empty(), "popping empty stack" );
775 m_xml_state.pop_back();
777 std::size_t write( const char* buffer, std::size_t length ){
778 switch ( m_xml_state.back().state() )
780 case xml_state_t::eDefault:
782 case xml_state_t::ePatch:
784 case xml_state_t::eMatrix:
785 case xml_state_t::eShader:
786 return m_xml_state.back().write( buffer, length );
789 ERROR_MESSAGE( "parse error" );
794 void exportXML( XMLImporter& importer ){
795 StaticElement patchElement( "patch" );
796 importer.pushElement( patchElement );
799 const StaticElement element( "shader" );
800 importer.pushElement( element );
801 importer.write( m_shader.c_str(), strlen( m_shader.c_str() ) );
802 importer.popElement( element.name() );
806 char width[16], height[16];
807 sprintf( width, "%u", Unsigned( m_width ) );
808 sprintf( height, "%u", Unsigned( m_height ) );
809 StaticElement element( "matrix" );
810 element.insertAttribute( "width", width );
811 element.insertAttribute( "height", height );
813 importer.pushElement( element );
815 for ( PatchControlIter i = m_ctrl.data(), end = m_ctrl.data() + m_ctrl.size(); i != end; ++i )
817 importer << ( *i ).m_vertex[0]
818 << ' ' << ( *i ).m_vertex[1]
819 << ' ' << ( *i ).m_vertex[2]
820 << ' ' << ( *i ).m_texcoord[0]
821 << ' ' << ( *i ).m_texcoord[1];
824 importer.popElement( element.name() );
827 importer.popElement( patchElement.name() );
830 void UpdateCachedData();
832 const char *GetShader() const {
833 return m_shader.c_str();
835 void SetShader( const char* name ){
836 ASSERT_NOTNULL( name );
838 if ( shader_equal( m_shader.c_str(), name ) ) {
844 if ( m_instanceCounter.m_count != 0 ) {
845 m_state->decrementUsed();
850 if ( m_instanceCounter.m_count != 0 ) {
851 m_state->incrementUsed();
855 Patch_textureChanged();
857 int getShaderFlags() const {
858 if ( m_state != 0 ) {
859 return m_state->getFlags();
864 typedef PatchControl* iterator;
865 typedef const PatchControl* const_iterator;
868 return m_ctrl.data();
870 const_iterator begin() const {
871 return m_ctrl.data();
874 return m_ctrl.data() + m_ctrl.size();
876 const_iterator end() const {
877 return m_ctrl.data() + m_ctrl.size();
880 PatchControlArray& getControlPoints(){
884 // Same as above, just for const arguments
885 const PatchControlArray& getControlPoints() const {
889 PatchControlArray& getControlPointsTransformed(){
890 return m_ctrlTransformed;
893 void setDims( std::size_t w, std::size_t h );
894 std::size_t getWidth() const {
897 std::size_t getHeight() const {
900 PatchControl& ctrlAt( std::size_t row, std::size_t col ){
901 return m_ctrl[row * m_width + col];
903 const PatchControl& ctrlAt( std::size_t row, std::size_t col ) const {
904 return m_ctrl[row * m_width + col];
907 void ConstructPrefab( const AABB& aabb, EPatchPrefab eType, int axis, std::size_t width = 3, std::size_t height = 3 );
908 void constructPlane( const AABB& aabb, int axis, std::size_t width, std::size_t height );
910 void TransposeMatrix();
911 void Redisperse( EMatrixMajor mt );
912 void Smooth( EMatrixMajor mt );
913 void InsertRemove( bool bInsert, bool bColumn, bool bFirst );
914 Patch* MakeCap( Patch* patch, EPatchCap eType, EMatrixMajor mt, bool bFirst );
915 void ConstructSeam( EPatchCap eType, Vector3* p, std::size_t width );
917 void FlipTexture( int nAxis );
918 void TranslateTexture( float s, float t );
919 void ScaleTexture( float s, float t );
920 void RotateTexture( float angle );
921 void SetTextureRepeat( float s, float t ); // call with s=1 t=1 for FIT
923 void NaturalTexture();
924 void ProjectTexture( int nAxis );
925 void createThickenedOpposite(const Patch& sourcePatch, const float thickness, const int axis, bool& no12, bool& no34 );
926 void createThickenedWall(const Patch& sourcePatch, const Patch& targetPatch, const int wallIndex);
932 if ( m_undoable_observer != 0 ) {
933 m_undoable_observer->save( this );
937 UndoMemento* exportState() const {
938 return new SavedState( m_width, m_height, m_ctrl, m_shader.c_str(), m_patchDef3, m_subdivisions_x, m_subdivisions_y );
940 void importState( const UndoMemento* state ){
943 const SavedState& other = *( static_cast<const SavedState*>( state ) );
945 // begin duplicate of SavedState copy constructor, needs refactoring
949 m_width = other.m_width;
950 m_height = other.m_height;
951 SetShader( other.m_shader.c_str() );
952 m_ctrl = other.m_ctrl;
953 onAllocate( m_ctrl.size() );
954 m_patchDef3 = other.m_patchDef3;
955 m_subdivisions_x = other.m_subdivisions_x;
956 m_subdivisions_y = other.m_subdivisions_y;
959 // end duplicate code
961 Patch_textureChanged();
963 controlPointsChanged();
966 static void constructStatic( EPatchType type ){
967 Patch::m_type = type;
968 Patch::m_state_ctrl = GlobalShaderCache().capture( "$POINT" );
969 Patch::m_state_lattice = GlobalShaderCache().capture( "$LATTICE" );
972 static void destroyStatic(){
973 GlobalShaderCache().release( "$LATTICE" );
974 GlobalShaderCache().release( "$POINT" );
977 void captureShader(){
978 m_state = GlobalShaderCache().capture( m_shader.c_str() );
981 void releaseShader(){
982 GlobalShaderCache().release( m_shader.c_str() );
986 if ( !shader_valid( GetShader() ) ) {
987 globalErrorStream() << "patch has invalid texture name: '" << GetShader() << "'\n";
991 void InsertPoints( EMatrixMajor mt, bool bFirst );
992 void RemovePoints( EMatrixMajor mt, bool bFirst );
994 void AccumulateBBox();
996 void TesselateSubMatrixFixed( ArbitraryMeshVertex * vertices, std::size_t strideX, std::size_t strideY, unsigned int nFlagsX, unsigned int nFlagsY, PatchControl * subMatrix[3][3] );
998 // uses binary trees representing bezier curves to recursively tesselate a bezier sub-patch
999 void TesselateSubMatrix( const BezierCurveTree *BX, const BezierCurveTree *BY,
1000 std::size_t offStartX, std::size_t offStartY,
1001 std::size_t offEndX, std::size_t offEndY,
1002 std::size_t nFlagsX, std::size_t nFlagsY,
1003 Vector3& left, Vector3& mid, Vector3& right,
1004 Vector2& texLeft, Vector2& texMid, Vector2& texRight,
1007 // tesselates the entire surface
1008 void BuildTesselationCurves( EMatrixMajor major );
1009 void accumulateVertexTangentSpace( std::size_t index, Vector3 tangentX[6], Vector3 tangentY[6], Vector2 tangentS[6], Vector2 tangentT[6], std::size_t index0, std::size_t index1 );
1010 void BuildVertexArray();
1013 inline bool Patch_importHeader( Patch& patch, Tokeniser& tokeniser ){
1014 tokeniser.nextLine();
1015 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, "{" ) );
1019 inline bool Patch_importShader( Patch& patch, Tokeniser& tokeniser ){
1020 // parse shader name
1021 tokeniser.nextLine();
1022 const char* texture = tokeniser.getToken();
1023 if ( texture == 0 ) {
1024 Tokeniser_unexpectedError( tokeniser, texture, "#texture-name" );
1027 if ( string_equal( texture, "NULL" ) ) {
1028 patch.SetShader( texdef_name_default() );
1032 StringOutputStream shader( string_length( GlobalTexturePrefix_get() ) + string_length( texture ) );
1033 shader << GlobalTexturePrefix_get() << texture;
1034 patch.SetShader( shader.c_str() );
1039 inline bool PatchDoom3_importShader( Patch& patch, Tokeniser& tokeniser ){
1040 // parse shader name
1041 tokeniser.nextLine();
1042 const char *shader = tokeniser.getToken();
1043 if ( shader == 0 ) {
1044 Tokeniser_unexpectedError( tokeniser, shader, "#shader-name" );
1047 if ( string_equal( shader, "_emptyname" ) ) {
1048 shader = texdef_name_default();
1050 patch.SetShader( shader );
1054 inline bool Patch_importParams( Patch& patch, Tokeniser& tokeniser ){
1055 tokeniser.nextLine();
1056 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, "(" ) );
1058 // parse matrix dimensions
1061 RETURN_FALSE_IF_FAIL( Tokeniser_getSize( tokeniser, c ) );
1062 RETURN_FALSE_IF_FAIL( Tokeniser_getSize( tokeniser, r ) );
1064 patch.setDims( c, r );
1067 if ( patch.m_patchDef3 ) {
1068 RETURN_FALSE_IF_FAIL( Tokeniser_getSize( tokeniser, patch.m_subdivisions_x ) );
1069 RETURN_FALSE_IF_FAIL( Tokeniser_getSize( tokeniser, patch.m_subdivisions_y ) );
1072 // ignore contents/flags/value
1074 RETURN_FALSE_IF_FAIL( Tokeniser_getInteger( tokeniser, tmp ) );
1075 RETURN_FALSE_IF_FAIL( Tokeniser_getInteger( tokeniser, tmp ) );
1076 RETURN_FALSE_IF_FAIL( Tokeniser_getInteger( tokeniser, tmp ) );
1078 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, ")" ) );
1082 inline bool Patch_importMatrix( Patch& patch, Tokeniser& tokeniser ){
1084 tokeniser.nextLine();
1085 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, "(" ) );
1087 for ( std::size_t c = 0; c < patch.getWidth(); c++ )
1089 tokeniser.nextLine();
1090 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, "(" ) );
1091 for ( std::size_t r = 0; r < patch.getHeight(); r++ )
1093 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, "(" ) );
1095 RETURN_FALSE_IF_FAIL( Tokeniser_getFloat( tokeniser, patch.ctrlAt( r,c ).m_vertex[0] ) );
1096 RETURN_FALSE_IF_FAIL( Tokeniser_getFloat( tokeniser, patch.ctrlAt( r,c ).m_vertex[1] ) );
1097 RETURN_FALSE_IF_FAIL( Tokeniser_getFloat( tokeniser, patch.ctrlAt( r,c ).m_vertex[2] ) );
1098 RETURN_FALSE_IF_FAIL( Tokeniser_getFloat( tokeniser, patch.ctrlAt( r,c ).m_texcoord[0] ) );
1099 RETURN_FALSE_IF_FAIL( Tokeniser_getFloat( tokeniser, patch.ctrlAt( r,c ).m_texcoord[1] ) );
1101 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, ")" ) );
1103 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, ")" ) );
1106 tokeniser.nextLine();
1107 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, ")" ) );
1111 inline bool Patch_importFooter( Patch& patch, Tokeniser& tokeniser ){
1112 patch.controlPointsChanged();
1114 tokeniser.nextLine();
1115 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, "}" ) );
1117 tokeniser.nextLine();
1118 RETURN_FALSE_IF_FAIL( Tokeniser_parseToken( tokeniser, "}" ) );
1122 class PatchTokenImporter : public MapImporter
1126 PatchTokenImporter( Patch& patch ) : m_patch( patch ){
1128 bool importTokens( Tokeniser& tokeniser ){
1129 RETURN_FALSE_IF_FAIL( Patch_importHeader( m_patch, tokeniser ) );
1130 RETURN_FALSE_IF_FAIL( Patch_importShader( m_patch, tokeniser ) );
1131 RETURN_FALSE_IF_FAIL( Patch_importParams( m_patch, tokeniser ) );
1132 RETURN_FALSE_IF_FAIL( Patch_importMatrix( m_patch, tokeniser ) );
1133 RETURN_FALSE_IF_FAIL( Patch_importFooter( m_patch, tokeniser ) );
1139 class PatchDoom3TokenImporter : public MapImporter
1143 PatchDoom3TokenImporter( Patch& patch ) : m_patch( patch ){
1145 bool importTokens( Tokeniser& tokeniser ){
1146 RETURN_FALSE_IF_FAIL( Patch_importHeader( m_patch, tokeniser ) );
1147 RETURN_FALSE_IF_FAIL( PatchDoom3_importShader( m_patch, tokeniser ) );
1148 RETURN_FALSE_IF_FAIL( Patch_importParams( m_patch, tokeniser ) );
1149 RETURN_FALSE_IF_FAIL( Patch_importMatrix( m_patch, tokeniser ) );
1150 RETURN_FALSE_IF_FAIL( Patch_importFooter( m_patch, tokeniser ) );
1156 inline void Patch_exportHeader( const Patch& patch, TokenWriter& writer ){
1157 writer.writeToken( "{" );
1159 writer.writeToken( patch.m_patchDef3 ? "patchDef3" : "patchDef2" );
1161 writer.writeToken( "{" );
1165 inline void Patch_exportShader( const Patch& patch, TokenWriter& writer ){
1166 // write shader name
1167 if ( *( shader_get_textureName( patch.GetShader() ) ) == '\0' ) {
1168 writer.writeToken( "NULL" );
1172 writer.writeToken( shader_get_textureName( patch.GetShader() ) );
1177 inline void PatchDoom3_exportShader( const Patch& patch, TokenWriter& writer ){
1178 // write shader name
1179 if ( *( shader_get_textureName( patch.GetShader() ) ) == '\0' ) {
1180 writer.writeString( "_emptyname" );
1184 writer.writeString( patch.GetShader() );
1189 inline void Patch_exportParams( const Patch& patch, TokenWriter& writer ){
1190 // write matrix dimensions
1191 writer.writeToken( "(" );
1192 writer.writeUnsigned( patch.getWidth() );
1193 writer.writeUnsigned( patch.getHeight() );
1194 if ( patch.m_patchDef3 ) {
1195 writer.writeUnsigned( patch.m_subdivisions_x );
1196 writer.writeUnsigned( patch.m_subdivisions_y );
1198 writer.writeInteger( 0 );
1199 writer.writeInteger( 0 );
1200 writer.writeInteger( 0 );
1201 writer.writeToken( ")" );
1205 inline void Patch_exportMatrix( const Patch& patch, TokenWriter& writer ){
1207 writer.writeToken( "(" );
1209 for ( std::size_t c = 0; c < patch.getWidth(); c++ )
1211 writer.writeToken( "(" );
1212 for ( std::size_t r = 0; r < patch.getHeight(); r++ )
1214 writer.writeToken( "(" );
1216 writer.writeFloat( patch.ctrlAt( r,c ).m_vertex[0] );
1217 writer.writeFloat( patch.ctrlAt( r,c ).m_vertex[1] );
1218 writer.writeFloat( patch.ctrlAt( r,c ).m_vertex[2] );
1219 writer.writeFloat( patch.ctrlAt( r,c ).m_texcoord[0] );
1220 writer.writeFloat( patch.ctrlAt( r,c ).m_texcoord[1] );
1222 writer.writeToken( ")" );
1224 writer.writeToken( ")" );
1227 writer.writeToken( ")" );
1231 inline void Patch_exportFooter( const Patch& patch, TokenWriter& writer ){
1232 writer.writeToken( "}" );
1234 writer.writeToken( "}" );
1238 class PatchTokenExporter : public MapExporter
1240 const Patch& m_patch;
1242 PatchTokenExporter( Patch& patch ) : m_patch( patch ){
1244 void exportTokens( TokenWriter& writer ) const {
1245 Patch_exportHeader( m_patch, writer );
1246 Patch_exportShader( m_patch, writer );
1247 Patch_exportParams( m_patch, writer );
1248 Patch_exportMatrix( m_patch, writer );
1249 Patch_exportFooter( m_patch, writer );
1253 class PatchDoom3TokenExporter : public MapExporter
1255 const Patch& m_patch;
1257 PatchDoom3TokenExporter( Patch& patch ) : m_patch( patch ){
1259 void exportTokens( TokenWriter& writer ) const {
1260 Patch_exportHeader( m_patch, writer );
1261 PatchDoom3_exportShader( m_patch, writer );
1262 Patch_exportParams( m_patch, writer );
1263 Patch_exportMatrix( m_patch, writer );
1264 Patch_exportFooter( m_patch, writer );
1268 class PatchControlInstance
1271 PatchControl* m_ctrl;
1272 ObservedSelectable m_selectable;
1274 PatchControlInstance( PatchControl* ctrl, const SelectionChangeCallback& observer )
1275 : m_ctrl( ctrl ), m_selectable( observer ){
1278 void testSelect( Selector& selector, SelectionTest& test ){
1279 SelectionIntersection best;
1280 test.TestPoint( m_ctrl->m_vertex, best );
1281 if ( best.valid() ) {
1282 Selector_add( selector, m_selectable, best );
1285 void snapto( float snap ){
1286 vector3_snap( m_ctrl->m_vertex, snap );
1291 class PatchInstance :
1292 public Patch::Observer,
1293 public scene::Instance,
1296 public SelectionTestable,
1297 public ComponentSelectionTestable,
1298 public ComponentEditable,
1299 public ComponentSnappable,
1300 public PlaneSelectable,
1301 public LightCullable
1305 InstanceTypeCastTable m_casts;
1308 InstanceStaticCast<PatchInstance, Selectable>::install( m_casts );
1309 InstanceContainedCast<PatchInstance, Bounded>::install( m_casts );
1310 InstanceContainedCast<PatchInstance, Cullable>::install( m_casts );
1311 InstanceStaticCast<PatchInstance, Renderable>::install( m_casts );
1312 InstanceStaticCast<PatchInstance, SelectionTestable>::install( m_casts );
1313 InstanceStaticCast<PatchInstance, ComponentSelectionTestable>::install( m_casts );
1314 InstanceStaticCast<PatchInstance, ComponentEditable>::install( m_casts );
1315 InstanceStaticCast<PatchInstance, ComponentSnappable>::install( m_casts );
1316 InstanceStaticCast<PatchInstance, PlaneSelectable>::install( m_casts );
1317 InstanceIdentityCast<PatchInstance>::install( m_casts );
1318 InstanceContainedCast<PatchInstance, Transformable>::install( m_casts );
1320 InstanceTypeCastTable& get(){
1327 typedef std::vector<PatchControlInstance> PatchControlInstances;
1328 PatchControlInstances m_ctrl_instances;
1330 ObservedSelectable m_selectable;
1332 DragPlanes m_dragPlanes;
1334 mutable RenderablePointVector m_render_selected;
1335 mutable AABB m_aabb_component;
1337 static Shader* m_state_selpoint;
1339 const LightList* m_lightList;
1341 TransformModifier m_transform;
1344 typedef LazyStatic<TypeCasts> StaticTypeCasts;
1346 void lightsChanged(){
1347 m_lightList->lightsChanged();
1349 typedef MemberCaller<PatchInstance, &PatchInstance::lightsChanged> LightsChangedCaller;
1351 STRING_CONSTANT( Name, "PatchInstance" );
1353 PatchInstance( const scene::Path& path, scene::Instance* parent, Patch& patch ) :
1354 Instance( path, parent, this, StaticTypeCasts::instance().get() ),
1356 m_selectable( SelectedChangedCaller( *this ) ),
1357 m_dragPlanes( SelectedChangedComponentCaller( *this ) ),
1358 m_render_selected( GL_POINTS ),
1359 m_transform( Patch::TransformChangedCaller( m_patch ), ApplyTransformCaller( *this ) ){
1360 m_patch.instanceAttach( Instance::path() );
1361 m_patch.attach( this );
1363 m_lightList = &GlobalShaderCache().attach( *this );
1364 m_patch.m_lightsChanged = LightsChangedCaller( *this );
1366 Instance::setTransformChangedCallback( LightsChangedCaller( *this ) );
1369 Instance::setTransformChangedCallback( Callback() );
1371 m_patch.m_lightsChanged = Callback();
1372 GlobalShaderCache().detach( *this );
1374 m_patch.detach( this );
1375 m_patch.instanceDetach( Instance::path() );
1378 void selectedChanged( const Selectable& selectable ){
1379 GlobalSelectionSystem().getObserver ( SelectionSystem::ePrimitive )( selectable );
1380 GlobalSelectionSystem().onSelectedChanged( *this, selectable );
1382 Instance::selectedChanged();
1384 typedef MemberCaller1<PatchInstance, const Selectable&, &PatchInstance::selectedChanged> SelectedChangedCaller;
1386 void selectedChangedComponent( const Selectable& selectable ){
1387 GlobalSelectionSystem().getObserver ( SelectionSystem::eComponent )( selectable );
1388 GlobalSelectionSystem().onComponentSelection( *this, selectable );
1390 typedef MemberCaller1<PatchInstance, const Selectable&, &PatchInstance::selectedChangedComponent> SelectedChangedComponentCaller;
1395 Bounded& get( NullType<Bounded>){
1398 Cullable& get( NullType<Cullable>){
1401 Transformable& get( NullType<Transformable>){
1405 static void constructStatic(){
1406 m_state_selpoint = GlobalShaderCache().capture( "$SELPOINT" );
1409 static void destroyStatic(){
1410 GlobalShaderCache().release( "$SELPOINT" );
1414 void allocate( std::size_t size ){
1415 m_ctrl_instances.clear();
1416 m_ctrl_instances.reserve( size );
1417 for ( Patch::iterator i = m_patch.begin(); i != m_patch.end(); ++i )
1419 m_ctrl_instances.push_back( PatchControlInstance( &( *i ), SelectedChangedComponentCaller( *this ) ) );
1423 void setSelected( bool select ){
1424 m_selectable.setSelected( select );
1425 if ( !select && parent() ){
1426 Selectable* sel_parent = Instance_getSelectable( *parent() );
1427 if ( sel_parent && sel_parent->isSelected() )
1428 sel_parent->setSelected( false );
1431 bool isSelected() const {
1432 return m_selectable.isSelected();
1436 void update_selected() const {
1437 m_render_selected.clear();
1438 Patch::iterator ctrl = m_patch.getControlPointsTransformed().begin();
1439 for ( PatchControlInstances::const_iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i, ++ctrl )
1441 if ( ( *i ).m_selectable.isSelected() ) {
1442 const Colour4b colour_selected( 0, 0, 255, 255 );
1443 m_render_selected.push_back( PointVertex( reinterpret_cast<Vertex3f&>( ( *ctrl ).m_vertex ), colour_selected ) );
1449 void render( Renderer& renderer, const VolumeTest& volume ) const {
1450 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
1451 && m_selectable.isSelected() ) {
1452 renderer.Highlight( Renderer::eFace, false );
1454 m_patch.render( renderer, volume, localToWorld() );
1456 if ( GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex ) {
1457 renderer.Highlight( Renderer::ePrimitive, false );
1459 m_patch.render_component( renderer, volume, localToWorld() );
1461 renderComponentsSelected( renderer, volume );
1465 m_patch.render( renderer, volume, localToWorld() );
1470 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
1471 m_patch.evaluateTransform();
1472 renderer.setLights( *m_lightList );
1473 m_patch.render_solid( renderer, volume, localToWorld() );
1475 renderComponentsSelected( renderer, volume );
1478 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
1479 m_patch.evaluateTransform();
1480 m_patch.render_wireframe( renderer, volume, localToWorld() );
1482 renderComponentsSelected( renderer, volume );
1485 void renderComponentsSelected( Renderer& renderer, const VolumeTest& volume ) const {
1486 m_patch.evaluateTransform();
1488 if ( !m_render_selected.empty() ) {
1489 renderer.Highlight( Renderer::ePrimitive, false );
1490 renderer.SetState( m_state_selpoint, Renderer::eWireframeOnly );
1491 renderer.SetState( m_state_selpoint, Renderer::eFullMaterials );
1492 renderer.addRenderable( m_render_selected, localToWorld() );
1495 void renderComponents( Renderer& renderer, const VolumeTest& volume ) const {
1496 m_patch.evaluateTransform();
1497 if ( GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex ) {
1498 m_patch.render_component( renderer, volume, localToWorld() );
1502 void testSelect( Selector& selector, SelectionTest& test ){
1503 test.BeginMesh( localToWorld(), true );
1504 m_patch.testSelect( selector, test );
1507 void selectCtrl( bool select ){
1508 for ( PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i )
1510 ( *i ).m_selectable.setSelected( select );
1513 bool isSelectedComponents() const {
1514 for ( PatchControlInstances::const_iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i )
1516 if ( ( *i ).m_selectable.isSelected() ) {
1522 void setSelectedComponents( bool select, SelectionSystem::EComponentMode mode ){
1523 if ( mode == SelectionSystem::eVertex ) {
1524 selectCtrl( select );
1526 else if ( mode == SelectionSystem::eFace ) {
1527 m_dragPlanes.setSelected( select );
1530 const AABB& getSelectedComponentsBounds() const {
1531 m_aabb_component = AABB();
1533 for ( PatchControlInstances::const_iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i )
1535 if ( ( *i ).m_selectable.isSelected() ) {
1536 aabb_extend_by_point_safe( m_aabb_component, ( *i ).m_ctrl->m_vertex );
1540 return m_aabb_component;
1543 void testSelectComponents( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode ){
1544 test.BeginMesh( localToWorld() );
1548 case SelectionSystem::eVertex:
1550 for ( PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i )
1552 ( *i ).testSelect( selector, test );
1561 bool selectedVertices(){
1562 for ( PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i )
1564 if ( ( *i ).m_selectable.isSelected() ) {
1571 void transformComponents( const Matrix4& matrix ){
1572 if ( selectedVertices() ) {
1573 PatchControlIter ctrl = m_patch.getControlPointsTransformed().begin();
1574 for ( PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i, ++ctrl )
1576 if ( ( *i ).m_selectable.isSelected() ) {
1577 matrix4_transform_point( matrix, ( *ctrl ).m_vertex );
1580 m_patch.UpdateCachedData();
1583 if ( m_dragPlanes.isSelected() ) { // this should only be true when the transform is a pure translation.
1584 m_patch.transform( m_dragPlanes.evaluateTransform( vector4_to_vector3( matrix.t() ) ) );
1589 void selectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
1590 test.BeginMesh( localToWorld() );
1592 m_dragPlanes.selectPlanes( m_patch.localAABB(), selector, test, selectedPlaneCallback );
1594 void selectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes ){
1595 m_dragPlanes.selectReversedPlanes( m_patch.localAABB(), selector, selectedPlanes );
1599 void snapComponents( float snap ){
1600 if ( selectedVertices() ) {
1602 for ( PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i )
1604 if ( ( *i ).m_selectable.isSelected() ) {
1605 ( *i ).snapto( snap );
1608 m_patch.controlPointsChanged();
1612 void evaluateTransform(){
1613 Matrix4 matrix( m_transform.calculateTransform() );
1615 if ( m_transform.getType() == TRANSFORM_PRIMITIVE ) {
1616 m_patch.transform( matrix );
1620 transformComponents( matrix );
1623 void applyTransform(){
1624 m_patch.revertTransform();
1625 evaluateTransform();
1626 m_patch.freezeTransform();
1628 typedef MemberCaller<PatchInstance, &PatchInstance::applyTransform> ApplyTransformCaller;
1631 bool testLight( const RendererLight& light ) const {
1632 return light.testAABB( worldAABB() );
1637 template<typename TokenImporter, typename TokenExporter>
1639 public scene::Node::Symbiot,
1640 public scene::Instantiable,
1641 public scene::Cloneable
1643 typedef PatchNode<TokenImporter, TokenExporter> Self;
1647 InstanceTypeCastTable m_casts;
1650 NodeStaticCast<PatchNode, scene::Instantiable>::install( m_casts );
1651 NodeStaticCast<PatchNode, scene::Cloneable>::install( m_casts );
1652 NodeContainedCast<PatchNode, Snappable>::install( m_casts );
1653 NodeContainedCast<PatchNode, TransformNode>::install( m_casts );
1654 NodeContainedCast<PatchNode, Patch>::install( m_casts );
1655 NodeContainedCast<PatchNode, XMLImporter>::install( m_casts );
1656 NodeContainedCast<PatchNode, XMLExporter>::install( m_casts );
1657 NodeContainedCast<PatchNode, MapImporter>::install( m_casts );
1658 NodeContainedCast<PatchNode, MapExporter>::install( m_casts );
1659 NodeContainedCast<PatchNode, Nameable>::install( m_casts );
1661 InstanceTypeCastTable& get(){
1668 InstanceSet m_instances;
1670 TokenImporter m_importMap;
1671 TokenExporter m_exportMap;
1675 typedef LazyStatic<TypeCasts> StaticTypeCasts;
1677 Snappable& get( NullType<Snappable>){
1680 TransformNode& get( NullType<TransformNode>){
1683 Patch& get( NullType<Patch>){
1686 XMLImporter& get( NullType<XMLImporter>){
1689 XMLExporter& get( NullType<XMLExporter>){
1692 MapImporter& get( NullType<MapImporter>){
1695 MapExporter& get( NullType<MapExporter>){
1698 Nameable& get( NullType<Nameable>){
1702 PatchNode( bool patchDef3 = false ) :
1703 m_node( this, this, StaticTypeCasts::instance().get() ),
1704 m_patch( m_node, InstanceSetEvaluateTransform<PatchInstance>::Caller( m_instances ), InstanceSet::BoundsChangedCaller( m_instances ) ),
1705 m_importMap( m_patch ),
1706 m_exportMap( m_patch ){
1707 m_patch.m_patchDef3 = patchDef3;
1709 PatchNode( const PatchNode& other ) :
1710 scene::Node::Symbiot( other ),
1711 scene::Instantiable( other ),
1712 scene::Cloneable( other ),
1713 m_node( this, this, StaticTypeCasts::instance().get() ),
1714 m_patch( other.m_patch, m_node, InstanceSetEvaluateTransform<PatchInstance>::Caller( m_instances ), InstanceSet::BoundsChangedCaller( m_instances ) ),
1715 m_importMap( m_patch ),
1716 m_exportMap( m_patch ){
1721 scene::Node& node(){
1727 const Patch& get() const {
1731 scene::Node& clone() const {
1732 return ( new PatchNode( *this ) )->node();
1735 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
1736 return new PatchInstance( path, parent, m_patch );
1738 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
1739 m_instances.forEachInstance( visitor );
1741 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
1742 m_instances.insert( observer, path, instance );
1744 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
1745 return m_instances.erase( observer, path );
1751 typedef PatchNode<PatchTokenImporter, PatchTokenExporter> PatchNodeQuake3;
1752 typedef PatchNode<PatchDoom3TokenImporter, PatchDoom3TokenExporter> PatchNodeDoom3;
1754 inline Patch* Node_getPatch( scene::Node& node ){
1755 return NodeTypeCast<Patch>::cast( node );
1758 inline PatchInstance* Instance_getPatch( scene::Instance& instance ){
1759 return InstanceTypeCast<PatchInstance>::cast( instance );
1762 template<typename Functor>
1763 class PatchSelectedVisitor : public SelectionSystem::Visitor
1765 const Functor& m_functor;
1767 PatchSelectedVisitor( const Functor& functor ) : m_functor( functor ){
1769 void visit( scene::Instance& instance ) const {
1770 PatchInstance* patch = Instance_getPatch( instance );
1772 m_functor( *patch );
1777 template<typename Functor>
1778 inline void Scene_forEachSelectedPatch( const Functor& functor ){
1779 GlobalSelectionSystem().foreachSelected( PatchSelectedVisitor<Functor>( functor ) );
1783 template<typename Functor>
1784 class PatchVisibleSelectedVisitor : public SelectionSystem::Visitor
1786 const Functor& m_functor;
1788 PatchVisibleSelectedVisitor( const Functor& functor ) : m_functor( functor ){
1790 void visit( scene::Instance& instance ) const {
1791 PatchInstance* patch = Instance_getPatch( instance );
1793 && instance.path().top().get().visible() ) {
1794 m_functor( *patch );
1799 template<typename Functor>
1800 inline void Scene_forEachVisibleSelectedPatchInstance( const Functor& functor ){
1801 GlobalSelectionSystem().foreachSelected( PatchVisibleSelectedVisitor<Functor>( functor ) );
1804 template<typename Functor>
1805 class PatchForEachWalker : public scene::Graph::Walker
1807 const Functor& m_functor;
1809 PatchForEachWalker( const Functor& functor ) : m_functor( functor ){
1811 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1812 if ( path.top().get().visible() ) {
1813 Patch* patch = Node_getPatch( path.top() );
1815 m_functor( *patch );
1825 template<typename Functor>
1826 inline void Scene_forEachVisiblePatch( const Functor& functor ){
1827 GlobalSceneGraph().traverse( PatchForEachWalker<Functor>( functor ) );
1830 template<typename Functor>
1831 class PatchForEachSelectedWalker : public scene::Graph::Walker
1833 const Functor& m_functor;
1835 PatchForEachSelectedWalker( const Functor& functor ) : m_functor( functor ){
1837 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1838 if ( path.top().get().visible() ) {
1839 Patch* patch = Node_getPatch( path.top() );
1841 && Instance_getSelectable( instance )->isSelected() ) {
1842 m_functor( *patch );
1852 template<typename Functor>
1853 inline void Scene_forEachVisibleSelectedPatch( const Functor& functor ){
1854 GlobalSceneGraph().traverse( PatchForEachSelectedWalker<Functor>( functor ) );
1857 template<typename Functor>
1858 class PatchForEachInstanceWalker : public scene::Graph::Walker
1860 const Functor& m_functor;
1862 PatchForEachInstanceWalker( const Functor& functor ) : m_functor( functor ){
1864 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1865 if ( path.top().get().visible() ) {
1866 PatchInstance* patch = Instance_getPatch( instance );
1868 m_functor( *patch );
1878 template<typename Functor>
1879 inline void Scene_forEachVisiblePatchInstance( const Functor& functor ){
1880 GlobalSceneGraph().traverse( PatchForEachInstanceWalker<Functor>( functor ) );