1 // copyright (c) 2001-2002 Lev Povalahev
2 // this is a 3ds importer version 2
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
};
23 // forward declaration breaks decent STL implemtations
27 //------------------------------------------------
29 /*! \ingroup GrpFileIO3DS
30 \ingroup GrpLibOSGFileIO
41 /*! \ingroup GrpFileIO3DS
42 \ingroup GrpLibOSGFileIO
52 /*! \ingroup GrpFileIO3DS
53 \ingroup GrpLibOSGFileIO
62 /*! \ingroup GrpFileIO3DS
63 \ingroup GrpLibOSGFileIO
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
88 /*! \ingroup GrpFileIO3DS
89 \ingroup GrpLibOSGFileIO
97 ulong smoothingGroups
;
104 /*! \ingroup GrpFileIO3DS
105 \ingroup GrpLibOSGFileIO
115 /*! \ingroup GrpFileIO3DS
116 \ingroup GrpLibOSGFileIO
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
133 LVector4 vertices
[3];
134 LVector3 vertexNormals
[3];
135 LVector2 textureCoords
[3];
140 // a structure for a texture map
141 /*! \ingroup GrpFileIO3DS
142 \ingroup GrpLibOSGFileIO
147 // the strength of the texture map
149 // the file name of the map. only 8.3 format in 3ds files :(
159 //------------------------------------------------
161 /*! \ingroup GrpFileIO3DS
162 \ingroup GrpLibOSGFileIO
168 // the default constructor, initilializes the m_name here
170 // the destructor frees memory (m_name)
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
);
183 // the name of the object
187 //------------------------------------------------
189 /*! \ingroup GrpFileIO3DS
190 \ingroup GrpLibOSGFileIO
193 class LMaterial
: public LObject
196 // the default constructor, does the initialization
199 virtual ~LMaterial();
200 // returns the material ID
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
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
);
245 // the unique material ID
247 // the first texture map
249 // the second texture map
253 // the reflection map
259 // material ambient color
261 // material diffuse color
263 // material specular color
268 float m_transparency
;
269 // the shading type for the material
273 //------------------------------------------------
275 /*! \ingroup GrpFileIO3DS
276 \ingroup GrpLibOSGFileIO
279 class LMesh
: public LObject
282 // the default constructor
286 // clears the mesh, deleteing all data
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();
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
;
345 std::vector
<LTriangle
> m_triangles
;
348 std::vector
<LTri
> m_tris
;
350 // the transformation 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
370 class LCamera
: public LObject
373 // the default constructor
377 // clears the data the class holds
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
);
391 // sets the bank - internal use
392 void SetBank(float value
);
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
412 //------------------------------------------------
414 /*! \ingroup GrpFileIO3DS
415 \ingroup GrpLibOSGFileIO
418 class LLight
: public LObject
421 // the default constructor
425 // clears the data the class holds
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
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
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
447 // sets falloff - internal use
448 void SetFalloff(float value
);
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();
466 float m_attenuationstart
;
467 float m_attenuationend
;
470 //------------------------------------------------
472 /*! \ingroup GrpFileIO3DS
473 \ingroup GrpLibOSGFileIO
479 // the default constructor
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
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();
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
;
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
;
527 virtual void Clear();
529 //------------------------------------------------
531 /*! \ingroup GrpFileIO3DS
532 \ingroup GrpLibOSGFileIO
535 class L3DS
: public LImporter
538 // the default contructor
540 // constructs the object and loads the file
541 L3DS(const char *filename
);
545 virtual bool Load(std::istream
&is
);
546 virtual bool Load(const char *filename
);
548 // used internally for reading
550 // true if end of file is reached
552 // buffer for loading, used for speedup
553 unsigned char *m_buffer
;
554 // the size of the buffer
556 // the current cursor position in the buffer
559 // reads a short value from the buffer
561 // reads an int value from the buffer
563 // reads a char from the buffer
565 //reada a floatvalue from the buffer
567 //reads an unsigned byte from the buffer
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
576 // read the chunk and return it.
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
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();
610 L3DS(const L3DS
&other
);
611 void operator =(const L3DS
&rhs
);
615 //---------------------------------------------------------