2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
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 "entity_entitymodel.h"
28 // CEntityMiscModel implementation
31 CEntityMiscModel::CEntityMiscModel (entity_t *e)
36 VectorSet(m_translate, 0,0,0);
37 VectorSet(m_euler, 0,0,0);
38 VectorSet(m_scale, 1,1,1);
39 VectorSet(m_pivot, 0,0,0);
40 m4x4_identity(m_transform);
41 m4x4_identity(m_inverse_transform);
44 CEntityMiscModel::~CEntityMiscModel ()
46 if(m_cachereq.GetBuffer()[0] != ':'
47 && m_version.c_str()[0] != '\0')
48 GetModelCache()->DeleteByID(m_cachereq.GetBuffer(), m_version.c_str());
54 void CEntityMiscModel::Draw(int state, int rflags) const
56 // push the current modelview matrix
57 // FIXME: put in a check for stack recursion depth..
58 // or avoid recursion of opengl matrix stack
59 g_QglTable.m_pfn_qglPushMatrix();
60 // apply the parent-to-local transform
61 g_QglTable.m_pfn_qglMultMatrixf(m_transform);
66 if(m_model && m_model->pRender)
68 m_model->pRender->Draw(state, rflags);
71 g_QglTable.m_pfn_qglPopMatrix();
76 bool CEntityMiscModel::TestRay(const ray_t *ray, vec_t *dist) const
78 vec_t dist_start = *dist;
79 vec_t dist_local = *dist;
80 ray_t ray_local = *ray;
82 if (aabb_test_ray(&m_BBox, ray) == 0)
85 ray_transform(&ray_local, m_inverse_transform);
87 if(m_model && m_model->pSelect)
89 if(m_model->pSelect->TestRay(&ray_local, &dist_local))
92 else *dist = dist_local;
94 return *dist < dist_start;
100 void CEntityMiscModel::Translate(const vec3_t translation)
102 VectorIncrement(translation, m_translate);
106 void CEntityMiscModel::Rotate(const vec3_t pivot, const vec3_t rotation)
108 m4x4_t rotation_matrix;
110 m4x4_identity(rotation_matrix);
111 m4x4_pivoted_rotate_by_vec3(rotation_matrix, rotation, eXYZ, pivot);
112 m4x4_transform_point(rotation_matrix, m_translate);
114 VectorIncrement(rotation, m_euler);
119 void CEntityMiscModel::OnKeyValueChanged(entity_t *e, const char *key, const char* value)
121 if(strcmp(key, "model") == 0)
123 else if(strcmp(key, "_frame") == 0)
125 SetName(ValueForKey(e, "model"));
127 else if(strcmp(key, "angle") == 0)
129 VectorSet(m_euler, 0.f, 0.f, 0.f);
130 m_euler[2] = atof(value);
133 else if(strcmp(key, "angles") == 0)
135 VectorSet(m_euler, 0.f, 0.f, 0.f);
136 if (value[0] != '\0')
137 sscanf (value, "%f %f %f", &m_euler[1], &m_euler[2], &m_euler[0]);
140 else if(strcmp(key, "modelscale") == 0 || strcmp(key,"modelscale_vec") == 0)
143 VectorSet(m_scale, 1.f, 1.f, 1.f);
144 s = ValueForKey(e,"modelscale");
149 VectorSet(m_scale, f, f, f);
151 Sys_FPrintf(SYS_WRN, "WARNING: ignoring 0 modelscale key\n");
153 s = ValueForKey(e,"modelscale_vec");
156 sscanf (s, "%f %f %f", &m_scale[0], &m_scale[1], &m_scale[2]);
157 if (m_scale[0] == 0.0 && m_scale[1] == 0.0 && m_scale[2] == 0.0)
159 VectorSet(m_scale, 1,1,1);
160 Sys_FPrintf(SYS_WRN, "WARNING: ignoring 0 0 0 modelscale_vec key\n");
165 else if(strcmp(key, "origin") == 0)
167 sscanf(value, "%f %f %f", &m_translate[0], &m_translate[1], &m_translate[2]);
170 else if(strncmp(key,"_remap",6) == 0)
172 SetName(ValueForKey(e, "model"));
182 void CEntityMiscModel::BuildCacheRequestString(const char *name)
184 bool hasRemaps = false;
186 m_cachereq.Format( "%s:%i", name, IntForKey(m_entity,"_frame") );
188 for (epair_t* ep = m_entity->epairs ; ep ; ep=ep->next)
190 if( strncmp(ep->key,"_remap",6) == 0 )
199 m_cachereq += ep->value;
204 void CEntityMiscModel::SetName(const char *name)
206 Str m_oldcachereq = m_cachereq;
208 if( name[0] == '\0' ) {
212 BuildCacheRequestString(name);
214 if(strcmp(m_oldcachereq, m_cachereq) == 0)
217 if(m_cachereq.GetBuffer()[0] != ':'
218 && m_version.c_str()[0] != '\0')
219 GetModelCache()->DeleteByID(m_cachereq.GetBuffer(), m_version.c_str());
225 const char* dot = strrchr(name, '.');
229 m_model = GetModelCache()->GetByID(m_cachereq.GetBuffer(), m_version.c_str());
237 void CEntityMiscModel::UpdateCachedData()
241 m4x4_identity(m_transform);
242 m4x4_pivoted_transform_by_vec3(m_transform, m_translate, m_euler, eXYZ, m_scale, m_pivot);
243 memcpy(m_inverse_transform, m_transform, sizeof(m4x4_t));
244 m4x4_invert(m_inverse_transform);
246 aabb_clear(&aabb_temp);
248 if(m_model && m_model->pRender)
249 aabb_extend_by_aabb(&aabb_temp, m_model->pRender->GetAABB());
251 VectorSet(aabb_temp.extents, 8, 8, 8);
253 aabb_for_transformed_aabb(&m_BBox, &aabb_temp, m_transform);