Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

WP_Model.h

Go to the documentation of this file.
00001 /* Copyright (C) 2001-2003 W.P. van Paassen - peter@paassen.tmfweb.nl
00002 
00003    This program is free software; you can redistribute it and/or modify it under
00004    the terms of the GNU General Public License as published by the Free
00005    Software Foundation; either version 2 of the License, or (at your
00006    option) any later version.
00007 
00008    This program is distributed in the hope that it will be useful, but WITHOUT
00009    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00010    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00011    for more details.
00012 
00013    You should have received a copy of the GNU General Public License
00014    along with this program; see the file COPYING.  If not, write to the Free
00015    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
00016 
00017 #ifndef WP_MODEL_H
00018 #define WP_MODEL_H
00019 
00020 #include <string>
00021 #include <list>
00022 #include <map>
00023 
00024 #include "WPCG.h"//FIXME don't include all headers!
00025 
00026 namespace WPCG
00027 {
00032 class WP_Model
00033  {
00034    friend class WP_Frame;
00035  public:
00036   WP_Model();
00037   
00041   WP_Model(const string& name): model_name(name) ,numberTriangles(0), triangles(0), numberVertices(0) 
00042     {
00043     };
00044 
00045   //COPY CONSTRUCTOR
00046   WP_Model(const WP_Model &model);
00047 
00048   virtual ~WP_Model();
00049 
00050   //ASSIGNMENT OPERATOR
00051   WP_Model& operator=(const WP_Model &model);
00052   
00057   virtual void drawOpenGL(const WP_Matrix3D& matrix, WP_Object *object) = 0;
00058   
00062   virtual bool initModel() = 0;
00063   
00067   bool init();
00068 
00072   string model_name;
00073   
00074   int numberTriangles;
00075 
00076   virtual OPCODE_Model* getCollisionModel(const WP_Object *object) = 0;
00077 
00078   virtual WP_Vertex* getVertex(const WP_Object *object, unsigned int index) = 0;
00079 
00083   unsigned int *triangles; 
00084 
00088   int numberVertices;
00089 
00093   int count;
00094 
00098   GLint tex_id;
00099 
00100  protected:
00101 
00105   virtual bool finalizeAll() = 0;  
00106 
00110   class WP_Frame
00111     {
00112     public:
00113       WP_Frame():vertices(0){};
00114       WP_Frame(WP_Model* m):model(m), vertices(0){};
00115 
00116       //copy constructor
00117       WP_Frame(const WP_Frame &frame);
00118 
00119       ~WP_Frame()
00120         {
00121           delete[] vertices;
00122         }
00123 
00124       //assignment operator
00125       WP_Frame& operator=(const WP_Frame &frame);
00126 
00127       OPCODE_Model collision_model;
00128 
00129       WP_Model *model;
00130 
00134       WP_Vertex* vertices;
00135 
00139       WP_Material material;
00140 
00144       string name;
00145     };
00146 };
00147 
00149 
00154 class WP_NonAnimatedModel: public WP_Model
00155 {
00156  public:
00157   WP_NonAnimatedModel(const string& name):WP_Model(name)
00158     {
00159       frame = new WP_Frame(this);
00160     };
00161   
00162   virtual ~WP_NonAnimatedModel()
00163     {
00164       delete frame;
00165     };
00166   
00167   //COPY CONSTRUCTOR
00168   WP_NonAnimatedModel(const WP_NonAnimatedModel& namodel):WP_Model(namodel)
00169     {
00170       frame = new WP_Frame(*namodel.frame);
00171     }
00172   
00173   //ASSIGNMENT OPERATOR
00174   WP_NonAnimatedModel& operator=(const WP_NonAnimatedModel &namodel)
00175     {
00176       if (this == &namodel)
00177         return *this;
00178       
00179       WP_Model::operator=(namodel);
00180       
00181       delete frame;
00182       frame = new WP_Frame(*namodel.frame);
00183       
00184       return *this;
00185     }
00186   
00187   OPCODE_Model* getCollisionModel(const WP_Object *object) 
00188     {
00189       return &frame->collision_model;
00190     }
00191 
00192   WP_Vertex* getVertex(const WP_Object *object, unsigned int index) 
00193     {
00194       return &frame->vertices[index];
00195     }
00196   
00197   virtual void drawOpenGL(const WP_Matrix3D& matrix, WP_Object *object) = 0;
00198   virtual bool initModel() = 0;
00199   
00200  protected:
00201   WP_Frame* frame;
00202 
00203   bool finalizeAll();
00204 };
00205 
00206 
00208 
00213 class WP_AnimatedModel: public WP_Model
00214  {
00215  public:
00216   WP_AnimatedModel(const string& name):WP_Model(name),
00217     frames(0), numberFrames(0){}; 
00218 
00219   //COPY CONSTRUCTOR
00220   WP_AnimatedModel(const WP_AnimatedModel& amodel);
00221 
00222   virtual ~WP_AnimatedModel()
00223     {
00224       delete[] frames;
00225 
00226       //empty map
00227       map<string, WP_FrameCategory*>::iterator i = categories.begin();
00228       while(i != categories.end())
00229           {
00230             delete (*i).second;
00231             i++;
00232           }
00233     }
00234 
00235   //ASSIGNMENT OPERATOR
00236   WP_AnimatedModel& operator=(const WP_AnimatedModel &amodel);
00237 
00238   virtual void drawOpenGL(const WP_Matrix3D& matrix, WP_Object *object) = 0;
00239   virtual bool initModel() = 0;
00240 
00241   OPCODE_Model* getCollisionModel(const WP_Object *object) 
00242     {
00243       static WP_AnimationManager *ani = WP_AnimationManager::getInstance();
00244       unsigned short cf = ani->getCurrentFrame(object);
00245       cf += categories[ani->getCategory(object)]->start_frame;
00246       return &frames[cf].collision_model;
00247     }
00248 
00249   WP_Vertex* getVertex(const WP_Object *object, unsigned int index) 
00250     {
00251       static WP_AnimationManager *ani = WP_AnimationManager::getInstance();
00252       unsigned short cf = ani->getCurrentFrame(object);
00253       cf += categories[ani->getCategory(object)]->start_frame;
00254       return &frames[cf].vertices[index];
00255     }
00256 
00257   unsigned short getCategoryStartFrame(const string& category) 
00258     {
00259       if (categories.count(category))
00260           return categories[category]->start_frame;
00261       return 0;
00262     }
00263 
00264   unsigned short getCategoryNumberFrames(const string& category) 
00265     {
00266       if (categories.count(category))
00267           return categories[category]->number_frames;
00268       return 0;
00269     }
00270 
00271   string getCategoryName(unsigned int index) const
00272     {
00273       map<string, WP_FrameCategory*>::const_iterator i = categories.begin();
00274       unsigned int count = 0;
00275 
00276       while(i != categories.end())
00277         {
00278           if (index == count)
00279             return (*i).first;
00280           i++;
00281           count++;
00282         }
00283 
00284       return ""; 
00285     }
00286 
00287   unsigned int getNumberCategories() const
00288     {
00289       return categories.size();
00290     }
00291 
00292 protected:
00293 
00294   WP_Frame* frames;
00295   unsigned int numberFrames;
00296 
00297   //nested class 
00298   class WP_FrameCategory
00299     {
00300     public:
00301       WP_FrameCategory(){};
00302       WP_FrameCategory(unsigned short start): start_frame(start), number_frames(1){};
00303       ~WP_FrameCategory(){};   
00304 
00305       unsigned short start_frame;
00306       unsigned short number_frames;
00307     };
00308 
00309   map<string, WP_FrameCategory*> categories;
00310 
00311   bool finalizeAll();
00312 };
00313 
00315 
00319 class WP_Model_MD2: public WP_AnimatedModel
00320 {
00321  public:
00325   WP_Model_MD2(const string& name);
00326 
00327   //COPY CONSTRUCTOR
00328   WP_Model_MD2(const WP_Model_MD2 &md2model);
00329 
00330   ~WP_Model_MD2()
00331     {
00332       list<WP_TriangleGroup*>::iterator i = triangle_groups.begin();
00333       while (i != triangle_groups.end())
00334         {
00335           delete *i;
00336           i++;
00337         }
00338     };
00339 
00340   //ASSIGNMENT OPERATOR
00341   WP_Model_MD2& operator=(const WP_Model_MD2 &md2model);
00342 
00343   void drawOpenGL(const WP_Matrix3D& matrix, WP_Object *object);
00344   
00348   bool initModel();
00349 
00350  private:
00351 
00352   static WP_Vector3D quake2_normals[];
00353 
00354   // nested class WP_MD2_HEADER 
00355   class WP_MD2_HEADER
00356     {
00357   public:
00358     int magic; 
00359     int version; 
00360     int skinWidth; 
00361     int skinHeight; 
00362     int frameSize; 
00363     int numSkins; 
00364     int numVertices; 
00365     int numTexCoords; 
00366     int numTriangles; 
00367     int numGlCommands; 
00368     int numFrames; 
00369     int offsetSkins; 
00370     int offsetTexCoords; 
00371     int offsetTriangles; 
00372     int offsetFrames; 
00373     int offsetGlCommands; 
00374     int offsetEnd; 
00375   }; 
00376 
00377   list<WP_TriangleGroup*> triangle_groups;
00378 };
00379 
00381 
00382 class WP_MetaBall: public WP_NonAnimatedModel
00383 {
00384  public:
00385   WP_MetaBall(const string& name);
00386   WP_MetaBall(const WP_MetaBall &ball);
00387 
00388   WP_MetaBall::~WP_MetaBall(){};
00389 
00390   WP_MetaBall& operator=(const WP_MetaBall &ball);
00391 
00392   void drawOpenGL(const WP_Matrix3D& matrix, WP_Object *object);
00393   
00397   bool initModel();
00398 
00399   WP_Point3D center;
00400 };
00401 }
00402 #endif
00403 

Generated on Tue Jan 28 20:26:34 2003 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002