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

WP_ObjectManager.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_OBJECTMANAGER_H
00018 #define WP_OBJECTMANAGER_H
00019 
00020 #include <string>
00021 #include <list>
00022 #include "WPCG.h"
00023 
00024 namespace WPCG
00025 {
00026 //forward declarations
00027 class WP_Model;
00028 class WP_Camera;
00029 
00048 class WP_Object
00049 {
00050  public: 
00051   virtual ~WP_Object(){}
00052   
00056   void drawOpenGL(); 
00057 
00061   WP_Matrix3D matrix;
00062 
00066   const string object_name;
00067 
00071   WP_Vector3D dir;
00072 
00076   WP_Vector3D up;
00077 
00081   WP_Model* model;
00082   
00086   scalar heading; 
00087   
00091   scalar pitch;
00092 
00096   scalar roll;
00097 
00098   PlanesCache planesCache;
00099   
00103   virtual void print() const;
00104 
00109   scalar getXPos() const
00110     {
00111       return matrix.data[12];
00112     };
00113 
00114   virtual void onCollision() = 0;
00115   
00120   scalar getYPos() const
00121     {
00122       return matrix.data[13];
00123     };
00124   
00129   scalar getZPos() const
00130     {
00131       return matrix.data[14];
00132     };
00133 
00138   void setXPos(scalar x) 
00139     {
00140       matrix.data[12] = x;
00141     };
00142   
00147   void setYPos(scalar y) 
00148     {
00149       matrix.data[13] = y;
00150     };
00151   
00156   void setZPos(scalar z) 
00157     {
00158       matrix.data[14] = z;
00159     };
00160 
00161   bool isAnimated() const;
00162 
00163   unsigned short getAnimationCategories(string **strings) const;
00164 
00165   bool setAnimationCategory(const string &category) const;
00166 
00167   string getAnimationCategory() const;
00168 
00172   bool inFrustum;
00173 
00177   GLuint name_id;
00178 
00179  protected:
00180   WP_Object(){};
00181   
00186   WP_Object(const WP_Matrix3D& _matrix, const string& name);
00187 };
00188 
00207 class WP_StaticObject: public WP_Object
00208 {
00209   friend class WP_ObjectManager;
00210   
00211  public:
00212   virtual ~WP_StaticObject(){};
00213 
00214   void onCollision()
00215     {
00216       // collision handling 
00217     }
00218 
00219  protected:
00220   WP_StaticObject(){};
00221   
00226   WP_StaticObject(const WP_Matrix3D& _matrix, const string& name):
00227     WP_Object(_matrix, name){};
00228 
00229 };
00230 
00249 class WP_DynamicObject: public WP_Object
00250 {
00251   friend class WP_ObjectManager;
00252   
00253  public:
00254   virtual ~WP_DynamicObject(){};
00255   
00259   WP_Vector3D velocity;;
00260  
00264   scalar speed;
00265 
00270   void changeHeading(scalar delta_degrees);
00271 
00276   void changePitch(scalar delta_degrees);
00277 
00282   void changeRoll(scalar delta_degrees);
00283 
00288   void changeSpeed(scalar delta_speed);
00289 
00294   void setNewHeading(scalar new_heading);
00295 
00300   void setNewPitch(scalar new_pitch);
00301 
00306   void setNewRoll(scalar new_roll);
00307 
00312   void setNewSpeed(scalar new_speed);
00313 
00318   void setVelocityVector(const WP_Vector3D& vector);
00319 
00323   void move();
00324 
00328   void print() const;
00329 
00330   void onCollision()
00331     {
00332       // collision handling
00333     }
00334 
00335  protected:
00336   WP_DynamicObject(){};
00337 
00343   WP_DynamicObject(const WP_Matrix3D& _matrix, const string& name, const WP_Vector3D& _velocity);
00344 
00352   WP_DynamicObject(const WP_Matrix3D& _matrix, const string& name, scalar _heading, scalar _speed, scalar _pitch);
00353 
00358   scalar computeHeading() const;
00359 
00364   scalar computePitch() const;
00365 
00370   WP_Vector3D computeVelocityVector();
00371 
00375   static WP_Math* math;
00376 };
00377 
00396 class WP_ObjectManager
00397 {
00398 public:
00399   ~WP_ObjectManager();
00400 
00405   static WP_ObjectManager* getInstance()
00406     {
00407       if (!om_instance)
00408         {
00409           om_instance = new WP_ObjectManager();
00410         }
00411       return om_instance;
00412     }
00413 
00421   WP_StaticObject* createStaticObject(const WP_Matrix3D& matrix, const string& object_name, const string& model_name);
00422 
00431   WP_DynamicObject* createDynamicObject(const WP_Matrix3D& matrix, const string& object_name, const string& model_name, 
00432                                  const WP_Vector3D& velocity = WP_Vector3D());
00433 
00434   void addLight(const WP_Point3D& position, const WP_Color& ambient, const WP_Color& diffuse, const WP_Color& specular, const WP_Color& emissive, bool remote = true);  
00435 
00441   WP_Object* getObject(const string& name) const;
00442 
00448   WP_StaticObject* getNextStaticObject(const WP_StaticObject* last) const;
00449 
00455   WP_DynamicObject* getNextDynamicObject(const WP_DynamicObject* last) const;
00456 
00461   WP_StaticObject* getStaticObject() const;
00462 
00467   WP_DynamicObject* getDynamicObject() const;
00468 
00474   WP_StaticObject* getStaticObject(const string& name) const;
00475 
00481   WP_DynamicObject* getDynamicObject(const string& name) const;
00482 
00488   bool removeObject(const string& name);
00489 
00495   bool removeStaticObject(const string& name);
00496 
00502   bool removeDynamicObject(const string& name);
00503 
00509   bool removeSameObjects(const string& name);
00510 
00515   bool removeAll();
00516 
00520   void drawObjectsSelection();
00521   
00528   WP_Object* pickObject(int x, int y);
00529 
00530   unsigned int number_collisions;
00531 
00532   void createCollisionPairs();
00533 
00534   void updateAll();
00535 
00536 private:
00537   WP_ObjectManager();
00538 
00542   void drawObjects();
00543 
00544   void checkCollisions();
00545 
00549   list<WP_StaticObject*> static_objects;
00550 
00554   list<WP_DynamicObject*> dynamic_objects; 
00555 
00559   list<WP_Light*> lights;
00560 
00561   unsigned char number_lights;
00562 
00566   static WP_ObjectManager* om_instance;  
00567 
00574   //FIXME this function is also used in WP_Image.C, therefore better to create a WP_File class and park this function there
00575   inline bool hasValidExtension(const string &file, const string &extension)
00576     {
00577       int pos = file.find('.' + extension);
00578       return pos != string::npos;
00579     }
00580 
00586   WP_Model* findInstance(const string& model_name);
00587 
00593   int countStaticObjects(const string& name) const;
00594 
00600   int countDynamicObjects(const string& name) const;
00601 
00605   WP_Camera* cam;
00606 
00610   unsigned int unique;
00611 
00612   static const unsigned char num_internal_models;
00613 
00617   static const string internal_models[];
00618 
00619   // nested class 
00620 
00621   class WP_CollisionPair
00622     {
00623     public:
00624       WP_CollisionPair(WP_Object *obj1, WP_Object *obj2);
00625       ~WP_CollisionPair(){};
00626       
00627       WP_Object *object1;
00628       WP_Object *object2;
00629       BVTCache cache;
00630     };
00631 
00635   list<WP_ObjectManager::WP_CollisionPair*> collision_pairs; 
00636 
00637 
00638   PlanesCollider PC;
00639   AABBTreeCollider TC;
00640 
00641   //OPCODE CALLBACK
00642   static void ColCallback(udword triangleindex, VertexPointers &triangle, udword user_data);
00643 };
00644 }
00645 #endif
00646 
00647 

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