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
24 #include "picomodel.h"
27 #include "idatastream.h"
29 #include "modelskin.h"
32 #include "renderable.h"
33 #include "selectable.h"
35 #include "math/frustum.h"
36 #include "string/string.h"
37 #include "generic/static.h"
38 #include "shaderlib.h"
40 #include "instancelib.h"
41 #include "transformlib.h"
42 #include "traverselib.h"
45 class VectorLightList : public LightList
47 typedef std::vector<const RendererLight*> Lights;
50 void addLight( const RendererLight& light ){
51 m_lights.push_back( &light );
56 void evaluateLights() const {
58 void lightsChanged() const {
60 void forEachLight( const RendererLightCallback& callback ) const {
61 for ( Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i )
69 public OpenGLRenderable
72 CopiedString m_shader;
75 Array<ArbitraryMeshVertex> m_vertices;
76 Array<RenderIndex> m_indices;
84 PicoSurface( picoSurface_t* surface ){
85 CopyPicoSurface( surface );
92 void render( RenderStateFlags state ) const {
93 if ( ( state & RENDER_BUMP ) != 0 ) {
94 if ( GlobalShaderCache().useShaderLanguage() ) {
95 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
96 glVertexAttribPointerARB( c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
97 glVertexAttribPointerARB( c_attr_Tangent, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->tangent );
98 glVertexAttribPointerARB( c_attr_Binormal, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->bitangent );
102 glVertexAttribPointerARB( 11, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
103 glVertexAttribPointerARB( 8, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
104 glVertexAttribPointerARB( 9, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->tangent );
105 glVertexAttribPointerARB( 10, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->bitangent );
110 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
111 glTexCoordPointer( 2, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
113 glVertexPointer( 3, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->vertex );
114 glDrawElements( GL_TRIANGLES, GLsizei( m_indices.size() ), RenderIndexTypeID, m_indices.data() );
116 #if defined( _DEBUG )
117 GLfloat modelview[16];
118 glGetFloatv( GL_MODELVIEW_MATRIX, modelview ); // I know this is slow as hell, but hey - we're in _DEBUG
119 Matrix4 modelview_inv(
120 modelview[0], modelview[1], modelview[2], modelview[3],
121 modelview[4], modelview[5], modelview[6], modelview[7],
122 modelview[8], modelview[9], modelview[10], modelview[11],
123 modelview[12], modelview[13], modelview[14], modelview[15] );
124 matrix4_full_invert( modelview_inv );
125 Matrix4 modelview_inv_transposed = matrix4_transposed( modelview_inv );
129 for ( Array<ArbitraryMeshVertex>::const_iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
131 Vector3 normal = normal3f_to_vector3( ( *i ).normal );
132 normal = matrix4_transformed_direction( modelview_inv, vector3_normalised( matrix4_transformed_direction( modelview_inv_transposed, normal ) ) ); // do some magic
133 Vector3 normalTransformed = vector3_added( vertex3f_to_vector3( ( *i ).vertex ), vector3_scaled( normal, 8 ) );
134 glVertex3fv( vertex3f_to_array( ( *i ).vertex ) );
135 glVertex3fv( vector3_to_array( normalTransformed ) );
141 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
142 return test.TestAABB( m_aabb_local, localToWorld );
145 const AABB& localAABB() const {
149 void render( Renderer& renderer, const Matrix4& localToWorld, Shader* state ) const {
150 renderer.SetState( state, Renderer::eFullMaterials );
151 renderer.addRenderable( *this, localToWorld );
154 void render( Renderer& renderer, const Matrix4& localToWorld ) const {
155 render( renderer, localToWorld, m_state );
158 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
159 test.BeginMesh( localToWorld );
161 SelectionIntersection best;
162 testSelect( test, best );
163 if ( best.valid() ) {
164 selector.addIntersection( best );
168 const char* getShader() const {
169 return m_shader.c_str();
171 Shader* getState() const {
177 void CaptureShader(){
178 m_state = GlobalShaderCache().capture( m_shader.c_str() );
180 void ReleaseShader(){
181 GlobalShaderCache().release( m_shader.c_str() );
185 m_aabb_local = AABB();
186 for ( std::size_t i = 0; i < m_vertices.size(); ++i )
187 aabb_extend_by_point_safe( m_aabb_local, reinterpret_cast<const Vector3&>( m_vertices[i].vertex ) );
190 for ( Array<RenderIndex>::iterator i = m_indices.begin(); i != m_indices.end(); i += 3 )
192 ArbitraryMeshVertex& a = m_vertices[*( i + 0 )];
193 ArbitraryMeshVertex& b = m_vertices[*( i + 1 )];
194 ArbitraryMeshVertex& c = m_vertices[*( i + 2 )];
196 ArbitraryMeshTriangle_sumTangents( a, b, c );
199 for ( Array<ArbitraryMeshVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
201 vector3_normalise( reinterpret_cast<Vector3&>( ( *i ).tangent ) );
202 vector3_normalise( reinterpret_cast<Vector3&>( ( *i ).bitangent ) );
206 void testSelect( SelectionTest& test, SelectionIntersection& best ){
208 VertexPointer( VertexPointer::pointer( &m_vertices.data()->vertex ), sizeof( ArbitraryMeshVertex ) ),
209 IndexPointer( m_indices.data(), IndexPointer::index_type( m_indices.size() ) ),
214 void CopyPicoSurface( picoSurface_t* surface ){
215 picoShader_t* shader = PicoGetSurfaceShader( surface );
220 m_shader = PicoGetShaderName( shader );
223 m_vertices.resize( PicoGetSurfaceNumVertexes( surface ) );
224 m_indices.resize( PicoGetSurfaceNumIndexes( surface ) );
226 for ( std::size_t i = 0; i < m_vertices.size(); ++i )
228 picoVec_t* xyz = PicoGetSurfaceXYZ( surface, int(i) );
229 m_vertices[i].vertex = vertex3f_from_array( xyz );
231 picoVec_t* normal = PicoGetSurfaceNormal( surface, int(i) );
232 m_vertices[i].normal = normal3f_from_array( normal );
234 picoVec_t* st = PicoGetSurfaceST( surface, 0, int(i) );
235 m_vertices[i].texcoord = TexCoord2f( st[0], st[1] );
238 picoVec_t* color = PicoGetSurfaceColor( surface, 0, int(i) );
239 m_vertices[i].colour = Colour4b( color[0], color[1], color[2], color[3] );
243 picoIndex_t* indexes = PicoGetSurfaceIndexes( surface, 0 );
244 for ( std::size_t j = 0; j < m_indices.size(); ++j )
245 m_indices[ j ] = indexes[ j ];
250 void constructQuad( std::size_t index, const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d, const Vector3& normal ){
251 m_vertices[index * 4 + 0] = ArbitraryMeshVertex(
252 vertex3f_for_vector3( a ),
253 normal3f_for_vector3( normal ),
254 texcoord2f_from_array( aabb_texcoord_topleft )
256 m_vertices[index * 4 + 1] = ArbitraryMeshVertex(
257 vertex3f_for_vector3( b ),
258 normal3f_for_vector3( normal ),
259 texcoord2f_from_array( aabb_texcoord_topright )
261 m_vertices[index * 4 + 2] = ArbitraryMeshVertex(
262 vertex3f_for_vector3( c ),
263 normal3f_for_vector3( normal ),
264 texcoord2f_from_array( aabb_texcoord_botright )
266 m_vertices[index * 4 + 3] = ArbitraryMeshVertex(
267 vertex3f_for_vector3( d ),
268 normal3f_for_vector3( normal ),
269 texcoord2f_from_array( aabb_texcoord_botleft )
273 void constructNull(){
274 AABB aabb( Vector3( 0, 0, 0 ), Vector3( 8, 8, 8 ) );
277 aabb_corners( aabb, points );
279 m_vertices.resize( 24 );
281 constructQuad( 0, points[2], points[1], points[5], points[6], aabb_normals[0] );
282 constructQuad( 1, points[1], points[0], points[4], points[5], aabb_normals[1] );
283 constructQuad( 2, points[0], points[1], points[2], points[3], aabb_normals[2] );
284 constructQuad( 3, points[0], points[3], points[7], points[4], aabb_normals[3] );
285 constructQuad( 4, points[3], points[2], points[6], points[7], aabb_normals[4] );
286 constructQuad( 5, points[7], points[6], points[5], points[4], aabb_normals[5] );
288 m_indices.resize( 36 );
290 RenderIndex indices[36] = {
294 12, 13, 14, 12, 14, 15,
295 16, 17, 18, 16, 18, 19,
296 20, 21, 22, 10, 22, 23,
300 Array<RenderIndex>::iterator j = m_indices.begin();
301 for ( RenderIndex* i = indices; i != indices + ( sizeof( indices ) / sizeof( RenderIndex ) ); ++i )
313 typedef std::pair<CopiedString, int> PicoModelKey;
320 typedef std::vector<PicoSurface*> surfaces_t;
321 surfaces_t m_surfaces;
325 Callback m_lightsChanged;
330 PicoModel( picoModel_t* model ){
331 CopyPicoModel( model );
334 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
338 typedef surfaces_t::const_iterator const_iterator;
340 const_iterator begin() const {
341 return m_surfaces.begin();
343 const_iterator end() const {
344 return m_surfaces.end();
346 std::size_t size() const {
347 return m_surfaces.size();
350 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
351 return test.TestAABB( m_aabb_local, localToWorld );
354 virtual const AABB& localAABB() const {
358 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, std::vector<Shader*> states ) const {
359 for ( surfaces_t::const_iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
361 if ( ( *i )->intersectVolume( volume, localToWorld ) != c_volumeOutside ) {
362 ( *i )->render( renderer, localToWorld, states[i - m_surfaces.begin()] );
367 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
368 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
370 if ( ( *i )->intersectVolume( test.getVolume(), localToWorld ) != c_volumeOutside ) {
371 ( *i )->testSelect( selector, test, localToWorld );
377 void CopyPicoModel( picoModel_t* model ){
378 m_aabb_local = AABB();
380 /* each surface on the model will become a new map drawsurface */
381 int numSurfaces = PicoGetModelNumSurfaces( model );
382 //% SYs_FPrintf( SYS_VRB, "Model %s has %d surfaces\n", name, numSurfaces );
383 for ( int s = 0; s < numSurfaces; ++s )
386 picoSurface_t* surface = PicoGetModelSurface( model, s );
387 if ( surface == 0 ) {
391 /* only handle triangle surfaces initially (fixme: support patches) */
392 if ( PicoGetSurfaceType( surface ) != PICO_TRIANGLES ) {
396 /* fix the surface's normals */
397 PicoFixSurfaceNormals( surface );
399 PicoSurface* picosurface = new PicoSurface( surface );
400 aabb_extend_by_aabb_safe( m_aabb_local, picosurface->localAABB() );
401 m_surfaces.push_back( picosurface );
404 void constructNull(){
405 PicoSurface* picosurface = new PicoSurface();
406 m_aabb_local = picosurface->localAABB();
407 m_surfaces.push_back( picosurface );
411 inline void Surface_addLight( PicoSurface& surface, VectorLightList& lights, const Matrix4& localToWorld, const RendererLight& light ){
412 if ( light.testAABB( aabb_for_oriented_aabb( surface.localAABB(), localToWorld ) ) ) {
413 lights.addLight( light );
417 class PicoModelInstance :
418 public scene::Instance,
420 public SelectionTestable,
421 public LightCullable,
426 InstanceTypeCastTable m_casts;
429 InstanceContainedCast<PicoModelInstance, Bounded>::install( m_casts );
430 InstanceContainedCast<PicoModelInstance, Cullable>::install( m_casts );
431 InstanceStaticCast<PicoModelInstance, Renderable>::install( m_casts );
432 InstanceStaticCast<PicoModelInstance, SelectionTestable>::install( m_casts );
433 InstanceStaticCast<PicoModelInstance, SkinnedModel>::install( m_casts );
435 InstanceTypeCastTable& get(){
440 PicoModel& m_picomodel;
442 const LightList* m_lightList;
443 typedef Array<VectorLightList> SurfaceLightLists;
444 SurfaceLightLists m_surfaceLightLists;
451 Remap() : second( 0 ){
454 typedef Array<Remap> SurfaceRemaps;
455 SurfaceRemaps m_skins;
457 PicoModelInstance( const PicoModelInstance& );
458 PicoModelInstance operator=( const PicoModelInstance& );
460 typedef LazyStatic<TypeCasts> StaticTypeCasts;
464 Bounded& get( NullType<Bounded>){
467 Cullable& get( NullType<Cullable>){
471 void lightsChanged(){
472 m_lightList->lightsChanged();
474 typedef MemberCaller<PicoModelInstance, &PicoModelInstance::lightsChanged> LightsChangedCaller;
476 void constructRemaps(){
477 ASSERT_MESSAGE( m_skins.size() == m_picomodel.size(), "ERROR" );
478 ModelSkin* skin = NodeTypeCast<ModelSkin>::cast( path().parent() );
479 if ( skin != 0 && skin->realised() ) {
480 SurfaceRemaps::iterator j = m_skins.begin();
481 for ( PicoModel::const_iterator i = m_picomodel.begin(); i != m_picomodel.end(); ++i, ++j )
483 const char* remap = skin->getRemap( ( *i )->getShader() );
484 if ( !string_empty( remap ) ) {
485 ( *j ).first = remap;
486 ( *j ).second = GlobalShaderCache().capture( remap );
496 void destroyRemaps(){
497 ASSERT_MESSAGE( m_skins.size() == m_picomodel.size(), "ERROR" );
498 for ( SurfaceRemaps::iterator i = m_skins.begin(); i != m_skins.end(); ++i )
500 if ( ( *i ).second != 0 ) {
501 GlobalShaderCache().release( ( *i ).first.c_str() );
511 PicoModelInstance( const scene::Path& path, scene::Instance* parent, PicoModel& picomodel ) :
512 Instance( path, parent, this, StaticTypeCasts::instance().get() ),
513 m_picomodel( picomodel ),
514 m_surfaceLightLists( m_picomodel.size() ),
515 m_skins( m_picomodel.size() ){
516 m_lightList = &GlobalShaderCache().attach( *this );
517 m_picomodel.m_lightsChanged = LightsChangedCaller( *this );
519 Instance::setTransformChangedCallback( LightsChangedCaller( *this ) );
523 ~PicoModelInstance(){
526 Instance::setTransformChangedCallback( Callback() );
528 m_picomodel.m_lightsChanged = Callback();
529 GlobalShaderCache().detach( *this );
532 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
533 SurfaceLightLists::const_iterator j = m_surfaceLightLists.begin();
534 SurfaceRemaps::const_iterator k = m_skins.begin();
535 for ( PicoModel::const_iterator i = m_picomodel.begin(); i != m_picomodel.end(); ++i, ++j, ++k )
537 if ( ( *i )->intersectVolume( volume, localToWorld ) != c_volumeOutside ) {
538 renderer.setLights( *j );
539 ( *i )->render( renderer, localToWorld, ( *k ).second != 0 ? ( *k ).second : ( *i )->getState() );
544 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
545 m_lightList->evaluateLights();
547 render( renderer, volume, Instance::localToWorld() );
549 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
550 renderSolid( renderer, volume );
553 void testSelect( Selector& selector, SelectionTest& test ){
554 m_picomodel.testSelect( selector, test, Instance::localToWorld() );
557 bool testLight( const RendererLight& light ) const {
558 return light.testAABB( worldAABB() );
560 void insertLight( const RendererLight& light ){
561 const Matrix4& localToWorld = Instance::localToWorld();
562 SurfaceLightLists::iterator j = m_surfaceLightLists.begin();
563 for ( PicoModel::const_iterator i = m_picomodel.begin(); i != m_picomodel.end(); ++i )
565 Surface_addLight( *( *i ), *j++, localToWorld, light );
569 for ( SurfaceLightLists::iterator i = m_surfaceLightLists.begin(); i != m_surfaceLightLists.end(); ++i )
576 class PicoModelNode : public scene::Node::Symbiot, public scene::Instantiable
580 NodeTypeCastTable m_casts;
583 NodeStaticCast<PicoModelNode, scene::Instantiable>::install( m_casts );
585 NodeTypeCastTable& get(){
592 InstanceSet m_instances;
593 PicoModel m_picomodel;
596 typedef LazyStatic<TypeCasts> StaticTypeCasts;
598 PicoModelNode() : m_node( this, this, StaticTypeCasts::instance().get() ){
600 PicoModelNode( picoModel_t* model ) : m_node( this, this, StaticTypeCasts::instance().get() ), m_picomodel( model ){
610 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
611 return new PicoModelInstance( path, parent, m_picomodel );
613 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
614 m_instances.forEachInstance( visitor );
616 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
617 m_instances.insert( observer, path, instance );
619 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
620 return m_instances.erase( observer, path );
627 template<typename Key, typename Type>
631 static Type* construct( const Key& key ){
632 return new Type( key );
634 static void destroy( Type* value ){
639 template<typename Key, typename Type, typename creation_policy = create_new<Key, Type> >
640 class cache_element : public creation_policy
643 inline cache_element() : m_count( 0 ), m_value( 0 ) {}
644 inline ~cache_element(){
645 ASSERT_MESSAGE( m_count == 0, "destroyed a reference before it was released\n" );
650 inline Type* capture( const Key& key ){
651 if ( ++m_count == 1 ) {
656 inline void release(){
657 ASSERT_MESSAGE( !empty(), "failed to release reference - not found in cache\n" );
658 if ( --m_count == 0 ) {
665 inline void refresh( const Key& key ){
666 m_value->refresh( key );
669 inline void construct( const Key& key ){
670 m_value = creation_policy::construct( key );
672 inline void destroy(){
673 creation_policy::destroy( m_value );
680 class create_picomodel
682 typedef PicoModelKey key_type;
683 typedef PicoModel value_type;
685 static value_type* construct( const key_type& key ){
686 picoModel_t* picomodel = PicoLoadModel( const_cast<char*>( key.first.c_str() ), key.second );
687 value_type* value = new value_type( picomodel );
688 PicoFreeModel( picomodel );
691 static void destroy( value_type* value ){
700 typedef PicoModel value_type;
703 typedef PicoModelKey key_type;
704 typedef cache_element<key_type, value_type, create_picomodel> elem_type;
705 typedef std::map<key_type, elem_type> cache_type;
707 value_type* capture( const key_type& key ){
708 return m_cache[key].capture( key );
710 void release( const key_type& key ){
711 m_cache[key].release();
718 ModelCache g_model_cache;
722 typedef struct remap_s {
723 char m_remapbuff[64 + 1024];
733 RemapWrapper( const char* name ){
734 parse_namestr( name );
736 m_model = g_model_cache.capture( ModelCache::key_type( m_name, m_frame ) );
740 virtual ~RemapWrapper(){
741 g_model_cache.release( ModelCache::key_type( m_name, m_frame ) );
743 for ( shaders_t::iterator i = m_shaders.begin(); i != m_shaders.end(); ++i )
745 GlobalShaderCache().release( ( *i ).c_str() );
748 for ( remaps_t::iterator j = m_remaps.begin(); j != m_remaps.end(); ++j )
754 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
755 return m_model->intersectVolume( test, localToWorld );
758 virtual const AABB& localAABB() const {
759 return m_model->localAABB();
762 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
763 m_model->render( renderer, volume, localToWorld, m_states );
766 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
767 m_model->testSelect( selector, test, localToWorld );
771 void add_remap( const char *remap ){
777 while ( *ch && *ch != ';' )
782 globalErrorStream() << "WARNING: Shader _remap key found in a model entity without a ; character\n";
785 pRemap = new remap_t;
787 strncpy( pRemap->m_remapbuff, remap, sizeof( pRemap->m_remapbuff ) );
789 pRemap->m_remapbuff[ch - remap] = '\0';
791 pRemap->original = pRemap->m_remapbuff;
792 pRemap->remap = pRemap->m_remapbuff + ( ch - remap ) + 1;
794 m_remaps.push_back( pRemap );
798 void parse_namestr( const char *name ){
800 bool hasName, hasFrame;
802 hasName = hasFrame = false;
806 for ( s = ptr = name; ; ++ptr )
808 if ( !hasName && ( *ptr == ':' || *ptr == '\0' ) ) {
811 m_name = CopiedString( s, ptr );
814 else if ( *ptr == '?' || *ptr == '\0' ) {
817 m_frame = atoi( CopiedString( s, ptr ).c_str() );
820 else if ( *ptr == '&' || *ptr == '\0' ) {
822 add_remap( CopiedString( s, ptr ).c_str() );
826 if ( *ptr == '\0' ) {
832 void construct_shaders(){
833 const char* global_shader = shader_for_remap( "*" );
835 m_shaders.reserve( m_model->size() );
836 m_states.reserve( m_model->size() );
837 for ( PicoModel::iterator i = m_model->begin(); i != m_model->end(); ++i )
839 const char* shader = shader_for_remap( ( *i )->getShader() );
841 ( shader[0] != '\0' )
843 : ( global_shader[0] != '\0' )
845 : ( *i )->getShader() );
846 m_states.push_back( GlobalShaderCache().capture( m_shaders.back().c_str() ) );
850 inline const char* shader_for_remap( const char* remap ){
851 for ( remaps_t::iterator i = m_remaps.begin(); i != m_remaps.end(); ++i )
853 if ( shader_equal( remap, ( *i )->original ) ) {
854 return ( *i )->remap;
864 typedef std::vector<remap_t*> remaps_t;
866 typedef std::vector<CopiedString> shaders_t;
868 typedef std::vector<Shader*> states_t;
872 class RemapWrapperInstance : public scene::Instance, public Renderable, public SelectionTestable
874 RemapWrapper& m_remapwrapper;
876 RemapWrapperInstance( const scene::Path& path, scene::Instance* parent, RemapWrapper& remapwrapper ) : Instance( path, parent ), m_remapwrapper( remapwrapper ){
877 scene::Instance::m_cullable = &m_remapwrapper;
878 scene::Instance::m_render = this;
879 scene::Instance::m_select = this;
882 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
883 m_remapwrapper.render( renderer, volume, Instance::localToWorld() );
885 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
886 renderSolid( renderer, volume );
889 void testSelect( Selector& selector, SelectionTest& test ){
890 m_remapwrapper.testSelect( selector, test, Instance::localToWorld() );
894 class RemapWrapperNode : public scene::Node::Symbiot, public scene::Instantiable
897 typedef RemapWrapperInstance instance_type;
898 InstanceSet m_instances;
899 RemapWrapper m_remapwrapper;
901 RemapWrapperNode( const char* name ) : m_node( this ), m_remapwrapper( name ){
902 m_node.m_instance = this;
912 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
913 return new instance_type( path, parent, m_remapwrapper );
915 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
916 m_instances.forEachInstance( visitor );
918 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
919 m_instances.insert( observer, path, instance );
921 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
922 return m_instances.erase( observer, path );
926 scene::Node& LoadRemapModel( const char* name ){
927 return ( new RemapWrapperNode( name ) )->node();
933 size_t picoInputStreamReam( void* inputStream, unsigned char* buffer, size_t length ){
934 return reinterpret_cast<InputStream*>( inputStream )->read( buffer, length );
937 scene::Node& loadPicoModel( const picoModule_t* module, ArchiveFile& file ){
938 picoModel_t* model = PicoModuleLoadModelStream( module, &file.getInputStream(), picoInputStreamReam, file.size(), 0, file.getName() );
939 PicoModelNode* modelNode = new PicoModelNode( model );
940 PicoFreeModel( model );
941 return modelNode->node();