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_MODEL_H )
23 #define INCLUDED_MODEL_H
26 #include "renderable.h"
27 #include "selectable.h"
28 #include "modelskin.h"
30 #include "math/frustum.h"
31 #include "string/string.h"
32 #include "generic/static.h"
33 #include "stream/stringstream.h"
36 #include "instancelib.h"
37 #include "transformlib.h"
38 #include "traverselib.h"
41 class VectorLightList : public LightList
43 typedef std::vector<const RendererLight*> Lights;
46 void addLight( const RendererLight& light ){
47 m_lights.push_back( &light );
52 void evaluateLights() const {
54 void lightsChanged() const {
56 void forEachLight( const RendererLightCallback& callback ) const {
57 for ( Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i )
64 inline VertexPointer vertexpointer_arbitrarymeshvertex( const ArbitraryMeshVertex* array ){
65 return VertexPointer( VertexPointer::pointer( &array->vertex ), sizeof( ArbitraryMeshVertex ) );
68 inline void parseTextureName( CopiedString& name, const char* token ){
69 StringOutputStream cleaned( 256 );
70 cleaned << PathCleaned( token );
71 name = StringRange( cleaned.c_str(), path_get_filename_base_end( cleaned.c_str() ) ); // remove extension
74 // generic renderable triangle surface
76 public OpenGLRenderable
79 typedef VertexBuffer<ArbitraryMeshVertex> vertices_t;
80 typedef IndexBuffer indices_t;
84 CopiedString m_shader;
87 vertices_t m_vertices;
91 m_state = GlobalShaderCache().capture( m_shader.c_str() );
94 GlobalShaderCache().release( m_shader.c_str() );
100 : m_shader( "" ), m_state( 0 ){
107 vertices_t& vertices(){
110 indices_t& indices(){
114 void setShader( const char* name ){
116 parseTextureName( m_shader, name );
119 const char* getShader() const {
120 return m_shader.c_str();
122 Shader* getState() const {
126 m_aabb_local = AABB();
127 for ( vertices_t::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
128 aabb_extend_by_point_safe( m_aabb_local, reinterpret_cast<const Vector3&>( ( *i ).vertex ) );
132 for ( Surface::indices_t::iterator i = m_indices.begin(); i != m_indices.end(); i += 3 )
134 ArbitraryMeshVertex& a = m_vertices[*( i + 0 )];
135 ArbitraryMeshVertex& b = m_vertices[*( i + 1 )];
136 ArbitraryMeshVertex& c = m_vertices[*( i + 2 )];
138 ArbitraryMeshTriangle_sumTangents( a, b, c );
141 for ( Surface::vertices_t::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
143 vector3_normalise( reinterpret_cast<Vector3&>( ( *i ).tangent ) );
144 vector3_normalise( reinterpret_cast<Vector3&>( ( *i ).bitangent ) );
148 void render( RenderStateFlags state ) const {
150 if ( ( state & RENDER_BUMP ) != 0 ) {
151 if ( GlobalShaderCache().useShaderLanguage() ) {
152 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
153 glVertexAttribPointerARB( c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
154 glVertexAttribPointerARB( c_attr_Tangent, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->tangent );
155 glVertexAttribPointerARB( c_attr_Binormal, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->bitangent );
159 glVertexAttribPointerARB( 11, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
160 glVertexAttribPointerARB( 8, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
161 glVertexAttribPointerARB( 9, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->tangent );
162 glVertexAttribPointerARB( 10, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->bitangent );
167 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
168 glTexCoordPointer( 2, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
170 glVertexPointer( 3, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->vertex );
171 glDrawElements( GL_TRIANGLES, GLsizei( m_indices.size() ), RenderIndexTypeID, m_indices.data() );
173 glBegin( GL_TRIANGLES );
174 for ( unsigned int i = 0; i < m_indices.size(); ++i )
176 glTexCoord2fv( &m_vertices[m_indices[i]].texcoord.s );
177 glNormal3fv( &m_vertices[m_indices[i]].normal.x );
178 glVertex3fv( &m_vertices[m_indices[i]].vertex.x );
183 #if defined( _DEBUG )
186 for ( VertexBuffer<ArbitraryMeshVertex>::const_iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
188 Vector3 normal = vector3_added( vertex3f_to_vector3( ( *i ).vertex ), vector3_scaled( normal3f_to_vector3( ( *i ).normal ), 8 ) );
189 glVertex3fv( vertex3f_to_array( ( *i ).vertex ) );
190 glVertex3fv( vector3_to_array( normal ) );
196 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
197 return test.TestAABB( m_aabb_local, localToWorld );
200 const AABB& localAABB() const {
204 void render( Renderer& renderer, const Matrix4& localToWorld, Shader* state ) const {
205 renderer.SetState( state, Renderer::eFullMaterials );
206 renderer.addRenderable( *this, localToWorld );
209 void render( Renderer& renderer, const Matrix4& localToWorld ) const {
210 render( renderer, localToWorld, m_state );
213 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
214 test.BeginMesh( localToWorld );
216 SelectionIntersection best;
218 vertexpointer_arbitrarymeshvertex( m_vertices.data() ),
219 IndexPointer( m_indices.data(), IndexPointer::index_type( m_indices.size() ) ),
222 if ( best.valid() ) {
223 selector.addIntersection( best );
228 // generic model node
233 typedef std::vector<Surface*> surfaces_t;
234 surfaces_t m_surfaces;
238 Callback m_lightsChanged;
241 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
247 typedef surfaces_t::const_iterator const_iterator;
249 const_iterator begin() const {
250 return m_surfaces.begin();
252 const_iterator end() const {
253 return m_surfaces.end();
255 std::size_t size() const {
256 return m_surfaces.size();
259 Surface& newSurface(){
260 m_surfaces.push_back( new Surface );
261 return *m_surfaces.back();
264 m_aabb_local = AABB();
265 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
267 aabb_extend_by_aabb_safe( m_aabb_local, ( *i )->localAABB() );
271 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
272 return test.TestAABB( m_aabb_local, localToWorld );
275 virtual const AABB& localAABB() const {
279 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
280 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
282 if ( ( *i )->intersectVolume( test.getVolume(), localToWorld ) != c_volumeOutside ) {
283 ( *i )->testSelect( selector, test, localToWorld );
289 inline void Surface_addLight( const Surface& surface, VectorLightList& lights, const Matrix4& localToWorld, const RendererLight& light ){
290 if ( light.testAABB( aabb_for_oriented_aabb( surface.localAABB(), localToWorld ) ) ) {
291 lights.addLight( light );
295 class ModelInstance :
296 public scene::Instance,
298 public SelectionTestable,
299 public LightCullable,
304 InstanceTypeCastTable m_casts;
307 InstanceContainedCast<ModelInstance, Bounded>::install( m_casts );
308 InstanceContainedCast<ModelInstance, Cullable>::install( m_casts );
309 InstanceStaticCast<ModelInstance, Renderable>::install( m_casts );
310 InstanceStaticCast<ModelInstance, SelectionTestable>::install( m_casts );
311 InstanceStaticCast<ModelInstance, SkinnedModel>::install( m_casts );
313 InstanceTypeCastTable& get(){
320 const LightList* m_lightList;
321 typedef Array<VectorLightList> SurfaceLightLists;
322 SurfaceLightLists m_surfaceLightLists;
329 Remap() : second( 0 ){
332 typedef Array<Remap> SurfaceRemaps;
333 SurfaceRemaps m_skins;
336 typedef LazyStatic<TypeCasts> StaticTypeCasts;
338 Bounded& get( NullType<Bounded>){
341 Cullable& get( NullType<Cullable>){
345 void lightsChanged(){
346 m_lightList->lightsChanged();
348 typedef MemberCaller<ModelInstance, &ModelInstance::lightsChanged> LightsChangedCaller;
350 void constructRemaps(){
351 ModelSkin* skin = NodeTypeCast<ModelSkin>::cast( path().parent() );
352 if ( skin != 0 && skin->realised() ) {
353 SurfaceRemaps::iterator j = m_skins.begin();
354 for ( Model::const_iterator i = m_model.begin(); i != m_model.end(); ++i, ++j )
356 const char* remap = skin->getRemap( ( *i )->getShader() );
357 if ( !string_empty( remap ) ) {
358 ( *j ).first = remap;
359 ( *j ).second = GlobalShaderCache().capture( remap );
369 void destroyRemaps(){
370 for ( SurfaceRemaps::iterator i = m_skins.begin(); i != m_skins.end(); ++i )
372 if ( ( *i ).second != 0 ) {
373 GlobalShaderCache().release( ( *i ).first.c_str() );
379 ASSERT_MESSAGE( m_skins.size() == m_model.size(), "ERROR" );
384 ModelInstance( const scene::Path& path, scene::Instance* parent, Model& model ) :
385 Instance( path, parent, this, StaticTypeCasts::instance().get() ),
387 m_surfaceLightLists( m_model.size() ),
388 m_skins( m_model.size() ){
389 m_lightList = &GlobalShaderCache().attach( *this );
390 m_model.m_lightsChanged = LightsChangedCaller( *this );
392 Instance::setTransformChangedCallback( LightsChangedCaller( *this ) );
399 Instance::setTransformChangedCallback( Callback() );
401 m_model.m_lightsChanged = Callback();
402 GlobalShaderCache().detach( *this );
405 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
406 SurfaceLightLists::const_iterator j = m_surfaceLightLists.begin();
407 SurfaceRemaps::const_iterator k = m_skins.begin();
408 for ( Model::const_iterator i = m_model.begin(); i != m_model.end(); ++i, ++j, ++k )
410 if ( ( *i )->intersectVolume( volume, localToWorld ) != c_volumeOutside ) {
411 renderer.setLights( *j );
412 ( *i )->render( renderer, localToWorld, ( *k ).second != 0 ? ( *k ).second : ( *i )->getState() );
417 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
418 m_lightList->evaluateLights();
420 render( renderer, volume, Instance::localToWorld() );
422 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
423 renderSolid( renderer, volume );
426 void testSelect( Selector& selector, SelectionTest& test ){
427 m_model.testSelect( selector, test, Instance::localToWorld() );
430 bool testLight( const RendererLight& light ) const {
431 return light.testAABB( worldAABB() );
433 void insertLight( const RendererLight& light ){
434 const Matrix4& localToWorld = Instance::localToWorld();
435 SurfaceLightLists::iterator j = m_surfaceLightLists.begin();
436 for ( Model::const_iterator i = m_model.begin(); i != m_model.end(); ++i )
438 Surface_addLight( *( *i ), *j++, localToWorld, light );
442 for ( SurfaceLightLists::iterator i = m_surfaceLightLists.begin(); i != m_surfaceLightLists.end(); ++i )
449 class ModelNode : public scene::Node::Symbiot, public scene::Instantiable
453 NodeTypeCastTable m_casts;
456 NodeStaticCast<ModelNode, scene::Instantiable>::install( m_casts );
458 NodeTypeCastTable& get(){
465 InstanceSet m_instances;
469 typedef LazyStatic<TypeCasts> StaticTypeCasts;
471 ModelNode() : m_node( this, this, StaticTypeCasts::instance().get() ){
485 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
486 return new ModelInstance( path, parent, m_model );
488 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
489 m_instances.forEachInstance( visitor );
491 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
492 m_instances.insert( observer, path, instance );
494 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
495 return m_instances.erase( observer, path );
500 inline void Surface_constructQuad( Surface& surface, const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d, const Vector3& normal ){
501 surface.vertices().push_back(
503 vertex3f_for_vector3( a ),
504 normal3f_for_vector3( normal ),
505 texcoord2f_from_array( aabb_texcoord_topleft )
508 surface.vertices().push_back(
510 vertex3f_for_vector3( b ),
511 normal3f_for_vector3( normal ),
512 texcoord2f_from_array( aabb_texcoord_topright )
515 surface.vertices().push_back(
517 vertex3f_for_vector3( c ),
518 normal3f_for_vector3( normal ),
519 texcoord2f_from_array( aabb_texcoord_botright )
522 surface.vertices().push_back(
524 vertex3f_for_vector3( d ),
525 normal3f_for_vector3( normal ),
526 texcoord2f_from_array( aabb_texcoord_botleft )
531 inline void Model_constructNull( Model& model ){
532 Surface& surface = model.newSurface();
534 AABB aabb( Vector3( 0, 0, 0 ), Vector3( 8, 8, 8 ) );
537 aabb_corners( aabb, points );
539 surface.vertices().reserve( 24 );
541 Surface_constructQuad( surface, points[2], points[1], points[5], points[6], aabb_normals[0] );
542 Surface_constructQuad( surface, points[1], points[0], points[4], points[5], aabb_normals[1] );
543 Surface_constructQuad( surface, points[0], points[1], points[2], points[3], aabb_normals[2] );
544 Surface_constructQuad( surface, points[0], points[3], points[7], points[4], aabb_normals[3] );
545 Surface_constructQuad( surface, points[3], points[2], points[6], points[7], aabb_normals[4] );
546 Surface_constructQuad( surface, points[7], points[6], points[5], points[4], aabb_normals[5] );
548 surface.indices().reserve( 36 );
550 RenderIndex indices[36] = {
554 12, 13, 14, 12, 14, 15,
555 16, 17, 18, 16, 18, 19,
556 20, 21, 22, 10, 22, 23,
559 for ( RenderIndex* i = indices; i != indices + ( sizeof( indices ) / sizeof( RenderIndex ) ); ++i )
561 surface.indices().insert( *i );
564 surface.setShader( "" );
566 surface.updateAABB();