Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mesh.h
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 #ifndef _MESH_H_
22 #define _MESH_H_
23 
24 #include <string>
25 #include <vector>
26 #include "xml_support.h"
27 #include "quaternion.h"
28 #include "matrix.h"
29 #include "gfxlib.h"
30 #include "gfxlib_struct.h"
31 #include "vsfilesystem.h"
32 #include "cmd/unit_generic.h"
33 #include "gfx/technique.h"
34 
35 using std::vector;
36 using std::string;
37 class Planet;
38 class Unit;
39 class Logo;
40 class AnimatedTexture;
41 class Texture;
42 struct GFXVertex;
43 class GFXVertexList;
44 class GFXQuadstrip;
45 struct GFXMaterial;
46 class BoundingBox;
47 
48 #define MESH_HASTHABLE_SIZE (503)
49 
50 
51 // Struct polygon format returned by GetPolys, usually a triangle
53 {
54  std::vector< Vector >v;
55 };
61 class MeshFX : public GFXLight
62 {
63 public:
65  float delta;
67  float TTL;
69  float TTD;
71  {
72  TTL = TTD = delta = 0;
73  }
75  MeshFX( const float TTL, const float delta, const bool enabled, const GFXColor &vect,
76  const GFXColor &diffuse = GFXColor( 0, 0, 0, 1 ),
77  const GFXColor &specular = GFXColor( 0, 0, 0, 1 ),
78  const GFXColor &ambient = GFXColor( 0, 0, 0, 1 ),
79  const GFXColor &attenuate = GFXColor( 1, 0, 0 ) );
81  void MergeLights( const MeshFX &other );
83  bool Update( float ttime ); //if false::dead
84 };
89 {
93  vector< MeshFX > *SpecialFX;
95  bool useXtraFX;
97  enum CLK {NONE=0x0, CLOAK=0x1, FOG=0x2, NEARINVIS=0x4, GLASSCLOAK=0x8, RENORMALIZE=0x10};
98  char cloaked;
99  char mesh_seq;
100  unsigned char damage; //0 is perfect 255 is dead
101  MeshDrawContext( const Matrix &m ) : mat( m )
102  , CloakFX( 1, 1, 1, 1 )
103  , cloaked( NONE )
104  , damage( 0 )
105  {useXtraFX=false;}
106 };
107 using XMLSupport::EnumMap;
109 
110 #define NUM_MESH_SEQUENCE (5)
111 #define NUM_ZBUF_SEQ (4)
112 #define MESH_SPECIAL_FX_ONLY (3)
113 
125 class Mesh
126 {
127 private:
128 //make sure to only use TempGetTexture when xml-> is valid \|/
129  Texture * TempGetTexture( struct MeshXML*, int index, std::string factionname ) const;
130  Texture * TempGetTexture( struct MeshXML*, std::string filename, std::string factionname, GFXBOOL detail ) const;
133  void LoadXML( const char *filename,
134  const Vector &scale,
135  int faction,
136  class Flightgroup*fg,
137  bool orig,
138  const vector< string >&overrideTexture );
139  void LoadXML( VSFileSystem::VSFile&f,
140  const Vector &scale,
141  int faction,
142  class Flightgroup*fg,
143  bool orig,
144  const vector< string >&overrideTexture );
146  void LoadBinary( const char *filename, int faction );
148  void CreateLogos( struct MeshXML*, int faction, class Flightgroup *fg );
149  static void beginElement( void *userData, const XML_Char *name, const XML_Char **atts );
150  static void endElement( void *userData, const XML_Char *name );
151 
152  void beginElement( struct MeshXML *xml, const string &name, const AttributeList &attributes );
153  void endElement( struct MeshXML *xml, const string &name );
154 
155 protected:
156  void PostProcessLoading( struct MeshXML *xml, const vector< string > &overrideTexture );
157 
158 public:
159  void initTechnique( const string &technique );
160 
161  // Low-level access to the technique
162  TechniquePtr getTechnique() const { return technique; }
163  void setTechnique(TechniquePtr tech) { technique = tech; }
164 
165 private: Mesh( const char *filename, const Vector &scalex, int faction, class Flightgroup *fg, bool orig,
166  const std::vector< std::string > &textureOverride = std::vector< std::string > () );
167 
168 protected:
169 //only may be called from subclass. orig request may be denied if unit was in past usage. (not likely in the case where a unit must be constructed in orig)
170  Mesh( std::string filename, const Vector &scalex, int faction, class Flightgroup *fg, bool orig = false );
171 
173 public:
174  bool LoadExistant( Mesh *mesh );
175  bool LoadExistant( const string filehash, const Vector &scale, int faction );
176 protected:
183  int refcount;
188  float radialSize;
190  int numlods;
191  float framespersecond; //for animation
194  float lodsize;
204  unsigned int myMatNum;
208  vector< Texture* > Decal;
210  vector< Vector > detailPlanes;
217  bool convex;
218  unsigned char alphatest;
221 
223  vector< MeshDrawContext > *draw_queue;
227  string hash_name;
229  void InitUnit();
231  friend class OrigMeshContainer;
233  vector< MeshFX >LocalFX;
235  Mesh * getLOD( float lod, bool bBypassDamping = true );
236 
237 private:
239  void ProcessFixedDrawQueue( size_t whichpass, int whichdrawqueue, bool zsort, const QVector &sortctr );
240 
242  void ProcessShaderDrawQueue( size_t whichpass, int whichdrawqueue, bool zsort, const QVector &sortctr );
243 
245  void activateTextureUnit( const Technique::Pass::TextureUnit &tu, bool deflt = false );
246 
247 public: Mesh();
248  Mesh( const Mesh &m );
249 
250  GFXVertexList *getVertexList() const;
251  void setVertexList( GFXVertexList* _vlist );
252  float getFramesPerSecond() const;
253  float getCurrentFrame() const;
254  void setCurrentFrame( float );
255  int getNumAnimationFrames( const string &which = string() ) const;
256  int getNumLOD() const;
257  int getNumTextureFrames() const;
258  float getTextureFramesPerSecond() const;
259  double getTextureCumulativeTime() const;
260  void setTextureCumulativeTime( double );
261  bool getConvex() const
262  {
263  return this->convex;
264  }
265  void setConvex( bool b );
266  virtual int MeshType() const
267  {
268  return 0;
269  }
271  {
272  return blendSrc;
273  }
275  {
276  return blendDst;
277  }
279 //private:
280 //public:
281  static Mesh*LoadMesh( const char *filename, const Vector &scalex, int faction, class Flightgroup*fg,
282  const std::vector< std::string > &textureOverride = std::vector< std::string > () );
283  static vector< Mesh* >LoadMeshes( const char *filename, const Vector &scalex, int faction, class Flightgroup*fg,
284  const std::vector< std::string > &textureOverride = std::vector< std::string > () );
285  static vector< Mesh* >LoadMeshes( VSFileSystem::VSFile&f, const Vector &scalex, int faction, class Flightgroup*fg,
286  std::string hash_name,
287  const std::vector< std::string > &textureOverride = std::vector< std::string > () );
288 
290  void Fork( Mesh* &one, Mesh* &two, float a, float b, float c, float d );
292  virtual ~Mesh();
294  unsigned int numFX() const
295  {
296  return LocalFX.size();
297  }
299  void EnableSpecialFX();
300  unsigned int numTextures() const
301  {
302  return Decal.size();
303  }
304  Texture * texture( int i ) const
305  {
306  return Decal[i];
307  }
308  void SetBlendMode( BLENDFUNC src, BLENDFUNC dst, bool lodcascade = false );
310  void GetPolys( vector< mesh_polygon >& );
312  void SetMaterial( const GFXMaterial &mat );
313 //Gets the material back from the mesh.
314  const GFXMaterial& GetMaterial() const;
317  {
318  return will_be_drawn;
319  }
321  void UnDraw()
322  {
324  }
326  Vector const & Position() const
327  {
328  return local_pos;
329  }
331  void Draw( float lod,
332  const Matrix &m = identity_matrix,
333  float toofar = 1,
334  int cloak = -1,
335  float nebdist = 0,
336  unsigned char damage = 0,
337  bool renormalize_normals = false,
338  const MeshFX*mfx=NULL); //short fix
340  void DrawNow( float lod, bool centered, const Matrix &m = identity_matrix, int cloak = -1, float nebdist = 0 ); //short fix
342  virtual void ProcessDrawQueue( size_t whichpass, int whichdrawqueue, bool zsort, const QVector &sortctr );
344  virtual void SelectCullFace( int whichdrawqueue );
345  virtual void RestoreCullFace( int whichdrawqueue );
346  static void ProcessZFarMeshes( bool nocamerasetup = false );
348  static void ProcessUndrawnMeshes( bool pushSpecialEffects = false, bool nocamerasetup = false );
350  void forceCullFace( GFXBOOL newValue )
351  {
352  if (newValue) envMapAndLit = ( envMapAndLit&~(0x8|0x4) );
353  if (!newValue) envMapAndLit = ( envMapAndLit&~(0x4|0x8) );
354  }
356  {
357  return (envMapAndLit&0x4) != 0;
358  }
360  {
361  return (envMapAndLit&0x8) != 0;
362  }
363  void setEnvMap( GFXBOOL newValue, bool lodcascade = false )
364  {
365  envMapAndLit = ( newValue ? (envMapAndLit|0x1) : ( envMapAndLit&(~0x1) ) );
366  if (lodcascade && orig)
367  for (int i = 0; i < numlods; i++)
368  orig[i].setEnvMap( newValue );
369  }
371  {
372  return (envMapAndLit&0x1) != 0;
373  }
374  void setLighting( GFXBOOL newValue, bool lodcascade = false )
375  {
376  envMapAndLit = ( newValue ? (envMapAndLit|0x2) : ( envMapAndLit&(~0x2) ) );
377  if (lodcascade && orig)
378  for (int i = 0; i < numlods; i++)
379  orig[i].setLighting( newValue );
380  }
382  {
383  return (envMapAndLit&0x2) != 0;
384  }
387  {
388  return mn;
389  }
391  {
392  return mx;
393  }
395  BoundingBox * getBoundingBox();
397  bool queryBoundingBox( const QVector &start, const float err ) const;
399  bool queryBoundingBox( const QVector &start, const QVector &end, const float err ) const;
401  float rSize() const
402  {
403  return radialSize;
404  }
405  virtual float clipRadialSize() const
406  {
407  return radialSize;
408  }
410  void UpdateFX( float ttime );
412  void AddDamageFX( const Vector &LocalPos, const Vector &LocalNorm, const float percentage, const GFXColor &color = GFXColor(
413  1,
414  1,
415  1,
416  1 ) );
417  void setVirtualBoundingBox( const Vector &mn, const Vector &mx, float rsize )
418  {
419  radialSize = rsize;
420  this->mn = mn;
421  this->mx = mx;
422  }
423 };
424 
425 #endif
426