Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
M3DS.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: M3DS.h
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Window class
13 */
14 
15 // parts copyright (c) 2001-2002 Lev Povalahev
16 
17 #ifndef M3DS_H
18 #define M3DS_H
19 
20 #include "List.h"
21 
22 // +--------------------------------------------------------------------+
23 
24 struct Chunk
25 {
26  WORD id;
27  DWORD len;
28  BYTE* start;
29 
31 };
32 
33 // +--------------------------------------------------------------------+
34 
35 struct LTriangle
36 {
37  WORD a,b,c;
38 };
39 
40 struct LTriangle2
41 {
46  DWORD materialId;
47 };
48 
49 struct TextureMap
50 {
51  char fname[256];
52  float strength;
53  float u_scale;
54  float v_scale;
55  float u_offset;
56  float v_offset;
57  float angle;
58 };
59 
60 // +--------------------------------------------------------------------+
61 
62 class LObject
63 {
64 public:
65  LObject();
66  virtual ~LObject();
67 
68  virtual const char* GetName();
69  virtual bool IsObject(const const char* name);
70 
71 protected:
73 };
74 
75 // +--------------------------------------------------------------------+
76 
77 class LMaterial : public LObject
78 {
79 public:
80  LMaterial();
81  virtual ~LMaterial();
82 
83  DWORD GetID();
93  float GetShininess();
94  float GetTransparency();
95  DWORD GetShadingType();
96 
97  // this methods should not be used by the "user", they're used internally to fill the class
98  // with valid data when reading from file. If you're about to add an importer for another format you'LL
99  // have to use these methods
100 
101  void SetID(DWORD value);
102  void SetAmbientColor(const ColorValue &color);
103  void SetDiffuseColor(const ColorValue &color);
104  void SetSpecularColor(const ColorValue &color);
105  void SetShininess(float value);
106  void SetTransparency(float value);
107  void SetShadingType(DWORD shading);
108 
109 protected:
110  int id;
120  float shininess;
122  DWORD shading;
123 };
124 
125 // +--------------------------------------------------------------------+
126 
127 class LMesh : public LObject
128 {
129 public:
130  LMesh();
131  virtual ~LMesh();
132 
133  void Clear();
134 
135  DWORD GetVertexCount();
136  void SetVertexArraySize(DWORD value);
137  DWORD GetTriangleCount();
138  void SetTriangleArraySize(DWORD value);
139 
140  // returns given vertex
141  const LVector4& GetVertex(DWORD index);
142  // returns the given normal
143  const LVector3& GetNormal(DWORD index);
144  // returns the given texture coordinates vector
145  const LVector2& GetUV(DWORD index);
146  // returns the pointer to the array of tangents
147  const LVector3& GetTangent(DWORD index);
148  // returns the pointer to the array of binormals
149  const LVector3& GetBinormal(DWORD index);
150  // sets the vertex at a given index to "vec" - for internal use
151  void SetVertex(const LVector4 &vec, DWORD index);
152  // sets the normal at a given index to "vec" - for internal use
153  void SetNormal(const LVector3 &vec, DWORD index);
154  // sets the texture coordinates vector at a given index to "vec" - for internal use
155  void SetUV(const LVector2 &vec, DWORD index);
156  // sets the tangent at a given index to "vec" - for internal use
157  void SetTangent(const LVector3 &vec, DWORD index);
158  // sets the binormal at a given index to "vec" - for internal use
159  void SetBinormal(const LVector3 &vec, DWORD index);
160  // returns the triangle with a given index
161  const LTriangle& GetTriangle(DWORD index);
162  // returns the triangle with a given index, see LTriangle2 structure description
163  LTriangle2 GetTriangle2(DWORD index);
164  // returns the mesh matrix, should be identity matrix after loading
166  // sets the mesh matrix to a given matrix - for internal use
167  void SetMatrix(LMatrix4 m);
168  // optimizises the mesh using a given optimization level
169  void Optimize(LOptimizationLevel value);
170  // sets an internal triangle structure with index "index" - for internal use only
171  void SetTri(const LTri &tri, DWORD index);
172  // returns the pointer to the internal triangle structure - for internal use only
173  LTri& GetTri(DWORD index);
174  // returns the material id with a given index for the mesh
175  DWORD GetMaterial(DWORD index);
176  // adds a material to the mesh and returns its index - for internal use
177  DWORD AddMaterial(DWORD id);
178  // returns the number of materials used in the mesh
179  DWORD GetMaterialCount();
180 protected:
181  // the vertices, normals, etc.
187 
188  // triangles
190 
191  //used internally
193 
194  // the transformation matrix.
196 
197  // the material ID array
199 
200  // calculates the normals, either using the smoothing groups information or not
201  void CalcNormals(bool useSmoothingGroups);
202  // calculates the texture(tangent) space for each vertex
203  void CalcTextureSpace();
204  // transforms the vertices by the mesh matrix
205  void TransformVertices();
206 };
207 
208 //------------------------------------------------
209 
210 class LImporter
211 {
212 public:
213  // the default constructor
214  LImporter();
215  // the destructor
216  virtual ~LImporter();
217  // reads the model from a file, must be overriden by the child classes
218  virtual bool LoadFile(const char *filename) = 0;
219  // returns the number of meshes in the scene
220  DWORD GetMeshCount();
221  // returns the number of lights in the scene
222  DWORD GetLightCount();
223  // returns the number of materials in the scene
224  DWORD GetMaterialCount();
225  // returns a pointer to a mesh
226  LMesh& GetMesh(DWORD index);
227  // returns a pointer to a light at a given index
228  LLight& GetLight(DWORD index);
229  // returns the pointer to the material
230  LMaterial& GetMaterial(DWORD index);
231  // returns the pointer to the material with a given name, or NULL if the material was not found
232  LMaterial* FindMaterial(const Text &name);
233  // returns the pointer to the mesh with a given name, or NULL if the mesh with such name
234  // is not present in the scene
235  LMesh* FindMesh(const Text &name);
236  // returns the pointer to the light with a given name, or NULL if not found
237  LLight* FindLight(const Text &name);
238  // sets the optimization level to a given value
240  // returns the current optimization level
242 protected:
243  // the lights found in the scene
245  // triangular meshes
247  // the materials in the scene
249  // level of optimization to perform on the meshes
251  // clears all data.
252  virtual void Clear();
253 };
254 //------------------------------------------------
255 
256 class L3DS : public LImporter
257 {
258 public:
259  // the default contructor
260  L3DS();
261  // constructs the object and loads the file
262  L3DS(const char *filename);
263  // destructor
264  virtual ~L3DS();
265  // load 3ds file
266  virtual bool LoadFile(const char *filename);
267 protected:
268  // used internally for reading
269  char objName[100];
270  // true if end of file is reached
271  bool eof;
272  // buffer for loading, used for speedup
273  unsigned char *buffer;
274  // the size of the buffer
275  DWORD bufferSize;
276  // the current cursor position in the buffer
277  DWORD pos;
278 
279  // reads a short value from the buffer
280  short ReadShort();
281  // reads an int value from the buffer
282  int ReadInt();
283  // reads a char from the buffer
284  char ReadChar();
285  //reada a floatvalue from the buffer
286  float ReadFloat();
287  //reads an unsigned byte from the buffer
288  byte ReadByte();
289  //reads an asciiz string
290  int ReadASCIIZ(char *buf, int max_count);
291  // seek wihtin the buffer
292  void Seek(int offset, int origin);
293  // returns the position of the cursor
294  DWORD Pos();
295 
296  // read the chunk and return it.
297  LChunk ReadChunk();
298  // read until given chunk is found
299  bool FindChunk(LChunk &target, const LChunk &parent);
300  // skip to the end of chunk "chunk"
301  void SkipChunk(const LChunk &chunk);
302  // goes to the beginning of the data in teh given chunk
303  void GotoChunk(const LChunk &chunk);
304 
305  // the function read the color chunk (any of the color chunks)
306  ColorValue ReadColor(const LChunk &chunk);
307  // the function that read the percentage chunk and returns a float from 0 to 1
308  float ReadPercentage(const LChunk &chunk);
309  // this is where 3ds file is being read
310  bool Read3DS();
311  // read a light chunk
312  void ReadLight(const LChunk &parent);
313  // read a trimesh chunk
314  void ReadMesh(const LChunk &parent);
315  // reads the face list, face materials, smoothing groups... and fill rthe information into the mesh
316  void ReadFaceList(const LChunk &chunk, LMesh &mesh);
317  // reads the material
318  void ReadMaterial(const LChunk &parent);
319  // reads the map info and fills the given map with this information
320  void ReadMap(const LChunk &chunk, TextureMap& map);
321  // reads keyframer data of the OBJECT_NODE_TAG chunk
322  void ReadKeyframeData(const LChunk &parent);
323  // reads the keyheader structure from the current offset and returns the frame number
324  long ReadKeyheader();
325 };
326 
327 //---------------------------------------------------------
328 
329 #endif