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
23 #include "globaldefs.h"
25 #include "picomodel.h"
28 #include "idatastream.h"
30 #include "modelskin.h"
33 #include "renderable.h"
34 #include "selectable.h"
36 #include "math/frustum.h"
37 #include "string/string.h"
38 #include "generic/static.h"
39 #include "shaderlib.h"
41 #include "instancelib.h"
42 #include "transformlib.h"
43 #include "traverselib.h"
46 class VectorLightList : public LightList
48 typedef std::vector<const RendererLight*> Lights;
51 void addLight( const RendererLight& light ){
52 m_lights.push_back( &light );
57 void evaluateLights() const {
59 void lightsChanged() const {
61 void forEachLight( const RendererLightCallback& callback ) const {
62 for ( Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i )
70 public OpenGLRenderable
73 CopiedString m_shader;
76 Array<ArbitraryMeshVertex> m_vertices;
77 Array<RenderIndex> m_indices;
85 PicoSurface( picoSurface_t* surface ){
86 CopyPicoSurface( surface );
93 void render( RenderStateFlags state ) const {
94 if ( ( state & RENDER_BUMP ) != 0 ) {
95 if ( GlobalShaderCache().useShaderLanguage() ) {
96 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
97 glVertexAttribPointerARB( c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
98 glVertexAttribPointerARB( c_attr_Tangent, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->tangent );
99 glVertexAttribPointerARB( c_attr_Binormal, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->bitangent );
103 glVertexAttribPointerARB( 11, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
104 glVertexAttribPointerARB( 8, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
105 glVertexAttribPointerARB( 9, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->tangent );
106 glVertexAttribPointerARB( 10, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->bitangent );
111 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
112 glTexCoordPointer( 2, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
114 glVertexPointer( 3, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->vertex );
115 glDrawElements( GL_TRIANGLES, GLsizei( m_indices.size() ), RenderIndexTypeID, m_indices.data() );
118 GLfloat modelview[16];
119 glGetFloatv( GL_MODELVIEW_MATRIX, modelview ); // I know this is slow as hell, but hey - we're in _DEBUG
120 Matrix4 modelview_inv(
121 modelview[0], modelview[1], modelview[2], modelview[3],
122 modelview[4], modelview[5], modelview[6], modelview[7],
123 modelview[8], modelview[9], modelview[10], modelview[11],
124 modelview[12], modelview[13], modelview[14], modelview[15] );
125 matrix4_full_invert( modelview_inv );
126 Matrix4 modelview_inv_transposed = matrix4_transposed( modelview_inv );
130 for ( Array<ArbitraryMeshVertex>::const_iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
132 Vector3 normal = normal3f_to_vector3( ( *i ).normal );
133 normal = matrix4_transformed_direction( modelview_inv, vector3_normalised( matrix4_transformed_direction( modelview_inv_transposed, normal ) ) ); // do some magic
134 Vector3 normalTransformed = vector3_added( vertex3f_to_vector3( ( *i ).vertex ), vector3_scaled( normal, 8 ) );
135 glVertex3fv( vertex3f_to_array( ( *i ).vertex ) );
136 glVertex3fv( vector3_to_array( normalTransformed ) );
142 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
143 return test.TestAABB( m_aabb_local, localToWorld );
146 const AABB& localAABB() const {
150 void render( Renderer& renderer, const Matrix4& localToWorld, Shader* state ) const {
151 renderer.SetState( state, Renderer::eFullMaterials );
152 renderer.addRenderable( *this, localToWorld );
155 void render( Renderer& renderer, const Matrix4& localToWorld ) const {
156 render( renderer, localToWorld, m_state );
159 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
160 test.BeginMesh( localToWorld );
162 SelectionIntersection best;
163 testSelect( test, best );
164 if ( best.valid() ) {
165 selector.addIntersection( best );
169 const char* getShader() const {
170 return m_shader.c_str();
172 Shader* getState() const {
178 void CaptureShader(){
179 m_state = GlobalShaderCache().capture( m_shader.c_str() );
181 void ReleaseShader(){
182 GlobalShaderCache().release( m_shader.c_str() );
186 m_aabb_local = AABB();
187 for ( std::size_t i = 0; i < m_vertices.size(); ++i )
188 aabb_extend_by_point_safe( m_aabb_local, reinterpret_cast<const Vector3&>( m_vertices[i].vertex ) );
191 for ( Array<RenderIndex>::iterator i = m_indices.begin(); i != m_indices.end(); i += 3 )
193 ArbitraryMeshVertex& a = m_vertices[*( i + 0 )];
194 ArbitraryMeshVertex& b = m_vertices[*( i + 1 )];
195 ArbitraryMeshVertex& c = m_vertices[*( i + 2 )];
197 ArbitraryMeshTriangle_sumTangents( a, b, c );
200 for ( Array<ArbitraryMeshVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
202 vector3_normalise( reinterpret_cast<Vector3&>( ( *i ).tangent ) );
203 vector3_normalise( reinterpret_cast<Vector3&>( ( *i ).bitangent ) );
207 void testSelect( SelectionTest& test, SelectionIntersection& best ){
209 VertexPointer( VertexPointer::pointer( &m_vertices.data()->vertex ), sizeof( ArbitraryMeshVertex ) ),
210 IndexPointer( m_indices.data(), IndexPointer::index_type( m_indices.size() ) ),
215 void CopyPicoSurface( picoSurface_t* surface ){
216 picoShader_t* shader = PicoGetSurfaceShader( surface );
221 m_shader = PicoGetShaderName( shader );
224 m_vertices.resize( PicoGetSurfaceNumVertexes( surface ) );
225 m_indices.resize( PicoGetSurfaceNumIndexes( surface ) );
227 for ( std::size_t i = 0; i < m_vertices.size(); ++i )
229 picoVec_t* xyz = PicoGetSurfaceXYZ( surface, int(i) );
230 m_vertices[i].vertex = vertex3f_from_array( xyz );
232 picoVec_t* normal = PicoGetSurfaceNormal( surface, int(i) );
233 m_vertices[i].normal = normal3f_from_array( normal );
235 picoVec_t* st = PicoGetSurfaceST( surface, 0, int(i) );
236 m_vertices[i].texcoord = TexCoord2f( st[0], st[1] );
239 picoVec_t* color = PicoGetSurfaceColor( surface, 0, int(i) );
240 m_vertices[i].colour = Colour4b( color[0], color[1], color[2], color[3] );
244 picoIndex_t* indexes = PicoGetSurfaceIndexes( surface, 0 );
245 for ( std::size_t j = 0; j < m_indices.size(); ++j )
246 m_indices[ j ] = indexes[ j ];
251 void constructQuad( std::size_t index, const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d, const Vector3& normal ){
252 m_vertices[index * 4 + 0] = ArbitraryMeshVertex(
253 vertex3f_for_vector3( a ),
254 normal3f_for_vector3( normal ),
255 texcoord2f_from_array( aabb_texcoord_topleft )
257 m_vertices[index * 4 + 1] = ArbitraryMeshVertex(
258 vertex3f_for_vector3( b ),
259 normal3f_for_vector3( normal ),
260 texcoord2f_from_array( aabb_texcoord_topright )
262 m_vertices[index * 4 + 2] = ArbitraryMeshVertex(
263 vertex3f_for_vector3( c ),
264 normal3f_for_vector3( normal ),
265 texcoord2f_from_array( aabb_texcoord_botright )
267 m_vertices[index * 4 + 3] = ArbitraryMeshVertex(
268 vertex3f_for_vector3( d ),
269 normal3f_for_vector3( normal ),
270 texcoord2f_from_array( aabb_texcoord_botleft )
274 void constructNull(){
275 AABB aabb( Vector3( 0, 0, 0 ), Vector3( 8, 8, 8 ) );
278 aabb_corners( aabb, points );
280 m_vertices.resize( 24 );
282 constructQuad( 0, points[2], points[1], points[5], points[6], aabb_normals[0] );
283 constructQuad( 1, points[1], points[0], points[4], points[5], aabb_normals[1] );
284 constructQuad( 2, points[0], points[1], points[2], points[3], aabb_normals[2] );
285 constructQuad( 3, points[0], points[3], points[7], points[4], aabb_normals[3] );
286 constructQuad( 4, points[3], points[2], points[6], points[7], aabb_normals[4] );
287 constructQuad( 5, points[7], points[6], points[5], points[4], aabb_normals[5] );
289 m_indices.resize( 36 );
291 RenderIndex indices[36] = {
295 12, 13, 14, 12, 14, 15,
296 16, 17, 18, 16, 18, 19,
297 20, 21, 22, 10, 22, 23,
301 Array<RenderIndex>::iterator j = m_indices.begin();
302 for ( RenderIndex* i = indices; i != indices + ( sizeof( indices ) / sizeof( RenderIndex ) ); ++i )
314 typedef std::pair<CopiedString, int> PicoModelKey;
321 typedef std::vector<PicoSurface*> surfaces_t;
322 surfaces_t m_surfaces;
326 Callback<void()> m_lightsChanged;
331 PicoModel( picoModel_t* model ){
332 CopyPicoModel( model );
335 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
339 typedef surfaces_t::const_iterator const_iterator;
341 const_iterator begin() const {
342 return m_surfaces.begin();
344 const_iterator end() const {
345 return m_surfaces.end();
347 std::size_t size() const {
348 return m_surfaces.size();
351 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
352 return test.TestAABB( m_aabb_local, localToWorld );
355 virtual const AABB& localAABB() const {
359 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, std::vector<Shader*> states ) const {
360 for ( surfaces_t::const_iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
362 if ( ( *i )->intersectVolume( volume, localToWorld ) != c_volumeOutside ) {
363 ( *i )->render( renderer, localToWorld, states[i - m_surfaces.begin()] );
368 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
369 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
371 if ( ( *i )->intersectVolume( test.getVolume(), localToWorld ) != c_volumeOutside ) {
372 ( *i )->testSelect( selector, test, localToWorld );
378 void CopyPicoModel( picoModel_t* model ){
379 m_aabb_local = AABB();
381 /* each surface on the model will become a new map drawsurface */
382 int numSurfaces = PicoGetModelNumSurfaces( model );
383 //% SYs_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
384 for ( int s = 0; s < numSurfaces; ++s )
387 picoSurface_t* surface = PicoGetModelSurface( model, s );
388 if ( surface == 0 ) {
392 /* only handle triangle surfaces initially (fixme: support patches) */
393 if ( PicoGetSurfaceType( surface ) != PICO_TRIANGLES ) {
397 /* fix the surface's normals */
398 PicoFixSurfaceNormals( surface );
400 PicoSurface* picosurface = new PicoSurface( surface );
401 aabb_extend_by_aabb_safe( m_aabb_local, picosurface->localAABB() );
402 m_surfaces.push_back( picosurface );
405 void constructNull(){
406 PicoSurface* picosurface = new PicoSurface();
407 m_aabb_local = picosurface->localAABB();
408 m_surfaces.push_back( picosurface );
412 inline void Surface_addLight( PicoSurface& surface, VectorLightList& lights, const Matrix4& localToWorld, const RendererLight& light ){
413 if ( light.testAABB( aabb_for_oriented_aabb( surface.localAABB(), localToWorld ) ) ) {
414 lights.addLight( light );
418 class PicoModelInstance :
419 public scene::Instance,
421 public SelectionTestable,
422 public LightCullable,
427 InstanceTypeCastTable m_casts;
430 InstanceContainedCast<PicoModelInstance, Bounded>::install( m_casts );
431 InstanceContainedCast<PicoModelInstance, Cullable>::install( m_casts );
432 InstanceStaticCast<PicoModelInstance, Renderable>::install( m_casts );
433 InstanceStaticCast<PicoModelInstance, SelectionTestable>::install( m_casts );
434 InstanceStaticCast<PicoModelInstance, SkinnedModel>::install( m_casts );
436 InstanceTypeCastTable& get(){
441 PicoModel& m_picomodel;
443 const LightList* m_lightList;
444 typedef Array<VectorLightList> SurfaceLightLists;
445 SurfaceLightLists m_surfaceLightLists;
452 Remap() : second( 0 ){
455 typedef Array<Remap> SurfaceRemaps;
456 SurfaceRemaps m_skins;
458 PicoModelInstance( const PicoModelInstance& );
459 PicoModelInstance operator=( const PicoModelInstance& );
461 typedef LazyStatic<TypeCasts> StaticTypeCasts;
465 Bounded& get( NullType<Bounded>){
468 Cullable& get( NullType<Cullable>){
472 void lightsChanged(){
473 m_lightList->lightsChanged();
475 typedef MemberCaller<PicoModelInstance, void(), &PicoModelInstance::lightsChanged> LightsChangedCaller;
477 void constructRemaps(){
478 ASSERT_MESSAGE( m_skins.size() == m_picomodel.size(), "ERROR" );
479 ModelSkin* skin = NodeTypeCast<ModelSkin>::cast( path().parent() );
480 if ( skin != 0 && skin->realised() ) {
481 SurfaceRemaps::iterator j = m_skins.begin();
482 for ( PicoModel::const_iterator i = m_picomodel.begin(); i != m_picomodel.end(); ++i, ++j )
484 const char* remap = skin->getRemap( ( *i )->getShader() );
485 if ( !string_empty( remap ) ) {
486 ( *j ).first = remap;
487 ( *j ).second = GlobalShaderCache().capture( remap );
497 void destroyRemaps(){
498 ASSERT_MESSAGE( m_skins.size() == m_picomodel.size(), "ERROR" );
499 for ( SurfaceRemaps::iterator i = m_skins.begin(); i != m_skins.end(); ++i )
501 if ( ( *i ).second != 0 ) {
502 GlobalShaderCache().release( ( *i ).first.c_str() );
512 PicoModelInstance( const scene::Path& path, scene::Instance* parent, PicoModel& picomodel ) :
513 Instance( path, parent, this, StaticTypeCasts::instance().get() ),
514 m_picomodel( picomodel ),
515 m_surfaceLightLists( m_picomodel.size() ),
516 m_skins( m_picomodel.size() ){
517 m_lightList = &GlobalShaderCache().attach( *this );
518 m_picomodel.m_lightsChanged = LightsChangedCaller( *this );
520 Instance::setTransformChangedCallback( LightsChangedCaller( *this ) );
524 ~PicoModelInstance(){
527 Instance::setTransformChangedCallback( Callback<void()>() );
529 m_picomodel.m_lightsChanged = Callback<void()>();
530 GlobalShaderCache().detach( *this );
533 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
534 SurfaceLightLists::const_iterator j = m_surfaceLightLists.begin();
535 SurfaceRemaps::const_iterator k = m_skins.begin();
536 for ( PicoModel::const_iterator i = m_picomodel.begin(); i != m_picomodel.end(); ++i, ++j, ++k )
538 if ( ( *i )->intersectVolume( volume, localToWorld ) != c_volumeOutside ) {
539 renderer.setLights( *j );
540 ( *i )->render( renderer, localToWorld, ( *k ).second != 0 ? ( *k ).second : ( *i )->getState() );
545 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
546 m_lightList->evaluateLights();
548 render( renderer, volume, Instance::localToWorld() );
550 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
551 renderSolid( renderer, volume );
554 void testSelect( Selector& selector, SelectionTest& test ){
555 m_picomodel.testSelect( selector, test, Instance::localToWorld() );
558 bool testLight( const RendererLight& light ) const {
559 return light.testAABB( worldAABB() );
561 void insertLight( const RendererLight& light ){
562 const Matrix4& localToWorld = Instance::localToWorld();
563 SurfaceLightLists::iterator j = m_surfaceLightLists.begin();
564 for ( PicoModel::const_iterator i = m_picomodel.begin(); i != m_picomodel.end(); ++i )
566 Surface_addLight( *( *i ), *j++, localToWorld, light );
570 for ( SurfaceLightLists::iterator i = m_surfaceLightLists.begin(); i != m_surfaceLightLists.end(); ++i )
577 class PicoModelNode : public scene::Node::Symbiot, public scene::Instantiable
581 NodeTypeCastTable m_casts;
584 NodeStaticCast<PicoModelNode, scene::Instantiable>::install( m_casts );
586 NodeTypeCastTable& get(){
593 InstanceSet m_instances;
594 PicoModel m_picomodel;
597 typedef LazyStatic<TypeCasts> StaticTypeCasts;
599 PicoModelNode() : m_node( this, this, StaticTypeCasts::instance().get() ){
601 PicoModelNode( picoModel_t* model ) : m_node( this, this, StaticTypeCasts::instance().get() ), m_picomodel( model ){
611 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
612 return new PicoModelInstance( path, parent, m_picomodel );
614 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
615 m_instances.forEachInstance( visitor );
617 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
618 m_instances.insert( observer, path, instance );
620 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
621 return m_instances.erase( observer, path );
628 template<typename Key, typename Type>
632 static Type* construct( const Key& key ){
633 return new Type( key );
635 static void destroy( Type* value ){
640 template<typename Key, typename Type, typename creation_policy = create_new<Key, Type> >
641 class cache_element : public creation_policy
644 inline cache_element() : m_count( 0 ), m_value( 0 ) {}
645 inline ~cache_element(){
646 ASSERT_MESSAGE( m_count == 0, "destroyed a reference before it was released\n" );
651 inline Type* capture( const Key& key ){
652 if ( ++m_count == 1 ) {
657 inline void release(){
658 ASSERT_MESSAGE( !empty(), "failed to release reference - not found in cache\n" );
659 if ( --m_count == 0 ) {
666 inline void refresh( const Key& key ){
667 m_value->refresh( key );
670 inline void construct( const Key& key ){
671 m_value = creation_policy::construct( key );
673 inline void destroy(){
674 creation_policy::destroy( m_value );
681 class create_picomodel
683 typedef PicoModelKey key_type;
684 typedef PicoModel value_type;
686 static value_type* construct( const key_type& key ){
687 picoModel_t* picomodel = PicoLoadModel( const_cast<char*>( key.first.c_str() ), key.second );
688 value_type* value = new value_type( picomodel );
689 PicoFreeModel( picomodel );
692 static void destroy( value_type* value ){
701 typedef PicoModel value_type;
704 typedef PicoModelKey key_type;
705 typedef cache_element<key_type, value_type, create_picomodel> elem_type;
706 typedef std::map<key_type, elem_type> cache_type;
708 value_type* capture( const key_type& key ){
709 return m_cache[key].capture( key );
711 void release( const key_type& key ){
712 m_cache[key].release();
719 ModelCache g_model_cache;
723 typedef struct remap_s {
724 char m_remapbuff[64 + 1024];
734 RemapWrapper( const char* name ){
735 parse_namestr( name );
737 m_model = g_model_cache.capture( ModelCache::key_type( m_name, m_frame ) );
741 virtual ~RemapWrapper(){
742 g_model_cache.release( ModelCache::key_type( m_name, m_frame ) );
744 for ( shaders_t::iterator i = m_shaders.begin(); i != m_shaders.end(); ++i )
746 GlobalShaderCache().release( ( *i ).c_str() );
749 for ( remaps_t::iterator j = m_remaps.begin(); j != m_remaps.end(); ++j )
755 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
756 return m_model->intersectVolume( test, localToWorld );
759 virtual const AABB& localAABB() const {
760 return m_model->localAABB();
763 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
764 m_model->render( renderer, volume, localToWorld, m_states );
767 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
768 m_model->testSelect( selector, test, localToWorld );
772 void add_remap( const char *remap ){
778 while ( *ch && *ch != ';' )
783 globalErrorStream() << "WARNING: Shader _remap key found in a model entity without a ; character\n";
786 pRemap = new remap_t;
788 strncpy( pRemap->m_remapbuff, remap, sizeof( pRemap->m_remapbuff ) );
790 pRemap->m_remapbuff[ch - remap] = '\0';
792 pRemap->original = pRemap->m_remapbuff;
793 pRemap->remap = pRemap->m_remapbuff + ( ch - remap ) + 1;
795 m_remaps.push_back( pRemap );
799 void parse_namestr( const char *name ){
801 bool hasName, hasFrame;
803 hasName = hasFrame = false;
807 for ( s = ptr = name; ; ++ptr )
809 if ( !hasName && ( *ptr == ':' || *ptr == '\0' ) ) {
812 m_name = CopiedString( s, ptr );
815 else if ( *ptr == '?' || *ptr == '\0' ) {
818 m_frame = atoi( CopiedString( s, ptr ).c_str() );
821 else if ( *ptr == '&' || *ptr == '\0' ) {
823 add_remap( CopiedString( s, ptr ).c_str() );
827 if ( *ptr == '\0' ) {
833 void construct_shaders(){
834 const char* global_shader = shader_for_remap( "*" );
836 m_shaders.reserve( m_model->size() );
837 m_states.reserve( m_model->size() );
838 for ( PicoModel::iterator i = m_model->begin(); i != m_model->end(); ++i )
840 const char* shader = shader_for_remap( ( *i )->getShader() );
842 ( shader[0] != '\0' )
844 : ( global_shader[0] != '\0' )
846 : ( *i )->getShader() );
847 m_states.push_back( GlobalShaderCache().capture( m_shaders.back().c_str() ) );
851 inline const char* shader_for_remap( const char* remap ){
852 for ( remaps_t::iterator i = m_remaps.begin(); i != m_remaps.end(); ++i )
854 if ( shader_equal( remap, ( *i )->original ) ) {
855 return ( *i )->remap;
865 typedef std::vector<remap_t*> remaps_t;
867 typedef std::vector<CopiedString> shaders_t;
869 typedef std::vector<Shader*> states_t;
873 class RemapWrapperInstance : public scene::Instance, public Renderable, public SelectionTestable
875 RemapWrapper& m_remapwrapper;
877 RemapWrapperInstance( const scene::Path& path, scene::Instance* parent, RemapWrapper& remapwrapper ) : Instance( path, parent ), m_remapwrapper( remapwrapper ){
878 scene::Instance::m_cullable = &m_remapwrapper;
879 scene::Instance::m_render = this;
880 scene::Instance::m_select = this;
883 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
884 m_remapwrapper.render( renderer, volume, Instance::localToWorld() );
886 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
887 renderSolid( renderer, volume );
890 void testSelect( Selector& selector, SelectionTest& test ){
891 m_remapwrapper.testSelect( selector, test, Instance::localToWorld() );
895 class RemapWrapperNode : public scene::Node::Symbiot, public scene::Instantiable
898 typedef RemapWrapperInstance instance_type;
899 InstanceSet m_instances;
900 RemapWrapper m_remapwrapper;
902 RemapWrapperNode( const char* name ) : m_node( this ), m_remapwrapper( name ){
903 m_node.m_instance = this;
913 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
914 return new instance_type( path, parent, m_remapwrapper );
916 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
917 m_instances.forEachInstance( visitor );
919 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
920 m_instances.insert( observer, path, instance );
922 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
923 return m_instances.erase( observer, path );
927 scene::Node& LoadRemapModel( const char* name ){
928 return ( new RemapWrapperNode( name ) )->node();
934 size_t picoInputStreamReam( void* inputStream, unsigned char* buffer, size_t length ){
935 return reinterpret_cast<InputStream*>( inputStream )->read( buffer, length );
938 scene::Node& loadPicoModel( const picoModule_t* module, ArchiveFile& file ){
939 picoModel_t* model = PicoModuleLoadModelStream( module, &file.getInputStream(), picoInputStreamReam, file.size(), 0, file.getName() );
940 PicoModelNode* modelNode = new PicoModelNode( model );
941 PicoFreeModel( model );
942 return modelNode->node();