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 ){
35 VectorSet( m_translate, 0,0,0 );
36 VectorSet( m_euler, 0,0,0 );
37 VectorSet( m_scale, 1,1,1 );
38 VectorSet( m_pivot, 0,0,0 );
39 m4x4_identity( m_transform );
40 m4x4_identity( m_inverse_transform );
43 CEntityMiscModel::~CEntityMiscModel (){
44 if ( m_cachereq.GetBuffer()[0] != ':'
45 && m_version.c_str()[0] != '\0' ) {
46 GetModelCache()->DeleteByID( m_cachereq.GetBuffer(), m_version.c_str() );
53 void CEntityMiscModel::Draw( int state, int rflags ) const {
54 // push the current modelview matrix
55 // FIXME: put in a check for stack recursion depth..
56 // or avoid recursion of opengl matrix stack
57 g_QglTable.m_pfn_qglPushMatrix();
58 // apply the parent-to-local transform
59 g_QglTable.m_pfn_qglMultMatrixf( m_transform );
61 pivot_draw( m_pivot );
64 if ( m_model && m_model->pRender ) {
65 m_model->pRender->Draw( state, rflags );
68 g_QglTable.m_pfn_qglPopMatrix();
73 bool CEntityMiscModel::TestRay( const ray_t *ray, vec_t *dist ) const {
74 vec_t dist_start = *dist;
75 vec_t dist_local = *dist;
76 ray_t ray_local = *ray;
78 if ( aabb_test_ray( &m_BBox, ray ) == 0 ) {
82 ray_transform( &ray_local, m_inverse_transform );
84 if ( m_model && m_model->pSelect ) {
85 if ( m_model->pSelect->TestRay( &ray_local, &dist_local ) ) {
89 else{ *dist = dist_local; }
91 return *dist < dist_start;
97 void CEntityMiscModel::Translate( const vec3_t translation ){
98 VectorIncrement( translation, m_translate );
102 void CEntityMiscModel::Rotate( const vec3_t pivot, const vec3_t rotation ){
103 m4x4_t rotation_matrix;
105 m4x4_identity( rotation_matrix );
106 m4x4_pivoted_rotate_by_vec3( rotation_matrix, rotation, eXYZ, pivot );
107 m4x4_transform_point( rotation_matrix, m_translate );
109 VectorIncrement( rotation, m_euler );
114 void CEntityMiscModel::OnKeyValueChanged( entity_t *e, const char *key, const char* value ){
115 if ( strcmp( key, "model" ) == 0 ) {
118 else if ( strcmp( key, "_frame" ) == 0 ) {
119 SetName( ValueForKey( e, "model" ) );
121 else if ( strcmp( key, "angle" ) == 0 ) {
122 VectorSet( m_euler, 0.f, 0.f, 0.f );
123 m_euler[2] = atof( value );
126 else if ( strcmp( key, "angles" ) == 0 ) {
127 VectorSet( m_euler, 0.f, 0.f, 0.f );
128 if ( value[0] != '\0' ) {
129 sscanf( value, "%f %f %f", &m_euler[1], &m_euler[2], &m_euler[0] );
133 else if ( strcmp( key, "modelscale" ) == 0 || strcmp( key,"modelscale_vec" ) == 0 ) {
135 VectorSet( m_scale, 1.f, 1.f, 1.f );
136 s = ValueForKey( e,"modelscale" );
137 if ( s[0] != '\0' ) {
140 VectorSet( m_scale, f, f, f );
143 Sys_FPrintf( SYS_WRN, "WARNING: ignoring 0 modelscale key\n" );
146 s = ValueForKey( e,"modelscale_vec" );
147 if ( s[0] != '\0' ) {
148 sscanf( s, "%f %f %f", &m_scale[0], &m_scale[1], &m_scale[2] );
149 if ( m_scale[0] == 0.0 && m_scale[1] == 0.0 && m_scale[2] == 0.0 ) {
150 VectorSet( m_scale, 1,1,1 );
151 Sys_FPrintf( SYS_WRN, "WARNING: ignoring 0 0 0 modelscale_vec key\n" );
156 else if ( strcmp( key, "origin" ) == 0 ) {
157 sscanf( value, "%f %f %f", &m_translate[0], &m_translate[1], &m_translate[2] );
160 else if ( strncmp( key,"_remap",6 ) == 0 ) {
161 SetName( ValueForKey( e, "model" ) );
171 void CEntityMiscModel::BuildCacheRequestString( const char *name ){
172 bool hasRemaps = false;
174 m_cachereq.Format( "%s:%i", name, IntForKey( m_entity,"_frame" ) );
176 for ( epair_t* ep = m_entity->epairs ; ep ; ep = ep->next )
178 if ( strncmp( ep->key,"_remap",6 ) == 0 ) {
186 m_cachereq += ep->value;
191 void CEntityMiscModel::SetName( const char *name ){
192 Str m_oldcachereq = m_cachereq;
194 if ( name[0] == '\0' ) {
198 BuildCacheRequestString( name );
200 if ( strcmp( m_oldcachereq, m_cachereq ) == 0 ) {
204 if ( m_cachereq.GetBuffer()[0] != ':'
205 && m_version.c_str()[0] != '\0' ) {
206 GetModelCache()->DeleteByID( m_cachereq.GetBuffer(), m_version.c_str() );
211 if ( name[0] != '\0' ) {
212 const char* dot = strrchr( name, '.' );
215 m_model = GetModelCache()->GetByID( m_cachereq.GetBuffer(), m_version.c_str() );
223 void CEntityMiscModel::UpdateCachedData(){
226 m4x4_identity( m_transform );
227 m4x4_pivoted_transform_by_vec3( m_transform, m_translate, m_euler, eXYZ, m_scale, m_pivot );
228 memcpy( m_inverse_transform, m_transform, sizeof( m4x4_t ) );
229 m4x4_invert( m_inverse_transform );
231 aabb_clear( &aabb_temp );
233 if ( m_model && m_model->pRender ) {
234 aabb_extend_by_aabb( &aabb_temp, m_model->pRender->GetAABB() );
237 VectorSet( aabb_temp.extents, 8, 8, 8 );
240 aabb_for_transformed_aabb( &m_BBox, &aabb_temp, m_transform );