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)
48 m_lights.push_back(&light);
54 void evaluateLights() const
57 void lightsChanged() const
60 void forEachLight(const RendererLightCallback& callback) const
62 for(Lights::const_iterator i = m_lights.begin(); i != m_lights.end(); ++i)
69 inline VertexPointer vertexpointer_arbitrarymeshvertex(const ArbitraryMeshVertex* array)
71 return VertexPointer(VertexPointer::pointer(&array->vertex), sizeof(ArbitraryMeshVertex));
74 inline void parseTextureName(CopiedString& name, const char* token)
76 StringOutputStream cleaned(256);
77 cleaned << PathCleaned(token);
78 name = StringRange(cleaned.c_str(), path_get_filename_base_end(cleaned.c_str())); // remove extension
81 // generic renderable triangle surface
83 public OpenGLRenderable
86 typedef VertexBuffer<ArbitraryMeshVertex> vertices_t;
87 typedef IndexBuffer indices_t;
91 CopiedString m_shader;
94 vertices_t m_vertices;
99 m_state = GlobalShaderCache().capture(m_shader.c_str());
103 GlobalShaderCache().release(m_shader.c_str());
109 : m_shader(""), m_state(0)
118 vertices_t& vertices()
127 void setShader(const char* name)
130 parseTextureName(m_shader, name);
133 const char* getShader() const
135 return m_shader.c_str();
137 Shader* getState() const
143 m_aabb_local = AABB();
144 for(vertices_t::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i)
145 aabb_extend_by_point_safe(m_aabb_local, reinterpret_cast<const Vector3&>((*i).vertex));
149 for(Surface::indices_t::iterator i = m_indices.begin(); i != m_indices.end(); i += 3)
151 ArbitraryMeshVertex& a = m_vertices[*(i + 0)];
152 ArbitraryMeshVertex& b = m_vertices[*(i + 1)];
153 ArbitraryMeshVertex& c = m_vertices[*(i + 2)];
155 ArbitraryMeshTriangle_sumTangents(a, b, c);
158 for(Surface::vertices_t::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i)
160 vector3_normalise(reinterpret_cast<Vector3&>((*i).tangent));
161 vector3_normalise(reinterpret_cast<Vector3&>((*i).bitangent));
165 void render(RenderStateFlags state) const
168 if((state & RENDER_BUMP) != 0)
170 if(GlobalShaderCache().useShaderLanguage())
172 glNormalPointer(GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_vertices.data()->normal);
173 glVertexAttribPointerARB(c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_vertices.data()->texcoord);
174 glVertexAttribPointerARB(c_attr_Tangent, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_vertices.data()->tangent);
175 glVertexAttribPointerARB(c_attr_Binormal, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_vertices.data()->bitangent);
179 glVertexAttribPointerARB(11, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_vertices.data()->normal);
180 glVertexAttribPointerARB(8, 2, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_vertices.data()->texcoord);
181 glVertexAttribPointerARB(9, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_vertices.data()->tangent);
182 glVertexAttribPointerARB(10, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_vertices.data()->bitangent);
187 glNormalPointer(GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_vertices.data()->normal);
188 glTexCoordPointer(2, GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_vertices.data()->texcoord);
190 glVertexPointer(3, GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_vertices.data()->vertex);
191 glDrawElements(GL_TRIANGLES, GLsizei(m_indices.size()), RenderIndexTypeID, m_indices.data());
193 glBegin(GL_TRIANGLES);
194 for(unsigned int i = 0; i < m_indices.size(); ++i)
196 glTexCoord2fv(&m_vertices[m_indices[i]].texcoord.s);
197 glNormal3fv(&m_vertices[m_indices[i]].normal.x);
198 glVertex3fv(&m_vertices[m_indices[i]].vertex.x);
206 for(VertexBuffer<ArbitraryMeshVertex>::const_iterator i = m_vertices.begin(); i != m_vertices.end(); ++i)
208 Vector3 normal = vector3_added(vertex3f_to_vector3((*i).vertex), vector3_scaled(normal3f_to_vector3((*i).normal), 8));
209 glVertex3fv(vertex3f_to_array((*i).vertex));
210 glVertex3fv(vector3_to_array(normal));
216 VolumeIntersectionValue intersectVolume(const VolumeTest& test, const Matrix4& localToWorld) const
218 return test.TestAABB(m_aabb_local, localToWorld);
221 const AABB& localAABB() const
226 void render(Renderer& renderer, const Matrix4& localToWorld, Shader* state) const
228 renderer.SetState(state, Renderer::eFullMaterials);
229 renderer.addRenderable(*this, localToWorld);
232 void render(Renderer& renderer, const Matrix4& localToWorld) const
234 render(renderer, localToWorld, m_state);
237 void testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld)
239 test.BeginMesh(localToWorld);
241 SelectionIntersection best;
243 vertexpointer_arbitrarymeshvertex(m_vertices.data()),
244 IndexPointer(m_indices.data(), IndexPointer::index_type(m_indices.size())),
249 selector.addIntersection(best);
254 // generic model node
259 typedef std::vector<Surface*> surfaces_t;
260 surfaces_t m_surfaces;
264 Callback m_lightsChanged;
268 for(surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i)
274 typedef surfaces_t::const_iterator const_iterator;
276 const_iterator begin() const
278 return m_surfaces.begin();
280 const_iterator end() const
282 return m_surfaces.end();
284 std::size_t size() const
286 return m_surfaces.size();
289 Surface& newSurface()
291 m_surfaces.push_back(new Surface);
292 return *m_surfaces.back();
296 m_aabb_local = AABB();
297 for(surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i)
299 aabb_extend_by_aabb_safe(m_aabb_local, (*i)->localAABB());
303 VolumeIntersectionValue intersectVolume(const VolumeTest& test, const Matrix4& localToWorld) const
305 return test.TestAABB(m_aabb_local, localToWorld);
308 virtual const AABB& localAABB() const
313 void testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld)
315 for(surfaces_t::iterator i = m_surfaces.begin(); i != m_surfaces.end(); ++i)
317 if((*i)->intersectVolume(test.getVolume(), localToWorld) != c_volumeOutside)
319 (*i)->testSelect(selector, test, localToWorld);
325 inline void Surface_addLight(const Surface& surface, VectorLightList& lights, const Matrix4& localToWorld, const RendererLight& light)
327 if(light.testAABB(aabb_for_oriented_aabb(surface.localAABB(), localToWorld)))
329 lights.addLight(light);
333 class ModelInstance :
334 public scene::Instance,
336 public SelectionTestable,
337 public LightCullable,
342 InstanceTypeCastTable m_casts;
346 InstanceContainedCast<ModelInstance, Bounded>::install(m_casts);
347 InstanceContainedCast<ModelInstance, Cullable>::install(m_casts);
348 InstanceStaticCast<ModelInstance, Renderable>::install(m_casts);
349 InstanceStaticCast<ModelInstance, SelectionTestable>::install(m_casts);
350 InstanceStaticCast<ModelInstance, SkinnedModel>::install(m_casts);
352 InstanceTypeCastTable& get()
360 const LightList* m_lightList;
361 typedef Array<VectorLightList> SurfaceLightLists;
362 SurfaceLightLists m_surfaceLightLists;
373 typedef Array<Remap> SurfaceRemaps;
374 SurfaceRemaps m_skins;
377 typedef LazyStatic<TypeCasts> StaticTypeCasts;
379 Bounded& get(NullType<Bounded>)
383 Cullable& get(NullType<Cullable>)
390 m_lightList->lightsChanged();
392 typedef MemberCaller<ModelInstance, &ModelInstance::lightsChanged> LightsChangedCaller;
394 void constructRemaps()
396 ModelSkin* skin = NodeTypeCast<ModelSkin>::cast(path().parent());
397 if(skin != 0 && skin->realised())
399 SurfaceRemaps::iterator j = m_skins.begin();
400 for(Model::const_iterator i = m_model.begin(); i != m_model.end(); ++i, ++j)
402 const char* remap = skin->getRemap((*i)->getShader());
403 if(!string_empty(remap))
406 (*j).second = GlobalShaderCache().capture(remap);
418 for(SurfaceRemaps::iterator i = m_skins.begin(); i != m_skins.end(); ++i)
422 GlobalShaderCache().release((*i).first.c_str());
429 ASSERT_MESSAGE(m_skins.size() == m_model.size(), "ERROR");
434 ModelInstance(const scene::Path& path, scene::Instance* parent, Model& model) :
435 Instance(path, parent, this, StaticTypeCasts::instance().get()),
437 m_surfaceLightLists(m_model.size()),
438 m_skins(m_model.size())
440 m_lightList = &GlobalShaderCache().attach(*this);
441 m_model.m_lightsChanged = LightsChangedCaller(*this);
443 Instance::setTransformChangedCallback(LightsChangedCaller(*this));
451 Instance::setTransformChangedCallback(Callback());
453 m_model.m_lightsChanged = Callback();
454 GlobalShaderCache().detach(*this);
457 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
459 SurfaceLightLists::const_iterator j = m_surfaceLightLists.begin();
460 SurfaceRemaps::const_iterator k = m_skins.begin();
461 for(Model::const_iterator i = m_model.begin(); i != m_model.end(); ++i, ++j, ++k)
463 if((*i)->intersectVolume(volume, localToWorld) != c_volumeOutside)
465 renderer.setLights(*j);
466 (*i)->render(renderer, localToWorld, (*k).second != 0 ? (*k).second : (*i)->getState());
471 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
473 m_lightList->evaluateLights();
475 render(renderer, volume, Instance::localToWorld());
477 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
479 renderSolid(renderer, volume);
482 void testSelect(Selector& selector, SelectionTest& test)
484 m_model.testSelect(selector, test, Instance::localToWorld());
487 bool testLight(const RendererLight& light) const
489 return light.testAABB(worldAABB());
491 void insertLight(const RendererLight& light)
493 const Matrix4& localToWorld = Instance::localToWorld();
494 SurfaceLightLists::iterator j = m_surfaceLightLists.begin();
495 for(Model::const_iterator i = m_model.begin(); i != m_model.end(); ++i)
497 Surface_addLight(*(*i), *j++, localToWorld, light);
502 for(SurfaceLightLists::iterator i = m_surfaceLightLists.begin(); i != m_surfaceLightLists.end(); ++i)
509 class ModelNode : public scene::Node::Symbiot, public scene::Instantiable
513 NodeTypeCastTable m_casts;
517 NodeStaticCast<ModelNode, scene::Instantiable>::install(m_casts);
519 NodeTypeCastTable& get()
527 InstanceSet m_instances;
531 typedef LazyStatic<TypeCasts> StaticTypeCasts;
533 ModelNode() : m_node(this, this, StaticTypeCasts::instance().get())
551 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
553 return new ModelInstance(path, parent, m_model);
555 void forEachInstance(const scene::Instantiable::Visitor& visitor)
557 m_instances.forEachInstance(visitor);
559 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
561 m_instances.insert(observer, path, instance);
563 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
565 return m_instances.erase(observer, path);
570 inline void Surface_constructQuad(Surface& surface, const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d, const Vector3& normal)
572 surface.vertices().push_back(
574 vertex3f_for_vector3(a),
575 normal3f_for_vector3(normal),
576 texcoord2f_from_array(aabb_texcoord_topleft)
579 surface.vertices().push_back(
581 vertex3f_for_vector3(b),
582 normal3f_for_vector3(normal),
583 texcoord2f_from_array(aabb_texcoord_topright)
586 surface.vertices().push_back(
588 vertex3f_for_vector3(c),
589 normal3f_for_vector3(normal),
590 texcoord2f_from_array(aabb_texcoord_botright)
593 surface.vertices().push_back(
595 vertex3f_for_vector3(d),
596 normal3f_for_vector3(normal),
597 texcoord2f_from_array(aabb_texcoord_botleft)
602 inline void Model_constructNull(Model& model)
604 Surface& surface = model.newSurface();
606 AABB aabb(Vector3(0, 0, 0), Vector3(8, 8, 8));
609 aabb_corners(aabb, points);
611 surface.vertices().reserve(24);
613 Surface_constructQuad(surface, points[2], points[1], points[5], points[6], aabb_normals[0]);
614 Surface_constructQuad(surface, points[1], points[0], points[4], points[5], aabb_normals[1]);
615 Surface_constructQuad(surface, points[0], points[1], points[2], points[3], aabb_normals[2]);
616 Surface_constructQuad(surface, points[0], points[3], points[7], points[4], aabb_normals[3]);
617 Surface_constructQuad(surface, points[3], points[2], points[6], points[7], aabb_normals[4]);
618 Surface_constructQuad(surface, points[7], points[6], points[5], points[4], aabb_normals[5]);
620 surface.indices().reserve(36);
622 RenderIndex indices[36] = {
626 12, 13, 14, 12, 14, 15,
627 16, 17, 18, 16, 18, 19,
628 20, 21, 22, 10, 22, 23,
631 for(RenderIndex* i = indices; i != indices+(sizeof(indices)/sizeof(RenderIndex)); ++i)
633 surface.indices().insert(*i);
636 surface.setShader("");
638 surface.updateAABB();