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 {
149 if ( ( state & RENDER_BUMP ) != 0 ) {
150 if ( GlobalShaderCache().useShaderLanguage() ) {
151 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
152 glVertexAttribPointerARB( c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
153 glVertexAttribPointerARB( c_attr_Tangent, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->tangent );
154 glVertexAttribPointerARB( c_attr_Binormal, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->bitangent );
158 glVertexAttribPointerARB( 11, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
159 glVertexAttribPointerARB( 8, 2, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
160 glVertexAttribPointerARB( 9, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->tangent );
161 glVertexAttribPointerARB( 10, 3, GL_FLOAT, 0, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->bitangent );
166 glNormalPointer( GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->normal );
167 glTexCoordPointer( 2, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->texcoord );
169 glVertexPointer( 3, GL_FLOAT, sizeof( ArbitraryMeshVertex ), &m_vertices.data()->vertex );
170 glDrawElements( GL_TRIANGLES, GLsizei( m_indices.size() ), RenderIndexTypeID, m_indices.data() );
173 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
174 return test.TestAABB( m_aabb_local, localToWorld );
177 const AABB& localAABB() const {
181 void render( Renderer& renderer, const Matrix4& localToWorld, Shader* state ) const {
182 renderer.SetState( state, Renderer::eFullMaterials );
183 renderer.addRenderable( *this, localToWorld );
186 void render( Renderer& renderer, const Matrix4& localToWorld ) const {
187 render( renderer, localToWorld, m_state );
190 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
191 test.BeginMesh( localToWorld );
193 SelectionIntersection best;
195 vertexpointer_arbitrarymeshvertex( m_vertices.data() ),
196 IndexPointer( m_indices.data(), IndexPointer::index_type( m_indices.size() ) ),
199 if ( best.valid() ) {
200 selector.addIntersection( best );
205 // generic model node
210 typedef std::vector<Surface*> surfaces_t;
211 surfaces_t m_surfaces;
215 Callback m_lightsChanged;
218 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
224 typedef surfaces_t::const_iterator const_iterator;
226 const_iterator begin() const {
227 return m_surfaces.begin();
229 const_iterator end() const {
230 return m_surfaces.end();
232 std::size_t size() const {
233 return m_surfaces.size();
236 Surface& newSurface(){
237 m_surfaces.push_back( new Surface );
238 return *m_surfaces.back();
241 m_aabb_local = AABB();
242 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
244 aabb_extend_by_aabb_safe( m_aabb_local, ( *i )->localAABB() );
248 VolumeIntersectionValue intersectVolume( const VolumeTest& test, const Matrix4& localToWorld ) const {
249 return test.TestAABB( m_aabb_local, localToWorld );
252 virtual const AABB& localAABB() const {
256 void testSelect( Selector& selector, SelectionTest& test, const Matrix4& localToWorld ){
257 for ( surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i )
259 if ( ( *i )->intersectVolume( test.getVolume(), localToWorld ) != c_volumeOutside ) {
260 ( *i )->testSelect( selector, test, localToWorld );
266 inline void Surface_addLight( const Surface& surface, VectorLightList& lights, const Matrix4& localToWorld, const RendererLight& light ){
267 if ( light.testAABB( aabb_for_oriented_aabb( surface.localAABB(), localToWorld ) ) ) {
268 lights.addLight( light );
272 class ModelInstance :
273 public scene::Instance,
275 public SelectionTestable,
276 public LightCullable,
281 InstanceTypeCastTable m_casts;
284 InstanceContainedCast<ModelInstance, Bounded>::install( m_casts );
285 InstanceContainedCast<ModelInstance, Cullable>::install( m_casts );
286 InstanceStaticCast<ModelInstance, Renderable>::install( m_casts );
287 InstanceStaticCast<ModelInstance, SelectionTestable>::install( m_casts );
288 InstanceStaticCast<ModelInstance, SkinnedModel>::install( m_casts );
290 InstanceTypeCastTable& get(){
297 const LightList* m_lightList;
298 typedef Array<VectorLightList> SurfaceLightLists;
299 SurfaceLightLists m_surfaceLightLists;
306 Remap() : second( 0 ){
309 typedef Array<Remap> SurfaceRemaps;
310 SurfaceRemaps m_skins;
313 typedef LazyStatic<TypeCasts> StaticTypeCasts;
315 Bounded& get( NullType<Bounded>){
318 Cullable& get( NullType<Cullable>){
322 void lightsChanged(){
323 m_lightList->lightsChanged();
325 typedef MemberCaller<ModelInstance, &ModelInstance::lightsChanged> LightsChangedCaller;
327 void constructRemaps(){
328 ModelSkin* skin = NodeTypeCast<ModelSkin>::cast( path().parent() );
329 if ( skin != 0 && skin->realised() ) {
330 SurfaceRemaps::iterator j = m_skins.begin();
331 for ( Model::const_iterator i = m_model.begin(); i != m_model.end(); ++i, ++j )
333 const char* remap = skin->getRemap( ( *i )->getShader() );
334 if ( !string_empty( remap ) ) {
335 ( *j ).first = remap;
336 ( *j ).second = GlobalShaderCache().capture( remap );
346 void destroyRemaps(){
347 for ( SurfaceRemaps::iterator i = m_skins.begin(); i != m_skins.end(); ++i )
349 if ( ( *i ).second != 0 ) {
350 GlobalShaderCache().release( ( *i ).first.c_str() );
356 ASSERT_MESSAGE( m_skins.size() == m_model.size(), "ERROR" );
361 ModelInstance( const scene::Path& path, scene::Instance* parent, Model& model ) :
362 Instance( path, parent, this, StaticTypeCasts::instance().get() ),
364 m_surfaceLightLists( m_model.size() ),
365 m_skins( m_model.size() ){
366 m_lightList = &GlobalShaderCache().attach( *this );
367 m_model.m_lightsChanged = LightsChangedCaller( *this );
369 Instance::setTransformChangedCallback( LightsChangedCaller( *this ) );
376 Instance::setTransformChangedCallback( Callback() );
378 m_model.m_lightsChanged = Callback();
379 GlobalShaderCache().detach( *this );
382 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
383 SurfaceLightLists::const_iterator j = m_surfaceLightLists.begin();
384 SurfaceRemaps::const_iterator k = m_skins.begin();
385 for ( Model::const_iterator i = m_model.begin(); i != m_model.end(); ++i, ++j, ++k )
387 if ( ( *i )->intersectVolume( volume, localToWorld ) != c_volumeOutside ) {
388 renderer.setLights( *j );
389 ( *i )->render( renderer, localToWorld, ( *k ).second != 0 ? ( *k ).second : ( *i )->getState() );
394 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
395 m_lightList->evaluateLights();
397 render( renderer, volume, Instance::localToWorld() );
399 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
400 renderSolid( renderer, volume );
403 void testSelect( Selector& selector, SelectionTest& test ){
404 m_model.testSelect( selector, test, Instance::localToWorld() );
407 bool testLight( const RendererLight& light ) const {
408 return light.testAABB( worldAABB() );
410 void insertLight( const RendererLight& light ){
411 const Matrix4& localToWorld = Instance::localToWorld();
412 SurfaceLightLists::iterator j = m_surfaceLightLists.begin();
413 for ( Model::const_iterator i = m_model.begin(); i != m_model.end(); ++i )
415 Surface_addLight( *( *i ), *j++, localToWorld, light );
419 for ( SurfaceLightLists::iterator i = m_surfaceLightLists.begin(); i != m_surfaceLightLists.end(); ++i )
426 class ModelNode : public scene::Node::Symbiot, public scene::Instantiable
430 NodeTypeCastTable m_casts;
433 NodeStaticCast<ModelNode, scene::Instantiable>::install( m_casts );
435 NodeTypeCastTable& get(){
442 InstanceSet m_instances;
446 typedef LazyStatic<TypeCasts> StaticTypeCasts;
448 ModelNode() : m_node( this, this, StaticTypeCasts::instance().get() ){
462 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
463 return new ModelInstance( path, parent, m_model );
465 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
466 m_instances.forEachInstance( visitor );
468 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
469 m_instances.insert( observer, path, instance );
471 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
472 return m_instances.erase( observer, path );
477 inline void Surface_constructQuad( Surface& surface, const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d, const Vector3& normal ){
478 surface.vertices().push_back(
480 vertex3f_for_vector3( a ),
481 normal3f_for_vector3( normal ),
482 texcoord2f_from_array( aabb_texcoord_topleft )
485 surface.vertices().push_back(
487 vertex3f_for_vector3( b ),
488 normal3f_for_vector3( normal ),
489 texcoord2f_from_array( aabb_texcoord_topright )
492 surface.vertices().push_back(
494 vertex3f_for_vector3( c ),
495 normal3f_for_vector3( normal ),
496 texcoord2f_from_array( aabb_texcoord_botright )
499 surface.vertices().push_back(
501 vertex3f_for_vector3( d ),
502 normal3f_for_vector3( normal ),
503 texcoord2f_from_array( aabb_texcoord_botleft )
508 inline void Model_constructNull( Model& model ){
509 Surface& surface = model.newSurface();
511 AABB aabb( Vector3( 0, 0, 0 ), Vector3( 8, 8, 8 ) );
514 aabb_corners( aabb, points );
516 surface.vertices().reserve( 24 );
518 Surface_constructQuad( surface, points[2], points[1], points[5], points[6], aabb_normals[0] );
519 Surface_constructQuad( surface, points[1], points[0], points[4], points[5], aabb_normals[1] );
520 Surface_constructQuad( surface, points[0], points[1], points[2], points[3], aabb_normals[2] );
521 Surface_constructQuad( surface, points[0], points[3], points[7], points[4], aabb_normals[3] );
522 Surface_constructQuad( surface, points[3], points[2], points[6], points[7], aabb_normals[4] );
523 Surface_constructQuad( surface, points[7], points[6], points[5], points[4], aabb_normals[5] );
525 surface.indices().reserve( 36 );
527 RenderIndex indices[36] = {
531 12, 13, 14, 12, 14, 15,
532 16, 17, 18, 16, 18, 19,
533 20, 21, 22, 10, 22, 23,
536 for ( RenderIndex* i = indices; i != indices + ( sizeof( indices ) / sizeof( RenderIndex ) ); ++i )
538 surface.indices().insert( *i );
541 surface.setShader( "" );
543 surface.updateAABB();