]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/miscmodel.cpp
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / plugins / entity / miscmodel.cpp
1 /*
2    Copyright (C) 1999-2007 id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include <stdlib.h>
23
24 #include "entity_entitymodel.h"
25 #include "entity.h"
26
27 //
28 // CEntityMiscModel implementation
29 //
30
31 CEntityMiscModel::CEntityMiscModel ( entity_t *e ){
32         refCount = 1;
33         m_entity = e;
34         m_model = NULL;
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 );
41 }
42
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() );
47         }
48 }
49
50
51 // IRender
52
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 );
60
61         pivot_draw( m_pivot );
62
63         // draw children
64         if ( m_model && m_model->pRender ) {
65                 m_model->pRender->Draw( state, rflags );
66         }
67
68         g_QglTable.m_pfn_qglPopMatrix();
69 }
70
71 // ISelect
72
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;
77
78         if ( aabb_test_ray( &m_BBox, ray ) == 0 ) {
79                 return false;
80         }
81
82         ray_transform( &ray_local, m_inverse_transform );
83
84         if ( m_model && m_model->pSelect ) {
85                 if ( m_model->pSelect->TestRay( &ray_local, &dist_local ) ) {
86                         *dist = dist_local;
87                 }
88         }
89         else{ *dist = dist_local; }
90
91         return *dist < dist_start;
92 }
93
94
95 //IEdit
96
97 void CEntityMiscModel::Translate( const vec3_t translation ){
98         VectorIncrement( translation, m_translate );
99         UpdateCachedData();
100 }
101
102 void CEntityMiscModel::Rotate( const vec3_t pivot, const vec3_t rotation ){
103         m4x4_t rotation_matrix;
104
105         m4x4_identity( rotation_matrix );
106         m4x4_pivoted_rotate_by_vec3( rotation_matrix, rotation, eXYZ, pivot );
107         m4x4_transform_point( rotation_matrix, m_translate );
108
109         VectorIncrement( rotation, m_euler );
110
111         UpdateCachedData();
112 }
113
114 void CEntityMiscModel::OnKeyValueChanged( entity_t *e, const char *key, const char* value ){
115         if ( strcmp( key, "model" ) == 0 ) {
116                 SetName( value );
117         }
118         else if ( strcmp( key, "_frame" ) == 0 ) {
119                 SetName( ValueForKey( e, "model" ) );
120         }
121         else if ( strcmp( key, "angle" ) == 0 ) {
122                 VectorSet( m_euler, 0.f, 0.f, 0.f );
123                 m_euler[2] = atof( value );
124                 UpdateCachedData();
125         }
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] );
130                 }
131                 UpdateCachedData();
132         }
133         else if ( strcmp( key, "modelscale" ) == 0 || strcmp( key,"modelscale_vec" ) == 0 ) {
134                 const char *s;
135                 VectorSet( m_scale, 1.f, 1.f, 1.f );
136                 s = ValueForKey( e,"modelscale" );
137                 if ( s[0] != '\0' ) {
138                         float f = atof( s );
139                         if ( f != 0 ) {
140                                 VectorSet( m_scale, f, f, f );
141                         }
142                         else{
143                                 Sys_FPrintf( SYS_WRN, "WARNING: ignoring 0 modelscale key\n" );
144                         }
145                 }
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" );
152                         }
153                 }
154                 UpdateCachedData();
155         }
156         else if ( strcmp( key, "origin" ) == 0 ) {
157                 sscanf( value, "%f %f %f", &m_translate[0], &m_translate[1], &m_translate[2] );
158                 UpdateCachedData();
159         }
160         else if ( strncmp( key,"_remap",6 ) == 0 ) {
161                 SetName( ValueForKey( e, "model" ) );
162         }
163 }
164
165 //
166 // CEntityMiscModel
167 //
168
169 // private:
170
171 void CEntityMiscModel::BuildCacheRequestString( const char *name ){
172         bool hasRemaps = false;
173
174         m_cachereq.Format( "%s:%i", name, IntForKey( m_entity,"_frame" ) );
175
176         for ( epair_t* ep = m_entity->epairs ; ep ; ep = ep->next )
177         {
178                 if ( strncmp( ep->key,"_remap",6 ) == 0 ) {
179                         if ( !hasRemaps ) {
180                                 hasRemaps = true;
181                                 m_cachereq += "?";
182                         }
183                         else {
184                                 m_cachereq += "&";
185                         }
186                         m_cachereq += ep->value;
187                 }
188         }
189 }
190
191 void CEntityMiscModel::SetName( const char *name ){
192         Str m_oldcachereq = m_cachereq;
193
194         if ( name[0] == '\0' ) {
195                 return;
196         }
197
198         BuildCacheRequestString( name );
199
200         if ( strcmp( m_oldcachereq, m_cachereq ) == 0 ) {
201                 return;
202         }
203
204         if ( m_cachereq.GetBuffer()[0] != ':'
205                  && m_version.c_str()[0] != '\0' ) {
206                 GetModelCache()->DeleteByID( m_cachereq.GetBuffer(), m_version.c_str() );
207         }
208
209         m_model = NULL;
210
211         if ( name[0] != '\0' ) {
212                 const char* dot = strrchr( name, '.' );
213                 if ( dot != NULL ) {
214                         m_version = ++dot;
215                         m_model = GetModelCache()->GetByID( m_cachereq.GetBuffer(), m_version.c_str() );
216                 }
217         }
218
219         UpdateCachedData();
220 }
221
222
223 void CEntityMiscModel::UpdateCachedData(){
224         aabb_t aabb_temp;
225
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 );
230
231         aabb_clear( &aabb_temp );
232
233         if ( m_model && m_model->pRender ) {
234                 aabb_extend_by_aabb( &aabb_temp, m_model->pRender->GetAABB() );
235         }
236         else{
237                 VectorSet( aabb_temp.extents, 8, 8, 8 );
238         }
239
240         aabb_for_transformed_aabb( &m_BBox, &aabb_temp, m_transform );
241 }