Labyrinth
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
model.hpp
1 //////////////////////////////
2 //
3 // File: lxs/coregraphics/model.hpp
4 // Author: Maciej Kaminski
5 // E-mail: maciej.a.kaminski@gmail.com
6 // Website: maciejkaminski.pl
7 //
8 // File creation date: Nov 27 2012
9 // This file is part of Project Labyrinth
10 // This file is going to be released under terms of GNU GPL in near future
11 //
12 //////////////////////////////
13 
14 #ifndef __LXS_MODEL__
15 #define __LXS_MODEL__
16 #include "helper/opengl.hpp"
17 //#include "helper/tr1.hpp"
18 #include <boost/shared_ptr.hpp>
19 #include<map>
20 #include<vector>
21 #include <string>
22 #include <utility>
23 #include "lxs/coregraphics/p_model.hpp"
24 #include "lxs/system/servicethread.hpp"
25 #include "lxs/coregraphics/primitives.hpp"
26 namespace LxS
27 {
28  namespace System
29  {
30  class MyMutex;
31  }
32  namespace CoreGraphics
33  {
34  class Renderable;
35  class Model;
36  //! model!
37  class Model: public Renderable
38  {
39  public:
40  //! enable/disable VBOs
41  static void SetUseVBO(bool do_use);
42  //! are VBOs used for displaying this particular model
43  static bool GetUseVBO();
44 
45  protected:
46  //! transformation matrix
47  float transmtx[16];
48  //! currently displayed animation frame
49  int _f;// frame
50  //! shader used when rendering ForReal
51  boost::shared_ptr<Shader> p_ShaderForReal;
52  //! shader used when rendering ForCoords3D
53  boost::shared_ptr<Shader> p_ShaderForCoords3D;
54  protected:
55  //! if loading is in progress, model isn't usable
57  //! class representing information on mesh (may be shared by many Model class objects).
58  class MeshInfo
59  {
60  public:
61  //! is ready to use
62  bool init_done;
63  //! mesh data (to be blitted to gpu
64  char * meshfile;
65  //! start of data within mesh file (data is placed after metadata)
66  const char * gl_data_start; // pointer within meshfile
67  //! num indices
69  //! fake indices are indices that exist for purpose of fixing z-layering
71  //! amount of vertices (some of them may be referenced many times through indices)
73  //! first frame of animation sequence (usually 0 or 1)
75  //! last frame of animation sequence
77  //! indices within meshfile
78  GLshort * indices;
79  //! size of vertices vector ((mi.num_vertices*6+mi.children.size()*16))
81  //! this is doubled here and in buffer object, as they're useful for colision detection
82  GLfloat * vertices;
83  //! uv coordinates
84  GLfloat * uvs;
85  //! setup for OpenGL buffer objects
86  struct {
87  //! 3 floats for position, 3 floats for normal
88  GLuint vertices;
89  //! pairs of floats (3 times less than vertices)
90  GLuint uvs;
91  //! indices to squares as triangles
92  GLuint indices;
93  //! fake indices (see above for definition
94  GLuint fake_indices;
95  } buffer_objects __attribute__((packed));
96  //! vector of associations string<->model
97  typedef std::vector<std::pair<std::string,p_Model > > modelVector;
98  //! child objects
100  //! reference counting
102  //! if true, normals are inverted against what said in binary file - state of mesh in RAM (=mesh in VRAM)
104  //! edges of an object
106  //! edges of an object
108  //! edges of an object
110  //! edges of an object
112  //! edges of an object
114  //! edges of an object
116 
117  };
118  //end of meshinfo internal subclass
119  //! if true, normals are inverted against what said in binary file (state expected of LxS::Model object)
121  //! collection of meshes
122  typedef std::map<std::string,MeshInfo> MeshManager;
123  //! global collection of mesh data files - to accelerate initialization
125  //! sync object
127  //! sync object used when loading data
129  //! iterator (smart pointer) to data of current mesh
130  MeshManager::iterator i_MeshInfo;
131  private:
132  //! OpenGL part of mesh loading routine
133  static void LoadMeshGL(MeshInfo * pmi);
134  //! load mesh to structure given
135  void LoadMesh(MeshManager::iterator it/*where to insert into GlobalMeshManager*/);
136 
137  bool has_to_recompute_children_matrices;
138  public:
139  Model();
140  Model(const Model & base);
141 
142  //! global reinitialization routine
143  static void ReloadAllMeshes();
144  //! diagnostic routine
145  static void PrintGlobalStatus();
146  //! select an animation frame
147  virtual void SelectFrame(float fno)
148  {
149  _f=fno-i_MeshInfo->second.frame_first;
150  has_to_recompute_children_matrices = true;
151 
152  }
153  private:
154  //! if normals are set to be inverted, this has to be called
155  void SetInvertedNormalsGL();
156  public:
157  //!set status of normals inversion; default off; affects all models loaded from same mesh
158  void SetInvertedNormals(bool do_invert);
159  //! typedef for C++ string, not to include whole namespace but to simplify notation
160  typedef std::string string;
161  private:
162  //! child objects synchronization
163  mutable System::MyMutex children_mtx;
164  //! children - submeshes
165  std::vector<std::pair<std::string,p_Model > > children;
166  //! filenames for textures
167  string color_fn;
168  //! filename for transparency texture
169  string alpha_fn;
170  //! filename for normal map texture
171  string normal_fn;
172  //! filename for mesh
173  string mesh_fn;
174  //! model transformation matrix
175  GLfloat translation_mtx[16];
176  //! if model is double sided
177  bool double_sided;
178  public:
179  //! enum for representing collision mechanics
181  {
182  COLLISION_NONE=0,
183  COLLISION_WALKABLE=1,
184  COLLISION_BOX=2,
185  COLLISION_CHECK_CHILDREN=4
186  };
187  private:
188  //! which model for collision detection is used
189  int collision_model;
190  //! check this if model is invisible (i.e. used only for collisions and such)
191  bool invisible;
192  public:
193  //! set collision model
194  void SetCollisionModel(int m) {collision_model=m;}
195  //! get collision model
196  int GetCollisionModel() {return collision_model;}
197  //! basically marking model as doublesided turns off face culling.
198  //! this shouldn't be necessary; is implemented as fix for bad models;
199  void SetDoubleSided(bool v) { double_sided=v;}
200  //! if model is invisible (i.e. used only for collisions and such)
201  void SetInvisible(bool i) {invisible=i;}
202  //! animate a model
203  void Animate(float dt) {}//use hl functtions to animate
204  //! set texture for color map
205  void SetColorTexture(string fn)
206  {
207  color_fn = fn;
208  }
209  //! set texture for alpha map
210  void SetAlphaTexture(string fn)
211  {
212  alpha_fn = fn;
213  }
214  //! set texture for normal map
215  void SetNormalTexture(string fn)
216  {
217  normal_fn = fn;
218  }
219  //! set package of three textures
220  void SetTextures(string color, string alpha, string normal)
221  {
222  color_fn=color;
223  normal_fn=normal;
224  alpha_fn=alpha;
225  }
226  //! set mesh
227  void SetMesh(string mesh)
228  {
229  mesh_fn=mesh;
230  }
231  private:
232  //! code common to Initialize and RefreshAssets
233  void InitializeCore();
234  public:
235  //! edges of an object
236  float GetFarYNegative();
237  //! edges of an object
238  float GetFarYPositive();
239  virtual void Initialize(); // load and set up some resources
240  virtual void RefreshAssets();
241  virtual void Render(Rendermode mode);
242  virtual void UnloadResources();
243  //! set mesh position (will alter transformation matrix)
244  void SetPosition(float x, float y, float z);
245  //! rotate around axis, that is perpendicular to the ground; must be called AFTER set position, as setposition RESETS model transformation matrix, while SetRotationY alters it;
246  void SetRotationY(float alpha);
247  //! get mesh position (extracted from current transformation matric)
248  virtual float GetPositionX();
249  //! get mesh position (extracted from current transformation matric)
250  virtual float GetPositionY();
251  //! get mesh position (extracted from current transformation matric)
252  virtual float GetPositionZ();
253  //!get mesh rotation (extracted from current transformation matric)
254  float GetRotationY();
255  //! assign predefined matrix
256  void AssignMatrix(float *);
257  //! cancel all transforms
258  void AssignUnityMatrix();
259  //! get pointer to model transformation matrix
260  float * GetMatrix();
261  //! assign child object
262  void AssignChild(const std::string & s,p_Model);
263  unsigned long GetTexelsCount();
264  unsigned long GetFacesCount();
265  //! true if item of given coordinates and radius, collies with object
266  bool CollisionDetection(float x, float y, float z,float r);
267  //! return triangle set for purposes of collision detection as a walkable area (map)
269  ~Model();
270  };
271  }
272 }
273 
274 #endif