-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
-*/\r
-\r
-#include "cpicomodel.h"\r
-#include "cpicosurface.h"\r
-\r
-CPicoModel::CPicoModel(const PicoModelKey& key)\r
-: m_refcount(1)\r
-{\r
- load(key.first.GetBuffer(), key.second);\r
-}\r
-\r
-CPicoModel::CPicoModel(const Str& name)\r
-: m_refcount(1)\r
-{\r
- load(name.GetBuffer(), 0);\r
-}\r
-\r
-CPicoModel::CPicoModel(const Str& name, const int frame)\r
-: m_refcount(1)\r
-{\r
- load(name.GetBuffer(), frame);\r
-}\r
-\r
-CPicoModel::CPicoModel(const char *name, const int frame)\r
-: m_refcount(1)\r
-{\r
- load(name, frame);\r
-}\r
-\r
-void CPicoModel::load(const char *name, const int frame)\r
-{\r
- CPicoSurface *surf;\r
- picoSurface_t *pSurface;\r
- int i;\r
-\r
- m_name= new char[strlen(name)+1];\r
- strcpy(m_name,name);\r
-\r
- m_frame = frame;\r
-\r
- if( !(m_pModel = PicoLoadModel(m_name, frame)) )\r
- {\r
- int len = strlen(m_name);\r
-\r
- // Try loading an mdc if md3 fails and vice-versa (fixme: only do this for games with mdc support)\r
- if( !strcmp( m_name + len - 4, ".md3" ) )\r
- {\r
- m_name[len - 1] = 'c';\r
- m_pModel = PicoLoadModel(m_name, frame);\r
- } else if( !strcmp( m_name + len - 4, ".mdc" ) )\r
- {\r
- m_name[len - 1] = '3';\r
- m_pModel = PicoLoadModel(m_name, frame);\r
- }\r
- }\r
-\r
- if( m_pModel )\r
- {\r
- m_children = g_ptr_array_new();\r
- aabb_clear(&m_BBox);\r
- for (i = 0; i < PicoGetModelNumSurfaces(m_pModel); i++ )\r
- {\r
- pSurface = PicoGetModelSurface(m_pModel,i);\r
- surf = new CPicoSurface(pSurface);\r
- g_ptr_array_add(m_children, surf);\r
- aabb_extend_by_aabb(&m_BBox, surf->GetAABB());\r
- }\r
- }\r
- else\r
- {\r
- m_BBox.origin[0] = m_BBox.origin[1] = m_BBox.origin[2] = 0;\r
- m_BBox.extents[0] = m_BBox.extents[1] = m_BBox.extents[2] = 0;\r
- }\r
-\r
- m_parents = g_ptr_array_new();\r
-}\r
-\r
-CPicoModel::~CPicoModel()\r
-{\r
- if( m_pModel ) {\r
- for(unsigned int i=0; i<m_children->len; i++)\r
- ((CPicoSurface*)m_children->pdata[i])->DecRef();\r
- g_ptr_array_free(m_children, FALSE);\r
- }\r
- g_ptr_array_free(m_parents, FALSE);\r
- delete [] m_name;\r
-}\r
-\r
-void CPicoModel::AddParent( CPicoParent *parent )\r
-{\r
- g_ptr_array_add(m_parents, parent);\r
-}\r
-\r
-void CPicoModel::RemoveParent( CPicoParent *parent )\r
-{\r
- unsigned int i;\r
- for(i=0; i<m_parents->len; i++) {\r
- if( parent == (CPicoParent*)m_parents->pdata[i] )\r
- g_ptr_array_remove_index_fast(m_parents, i);\r
- }\r
-}\r
-\r
-void CPicoModel::Reload( void )\r
-{\r
- CPicoSurface *surf;\r
- picoSurface_t *pSurface;\r
- int i;\r
- unsigned int j;\r
-\r
- // Get rid of the old model\r
- if( m_pModel ) {\r
- for(j=0; j<m_children->len; j++) {\r
- ((CPicoSurface*)m_children->pdata[j])->DecRef();\r
- g_ptr_array_remove_index_fast(m_children, j);\r
- }\r
- }\r
-\r
- // And reload it\r
- m_pModel = PicoLoadModel(m_name, m_frame);\r
-\r
- if( m_pModel )\r
- {\r
- m_children = g_ptr_array_new();\r
- aabb_clear(&m_BBox);\r
- for (i = 0; i < PicoGetModelNumSurfaces(m_pModel); i++ )\r
- {\r
- pSurface = PicoGetModelSurface(m_pModel,i);\r
- surf = new CPicoSurface(pSurface);\r
- g_ptr_array_add(m_children, surf);\r
- aabb_extend_by_aabb(&m_BBox, surf->GetAABB());\r
- }\r
- }\r
- else\r
- {\r
- m_BBox.origin[0] = m_BBox.origin[1] = m_BBox.origin[2] = 0;\r
- m_BBox.extents[0] = m_BBox.extents[1] = m_BBox.extents[2] = 0;\r
- }\r
-\r
- for(j=0; j<m_parents->len; j++) {\r
- ((CPicoParent*)m_parents->pdata[j])->UpdateShaders();\r
- }\r
-}\r
-\r
-void CPicoModel::Draw(int state, vector<IShader*> shaders, int rflags) const\r
-{\r
- if( m_pModel ) {\r
- for(unsigned int i=0; i<m_children->len; i++)\r
- ((CPicoSurface*)m_children->pdata[i])->Draw(state, shaders[i], rflags);\r
- }\r
-}\r
-\r
-void CPicoModel::Draw(int state, int rflags) const\r
-{\r
- if( m_pModel ) {\r
- for(unsigned int i=0; i<m_children->len; i++)\r
- ((CPicoSurface*)m_children->pdata[i])->Draw(state, rflags);\r
- }\r
-}\r
-\r
-bool CPicoModel::TestRay(const ray_t *ray, vec_t *dist) const\r
-{\r
- vec_t dist_start = *dist;\r
- vec_t dist_local = *dist;\r
- ray_t ray_local = *ray;\r
-\r
- if( !m_pModel ) {\r
- return false;\r
- }\r
-\r
- if (!aabb_intersect_ray(&m_BBox, &ray_local, &dist_local))\r
- return false;\r
- dist_local = dist_start;\r
-\r
- for(unsigned int i=0; i<m_children->len; i++)\r
- {\r
- if(((CPicoSurface*)m_children->pdata[i])->TestRay(&ray_local, &dist_local))\r
- {\r
- *dist = dist_local;\r
- }\r
- }\r
-\r
- return *dist < dist_start;\r
-}\r
-\r
-int CPicoModel::GetNumSurfaces( void )\r
-{\r
- if( !m_pModel ) {\r
- return 0;\r
- }\r
-\r
- return m_children->len;\r
-}\r
-\r
-char *CPicoModel::GetShaderNameForSurface( const unsigned int surf )\r
-{\r
- if( !m_pModel || surf < 0 || surf >= m_children->len ) {\r
- return 0;\r
- }\r
-\r
- return ((CPicoSurface*)m_children->pdata[surf])->GetShaderName();\r
-}\r
+/*
+Copyright (C) 1999-2007 id Software, Inc. and contributors.
+For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+This file is part of GtkRadiant.
+
+GtkRadiant is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+GtkRadiant is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GtkRadiant; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "cpicomodel.h"
+#include "cpicosurface.h"
+
+CPicoModel::CPicoModel(const PicoModelKey& key)
+: m_refcount(1)
+{
+ load(key.first.GetBuffer(), key.second);
+}
+
+CPicoModel::CPicoModel(const Str& name)
+: m_refcount(1)
+{
+ load(name.GetBuffer(), 0);
+}
+
+CPicoModel::CPicoModel(const Str& name, const int frame)
+: m_refcount(1)
+{
+ load(name.GetBuffer(), frame);
+}
+
+CPicoModel::CPicoModel(const char *name, const int frame)
+: m_refcount(1)
+{
+ load(name, frame);
+}
+
+void CPicoModel::load(const char *name, const int frame)
+{
+ CPicoSurface *surf;
+ picoSurface_t *pSurface;
+ int i;
+
+ m_name= new char[strlen(name)+1];
+ strcpy(m_name,name);
+
+ m_frame = frame;
+
+ if( !(m_pModel = PicoLoadModel(m_name, frame)) )
+ {
+ int len = strlen(m_name);
+
+ // Try loading an mdc if md3 fails and vice-versa (fixme: only do this for games with mdc support)
+ if( !strcmp( m_name + len - 4, ".md3" ) )
+ {
+ m_name[len - 1] = 'c';
+ m_pModel = PicoLoadModel(m_name, frame);
+ } else if( !strcmp( m_name + len - 4, ".mdc" ) )
+ {
+ m_name[len - 1] = '3';
+ m_pModel = PicoLoadModel(m_name, frame);
+ }
+ }
+
+ if( m_pModel )
+ {
+ m_children = g_ptr_array_new();
+ aabb_clear(&m_BBox);
+ for (i = 0; i < PicoGetModelNumSurfaces(m_pModel); i++ )
+ {
+ pSurface = PicoGetModelSurface(m_pModel,i);
+ surf = new CPicoSurface(pSurface);
+ g_ptr_array_add(m_children, surf);
+ aabb_extend_by_aabb(&m_BBox, surf->GetAABB());
+ }
+ }
+ else
+ {
+ m_BBox.origin[0] = m_BBox.origin[1] = m_BBox.origin[2] = 0;
+ m_BBox.extents[0] = m_BBox.extents[1] = m_BBox.extents[2] = 0;
+ }
+
+ m_parents = g_ptr_array_new();
+}
+
+CPicoModel::~CPicoModel()
+{
+ if( m_pModel ) {
+ for(unsigned int i=0; i<m_children->len; i++)
+ ((CPicoSurface*)m_children->pdata[i])->DecRef();
+ g_ptr_array_free(m_children, FALSE);
+ }
+ g_ptr_array_free(m_parents, FALSE);
+ delete [] m_name;
+}
+
+void CPicoModel::AddParent( CPicoParent *parent )
+{
+ g_ptr_array_add(m_parents, parent);
+}
+
+void CPicoModel::RemoveParent( CPicoParent *parent )
+{
+ unsigned int i;
+ for(i=0; i<m_parents->len; i++) {
+ if( parent == (CPicoParent*)m_parents->pdata[i] )
+ g_ptr_array_remove_index_fast(m_parents, i);
+ }
+}
+
+void CPicoModel::Reload( void )
+{
+ CPicoSurface *surf;
+ picoSurface_t *pSurface;
+ int i;
+ unsigned int j;
+
+ // Get rid of the old model
+ if( m_pModel ) {
+ for(j=0; j<m_children->len; j++) {
+ ((CPicoSurface*)m_children->pdata[j])->DecRef();
+ g_ptr_array_remove_index_fast(m_children, j);
+ }
+ }
+
+ // And reload it
+ m_pModel = PicoLoadModel(m_name, m_frame);
+
+ if( m_pModel )
+ {
+ m_children = g_ptr_array_new();
+ aabb_clear(&m_BBox);
+ for (i = 0; i < PicoGetModelNumSurfaces(m_pModel); i++ )
+ {
+ pSurface = PicoGetModelSurface(m_pModel,i);
+ surf = new CPicoSurface(pSurface);
+ g_ptr_array_add(m_children, surf);
+ aabb_extend_by_aabb(&m_BBox, surf->GetAABB());
+ }
+ }
+ else
+ {
+ m_BBox.origin[0] = m_BBox.origin[1] = m_BBox.origin[2] = 0;
+ m_BBox.extents[0] = m_BBox.extents[1] = m_BBox.extents[2] = 0;
+ }
+
+ for(j=0; j<m_parents->len; j++) {
+ ((CPicoParent*)m_parents->pdata[j])->UpdateShaders();
+ }
+}
+
+void CPicoModel::Draw(int state, vector<IShader*> shaders, int rflags) const
+{
+ if( m_pModel ) {
+ for(unsigned int i=0; i<m_children->len; i++)
+ ((CPicoSurface*)m_children->pdata[i])->Draw(state, shaders[i], rflags);
+ }
+}
+
+void CPicoModel::Draw(int state, int rflags) const
+{
+ if( m_pModel ) {
+ for(unsigned int i=0; i<m_children->len; i++)
+ ((CPicoSurface*)m_children->pdata[i])->Draw(state, rflags);
+ }
+}
+
+bool CPicoModel::TestRay(const ray_t *ray, vec_t *dist) const
+{
+ vec_t dist_start = *dist;
+ vec_t dist_local = *dist;
+ ray_t ray_local = *ray;
+
+ if( !m_pModel ) {
+ return false;
+ }
+
+ if (!aabb_intersect_ray(&m_BBox, &ray_local, &dist_local))
+ return false;
+ dist_local = dist_start;
+
+ for(unsigned int i=0; i<m_children->len; i++)
+ {
+ if(((CPicoSurface*)m_children->pdata[i])->TestRay(&ray_local, &dist_local))
+ {
+ *dist = dist_local;
+ }
+ }
+
+ return *dist < dist_start;
+}
+
+int CPicoModel::GetNumSurfaces( void )
+{
+ if( !m_pModel ) {
+ return 0;
+ }
+
+ return m_children->len;
+}
+
+char *CPicoModel::GetShaderNameForSurface( const unsigned int surf )
+{
+ if( !m_pModel || surf < 0 || surf >= m_children->len ) {
+ return 0;
+ }
+
+ return ((CPicoSurface*)m_children->pdata[surf])->GetShaderName();
+}