/*
-Copyright (C) 2001-2006, William Joseph.
-All Rights Reserved.
-
-This file is part of GtkRadiant.
-
-GtkRadiant is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
-
-GtkRadiant is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with GtkRadiant; if not, write to the Free Software
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-*/
-
-///\file
-///\brief Represents any entity which has a fixed size specified in its entity-definition and displays a model (e.g. ammo_bfg).
-///
-/// This entity displays the model specified in its entity-definition.
-/// The "origin" and "angle" keys directly control the entity's local-to-parent transform.
-/// The "rotation" key directly controls the entity's local-to-parent transform for Doom3 only.
-
-#include "eclassmodel.h"
-
-#include "cullable.h"
-#include "renderable.h"
-#include "editable.h"
-
-#include "selectionlib.h"
-#include "instancelib.h"
-#include "transformlib.h"
-#include "traverselib.h"
-#include "entitylib.h"
-#include "render.h"
-#include "eclasslib.h"
-#include "pivot.h"
-
-#include "targetable.h"
-#include "origin.h"
-#include "angle.h"
-#include "rotation.h"
-#include "model.h"
-#include "filters.h"
-#include "namedentity.h"
-#include "keyobservers.h"
-#include "namekeys.h"
-#include "modelskinkey.h"
-
-#include "entity.h"
-
-class EclassModel :
- public Snappable
-{
- MatrixTransform m_transform;
- EntityKeyValues m_entity;
- KeyObserverMap m_keyObservers;
-
- OriginKey m_originKey;
- Vector3 m_origin;
- AngleKey m_angleKey;
- float m_angle;
- RotationKey m_rotationKey;
- Float9 m_rotation;
- SingletonModel m_model;
-
- ClassnameFilter m_filter;
- NamedEntity m_named;
- NameKeys m_nameKeys;
- RenderablePivot m_renderOrigin;
- RenderableNamedEntity m_renderName;
- ModelSkinKey m_skin;
-
- Callback m_transformChanged;
- Callback m_evaluateTransform;
-
- void construct()
- {
- default_rotation(m_rotation);
-
- m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
- m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
- if(g_gameType == eGameTypeDoom3)
- {
- m_keyObservers.insert("angle", RotationKey::AngleChangedCaller(m_rotationKey));
- m_keyObservers.insert("rotation", RotationKey::RotationChangedCaller(m_rotationKey));
- }
- else
- {
- m_keyObservers.insert("angle", AngleKey::AngleChangedCaller(m_angleKey));
- }
- m_keyObservers.insert("origin", OriginKey::OriginChangedCaller(m_originKey));
- }
-
-// vc 2k5 compiler fix
-#if _MSC_VER >= 1400
- public:
-#endif
-
- void updateTransform()
- {
- m_transform.localToParent() = g_matrix4_identity;
- matrix4_translate_by_vec3(m_transform.localToParent(), m_origin);
-
- if(g_gameType == eGameTypeDoom3)
- {
- matrix4_multiply_by_matrix4(m_transform.localToParent(), rotation_toMatrix(m_rotation));
- }
- else
- {
- matrix4_multiply_by_matrix4(m_transform.localToParent(), matrix4_rotation_for_z_degrees(m_angle));
- }
-
- m_transformChanged();
- }
- typedef MemberCaller<EclassModel, &EclassModel::updateTransform> UpdateTransformCaller;
-
- void originChanged()
- {
- m_origin = m_originKey.m_origin;
- updateTransform();
- }
- typedef MemberCaller<EclassModel, &EclassModel::originChanged> OriginChangedCaller;
- void angleChanged()
- {
- m_angle = m_angleKey.m_angle;
- updateTransform();
- }
- typedef MemberCaller<EclassModel, &EclassModel::angleChanged> AngleChangedCaller;
- void rotationChanged()
- {
- rotation_assign(m_rotation, m_rotationKey.m_rotation);
- updateTransform();
- }
- typedef MemberCaller<EclassModel, &EclassModel::rotationChanged> RotationChangedCaller;
-
- void skinChanged()
- {
- scene::Node* node = m_model.getNode();
- if(node != 0)
- {
- Node_modelSkinChanged(*node);
- }
- }
- typedef MemberCaller<EclassModel, &EclassModel::skinChanged> SkinChangedCaller;
-
-public:
-
- EclassModel(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform) :
- m_entity(eclass),
- m_originKey(OriginChangedCaller(*this)),
- m_origin(ORIGINKEY_IDENTITY),
- m_angleKey(AngleChangedCaller(*this)),
- m_angle(ANGLEKEY_IDENTITY),
- m_rotationKey(RotationChangedCaller(*this)),
- m_filter(m_entity, node),
- m_named(m_entity),
- m_nameKeys(m_entity),
- m_renderName(m_named, g_vector3_identity),
- m_skin(SkinChangedCaller(*this)),
- m_transformChanged(transformChanged),
- m_evaluateTransform(evaluateTransform)
- {
- construct();
- }
- EclassModel(const EclassModel& other, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform) :
- m_entity(other.m_entity),
- m_originKey(OriginChangedCaller(*this)),
- m_origin(ORIGINKEY_IDENTITY),
- m_angleKey(AngleChangedCaller(*this)),
- m_angle(ANGLEKEY_IDENTITY),
- m_rotationKey(RotationChangedCaller(*this)),
- m_filter(m_entity, node),
- m_named(m_entity),
- m_nameKeys(m_entity),
- m_renderName(m_named, g_vector3_identity),
- m_skin(SkinChangedCaller(*this)),
- m_transformChanged(transformChanged),
- m_evaluateTransform(evaluateTransform)
- {
- construct();
- }
-
- InstanceCounter m_instanceCounter;
- void instanceAttach(const scene::Path& path)
- {
- if(++m_instanceCounter.m_count == 1)
- {
- m_filter.instanceAttach();
- m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
- m_entity.attach(m_keyObservers);
- m_model.modelChanged(m_entity.getEntityClass().modelpath());
- m_skin.skinChanged(m_entity.getEntityClass().skin());
- }
- }
- void instanceDetach(const scene::Path& path)
- {
- if(--m_instanceCounter.m_count == 0)
- {
- m_skin.skinChanged("");
- m_model.modelChanged("");
- m_entity.detach(m_keyObservers);
- m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
- m_filter.instanceDetach();
- }
- }
-
- EntityKeyValues& getEntity()
- {
- return m_entity;
- }
- const EntityKeyValues& getEntity() const
- {
- return m_entity;
- }
-
- scene::Traversable& getTraversable()
- {
- return m_model.getTraversable();
- }
- Namespaced& getNamespaced()
- {
- return m_nameKeys;
- }
- Nameable& getNameable()
- {
- return m_named;
- }
- TransformNode& getTransformNode()
- {
- return m_transform;
- }
- ModelSkin& getModelSkin()
- {
- return m_skin.get();
- }
-
- void attach(scene::Traversable::Observer* observer)
- {
- m_model.attach(observer);
- }
- void detach(scene::Traversable::Observer* observer)
- {
- m_model.detach(observer);
- }
-
- void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
- {
- if(selected)
- {
- m_renderOrigin.render(renderer, volume, localToWorld);
- }
-
- renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
- }
- void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
- {
- renderSolid(renderer, volume, localToWorld, selected);
- if(g_showNames)
- {
- renderer.addRenderable(m_renderName, localToWorld);
- }
- }
-
- void translate(const Vector3& translation)
- {
- m_origin = origin_translated(m_origin, translation);
- }
- void rotate(const Quaternion& rotation)
- {
- if(g_gameType == eGameTypeDoom3)
- {
- rotation_rotate(m_rotation, rotation);
- }
- else
- {
- m_angle = angle_rotated(m_angle, rotation);
- }
- }
- void snapto(float snap)
- {
- m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
- m_originKey.write(&m_entity);
- }
- void revertTransform()
- {
- m_origin = m_originKey.m_origin;
- if(g_gameType == eGameTypeDoom3)
- {
- rotation_assign(m_rotation, m_rotationKey.m_rotation);
- }
- else
- {
- m_angle = m_angleKey.m_angle;
- }
- }
- void freezeTransform()
- {
- m_originKey.m_origin = m_origin;
- m_originKey.write(&m_entity);
- if(g_gameType == eGameTypeDoom3)
- {
- rotation_assign(m_rotationKey.m_rotation, m_rotation);
- m_rotationKey.write(&m_entity);
- }
- else
- {
- m_angleKey.m_angle = m_angle;
- m_angleKey.write(&m_entity);
- }
- }
- void transformChanged()
- {
- revertTransform();
- m_evaluateTransform();
- updateTransform();
- }
- typedef MemberCaller<EclassModel, &EclassModel::transformChanged> TransformChangedCaller;
-};
-
-class EclassModelInstance : public TargetableInstance, public TransformModifier, public Renderable
-{
- class TypeCasts
- {
- InstanceTypeCastTable m_casts;
- public:
- TypeCasts()
- {
- m_casts = TargetableInstance::StaticTypeCasts::instance().get();
- InstanceStaticCast<EclassModelInstance, Renderable>::install(m_casts);
- InstanceStaticCast<EclassModelInstance, Transformable>::install(m_casts);
- InstanceIdentityCast<EclassModelInstance>::install(m_casts);
- }
- InstanceTypeCastTable& get()
- {
- return m_casts;
- }
- };
-
- EclassModel& m_contained;
-public:
- typedef LazyStatic<TypeCasts> StaticTypeCasts;
-
- STRING_CONSTANT(Name, "EclassModelInstance");
-
- EclassModelInstance(const scene::Path& path, scene::Instance* parent, EclassModel& contained) :
- TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this),
- TransformModifier(EclassModel::TransformChangedCaller(contained), ApplyTransformCaller(*this)),
- m_contained(contained)
- {
- m_contained.instanceAttach(Instance::path());
-
- StaticRenderableConnectionLines::instance().attach(*this);
- }
- ~EclassModelInstance()
- {
- StaticRenderableConnectionLines::instance().detach(*this);
-
- m_contained.instanceDetach(Instance::path());
- }
- void renderSolid(Renderer& renderer, const VolumeTest& volume) const
- {
- m_contained.renderSolid(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
- }
- void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
- {
- m_contained.renderWireframe(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
- }
-
- void evaluateTransform()
- {
- if(getType() == TRANSFORM_PRIMITIVE)
- {
- m_contained.translate(getTranslation());
- m_contained.rotate(getRotation());
- }
- }
- void applyTransform()
- {
- m_contained.revertTransform();
- evaluateTransform();
- m_contained.freezeTransform();
- }
- typedef MemberCaller<EclassModelInstance, &EclassModelInstance::applyTransform> ApplyTransformCaller;
-};
-
-class EclassModelNode :
- public scene::Node::Symbiot,
- public scene::Instantiable,
- public scene::Cloneable,
- public scene::Traversable::Observer
-{
- class TypeCasts
- {
- NodeTypeCastTable m_casts;
- public:
- TypeCasts()
- {
- NodeStaticCast<EclassModelNode, scene::Instantiable>::install(m_casts);
- NodeStaticCast<EclassModelNode, scene::Cloneable>::install(m_casts);
- NodeContainedCast<EclassModelNode, scene::Traversable>::install(m_casts);
- NodeContainedCast<EclassModelNode, Snappable>::install(m_casts);
- NodeContainedCast<EclassModelNode, TransformNode>::install(m_casts);
- NodeContainedCast<EclassModelNode, Entity>::install(m_casts);
- NodeContainedCast<EclassModelNode, Nameable>::install(m_casts);
- NodeContainedCast<EclassModelNode, Namespaced>::install(m_casts);
- NodeContainedCast<EclassModelNode, ModelSkin>::install(m_casts);
- }
- NodeTypeCastTable& get()
- {
- return m_casts;
- }
- };
-
-
- scene::Node m_node;
- InstanceSet m_instances;
- EclassModel m_contained;
-
- void construct()
- {
- m_contained.attach(this);
- }
- void destroy()
- {
- m_contained.detach(this);
- }
-
-public:
- typedef LazyStatic<TypeCasts> StaticTypeCasts;
-
- scene::Traversable& get(NullType<scene::Traversable>)
- {
- return m_contained.getTraversable();
- }
- Snappable& get(NullType<Snappable>)
- {
- return m_contained;
- }
- TransformNode& get(NullType<TransformNode>)
- {
- return m_contained.getTransformNode();
- }
- Entity& get(NullType<Entity>)
- {
- return m_contained.getEntity();
- }
- Nameable& get(NullType<Nameable>)
- {
- return m_contained.getNameable();
- }
- Namespaced& get(NullType<Namespaced>)
- {
- return m_contained.getNamespaced();
- }
- ModelSkin& get(NullType<ModelSkin>)
- {
- return m_contained.getModelSkin();
- }
-
- EclassModelNode(EntityClass* eclass) :
- m_node(this, this, StaticTypeCasts::instance().get()),
- m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSetEvaluateTransform<EclassModelInstance>::Caller(m_instances))
- {
- construct();
- }
- EclassModelNode(const EclassModelNode& other) :
- scene::Node::Symbiot(other),
- scene::Instantiable(other),
- scene::Cloneable(other),
- scene::Traversable::Observer(other),
- m_node(this, this, StaticTypeCasts::instance().get()),
- m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSetEvaluateTransform<EclassModelInstance>::Caller(m_instances))
- {
- construct();
- }
- ~EclassModelNode()
- {
- destroy();
- }
- void release()
- {
- delete this;
- }
- scene::Node& node()
- {
- return m_node;
- }
-
- void insert(scene::Node& child)
- {
- m_instances.insert(child);
- }
- void erase(scene::Node& child)
- {
- m_instances.erase(child);
- }
-
- scene::Node& clone() const
- {
- return (new EclassModelNode(*this))->node();
- }
-
- scene::Instance* create(const scene::Path& path, scene::Instance* parent)
- {
- return new EclassModelInstance(path, parent, m_contained);
- }
- void forEachInstance(const scene::Instantiable::Visitor& visitor)
- {
- m_instances.forEachInstance(visitor);
- }
- void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
- {
- m_instances.insert(observer, path, instance);
- }
- scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
- {
- return m_instances.erase(observer, path);
- }
-};
-
-scene::Node& New_EclassModel(EntityClass* eclass)
-{
- return (new EclassModelNode(eclass))->node();
+ Copyright (C) 1999-2007 id Software, Inc. and contributors.
+ For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+ This file is part of GtkRadiant.
+
+ GtkRadiant is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ GtkRadiant is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GtkRadiant; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdlib.h>
+
+#include "entity_entitymodel.h"
+
+//
+// CEntityEclassModel implementation
+//
+
+CEntityEclassModel::CEntityEclassModel (){
+ refCount = 1;
+ m_eclass = NULL;
+ m_model = NULL;
+ VectorSet( m_translate, 0,0,0 );
+ VectorSet( m_euler, 0,0,0 );
+ VectorSet( m_scale, 1,1,1 );
+ VectorSet( m_pivot, 0,0,0 );
+ m4x4_identity( m_transform );
+ m4x4_identity( m_inverse_transform );
}
+CEntityEclassModel::~CEntityEclassModel (){
+ if ( m_name.c_str()[0] != '\0'
+ && m_version.c_str()[0] != '\0' ) {
+ GetModelCache()->DeleteByID( m_name.c_str(), m_version.c_str() );
+ }
+}
+
+
+// IRender
+
+void CEntityEclassModel::Draw( int state, int rflags ) const {
+ // push the current modelview matrix
+ // FIXME: put in a check for stack recursion depth..
+ // or avoid recursion of opengl matrix stack
+ g_QglTable.m_pfn_qglPushMatrix();
+ // apply the parent-to-local transform
+ g_QglTable.m_pfn_qglMultMatrixf( m_transform );
+
+ // draw children
+ if ( m_model && m_model->pRender ) {
+ m_model->pRender->Draw( state, rflags );
+ }
+ g_QglTable.m_pfn_qglPopMatrix();
+}
+
+// ISelect
+
+bool CEntityEclassModel::TestRay( const ray_t *ray, vec_t *dist ) const {
+ vec_t dist_start = *dist;
+ vec_t dist_local = *dist;
+ ray_t ray_local = *ray;
+
+ if ( aabb_intersect_ray( &m_BBox, &ray_local, &dist_local ) ) {
+ *dist = dist_local;
+ }
+ return *dist < dist_start;
+}
+
+
+//IEdit
+
+void CEntityEclassModel::Translate( const vec3_t translation ){
+ VectorIncrement( translation, m_translate );
+ UpdateCachedData();
+}
+
+void CEntityEclassModel::Rotate( const vec3_t pivot, const vec3_t rotation ){
+ m4x4_t rotation_matrix;
+
+ m4x4_identity( rotation_matrix );
+ m4x4_pivoted_rotate_by_vec3( rotation_matrix, rotation, eXYZ, pivot );
+ m4x4_transform_point( rotation_matrix, m_translate );
+
+ VectorIncrement( rotation, m_euler );
+
+ UpdateCachedData();
+}
+
+void CEntityEclassModel::OnKeyValueChanged( entity_t *e, const char *key, const char* value ){
+ if ( strcmp( key,"origin" ) == 0 ) {
+ sscanf( value, "%f %f %f", &m_translate[0], &m_translate[1], &m_translate[2] );
+ UpdateCachedData();
+ }
+ else if ( strcmp( key,"angle" ) == 0 ) {
+ VectorSet( m_euler, 0, 0, (float) atof( value ) );
+ UpdateCachedData();
+ }
+}
+
+void CEntityEclassModel::SetEclass( const eclass_t* eclass ){
+ m_eclass = eclass;
+}
+
+void CEntityEclassModel::SetName( const char *name ){
+ if ( strcmp( m_name.c_str(), name ) == 0 ) {
+ return;
+ }
+
+ if ( m_name.c_str()[0] != '\0'
+ && m_version.c_str()[0] != '\0' ) {
+ GetModelCache()->DeleteByID( m_name.c_str(), m_version.c_str() );
+ }
+
+ m_model = NULL;
+ m_name = name;
+
+ if ( m_name.c_str()[0] != '\0' ) {
+ const char* dot = strrchr( m_name.c_str(), '.' );
+ if ( dot != NULL ) {
+ m_version = ++dot;
+ m_model = GetModelCache()->GetByID( m_name.c_str(), m_version.c_str() );
+ }
+ }
+
+ UpdateCachedData();
+}
+
+//
+// CEntityEclassModel
+//
+
+// private:
+
+void CEntityEclassModel::UpdateCachedData(){
+ aabb_t aabb_temp;
+
+ aabb_clear( &aabb_temp );
+
+ m4x4_identity( m_transform );
+ m4x4_pivoted_transform_by_vec3( m_transform, m_translate, m_euler, eXYZ, m_scale, m_pivot );
+ memcpy( m_inverse_transform, m_transform, sizeof( m4x4_t ) );
+ if ( m4x4_invert( m_inverse_transform ) == 1 ) {
+ Sys_Printf( "ERROR: Singular Matrix, cannot invert" );
+ }
+
+ if ( m_eclass ) {
+ aabb_construct_for_vec3( &aabb_temp, m_eclass->mins, m_eclass->maxs );
+ }
+ else{
+ VectorSet( aabb_temp.extents, 8, 8, 8 );
+ }
+
+ aabb_for_transformed_aabb( &m_BBox, &aabb_temp, m_transform );
+}