]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/miscmodel.cpp
more eol-style
[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 {
33   refCount = 1;
34   m_entity = e;
35   m_model = NULL;
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);
42 }
43
44 CEntityMiscModel::~CEntityMiscModel ()
45 {
46   if(m_cachereq.GetBuffer()[0] != ':'
47     && m_version.c_str()[0] != '\0')
48     GetModelCache()->DeleteByID(m_cachereq.GetBuffer(), m_version.c_str());
49 }
50
51
52 // IRender
53
54 void CEntityMiscModel::Draw(int state, int rflags) const
55 {
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);
62
63   pivot_draw(m_pivot);
64
65   // draw children
66   if(m_model && m_model->pRender)  
67   {
68     m_model->pRender->Draw(state, rflags);
69   }
70  
71   g_QglTable.m_pfn_qglPopMatrix();
72 }
73
74 // ISelect
75
76 bool CEntityMiscModel::TestRay(const ray_t *ray, vec_t *dist) const
77 {
78   vec_t dist_start = *dist;
79   vec_t dist_local = *dist;
80         ray_t ray_local = *ray;
81
82   if (aabb_test_ray(&m_BBox, ray) == 0)
83     return false;
84
85   ray_transform(&ray_local, m_inverse_transform);
86
87   if(m_model && m_model->pSelect)
88   {
89     if(m_model->pSelect->TestRay(&ray_local, &dist_local))
90         *dist = dist_local;
91   }
92   else *dist = dist_local;
93
94   return *dist < dist_start;
95 }
96
97
98 //IEdit
99
100 void CEntityMiscModel::Translate(const vec3_t translation)
101 {
102   VectorIncrement(translation, m_translate);
103   UpdateCachedData();
104 }
105
106 void CEntityMiscModel::Rotate(const vec3_t pivot, const vec3_t rotation)
107 {
108   m4x4_t rotation_matrix;
109
110   m4x4_identity(rotation_matrix);
111   m4x4_pivoted_rotate_by_vec3(rotation_matrix, rotation, eXYZ, pivot);
112   m4x4_transform_point(rotation_matrix, m_translate);
113
114   VectorIncrement(rotation, m_euler);
115
116   UpdateCachedData();
117 }
118
119 void CEntityMiscModel::OnKeyValueChanged(entity_t *e, const char *key, const char* value)
120 {
121   if(strcmp(key, "model") == 0)
122     SetName(value);
123   else if(strcmp(key, "_frame") == 0)
124   {
125     SetName(ValueForKey(e, "model"));
126   }
127   else if(strcmp(key, "angle") == 0)
128   {
129     VectorSet(m_euler, 0.f, 0.f, 0.f);
130     m_euler[2] = atof(value);
131     UpdateCachedData();
132   }
133   else if(strcmp(key, "angles") == 0)
134   {
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]);
138     UpdateCachedData();
139   }
140   else if(strcmp(key, "modelscale") == 0 || strcmp(key,"modelscale_vec") == 0)
141   {
142     const char *s;
143     VectorSet(m_scale, 1.f, 1.f, 1.f);
144     s = ValueForKey(e,"modelscale");
145     if (s[0] != '\0')
146     {
147       float f = atof(s);
148       if( f != 0 )
149         VectorSet(m_scale, f, f, f);
150       else
151         Sys_FPrintf(SYS_WRN, "WARNING: ignoring 0 modelscale key\n");
152     }
153     s = ValueForKey(e,"modelscale_vec");
154     if (s[0] != '\0')
155     {
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)
158       {
159         VectorSet(m_scale, 1,1,1);
160         Sys_FPrintf(SYS_WRN, "WARNING: ignoring 0 0 0 modelscale_vec key\n");
161       }
162     }
163     UpdateCachedData();
164   }
165   else if(strcmp(key, "origin") == 0)
166   {
167     sscanf(value, "%f %f %f", &m_translate[0], &m_translate[1], &m_translate[2]); 
168     UpdateCachedData();
169   }
170   else if(strncmp(key,"_remap",6) == 0)
171   {
172     SetName(ValueForKey(e, "model"));
173   }
174 }
175
176 //
177 // CEntityMiscModel
178 //
179
180 // private:
181
182 void CEntityMiscModel::BuildCacheRequestString(const char *name)
183 {
184   bool hasRemaps = false;
185
186   m_cachereq.Format( "%s:%i", name, IntForKey(m_entity,"_frame") );
187
188   for (epair_t* ep = m_entity->epairs ; ep ; ep=ep->next)
189   {
190     if( strncmp(ep->key,"_remap",6) == 0 )
191     {
192       if( !hasRemaps )
193       {
194         hasRemaps = true;
195         m_cachereq += "?";
196       } else {
197         m_cachereq += "&";
198       }
199       m_cachereq += ep->value;
200     }
201   }
202 }
203
204 void CEntityMiscModel::SetName(const char *name)
205 {
206   Str m_oldcachereq = m_cachereq;
207
208   if( name[0] == '\0' ) {
209     return;
210   }
211
212   BuildCacheRequestString(name);
213
214   if(strcmp(m_oldcachereq, m_cachereq) == 0)
215     return;
216
217   if(m_cachereq.GetBuffer()[0] != ':'
218     && m_version.c_str()[0] != '\0')
219     GetModelCache()->DeleteByID(m_cachereq.GetBuffer(), m_version.c_str());
220
221   m_model = NULL;
222
223   if(name[0] != '\0')
224   {
225     const char* dot = strrchr(name, '.');
226     if(dot != NULL)
227     {
228       m_version = ++dot;
229       m_model = GetModelCache()->GetByID(m_cachereq.GetBuffer(), m_version.c_str());
230     }
231   }
232
233   UpdateCachedData();
234 }
235
236
237 void CEntityMiscModel::UpdateCachedData()
238 {
239   aabb_t aabb_temp;
240
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);
245
246   aabb_clear(&aabb_temp);
247
248   if(m_model && m_model->pRender)
249     aabb_extend_by_aabb(&aabb_temp, m_model->pRender->GetAABB());
250   else
251     VectorSet(aabb_temp.extents, 8, 8, 8);
252
253   aabb_for_transformed_aabb(&m_BBox, &aabb_temp, m_transform);
254 }