Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Solid.h
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: Solid.h
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Classes for rendering solid meshes of polygons
13 */
14 
15 #ifndef Solid_h
16 #define Solid_h
17 
18 #include "Polygon.h"
19 #include "Graphic.h"
20 #include "Video.h"
21 #include "List.h"
22 
23 // +--------------------------------------------------------------------+
24 
25 class Solid;
26 class Model;
27 class ModelFile;
28 class Surface;
29 class Segment;
30 class Shadow;
31 class Light;
32 
33 class OPCODE_data; // for collision detection
34 
35 // +--------------------------------------------------------------------+
36 
37 class Solid : public Graphic
38 {
39 public:
40  static const char* TYPENAME() { return "Solid"; }
41 
42  enum { NAMELEN = 64 };
43 
44  static bool IsCollisionEnabled();
45  static void EnableCollision(bool enable);
46 
47  Solid();
48  virtual ~Solid();
49 
50  // operations
51  virtual void Render(Video* video, DWORD flags);
52  virtual void SelectDetail(Projector* p);
53  virtual void ProjectScreenRect(Projector* p);
54  virtual void Update();
55 
56  // accessors / mutators
57  Model* GetModel() const { return model; }
58  void GetAllTextures(List<Bitmap>& textures);
59 
60  virtual bool IsDynamic() const;
61  virtual void SetDynamic(bool d);
62  virtual void SetLuminous(bool l);
63  virtual void SetOrientation(const Matrix& o);
64  virtual void SetOrientation(const Solid& match);
65  const Matrix& Orientation() const { return orientation; }
66  float Roll() const { return roll; }
67  float Pitch() const { return pitch; }
68  float Yaw() const { return yaw; }
69  virtual bool IsSolid() const { return true; }
70 
71  // stencil shadows
72  virtual void CreateShadows(int nlights=1);
73  virtual void UpdateShadows(List<Light>& lights);
75 
76  bool Load(const char* mag_file, double scale=1.0);
77  bool Load(ModelFile* loader, double scale=1.0);
78  void UseModel(Model* model);
79  void ClearModel();
80  bool Rescale(double scale);
81 
82  // collision detection
83  virtual int CollidesWith(Graphic& o);
84  virtual int CheckRayIntersection(Point pt, Point vpn, double len, Point& ipt,
85  bool treat_translucent_polys_as_solid=true);
86  virtual Poly* GetIntersectionPoly() const { return intersection_poly; }
87 
88  // buffer management
89  virtual void DeletePrivateData();
90  virtual void InvalidateSurfaceData();
91  virtual void InvalidateSegmentData();
92 
93 protected:
95  bool own_model;
96 
97  float roll, pitch, yaw;
100 
102 };
103 
104 // +--------------------------------------------------------------------+
105 
106 class Model
107 {
108  friend class Solid;
109  friend class ModelFile;
110 
111 public:
112  static const char* TYPENAME() { return "Model"; }
113 
114  enum { MAX_VERTS = 64000, MAX_POLYS = 16000 };
115 
116  Model();
117  Model(const Model& m);
118  ~Model();
119 
120  Model& operator = (const Model& m);
121  int operator == (const Model& that) const { return this == &that; }
122 
123  bool Load(const char* mag_file, double scale=1.0);
124  bool Load(ModelFile* loader, double scale=1.0);
125 
126  const char* Name() const { return name; }
127  int NumVerts() const { return nverts; }
128  int NumSurfaces() const { return surfaces.size(); }
129  int NumMaterials() const { return materials.size(); }
130  int NumPolys() const { return npolys; }
131  int NumSegments() const;
132  double Radius() const { return radius; }
133  bool IsDynamic() const { return dynamic; }
134  void SetDynamic(bool d) { dynamic = d; }
135  bool IsLuminous() const { return luminous; }
136  void SetLuminous(bool l) { luminous = l; }
137 
138  List<Surface>& GetSurfaces() { return surfaces; }
139  List<Material>& GetMaterials() { return materials; }
140  const Material* FindMaterial(const char* mtl_name) const;
141  const Material* ReplaceMaterial(const Material* mtl);
142  void GetAllTextures(List<Bitmap>& textures);
143 
144  Poly* AddPolys(int nsurf, int npolys, int nverts);
145  void ExplodeMesh();
146  void OptimizeMesh();
147  void OptimizeMaterials();
148  void ScaleBy(double factor);
149 
150  void Normalize();
151  void SelectPolys(List<Poly>&, Material* mtl);
152  void SelectPolys(List<Poly>&, Vec3 loc);
153 
154  void AddSurface(Surface* s);
155  void ComputeTangents();
156 
157  // buffer management
158  void DeletePrivateData();
159 
160 private:
161  bool LoadMag5(BYTE* block, int len, double scale);
162  bool LoadMag6(BYTE* block, int len, double scale);
163 
164  char name[Solid::NAMELEN];
165  List<Surface> surfaces;
166  List<Material> materials;
167  int nverts;
168  int npolys;
169  float radius;
170  float extents[6];
171  bool luminous;
172  bool dynamic;
173 };
174 
175 // +--------------------------------------------------------------------+
176 
177 class Surface
178 {
179  friend class Solid;
180  friend class Model;
181 
182 public:
183  static const char* TYPENAME() { return "Surface"; }
184 
185  enum { HIDDEN=1, LOCKED=2, SIMPLE=4, MAX_VERTS=64000, MAX_POLYS=16000 };
186 
187  Surface();
188  ~Surface();
189 
190  int operator == (const Surface& s) const { return this == &s; }
191 
192  const char* Name() const { return name; }
193  int NumVerts() const { return vertex_set ? vertex_set->nverts : 0; }
194  int NumSegments() const { return segments.size(); }
195  int NumPolys() const { return npolys; }
196  int NumIndices() const { return nindices; }
197  bool IsHidden() const { return state & HIDDEN ? true : false; }
198  bool IsLocked() const { return state & LOCKED ? true : false; }
199  bool IsSimplified() const { return state & SIMPLE ? true : false; }
200 
201  Model* GetModel() const { return model; }
202  List<Segment>& GetSegments() { return segments; }
203  const Point& GetOffset() const { return offset; }
204  const Matrix& GetOrientation() const { return orientation; }
205  double Radius() const { return radius; }
206  VertexSet* GetVertexSet() const { return vertex_set; }
207  Vec3* GetVLoc() const { return vloc; }
208  Poly* GetPolys() const { return polys; }
209 
210  void SetName(const char* n);
211  void SetHidden(bool b);
212  void SetLocked(bool b);
213  void SetSimplified(bool b);
214 
215  void CreateVerts(int nverts);
216  void CreatePolys(int npolys);
217  void AddIndices(int n) { nindices += n; }
218  Poly* AddPolys(int npolys, int nverts);
219 
220  VideoPrivateData* GetVideoPrivateData() const { return video_data; }
222  { video_data = vpd; }
223 
224  void ScaleBy(double factor);
225 
226  void BuildHull();
227  void Normalize();
228  void SelectPolys(List<Poly>&, Material* mtl);
229  void SelectPolys(List<Poly>&, Vec3 loc);
230 
232  void ComputeTangents();
233  void CalcGradients(Poly& p, Vec3& tangent, Vec3& binormal);
234 
235  void Copy(Surface& s, Model* m);
236  void OptimizeMesh();
237  void ExplodeMesh();
238 
239 private:
240  char name[Solid::NAMELEN];
241  Model* model;
242  VertexSet* vertex_set; // for rendering
243  Vec3* vloc; // for shadow hull
244  float radius;
245  int nhull;
246  int npolys;
247  int nindices;
248  int state;
249  Poly* polys;
250  List<Segment> segments;
251 
252  Point offset;
253  Matrix orientation;
254 
255 public:
257 
258 private:
259  VideoPrivateData* video_data;
260 };
261 
262 // +--------------------------------------------------------------------+
263 
264 class Segment
265 {
266 public:
267  static const char* TYPENAME() { return "Segment"; }
268 
269  Segment();
270  Segment(int n, Poly* p, Material* mtl, Model* mod=0);
271  ~Segment();
272 
273  bool IsSolid() const { return material ? material->IsSolid() : true; }
274  bool IsTranslucent() const { return material ? material->IsTranslucent(): false; }
275  bool IsGlowing() const { return material ? material->IsGlowing() : false; }
276 
279  { video_data = vpd; }
280 
281  int npolys;
286 };
287 
288 // +--------------------------------------------------------------------+
289 
291 {
292 public:
293  ModelFile(const char* fname);
294  virtual ~ModelFile();
295 
296  virtual bool Load(Model* m, double scale=1.0);
297  virtual bool Save(Model* m);
298 
299 protected:
300  char filename[256];
302 
303  // internal accessors:
304  char* pname;
305  int* pnverts;
306  int* pnpolys;
307  float* pradius;
308 };
309 
310 // +--------------------------------------------------------------------+
311 
312 #endif Solid_h
313