2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
22 #include "cpicomodel.h"
\r
23 #include "cpicosurface.h"
\r
25 CPicoModel::CPicoModel(const PicoModelKey& key)
\r
28 load(key.first.GetBuffer(), key.second);
\r
31 CPicoModel::CPicoModel(const Str& name)
\r
34 load(name.GetBuffer(), 0);
\r
37 CPicoModel::CPicoModel(const Str& name, const int frame)
\r
40 load(name.GetBuffer(), frame);
\r
43 CPicoModel::CPicoModel(const char *name, const int frame)
\r
49 void CPicoModel::load(const char *name, const int frame)
\r
52 picoSurface_t *pSurface;
\r
55 m_name= new char[strlen(name)+1];
\r
56 strcpy(m_name,name);
\r
60 if( !(m_pModel = PicoLoadModel(m_name, frame)) )
\r
62 int len = strlen(m_name);
\r
64 // Try loading an mdc if md3 fails and vice-versa (fixme: only do this for games with mdc support)
\r
65 if( !strcmp( m_name + len - 4, ".md3" ) )
\r
67 m_name[len - 1] = 'c';
\r
68 m_pModel = PicoLoadModel(m_name, frame);
\r
69 } else if( !strcmp( m_name + len - 4, ".mdc" ) )
\r
71 m_name[len - 1] = '3';
\r
72 m_pModel = PicoLoadModel(m_name, frame);
\r
78 m_children = g_ptr_array_new();
\r
79 aabb_clear(&m_BBox);
\r
80 for (i = 0; i < PicoGetModelNumSurfaces(m_pModel); i++ )
\r
82 pSurface = PicoGetModelSurface(m_pModel,i);
\r
83 surf = new CPicoSurface(pSurface);
\r
84 g_ptr_array_add(m_children, surf);
\r
85 aabb_extend_by_aabb(&m_BBox, surf->GetAABB());
\r
90 m_BBox.origin[0] = m_BBox.origin[1] = m_BBox.origin[2] = 0;
\r
91 m_BBox.extents[0] = m_BBox.extents[1] = m_BBox.extents[2] = 0;
\r
94 m_parents = g_ptr_array_new();
\r
97 CPicoModel::~CPicoModel()
\r
100 for(unsigned int i=0; i<m_children->len; i++)
\r
101 ((CPicoSurface*)m_children->pdata[i])->DecRef();
\r
102 g_ptr_array_free(m_children, FALSE);
\r
104 g_ptr_array_free(m_parents, FALSE);
\r
108 void CPicoModel::AddParent( CPicoParent *parent )
\r
110 g_ptr_array_add(m_parents, parent);
\r
113 void CPicoModel::RemoveParent( CPicoParent *parent )
\r
116 for(i=0; i<m_parents->len; i++) {
\r
117 if( parent == (CPicoParent*)m_parents->pdata[i] )
\r
118 g_ptr_array_remove_index_fast(m_parents, i);
\r
122 void CPicoModel::Reload( void )
\r
124 CPicoSurface *surf;
\r
125 picoSurface_t *pSurface;
\r
129 // Get rid of the old model
\r
131 for(j=0; j<m_children->len; j++) {
\r
132 ((CPicoSurface*)m_children->pdata[j])->DecRef();
\r
133 g_ptr_array_remove_index_fast(m_children, j);
\r
138 m_pModel = PicoLoadModel(m_name, m_frame);
\r
142 m_children = g_ptr_array_new();
\r
143 aabb_clear(&m_BBox);
\r
144 for (i = 0; i < PicoGetModelNumSurfaces(m_pModel); i++ )
\r
146 pSurface = PicoGetModelSurface(m_pModel,i);
\r
147 surf = new CPicoSurface(pSurface);
\r
148 g_ptr_array_add(m_children, surf);
\r
149 aabb_extend_by_aabb(&m_BBox, surf->GetAABB());
\r
154 m_BBox.origin[0] = m_BBox.origin[1] = m_BBox.origin[2] = 0;
\r
155 m_BBox.extents[0] = m_BBox.extents[1] = m_BBox.extents[2] = 0;
\r
158 for(j=0; j<m_parents->len; j++) {
\r
159 ((CPicoParent*)m_parents->pdata[j])->UpdateShaders();
\r
163 void CPicoModel::Draw(int state, vector<IShader*> shaders, int rflags) const
\r
166 for(unsigned int i=0; i<m_children->len; i++)
\r
167 ((CPicoSurface*)m_children->pdata[i])->Draw(state, shaders[i], rflags);
\r
171 void CPicoModel::Draw(int state, int rflags) const
\r
174 for(unsigned int i=0; i<m_children->len; i++)
\r
175 ((CPicoSurface*)m_children->pdata[i])->Draw(state, rflags);
\r
179 bool CPicoModel::TestRay(const ray_t *ray, vec_t *dist) const
\r
181 vec_t dist_start = *dist;
\r
182 vec_t dist_local = *dist;
\r
183 ray_t ray_local = *ray;
\r
189 if (!aabb_intersect_ray(&m_BBox, &ray_local, &dist_local))
\r
191 dist_local = dist_start;
\r
193 for(unsigned int i=0; i<m_children->len; i++)
\r
195 if(((CPicoSurface*)m_children->pdata[i])->TestRay(&ray_local, &dist_local))
\r
197 *dist = dist_local;
\r
201 return *dist < dist_start;
\r
204 int CPicoModel::GetNumSurfaces( void )
\r
210 return m_children->len;
\r
213 char *CPicoModel::GetShaderNameForSurface( const unsigned int surf )
\r
215 if( !m_pModel || surf < 0 || surf >= m_children->len ) {
\r
219 return ((CPicoSurface*)m_children->pdata[surf])->GetShaderName();
\r