fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / 3DS / OSG3DSLoader.h
blob8ce70b2d01943f273228d66ec90aaa45d65ba33b
1 // copyright (c) 2001-2002 Lev Povalahev
2 // this is a 3ds importer version 2
4 #ifndef L3DS_H
5 #define L3DS_H
7 // includes
8 #include <vector>
9 #include <string>
10 #include <iostream>
12 //---------------------------------------------------------
14 typedef unsigned long ulong;
15 typedef unsigned int uint;
16 typedef unsigned char byte;
18 enum LShading {sWireframe, sFlat, sGouraud, sPhong, sMetal};
20 enum LOptimizationLevel {oNone, oSimple, oFull};
22 // for internal use
23 // forward declaration breaks decent STL implemtations
24 //struct LChunk;
25 //struct LTri;
27 //------------------------------------------------
29 /*! \ingroup GrpFileIO3DS
30 \ingroup GrpLibOSGFileIO
31 \nohierarchy
33 struct LVector4
35 float x;
36 float y;
37 float z;
38 float w;
41 /*! \ingroup GrpFileIO3DS
42 \ingroup GrpLibOSGFileIO
43 \nohierarchy
45 struct LVector3
47 float x;
48 float y;
49 float z;
52 /*! \ingroup GrpFileIO3DS
53 \ingroup GrpLibOSGFileIO
54 \nohierarchy
56 struct LVector2
58 float x;
59 float y;
62 /*! \ingroup GrpFileIO3DS
63 \ingroup GrpLibOSGFileIO
64 \nohierarchy
66 struct LColor3
68 float r;
69 float g;
70 float b;
73 //------------------------------------------------
75 // LChunk, LTri internal, but as the are used in a
76 // vector<LTri> declaration they cannot be incomplete
77 /*! \ingroup GrpFileIO3DS
78 \ingroup GrpLibOSGFileIO
79 \nohierarchy
81 struct LChunk
83 unsigned short id;
84 uint start;
85 uint end;
88 /*! \ingroup GrpFileIO3DS
89 \ingroup GrpLibOSGFileIO
90 \nohierarchy
92 struct LTri
94 unsigned short a;
95 unsigned short b;
96 unsigned short c;
97 ulong smoothingGroups;
98 LVector3 normal;
99 LVector3 tangent;
100 LVector3 binormal;
101 uint materialId;
104 /*! \ingroup GrpFileIO3DS
105 \ingroup GrpLibOSGFileIO
106 \nohierarchy
108 struct LTriangle
110 unsigned short a;
111 unsigned short b;
112 unsigned short c;
115 /*! \ingroup GrpFileIO3DS
116 \ingroup GrpLibOSGFileIO
117 \nohierarchy
119 struct LMatrix4
121 float _11, _12, _13, _14;
122 float _21, _22, _23, _24;
123 float _31, _32, _33, _34;
124 float _41, _42, _43, _44;
127 /*! \ingroup GrpFileIO3DS
128 \ingroup GrpLibOSGFileIO
129 \nohierarchy
131 struct LTriangle2
133 LVector4 vertices[3];
134 LVector3 vertexNormals[3];
135 LVector2 textureCoords[3];
136 LVector3 faceNormal;
137 uint materialId;
140 // a structure for a texture map
141 /*! \ingroup GrpFileIO3DS
142 \ingroup GrpLibOSGFileIO
143 \nohierarchy
145 struct LMap
147 // the strength of the texture map
148 float strength;
149 // the file name of the map. only 8.3 format in 3ds files :(
150 char mapName[255];
151 float uScale;
152 float vScale;
153 float uOffset;
154 float vOffset;
155 float angle;
156 short tiling;
159 //------------------------------------------------
161 /*! \ingroup GrpFileIO3DS
162 \ingroup GrpLibOSGFileIO
163 \nohierarchy
165 class LObject
167 public:
168 // the default constructor, initilializes the m_name here
169 LObject();
170 // the destructor frees memory (m_name)
171 virtual ~LObject();
172 // call this to get the name of the object
173 virtual const std::string& GetName();
175 // this methods should not be used by the "user", they're used internally to fill the class
176 // with valid data when reading from file. If you're about to add an importer for another format you'LL
177 // have to use these methods
178 // call this to set the name of the object
179 virtual void SetName(const std::string& value);
180 // returns true if the object's name is the name passed as parameter
181 bool IsObject(const std::string &name);
182 protected:
183 // the name of the object
184 std::string m_name;
187 //------------------------------------------------
189 /*! \ingroup GrpFileIO3DS
190 \ingroup GrpLibOSGFileIO
191 \nohierarchy
193 class LMaterial : public LObject
195 public:
196 // the default constructor, does the initialization
197 LMaterial();
198 // the destructor
199 virtual ~LMaterial();
200 // returns the material ID
201 uint GetID();
202 // returns the pointer to teh texture map 1
203 LMap& GetTextureMap1();
204 // returns the pointer to the texture map 2
205 LMap& GetTextureMap2();
206 // returns the pointer to teh opacity map
207 LMap& GetOpacityMap();
208 // returns the pointer to the specular (gloss) map
209 LMap& GetSpecularMap();
210 // returns the pointer to the bump map
211 LMap& GetBumpMap();
212 // returns the pointer to the reflection map
213 LMap& GetReflectionMap();
214 // returns the ambient color of the material
215 LColor3 GetAmbientColor();
216 // returns the diffuse color of the material
217 LColor3 GetDiffuseColor();
218 // returns the specular color of the material
219 LColor3 GetSpecularColor();
220 // returns the shininess of the material, ranging from 0(matte) to 1(shiny)
221 float GetShininess();
222 // returns the transparency of the material, ranging from 1(fully transparent) to 0(opaque)
223 float GetTransparency();
224 // returns the type of shading, see LShading type
225 LShading GetShadingType();
227 // this methods should not be used by the "user", they're used internally to fill the class
228 // with valid data when reading from file. If you're about to add an importer for another format you'LL
229 // have to use these methods
230 // sets the material ID to "value"
231 void SetID(uint value);
232 // call this to set the ambient color of the material
233 void SetAmbientColor(const LColor3 &color);
234 // sets the diffuse color of the material
235 void SetDiffuseColor(const LColor3 &color);
236 // sets the specular color of the material
237 void SetSpecularColor(const LColor3 &color);
238 // sets the shininess of the material
239 void SetShininess(float value);
240 // sets the transparency of the material
241 void SetTransparency(float value);
242 // sets the shading type
243 void SetShadingType(LShading shading);
244 protected:
245 // the unique material ID
246 int m_id;
247 // the first texture map
248 LMap m_texMap1;
249 // the second texture map
250 LMap m_texMap2;
251 // the opacity map
252 LMap m_opacMap;
253 // the reflection map
254 LMap m_reflMap;
255 // the bump map
256 LMap m_bumpMap;
257 // specular map
258 LMap m_specMap;
259 // material ambient color
260 LColor3 m_ambient;
261 // material diffuse color
262 LColor3 m_diffuse;
263 // material specular color
264 LColor3 m_specular;
265 // shininess
266 float m_shininess;
267 // transparency
268 float m_transparency;
269 // the shading type for the material
270 LShading m_shading;
273 //------------------------------------------------
275 /*! \ingroup GrpFileIO3DS
276 \ingroup GrpLibOSGFileIO
277 \nohierarchy
279 class LMesh : public LObject
281 public:
282 // the default constructor
283 LMesh();
284 // the destructor
285 virtual ~LMesh();
286 // clears the mesh, deleteing all data
287 void Clear();
288 // returns the number of vertices in the mesh
289 uint GetVertexCount();
290 // sets the the size fo the vertex array - for internal use
291 void SetVertexArraySize(uint value);
292 // returns the number of triangles in the mesh
293 uint GetTriangleCount();
294 // sets the size of the triangle array - for internal use
295 void SetTriangleArraySize(uint value);
296 // returns given vertex
297 const LVector4& GetVertex(uint index);
298 // returns the given normal
299 const LVector3& GetNormal(uint index);
300 // returns the given texture coordinates vector
301 const LVector2& GetUV(uint index);
302 // returns the pointer to the array of tangents
303 const LVector3& GetTangent(uint index);
304 // returns the pointer to the array of binormals
305 const LVector3& GetBinormal(uint index);
306 // sets the vertex at a given index to "vec" - for internal use
307 void SetVertex(const LVector4 &vec, uint index);
308 // sets the normal at a given index to "vec" - for internal use
309 void SetNormal(const LVector3 &vec, uint index);
310 // sets the texture coordinates vector at a given index to "vec" - for internal use
311 void SetUV(const LVector2 &vec, uint index);
312 // sets the tangent at a given index to "vec" - for internal use
313 void SetTangent(const LVector3 &vec, uint index);
314 // sets the binormal at a given index to "vec" - for internal use
315 void SetBinormal(const LVector3 &vec, uint index);
316 // returns the triangle with a given index
317 const LTriangle& GetTriangle(uint index);
318 // returns the triangle with a given index, see LTriangle2 structure description
319 LTriangle2 GetTriangle2(uint index);
320 // returns the mesh matrix, should be identity matrix after loading
321 LMatrix4 GetMatrix();
322 // sets the mesh matrix to a given matrix - for internal use
323 void SetMatrix(LMatrix4 m);
324 // optimizises the mesh using a given optimization level
325 void Optimize(LOptimizationLevel value);
326 // sets an internal triangle structure with index "index" - for internal use only
327 void SetTri(const LTri &tri, uint index);
328 // returns the pointer to the internal triangle structure - for internal use only
329 LTri& GetTri(uint index);
330 // returns the material id with a given index for the mesh
331 uint GetMaterial(uint index);
332 // adds a material to the mesh and returns its index - for internal use
333 uint AddMaterial(uint id);
334 // returns the number of materials used in the mesh
335 uint GetMaterialCount();
336 protected:
337 // the vertices, normals, etc.
338 std::vector<LVector4> m_vertices;
339 std::vector<LVector3> m_normals;
340 std::vector<LVector3> m_binormals;
341 std::vector<LVector3> m_tangents;
342 std::vector<LVector2> m_uv;
344 // triangles
345 std::vector<LTriangle> m_triangles;
347 //used internally
348 std::vector<LTri> m_tris;
350 // the transformation matrix.
351 LMatrix4 m_matrix;
353 // the material ID array
354 std::vector<uint> m_materials;
356 // calculates the normals, either using the smoothing groups information or not
357 void CalcNormals(bool useSmoothingGroups);
358 // calculates the texture(tangent) space for each vertex
359 void CalcTextureSpace();
360 // transforms the vertices by the mesh matrix
361 void TransformVertices();
364 //------------------------------------------------
366 /*! \ingroup GrpFileIO3DS
367 \ingroup GrpLibOSGFileIO
368 \nohierarchy
370 class LCamera : public LObject
372 public:
373 // the default constructor
374 LCamera();
375 // the destructor
376 virtual ~LCamera();
377 // clears the data the class holds
378 void Clear();
379 // sets the position of the camera - for internal use
380 void SetPosition(LVector3 vec);
381 // returns the position of the camera
382 LVector3 GetPosition();
383 // sets the target of the camera - internal use
384 void SetTarget(LVector3 target);
385 // returns the target of the camera
386 LVector3 GetTarget();
387 // sets the fov - internal use
388 void SetFOV(float value);
389 // returns the fov
390 float GetFOV();
391 // sets the bank - internal use
392 void SetBank(float value);
393 // returns the bank
394 float GetBank();
395 // sets the near plane - internal use
396 void SetNearplane(float value);
397 // returns the near plane distance
398 float GetNearplane();
399 // sets the far plane - internal use
400 void SetFarplane(float value);
401 // returns the far plane distance
402 float GetFarplane();
403 protected:
404 LVector3 m_pos;
405 LVector3 m_target;
406 float m_bank;
407 float m_fov;
408 float m_near;
409 float m_far;
412 //------------------------------------------------
414 /*! \ingroup GrpFileIO3DS
415 \ingroup GrpLibOSGFileIO
416 \nohierarchy
418 class LLight : public LObject
420 public:
421 // the default constructor
422 LLight();
423 // the destructor
424 virtual ~LLight();
425 // clears the data the class holds
426 void Clear();
427 // sets the position of the light source - for internal use
428 void SetPosition(LVector3 vec);
429 // returns the position of the light source
430 LVector3 GetPosition();
431 // sets the color of the light - for internal use
432 void SetColor(LColor3 color);
433 // returns the color of the light
434 LColor3 GetColor();
435 // sets whether the light is a spotlight or not - internal use
436 void SetSpotlight(bool value);
437 // returns true if the light is a spotlight
438 bool GetSpotlight();
439 // sets the target of the light - internal use
440 void SetTarget(LVector3 target);
441 // returns the target of the spotlight
442 LVector3 GetTarget();
443 // sets the hotspot - internal use
444 void SetHotspot(float value);
445 // returns the hotspot
446 float GetHotspot();
447 // sets falloff - internal use
448 void SetFalloff(float value);
449 // returns falloff
450 float GetFalloff();
451 // sets attenuationstart - internal use
452 void SetAttenuationstart(float value);
453 // returns attenuationstart
454 float GetAttenuationstart();
455 // sets attenuationend - internal use
456 void SetAttenuationend(float value);
457 // returns attenuationend
458 float GetAttenuationend();
459 protected:
460 LVector3 m_pos;
461 LColor3 m_color;
462 bool m_spotlight;
463 LVector3 m_target;
464 float m_hotspot;
465 float m_falloff;
466 float m_attenuationstart;
467 float m_attenuationend;
470 //------------------------------------------------
472 /*! \ingroup GrpFileIO3DS
473 \ingroup GrpLibOSGFileIO
474 \nohierarchy
476 class LImporter
478 public:
479 // the default constructor
480 LImporter();
481 // the destructor
482 virtual ~LImporter();
483 // reads the model from a file, must be overriden by the child classes
484 virtual bool Load(std::istream &is) = 0;
485 virtual bool Load(const char *filename) = 0;
486 // returns the number of meshes in the scene
487 uint GetMeshCount();
488 // returns the number of lights in the scene
489 uint GetLightCount();
490 // returns the number of materials in the scene
491 uint GetMaterialCount();
492 // returns the number of cameras in the scene
493 uint GetCameraCount();
494 // returns a pointer to a mesh
495 LMesh& GetMesh(uint index);
496 // returns a pointer to a camera at a given index
497 LCamera& GetCamera(uint index);
498 // returns a pointer to a light at a given index
499 LLight& GetLight(uint index);
500 // returns the pointer to the material
501 LMaterial& GetMaterial(uint index);
502 // returns the pointer to the material with a given name, or NULL if the material was not found
503 LMaterial* FindMaterial(const std::string &name);
504 // returns the pointer to the mesh with a given name, or NULL if the mesh with such name
505 // is not present in the scene
506 LMesh* FindMesh(const std::string &name);
507 // returns the pointer to the camera with a given name, or NULL if not found
508 LLight* FindCamera(const std::string &name);
509 // returns the pointer to the light with a given name, or NULL if not found
510 LLight* FindLight(const std::string &name);
511 // sets the optimization level to a given value
512 void SetOptimizationLevel(LOptimizationLevel value);
513 // returns the current optimization level
514 LOptimizationLevel GetOptimizationLevel();
515 protected:
516 // the cameras found in the scene
517 std::vector<LCamera> m_cameras;
518 // the lights found in the scene
519 std::vector<LLight> m_lights;
520 // triangular meshes
521 std::vector<LMesh> m_meshes;
522 // the materials in the scene
523 std::vector<LMaterial> m_materials;
524 // level of optimization to perform on the meshes
525 LOptimizationLevel m_optLevel;
526 // clears all data.
527 virtual void Clear();
529 //------------------------------------------------
531 /*! \ingroup GrpFileIO3DS
532 \ingroup GrpLibOSGFileIO
533 \nohierarchy
535 class L3DS : public LImporter
537 public:
538 // the default contructor
539 L3DS();
540 // constructs the object and loads the file
541 L3DS(const char *filename);
542 // destructor
543 virtual ~L3DS();
544 // load 3ds file
545 virtual bool Load(std::istream &is);
546 virtual bool Load(const char *filename);
547 protected:
548 // used internally for reading
549 char m_objName[100];
550 // true if end of file is reached
551 bool m_eof;
552 // buffer for loading, used for speedup
553 unsigned char *m_buffer;
554 // the size of the buffer
555 uint m_bufferSize;
556 // the current cursor position in the buffer
557 uint m_pos;
559 // reads a short value from the buffer
560 short ReadShort();
561 // reads an int value from the buffer
562 int ReadInt();
563 // reads a char from the buffer
564 char ReadChar();
565 //reada a floatvalue from the buffer
566 float ReadFloat();
567 //reads an unsigned byte from the buffer
568 byte ReadByte();
569 //reads an asciiz string
570 int ReadASCIIZ(char *buf, int max_count);
571 // seek wihtin the buffer
572 void Seek(int offset, int origin);
573 // returns the position of the cursor
574 uint Pos();
576 // read the chunk and return it.
577 LChunk ReadChunk();
578 // read until given chunk is found
579 bool FindChunk(LChunk &target, const LChunk &parent);
580 // skip to the end of chunk "chunk"
581 void SkipChunk(const LChunk &chunk);
582 // goes to the beginning of the data in teh given chunk
583 void GotoChunk(const LChunk &chunk);
585 // the function read the color chunk (any of the color chunks)
586 LColor3 ReadColor(const LChunk &chunk);
587 // the function that read the percentage chunk and returns a float from 0 to 1
588 float ReadPercentage(const LChunk &chunk);
589 // this is where 3ds file is being read
590 bool Read3DS();
591 // read a light chunk
592 void ReadLight(const LChunk &parent);
593 // read a camera chunk
594 void ReadCamera(const LChunk &parent);
595 // read a trimesh chunk
596 void ReadMesh(const LChunk &parent);
597 // reads the face list, face materials, smoothing groups... and fill rthe information into the mesh
598 void ReadFaceList(const LChunk &chunk, LMesh &mesh);
599 // reads the material
600 void ReadMaterial(const LChunk &parent);
601 // reads the map info and fills the given map with this information
602 void ReadMap(const LChunk &chunk, LMap& map);
603 // reads keyframer data of the OBJECT_NODE_TAG chunk
604 void ReadKeyframeData(const LChunk &parent);
605 // reads the keyheader structure from the current offset and returns the frame number
606 long ReadKeyheader();
608 private:
610 L3DS(const L3DS &other);
611 void operator =(const L3DS &rhs);
615 //---------------------------------------------------------
617 #endif