]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/model/cpicomodel.cpp
Merge pull request #47 from mrwonko/MapLoading
[xonotic/netradiant.git] / plugins / model / cpicomodel.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 "cpicomodel.h"
23 #include "cpicosurface.h"
24
25 CPicoModel::CPicoModel( const PicoModelKey& key )
26         : m_refcount( 1 ){
27         load( key.first.GetBuffer(), key.second );
28 }
29
30 CPicoModel::CPicoModel( const Str& name )
31         : m_refcount( 1 ){
32         load( name.GetBuffer(), 0 );
33 }
34
35 CPicoModel::CPicoModel( const Str& name, const int frame )
36         : m_refcount( 1 ){
37         load( name.GetBuffer(), frame );
38 }
39
40 CPicoModel::CPicoModel( const char *name, const int frame )
41         : m_refcount( 1 ){
42         load( name, frame );
43 }
44
45 void CPicoModel::load( const char *name, const int frame ){
46         CPicoSurface *surf;
47         picoSurface_t *pSurface;
48         int i;
49
50         m_name = new char[strlen( name ) + 1];
51         strcpy( m_name,name );
52
53         m_frame = frame;
54
55         if ( !( m_pModel = PicoLoadModel( m_name, frame ) ) ) {
56                 int len = strlen( m_name );
57
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 );
62                 }
63                 else if ( !strcmp( m_name + len - 4, ".mdc" ) ) {
64                         m_name[len - 1] = '3';
65                         m_pModel = PicoLoadModel( m_name, frame );
66                 }
67         }
68
69         if ( m_pModel ) {
70                 m_children = g_ptr_array_new();
71                 aabb_clear( &m_BBox );
72                 for ( i = 0; i < PicoGetModelNumSurfaces( m_pModel ); i++ )
73                 {
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() );
78                 }
79         }
80         else
81         {
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;
84         }
85
86         m_parents = g_ptr_array_new();
87 }
88
89 CPicoModel::~CPicoModel(){
90         if ( m_pModel ) {
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 );
94         }
95         g_ptr_array_free( m_parents, FALSE );
96         delete [] m_name;
97 }
98
99 void CPicoModel::AddParent( CPicoParent *parent ){
100         g_ptr_array_add( m_parents, parent );
101 }
102
103 void CPicoModel::RemoveParent( CPicoParent *parent ){
104         unsigned int i;
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 );
108                 }
109         }
110 }
111
112 void CPicoModel::Reload( void ){
113         CPicoSurface *surf;
114         picoSurface_t *pSurface;
115         int i;
116         unsigned int j;
117
118         // Get rid of the old model
119         if ( m_pModel ) {
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 );
123                 }
124         }
125
126         // And reload it
127         m_pModel = PicoLoadModel( m_name, m_frame );
128
129         if ( m_pModel ) {
130                 m_children = g_ptr_array_new();
131                 aabb_clear( &m_BBox );
132                 for ( i = 0; i < PicoGetModelNumSurfaces( m_pModel ); i++ )
133                 {
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() );
138                 }
139         }
140         else
141         {
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;
144         }
145
146         for ( j = 0; j < m_parents->len; j++ ) {
147                 ( (CPicoParent*)m_parents->pdata[j] )->UpdateShaders();
148         }
149 }
150
151 void CPicoModel::Draw( int state, vector<IShader*> shaders, int rflags ) const {
152         if ( m_pModel ) {
153                 for ( unsigned int i = 0; i < m_children->len; i++ )
154                         ( (CPicoSurface*)m_children->pdata[i] )->Draw( state, shaders[i], rflags );
155         }
156 }
157
158 void CPicoModel::Draw( int state, int rflags ) const {
159         if ( m_pModel ) {
160                 for ( unsigned int i = 0; i < m_children->len; i++ )
161                         ( (CPicoSurface*)m_children->pdata[i] )->Draw( state, rflags );
162         }
163 }
164
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;
169
170         if ( !m_pModel ) {
171                 return false;
172         }
173
174         if ( !aabb_intersect_ray( &m_BBox, &ray_local, &dist_local ) ) {
175                 return false;
176         }
177         dist_local = dist_start;
178
179         for ( unsigned int i = 0; i < m_children->len; i++ )
180         {
181                 if ( ( (CPicoSurface*)m_children->pdata[i] )->TestRay( &ray_local, &dist_local ) ) {
182                         *dist = dist_local;
183                 }
184         }
185
186         return *dist < dist_start;
187 }
188
189 int CPicoModel::GetNumSurfaces( void ){
190         if ( !m_pModel ) {
191                 return 0;
192         }
193
194         return m_children->len;
195 }
196
197 char *CPicoModel::GetShaderNameForSurface( const unsigned int surf ){
198         if ( !m_pModel || surf < 0 || surf >= m_children->len ) {
199                 return 0;
200         }
201
202         return ( (CPicoSurface*)m_children->pdata[surf] )->GetShaderName();
203 }