Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
l3ds.h
Go to the documentation of this file.
1 // copyright (c) 2001-2002 Lev Povalahev
2 // this is a 3ds importer version 2
3 
4 #ifndef L3DS_H
5 #define L3DS_H
6 
7 // includes
8 #include <vector>
9 #include <string>
10 
11 //---------------------------------------------------------
12 
13 typedef unsigned int uint;
14 typedef unsigned char byte;
15 
17 
19 
20 // for internal use
21 struct LChunk;
22 struct LTri;
23 
24 //------------------------------------------------
25 
26 struct LVector4
27 {
28  float x;
29  float y;
30  float z;
31  float w;
32 
33  int operator==(const LVector4& v) const { return x==v.x && y==v.y && z==v.z && w==v.w; }
34 };
35 
36 struct LVector3
37 {
38  float x;
39  float y;
40  float z;
41 
42  int operator==(const LVector3& v) const { return x==v.x && y==v.y && z==v.z; }
43 };
44 
45 struct LVector2
46 {
47  float x;
48  float y;
49 
50  int operator==(const LVector2& v) const { return x==v.x && y==v.y; }
51 };
52 
53 struct LColor3
54 {
55  float r;
56  float g;
57  float b;
58 
59  int operator==(const LColor3& v) const { return r==v.r && g==v.g && b==v.b; }
60 };
61 
62 //------------------------------------------------
63 
64 struct LTriangle
65 {
66  unsigned short a;
67  unsigned short b;
68  unsigned short c;
69 };
70 
71 struct LMatrix4
72 {
73  float m[4][4];
74 };
75 
76 struct LTriangle2
77 {
83 };
84 
85 // a structure for a texture map
86 struct LMap
87 {
88  // the strength of the texture map
89  float strength;
90  // the file name of the map. only 8.3 format in 3ds files :(
91  char mapName[255];
92  float uScale;
93  float vScale;
94  float uOffset;
95  float vOffset;
96  float angle;
97 };
98 
99 //------------------------------------------------
100 
101 class LObject
102 {
103 public:
104  // the default constructor, initilializes the m_name here
105  LObject();
106  // the destructor frees memory (m_name)
107  virtual ~LObject();
108  // call this to get the name of the object
109  virtual const std::string& GetName();
110 
111  // this methods should not be used by the "user", they're used internally to fill the class
112  // with valid data when reading from file. If you're about to add an importer for another format you'LL
113  // have to use these methods
114  // call this to set the name of the object
115  virtual void SetName(const std::string& value);
116  // returns true if the object's name is the name passed as parameter
117  bool IsObject(const std::string &name);
118 protected:
119  // the name of the object
120  std::string m_name;
121 };
122 
123 //------------------------------------------------
124 
125 class LMaterial : public LObject
126 {
127 public:
128  // the default constructor, does the initialization
129  LMaterial();
130  // the destructor
131  virtual ~LMaterial();
132  // returns the material ID
133  uint GetID();
134  // returns the pointer to teh texture map 1
135  LMap& GetTextureMap1();
136  // returns the pointer to the texture map 2
137  LMap& GetTextureMap2();
138  // returns the pointer to teh opacity map
139  LMap& GetOpacityMap();
140  // returns the pointer to the specular (gloss) map
141  LMap& GetSpecularMap();
142  // returns the pointer to the bump map
143  LMap& GetBumpMap();
144  // returns the pointer to the reflection map
146  // returns the ambient color of the material
148  // returns the diffuse color of the material
150  // returns the specular color of the material
152  // returns the shininess of the material, ranging from 0(matte) to 1(shiny)
153  float GetShininess();
154  // returns the transparency of the material, ranging from 1(fully transparent) to 0(opaque)
155  float GetTransparency();
156  // returns the type of shading, see LShading type
158 
159  // this methods should not be used by the "user", they're used internally to fill the class
160  // with valid data when reading from file. If you're about to add an importer for another format you'LL
161  // have to use these methods
162  // sets the material ID to "value"
163  void SetID(uint value);
164  // call this to set the ambient color of the material
165  void SetAmbientColor(const LColor3 &color);
166  // sets the diffuse color of the material
167  void SetDiffuseColor(const LColor3 &color);
168  // sets the specular color of the material
169  void SetSpecularColor(const LColor3 &color);
170  // sets the shininess of the material
171  void SetShininess(float value);
172  // sets the transparency of the material
173  void SetTransparency(float value);
174  // sets the shading type
176 protected:
177  // the unique material ID
178  int m_id;
179  // the first texture map
181  // the second texture map
183  // the opacity map
185  // the reflection map
187  // the bump map
189  // specular map
191  // material ambient color
193  // material diffuse color
195  // material specular color
197  // shininess
198  float m_shininess;
199  // transparency
201  // the shading type for the material
203 };
204 
205 //------------------------------------------------
206 
207 class LMesh : public LObject
208 {
209 public:
210  // the default constructor
211  LMesh();
212  // the destructor
213  virtual ~LMesh();
214  // clears the mesh, deleteing all data
215  void Clear();
216  // returns the number of vertices in the mesh
218  // sets the the size fo the vertex array - for internal use
219  void SetVertexArraySize(uint value);
220  // returns the number of triangles in the mesh
222  // sets the size of the triangle array - for internal use
223  void SetTriangleArraySize(uint value);
224  // returns given vertex
225  const LVector4& GetVertex(uint index);
226  // returns the given normal
227  const LVector3& GetNormal(uint index);
228  // returns the given texture coordinates vector
229  const LVector2& GetUV(uint index);
230  // returns the pointer to the array of tangents
231  const LVector3& GetTangent(uint index);
232  // returns the pointer to the array of binormals
233  const LVector3& GetBinormal(uint index);
234  // sets the vertex at a given index to "vec" - for internal use
235  void SetVertex(const LVector4 &vec, uint index);
236  // sets the normal at a given index to "vec" - for internal use
237  void SetNormal(const LVector3 &vec, uint index);
238  // sets the texture coordinates vector at a given index to "vec" - for internal use
239  void SetUV(const LVector2 &vec, uint index);
240  // sets the tangent at a given index to "vec" - for internal use
241  void SetTangent(const LVector3 &vec, uint index);
242  // sets the binormal at a given index to "vec" - for internal use
243  void SetBinormal(const LVector3 &vec, uint index);
244  // returns the triangle with a given index
245  const LTriangle& GetTriangle(uint index);
246  // returns the triangle with a given index, see LTriangle2 structure description
248  // returns the mesh matrix, should be identity matrix after loading
250  // sets the mesh matrix to a given matrix - for internal use
251  void SetMatrix(LMatrix4 m);
252  // optimizises the mesh using a given optimization level
253  void Optimize(LOptimizationLevel value);
254  // sets an internal triangle structure with index "index" - for internal use only
255  void SetTri(const LTri &tri, uint index);
256  // returns the pointer to the internal triangle structure - for internal use only
257  LTri& GetTri(uint index);
258  // returns the material id with a given index for the mesh
259  uint GetMaterial(uint index);
260  // adds a material to the mesh and returns its index - for internal use
261  uint AddMaterial(uint id);
262  // returns the number of materials used in the mesh
264 protected:
265  // the vertices, normals, etc.
266  std::vector<LVector4> m_vertices;
267  std::vector<LVector3> m_normals;
268  std::vector<LVector3> m_binormals;
269  std::vector<LVector3> m_tangents;
270  std::vector<LVector2> m_uv;
271 
272  // triangles
273  std::vector<LTriangle> m_triangles;
274 
275  //used internally
276  std::vector<LTri> m_tris;
277 
278  // the transformation matrix.
280 
281  // the material ID array
282  std::vector<uint> m_materials;
283 
284  // calculates the normals, either using the smoothing groups information or not
285  void CalcNormals(bool useSmoothingGroups);
286  // calculates the texture(tangent) space for each vertex
287  void CalcTextureSpace();
288  // transforms the vertices by the mesh matrix
289  void TransformVertices();
290 };
291 
292 //------------------------------------------------
293 
294 class LLight : public LObject
295 {
296 public:
297  // the default constructor
298  LLight();
299  // the destructor
300  virtual ~LLight();
301  // clears the data the class holds
302  void Clear();
303  // sets the position of the light source - for internal use
304  void SetPosition(LVector3 vec);
305  // returns the position of the light source
307  // sets the color of the light - for internal use
308  void SetColor(LColor3 color);
309  // returns the color of the light
310  LColor3 GetColor();
311  // sets whether the light is a spotlight or not - internal use
312  void SetSpotlight(bool value);
313  // returns true if the light is a spotlight
314  bool GetSpotlight();
315  // sets the target of the light - internal use
316  void SetTarget(LVector3 target);
317  // returns the target of the spotlight
319  // sets the hotspot - internal use
320  void SetHotspot(float value);
321  // returns the hotspot
322  float GetHotspot();
323  // sets falloff - internal use
324  void SetFalloff(float value);
325  // returns falloff
326  float GetFalloff();
327 protected:
332  float m_hotspot;
333  float m_falloff;
334 };
335 
336 //------------------------------------------------
337 
339 {
340 public:
341  // the default constructor
342  LImporter();
343  // the destructor
344  virtual ~LImporter();
345  // reads the model from a file, must be overriden by the child classes
346  virtual bool LoadFile(const char *filename) = 0;
347  // returns the number of meshes in the scene
348  uint GetMeshCount();
349  // returns the number of lights in the scene
351  // returns the number of materials in the scene
353  // returns a pointer to a mesh
354  LMesh& GetMesh(uint index);
355  // returns a pointer to a light at a given index
356  LLight& GetLight(uint index);
357  // returns the pointer to the material
358  LMaterial& GetMaterial(uint index);
359  // returns the pointer to the material with a given name, or NULL if the material was not found
360  LMaterial* FindMaterial(const std::string &name);
361  // returns the pointer to the mesh with a given name, or NULL if the mesh with such name
362  // is not present in the scene
363  LMesh* FindMesh(const std::string &name);
364  // returns the pointer to the light with a given name, or NULL if not found
365  LLight* FindLight(const std::string &name);
366  // sets the optimization level to a given value
368  // returns the current optimization level
370 protected:
371  // the lights found in the scene
372  std::vector<LLight> m_lights;
373  // triangular meshes
374  std::vector<LMesh> m_meshes;
375  // the materials in the scene
376  std::vector<LMaterial> m_materials;
377  // level of optimization to perform on the meshes
379  // clears all data.
380  virtual void Clear();
381 };
382 //------------------------------------------------
383 
384 class L3DS : public LImporter
385 {
386 public:
387  // the default contructor
388  L3DS();
389  // constructs the object and loads the file
390  L3DS(const char *filename);
391  // destructor
392  virtual ~L3DS();
393  // load 3ds file
394  virtual bool LoadFile(const char *filename);
395 protected:
396  // used internally for reading
397  char m_objName[100];
398  // true if end of file is reached
399  bool m_eof;
400  // buffer for loading, used for speedup
401  unsigned char *m_buffer;
402  // the size of the buffer
404  // the current cursor position in the buffer
406 
407  // reads a short value from the buffer
408  short ReadShort();
409  // reads an int value from the buffer
410  int ReadInt();
411  // reads a char from the buffer
412  char ReadChar();
413  //reada a floatvalue from the buffer
414  float ReadFloat();
415  //reads an unsigned byte from the buffer
416  byte ReadByte();
417  //reads an asciiz string
418  int ReadASCIIZ(char *buf, int max_count);
419  // seek wihtin the buffer
420  void Seek(int offset, int origin);
421  // returns the position of the cursor
422  uint Pos();
423 
424  // read the chunk and return it.
425  LChunk ReadChunk();
426  // read until given chunk is found
427  bool FindChunk(LChunk &target, const LChunk &parent);
428  // skip to the end of chunk "chunk"
429  void SkipChunk(const LChunk &chunk);
430  // goes to the beginning of the data in teh given chunk
431  void GotoChunk(const LChunk &chunk);
432 
433  // the function read the color chunk (any of the color chunks)
434  LColor3 ReadColor(const LChunk &chunk);
435  // the function that read the percentage chunk and returns a float from 0 to 1
436  float ReadPercentage(const LChunk &chunk);
437  // this is where 3ds file is being read
438  bool Read3DS();
439  // read a light chunk
440  void ReadLight(const LChunk &parent);
441  // read a trimesh chunk
442  void ReadMesh(const LChunk &parent);
443  // reads the face list, face materials, smoothing groups... and fill rthe information into the mesh
444  void ReadFaceList(const LChunk &chunk, LMesh &mesh);
445  // reads the material
446  void ReadMaterial(const LChunk &parent);
447  // reads the map info and fills the given map with this information
448  void ReadMap(const LChunk &chunk, LMap& map);
449  // reads keyframer data of the OBJECT_NODE_TAG chunk
450  void ReadKeyframeData(const LChunk &parent);
451  // reads the keyheader structure from the current offset and returns the frame number
452  long ReadKeyheader();
453 };
454 
455 //---------------------------------------------------------
456 
457 #endif