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
22 #include "cpicomodel.h"
23 #include "cpicosurface.h"
25 CPicoModel::CPicoModel( const PicoModelKey& key )
27 load( key.first.GetBuffer(), key.second );
30 CPicoModel::CPicoModel( const Str& name )
32 load( name.GetBuffer(), 0 );
35 CPicoModel::CPicoModel( const Str& name, const int frame )
37 load( name.GetBuffer(), frame );
40 CPicoModel::CPicoModel( const char *name, const int frame )
45 void CPicoModel::load( const char *name, const int frame ){
47 picoSurface_t *pSurface;
50 m_name = new char[strlen( name ) + 1];
51 strcpy( m_name,name );
55 if ( !( m_pModel = PicoLoadModel( m_name, frame ) ) ) {
56 int len = strlen( m_name );
58 // Try loading an mdc if md3 fails and vice-versa (fixme: only do this for games with mdc support)
59 if ( !strcmp( m_name + len - 4, ".md3" ) ) {
60 m_name[len - 1] = 'c';
61 m_pModel = PicoLoadModel( m_name, frame );
63 else if ( !strcmp( m_name + len - 4, ".mdc" ) ) {
64 m_name[len - 1] = '3';
65 m_pModel = PicoLoadModel( m_name, frame );
70 m_children = g_ptr_array_new();
71 aabb_clear( &m_BBox );
72 for ( i = 0; i < PicoGetModelNumSurfaces( m_pModel ); i++ )
74 pSurface = PicoGetModelSurface( m_pModel,i );
75 surf = new CPicoSurface( pSurface );
76 g_ptr_array_add( m_children, surf );
77 aabb_extend_by_aabb( &m_BBox, surf->GetAABB() );
82 m_BBox.origin[0] = m_BBox.origin[1] = m_BBox.origin[2] = 0;
83 m_BBox.extents[0] = m_BBox.extents[1] = m_BBox.extents[2] = 0;
86 m_parents = g_ptr_array_new();
89 CPicoModel::~CPicoModel(){
91 for ( unsigned int i = 0; i < m_children->len; i++ )
92 ( (CPicoSurface*)m_children->pdata[i] )->DecRef();
93 g_ptr_array_free( m_children, FALSE );
95 g_ptr_array_free( m_parents, FALSE );
99 void CPicoModel::AddParent( CPicoParent *parent ){
100 g_ptr_array_add( m_parents, parent );
103 void CPicoModel::RemoveParent( CPicoParent *parent ){
105 for ( i = 0; i < m_parents->len; i++ ) {
106 if ( parent == (CPicoParent*)m_parents->pdata[i] ) {
107 g_ptr_array_remove_index_fast( m_parents, i );
112 void CPicoModel::Reload( void ){
114 picoSurface_t *pSurface;
118 // Get rid of the old model
120 for ( j = 0; j < m_children->len; j++ ) {
121 ( (CPicoSurface*)m_children->pdata[j] )->DecRef();
122 g_ptr_array_remove_index_fast( m_children, j );
127 m_pModel = PicoLoadModel( m_name, m_frame );
130 m_children = g_ptr_array_new();
131 aabb_clear( &m_BBox );
132 for ( i = 0; i < PicoGetModelNumSurfaces( m_pModel ); i++ )
134 pSurface = PicoGetModelSurface( m_pModel,i );
135 surf = new CPicoSurface( pSurface );
136 g_ptr_array_add( m_children, surf );
137 aabb_extend_by_aabb( &m_BBox, surf->GetAABB() );
142 m_BBox.origin[0] = m_BBox.origin[1] = m_BBox.origin[2] = 0;
143 m_BBox.extents[0] = m_BBox.extents[1] = m_BBox.extents[2] = 0;
146 for ( j = 0; j < m_parents->len; j++ ) {
147 ( (CPicoParent*)m_parents->pdata[j] )->UpdateShaders();
151 void CPicoModel::Draw( int state, vector<IShader*> shaders, int rflags ) const {
153 for ( unsigned int i = 0; i < m_children->len; i++ )
154 ( (CPicoSurface*)m_children->pdata[i] )->Draw( state, shaders[i], rflags );
158 void CPicoModel::Draw( int state, int rflags ) const {
160 for ( unsigned int i = 0; i < m_children->len; i++ )
161 ( (CPicoSurface*)m_children->pdata[i] )->Draw( state, rflags );
165 bool CPicoModel::TestRay( const ray_t *ray, vec_t *dist ) const {
166 vec_t dist_start = *dist;
167 vec_t dist_local = *dist;
168 ray_t ray_local = *ray;
174 if ( !aabb_intersect_ray( &m_BBox, &ray_local, &dist_local ) ) {
177 dist_local = dist_start;
179 for ( unsigned int i = 0; i < m_children->len; i++ )
181 if ( ( (CPicoSurface*)m_children->pdata[i] )->TestRay( &ray_local, &dist_local ) ) {
186 return *dist < dist_start;
189 int CPicoModel::GetNumSurfaces( void ){
194 return m_children->len;
197 char *CPicoModel::GetShaderNameForSurface( const unsigned int surf ){
198 if ( !m_pModel || surf < 0 || surf >= m_children->len ) {
202 return ( (CPicoSurface*)m_children->pdata[surf] )->GetShaderName();