]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/model/miscmodel.cpp
* readded copyright notice to picomodel plugin
[xonotic/netradiant.git] / plugins / model / 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 "entitymodel.h"
25
26 extern CModelManager g_model_cache;
27
28 //
29 // CEntityMiscModel implementation
30 //
31
32 CEntityMiscModel::CEntityMiscModel ()
33 {
34   refCount = 1;
35   m_name = NULL;
36   m_model = NULL;
37         m_entity = NULL;
38   m_frame = 0;
39   m_remaps = g_ptr_array_new ();
40   m_shaders = g_ptr_array_new ();
41   VectorSet(m_translate, 0,0,0);
42   VectorSet(m_euler, 0,0,0);
43   VectorSet(m_scale, 1,1,1);
44   VectorSet(m_pivot, 0,0,0);
45   m4x4_identity(m_transform);
46   m4x4_identity(m_inverse_transform);
47 }
48
49 typedef struct remap_s {
50   char m_key[64];
51   char m_remapbuff[64+1024];
52   char *m_remap[2];
53 } remap_t;
54
55 CEntityMiscModel::~CEntityMiscModel ()
56 {
57   unsigned int i;
58
59   if(m_name && *m_name != '\0') {
60     if( !g_model_cache.DeleteByNameAndFrame(m_name,m_frame) && m_model )
61       m_model->RemoveParent( this );
62     m_model = NULL;
63     delete [] m_name;
64   }
65
66   for( i = 0; i < m_remaps->len; i++ )
67     delete (remap_t*)m_remaps->pdata[i];
68   g_ptr_array_free(m_remaps, FALSE);
69
70   for( i = 0; i < m_shaders->len; i++ )
71   {
72     (*(IShader**)m_shaders->pdata[i])->DecRef();
73     delete (IShader**)m_shaders->pdata[i];
74   }
75   g_ptr_array_free(m_shaders, FALSE);
76
77   if(m_entity) {
78     // This might be just an evasion of the actual problem
79     m_entity->model.pRender = NULL;
80     m_entity->model.pSelect = NULL;
81     m_entity->model.pEdit = NULL;
82   }
83 }
84
85 // IRender
86
87 void CEntityMiscModel::Draw(int state, int rflags) const
88 {
89   m4x4_t matrix;
90   vec3_t pivot;
91  
92   memcpy(matrix, m_transform, sizeof(m4x4_t));
93   m4x4_transpose(matrix);
94
95   VectorAdd(m_pivot, m_translate, pivot);
96   pivot_draw(pivot);
97   
98   // push the current modelview matrix
99   // FIXME: put in a check for stack recursion depth..
100   // or avoid recursion of opengl matrix stack
101   g_QglTable.m_pfn_qglPushMatrix();
102   // apply the parent-to-local transform
103   g_QglTable.m_pfn_qglMultMatrixf(matrix);
104
105   // draw children
106   if(m_model)
107     m_model->Draw(state, m_shaders, rflags);
108  
109   g_QglTable.m_pfn_qglPopMatrix();
110 }
111
112 // ISelect
113
114 bool CEntityMiscModel::TestRay(const ray_t *ray, vec_t *dist) const
115 {
116   vec_t dist_start = *dist;
117   vec_t dist_local = *dist;
118         ray_t ray_local = *ray;
119
120   if (!aabb_intersect_ray(&m_BBox, &ray_local, &dist_local))
121     return false;
122
123   if(m_model){
124     ray_transform(&ray_local, m_inverse_transform);
125     dist_local = dist_start;
126     if(m_model->TestRay(&ray_local, &dist_local))
127         *dist = dist_local;
128   } else *dist = dist_local;
129
130   return *dist < dist_start;
131 }
132
133
134 //IEdit
135
136 void CEntityMiscModel::Translate(const vec3_t translation)
137 {
138   VectorIncrement(translation, m_translate);
139   UpdateCachedData();
140 }
141
142 void CEntityMiscModel::Rotate(const vec3_t pivot, const vec3_t rotation)
143 {
144   m4x4_t rotation_matrix;
145
146   m4x4_identity(rotation_matrix);
147   m4x4_pivoted_rotate_by_vec3(rotation_matrix, rotation, pivot);
148   m4x4_transform_point(rotation_matrix, m_translate);
149
150   VectorIncrement(rotation, m_euler);
151
152   UpdateCachedData();
153 }
154
155 void CEntityMiscModel::OnKeyChanged(entity_t *e, const char *key)
156 {
157   const char *value;
158
159   // FIXME: keys are case-sensitive?
160         
161         m_entity = e;
162
163   if(strcmp(key,"model") == 0)
164     SetName(ValueForKey(e,"model"));
165   else if(strcmp(key,"_frame") == 0)
166     SetFrame(IntForKey(e,"_frame"));
167   else if(strcmp(key,"angle") == 0 || strcmp(key,"angles") == 0)
168   {
169     VectorSet(m_euler, 0.f, 0.f, 0.f);
170     m_euler[2] = FloatForKey(e,"angle");
171     value = ValueForKey(e,"angles");
172     if (value[0] != '\0')
173       sscanf (value, "%f %f %f", &m_euler[0], &m_euler[2], &m_euler[1]);
174     UpdateCachedData();
175   }
176   else if(strcmp(key,"modelscale") == 0 || strcmp(key,"modelscale_vec") == 0)
177   {
178     VectorSet(m_scale, 1.f, 1.f, 1.f);
179     value = ValueForKey(e,"modelscale");
180     if (value[0] != '\0')
181     {
182       float f = atof(value);
183       if( f != 0 )
184         VectorSet(m_scale, f, f, f);
185       else
186         Sys_FPrintf(SYS_WRN, "WARNING: ignoring 0 modelscale key\n");
187     }
188     value = ValueForKey(e,"modelscale_vec");
189     if (value[0] != '\0')
190     {
191       sscanf (value, "%f %f %f", &m_scale[0], &m_scale[1], &m_scale[2]);
192       if (m_scale[0] == 0.0 && m_scale[1] == 0.0 && m_scale[2] == 0.0)
193       {
194         VectorSet(m_scale, 1,1,1);
195         Sys_FPrintf(SYS_WRN, "WARNING: ignoring 0 0 0 modelscale_vec key\n");
196       }
197     }
198     UpdateCachedData();
199   }
200   else if(strcmp(key,"origin") == 0)
201   {
202     value = ValueForKey(e,"origin");
203     sscanf(value, "%f %f %f", &m_translate[0], &m_translate[1], &m_translate[2]); 
204     UpdateCachedData();
205   }
206   else if(strncmp(key,"_remap",6) == 0)
207   {
208     unsigned int i;
209     remap_t *pRemap;
210     char *ch;
211
212     value = ValueForKey(e,key);
213
214     for(i=0; i<m_remaps->len; i++)
215     {
216       pRemap = (remap_t*)m_remaps->pdata[i];
217       if(strcmp(key,pRemap->m_key) == 0)
218         break;
219     }
220
221     if( i == m_remaps->len )
222     {
223       if( value[0] == '\0' )
224         return;
225
226       pRemap = new remap_t;
227       g_ptr_array_add(m_remaps, pRemap);
228     }
229     else if( value[0] == '\0' )
230     {
231       g_ptr_array_remove_index_fast(m_remaps, i);
232       delete pRemap;
233
234       UpdateShaders();
235       return;
236     }
237
238     strncpy(pRemap->m_remapbuff,value,sizeof(pRemap->m_remapbuff));
239     strncpy(pRemap->m_key,key,sizeof(pRemap->m_key));
240
241     pRemap->m_remap[0] = ch = pRemap->m_remapbuff;
242
243     while( *ch && *ch != ';' )
244       ch++;
245
246     if( *ch == '\0' )
247     {    
248       // bad remap
249       Sys_FPrintf(SYS_WRN, "WARNING: Shader _remap key found in misc_model without a ; character\n" );
250       g_ptr_array_remove_index_fast(m_remaps, i);
251       delete pRemap;
252       return;
253     }
254     else
255     {
256       *ch = '\0';
257       pRemap->m_remap[1] = ch + 1;
258     }
259
260     UpdateShaders();
261   }
262 }
263
264 //
265 // CEntityMiscModel
266 //
267
268 // private:
269
270 void CEntityMiscModel::SetName(const char *name)
271 {
272   if(m_name && *m_name != '\0') {
273     if(strcmp(m_name, name) == 0)
274       return;
275     if( !g_model_cache.DeleteByNameAndFrame(m_name,m_frame) && m_model )
276       m_model->RemoveParent( this );
277     delete [] m_name;
278   }
279
280   m_model = NULL;
281   m_name = new char[strlen(name)+1];
282   strcpy(m_name,name);
283
284   if(*m_name != '\0') {
285     m_model = g_model_cache.GetByNameAndFrame(m_name, m_frame);
286     m_model->AddParent( this );
287   }
288
289   UpdateCachedData();
290   UpdateShaders();
291 }
292
293 void CEntityMiscModel::SetFrame(const int frame)
294 {
295   if( m_frame == frame )
296     return;
297
298   if(m_name && *m_name != '\0') {
299     if( !g_model_cache.DeleteByNameAndFrame(m_name,m_frame) && m_model )
300       m_model->RemoveParent( this );
301   }
302
303   m_model = NULL;
304
305   m_frame = frame;
306
307   if(*m_name != '\0') {
308     m_model = g_model_cache.GetByNameAndFrame(m_name, m_frame);
309     m_model->AddParent( this );
310   }
311
312   UpdateCachedData();
313 }
314
315 void CEntityMiscModel::UpdateCachedData()
316 {
317   aabb_t aabb_temp;
318   bbox_t bbox_temp;
319
320   m4x4_identity(m_transform);
321   m4x4_pivoted_transform_by_vec3(m_transform, m_translate, m_euler, m_scale, m_pivot);
322   memcpy(m_inverse_transform, m_transform, sizeof(m4x4_t));
323   if(m4x4_invert(m_inverse_transform) == 1) {
324     Sys_Printf("ERROR: Singular Matrix, cannot invert");
325   }
326
327   aabb_clear(&aabb_temp);
328
329   if(m_model)
330     aabb_extend_by_aabb(&aabb_temp, m_model->GetAABB());
331   else
332         {
333                 if (m_entity->eclass)
334                         VectorSet(aabb_temp.extents, m_entity->eclass->maxs[0], m_entity->eclass->maxs[1], m_entity->eclass->maxs[2]);
335                 else
336                         VectorSet(aabb_temp.extents, 8, 8, 8);
337         }
338
339   // create an oriented BBox in world-space
340   bbox_for_oriented_aabb(&bbox_temp, &aabb_temp, m_transform, m_euler, m_scale);
341   // create an axis aligned bbox in world-space
342   aabb_for_bbox(&m_BBox, &bbox_temp);
343
344   aabb_update_radius(&m_BBox);
345 }
346
347 void CEntityMiscModel::UpdateShaders()
348 {
349   unsigned int i, j, numSurfaces;
350   remap_t *pRemap, *pGlobRemap = NULL;
351   char *surfShaderName;
352   IShader **pShader;
353
354   if( !m_model )
355   {
356     if( m_shaders->len )
357     {
358       // free our shaders
359       for( i = 0; i < m_shaders->len; i++ )
360       {
361         g_ptr_array_remove_index_fast(m_shaders, i);
362         (*(IShader**)m_shaders->pdata[i])->DecRef();
363         delete (IShader**)m_shaders->pdata[i];
364       }
365     }
366     return;
367   }
368   
369   numSurfaces = m_model->GetNumSurfaces();
370
371   if( numSurfaces < m_shaders->len )
372   {
373     // free unneeded shader pointers
374     for( i = m_shaders->len - 1; i >= numSurfaces; i-- )
375     {
376       g_ptr_array_remove_index_fast(m_shaders, i);
377       (*(IShader**)m_shaders->pdata[i])->DecRef();
378       delete (IShader**)m_shaders->pdata[i];
379     }
380   }
381
382   // now go through our surface and find our shaders, remap if needed
383   for( j = 0; j < numSurfaces; j++ )
384   {
385     surfShaderName = m_model->GetShaderNameForSurface(j);
386
387     if( j < m_shaders->len )
388     {
389       pShader = (IShader **)m_shaders->pdata[j];
390     }
391     else
392     {
393       pShader = new (IShader *);
394       *pShader = NULL;
395       g_ptr_array_add(m_shaders, pShader);
396     }
397
398     if( m_remaps->len )
399     {
400       for( i = 0; i < m_remaps->len; i++ )
401       {
402         pRemap = (remap_t*)m_remaps->pdata[i];
403         if( stricmp(pRemap->m_remap[0],surfShaderName) == 0 )
404         {
405           // only do the shader lookups if really needed
406           if( !(*pShader) || stricmp(pRemap->m_remap[1],(*pShader)->getName()) )
407           {
408             if( *pShader )
409               (*pShader)->DecRef();
410             *pShader = QERApp_Shader_ForName(pRemap->m_remap[1]);
411           }
412
413           pGlobRemap = NULL;
414           break;
415         }
416         else if( pRemap->m_remap[0][0] == '*' && pRemap->m_remap[0][1] == '\0' )
417           pGlobRemap = pRemap;
418       }
419
420       if( pGlobRemap )
421       {
422         if( !(*pShader) || stricmp(pGlobRemap->m_remap[1],(*pShader)->getName()) )
423         {
424           if( *pShader )
425             (*pShader)->DecRef();
426           *pShader = QERApp_Shader_ForName(pGlobRemap->m_remap[1]);
427         }
428       }
429       else if( i == m_remaps->len )
430       {
431         // Back to the default one, if needed
432         if( !(*pShader) || (stricmp(surfShaderName,(*pShader)->getName()) && !(surfShaderName[0] == '\0')) )
433         {
434           if( *pShader )
435             (*pShader)->DecRef();
436           *pShader = QERApp_Shader_ForName(surfShaderName);
437         }
438       }
439     }
440     else
441     {
442       // Model specified shader, if needed
443       if( !(*pShader) || (stricmp(surfShaderName,(*pShader)->getName()) && !(surfShaderName[0] == '\0')) )
444       {
445         if( *pShader )
446           (*pShader)->DecRef();
447         *pShader = QERApp_Shader_ForName(surfShaderName);
448       }
449     }
450   }
451 }