Back to index

extremetuxracer  0.5beta
model.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  */
00018 
00019 
00020 #include "model.h"
00021 
00022 #include "model_ac.h"
00023 
00024 #ifdef _WIN32
00025        #include <WTypes.h>
00026 #endif
00027 
00028 #include <GL/gl.h>
00029 #include <iostream>
00030 
00031 namespace pp{
00032        
00033 Model::Model(const char* filename)
00034  : m_displayList(-1)
00035 {      
00036        mp_model = new ModelAC(filename);
00037        
00038        m_displayList = mp_model->getDisplayList();
00039 }
00040 
00041 Model::~Model()
00042 {
00043        delete mp_model;
00044 }
00045 
00046 void
00047 Model::draw()
00048 {
00049        if(m_displayList != -1){
00050               glCallList( m_displayList );
00051        }
00052 }
00053 
00054 
00055 void
00056 Model::copyObject(pp::Polyhedron* ph, pp::ModelObject* object)
00057 {
00058        if(object->num_vert>0 && object->num_surf>0){
00059               int vert0 = ph->num_vertices;
00060               int poly0 = ph->num_polygons;
00061               
00062               for(int i=0; i<object->num_vert; i++){
00063                      ph->vertices[vert0+i]=object->vertices[i].vec;
00064                      ph->num_vertices++;
00065               }
00066 
00067               for(int i=0; i<object->num_surf; i++){
00068                      int num_vertices = object->surfaces[i].numVertices;
00069                      
00070                      if(num_vertices >10){
00071                             std::cout << "Warning: Vertices-> " <<
00072                                           num_vertices << std::endl;
00073                      }else{               
00074                             ph->polygons[poly0+i].numVertices = num_vertices; 
00075                             ph->polygons[poly0+i].vertices = new int[num_vertices];
00076                             
00077                             for(int u=0; u<num_vertices;u++){
00078                                    ph->polygons[poly0+i].vertices[u]=
00079                                           object->surfaces[i].vertices[u] + vert0;         
00080                             }
00081                             ph->num_polygons++;
00082                      }
00083               }
00084        }      
00085        
00086        for (int i = 0; i < object->num_kids; i++){
00087               copyObject(ph,object->kids[i]);    
00088        }      
00089 }
00090 
00091 pp::Polyhedron*
00092 Model::getPolyhedron()
00093 {
00094        pp::Polyhedron* ph = new pp::Polyhedron;
00095        
00096        ph->num_vertices=0;
00097        ph->num_polygons=0;
00098        ph->vertices = new pp::Vec3d[1024];
00099        ph->polygons = new pp::Polygon[256];
00100               
00101        copyObject(ph,mp_model->getModel());
00102               
00103     return ph;       
00104 }
00105 
00106 } //namespace pp