Main Page | Class Hierarchy | Compound List | File List | Compound Members

l3ds.h

00001 /*
00002   copyright (c) 2001-2002 Lev Povalahev
00003   this is a 3ds importer version 2
00004 
00005 
00006   file: l3ds.h
00007 */
00008 
00009 #pragma once
00010 
00011 // includes
00012 #include <vector>
00013 #include <string>
00014 
00015 //---------------------------------------------------------
00016 
00017 typedef unsigned int uint;
00018 typedef unsigned char byte;
00019 
00020 enum LShading {sWireframe, sFlat, sGouraud, sPhong, sMetal};
00021 
00022 enum LOptimizationLevel {oNone, oSimple, oFull};
00023 
00024 // for internal use
00025 struct LChunk;
00026 struct LTri;
00027 
00028 //------------------------------------------------
00029 
00030 struct LVector4
00031 {
00032     float x;
00033     float y;
00034     float z;
00035     float w;
00036 };
00037 
00038 struct LVector3
00039 {
00040     float x;
00041     float y;
00042     float z;
00043 };
00044 
00045 struct LVector2
00046 {
00047     float x;
00048     float y;
00049 };
00050 
00051 struct LColor3
00052 {   
00053     float r;
00054     float g;
00055     float b;
00056 };
00057 
00058 //------------------------------------------------
00059 
00060 struct LTriangle
00061 {
00062     unsigned short a;
00063     unsigned short b;
00064     unsigned short c;
00065 };
00066 
00067 struct LMatrix4
00068 {
00069     float _11, _12, _13, _14;
00070     float _21, _22, _23, _24;
00071     float _31, _32, _33, _34;
00072     float _41, _42, _43, _44;
00073 };
00074 
00075 struct LTriangle2
00076 {
00077     LVector4 vertices[3];
00078     LVector3 vertexNormals[3];
00079     LVector2 textureCoords[3];
00080     LVector3 faceNormal;
00081     uint materialId;
00082 };
00083 
00084 // a structure for a texture map
00085 struct LMap
00086 {
00087     // the strength of the texture map
00088     float strength;
00089     // the file name of the map. only 8.3 format in 3ds files :(
00090     char mapName[255];
00091     float uScale;
00092     float vScale;
00093     float uOffset;
00094     float vOffset;
00095     float angle;
00096 };
00097 
00098 //------------------------------------------------
00099 
00100 class LObject
00101 {
00102 public:
00103     // the default constructor, initilializes the m_name here
00104     LObject();
00105     // the destructor frees memory (m_name)
00106     virtual ~LObject();
00107     // call this to get the name of the object
00108     virtual const std::string& GetName();
00109     
00110     // this methods should not be used by the "user", they're used internally to fill the class
00111     // with valid data when reading from file. If you're about to add an importer for another format you'LL
00112     // have to use these methods
00113     // call this to set the name of the object
00114     virtual void SetName(const std::string& value);
00115     // returns true if the object's name is the name passed as parameter
00116     bool IsObject(const std::string &name);
00117 protected:
00118     // the name of the object
00119     std::string m_name;
00120 };
00121 
00122 //------------------------------------------------
00123 
00124 class LMaterial : public LObject
00125 {
00126 public:     
00127     // the default constructor, does the initialization
00128     LMaterial();
00129     // the destructor
00130     virtual ~LMaterial();
00131     // returns the material ID    
00132     uint GetID();
00133     // returns the pointer to teh texture map 1
00134     LMap& GetTextureMap1();
00135     // returns the pointer to the texture map 2
00136     LMap& GetTextureMap2();
00137     // returns the pointer to teh opacity map
00138     LMap& GetOpacityMap();
00139     // returns the pointer to the specular (gloss) map
00140     LMap& GetSpecularMap();
00141     // returns the pointer to the bump map
00142     LMap& GetBumpMap();
00143     // returns the pointer to the reflection map
00144     LMap& GetReflectionMap();
00145     // returns the ambient color of the material
00146     LColor3 GetAmbientColor();
00147     // returns the diffuse color of the material
00148     LColor3 GetDiffuseColor();
00149     // returns the specular color of the material
00150     LColor3 GetSpecularColor();
00151     // returns the shininess of the material, ranging from 0(matte) to 1(shiny)
00152     float GetShininess();
00153     // returns the transparency of the material, ranging from 1(fully transparent) to 0(opaque)
00154     float GetTransparency();
00155     // returns the type of shading, see LShading type
00156     LShading GetShadingType();
00157 
00158     // this methods should not be used by the "user", they're used internally to fill the class
00159     // with valid data when reading from file. If you're about to add an importer for another format you'LL
00160     // have to use these methods
00161     // sets the material ID to "value"
00162     void SetID(uint value);
00163     // call this to set the ambient color of the material
00164     void SetAmbientColor(const LColor3 &color);
00165     // sets the diffuse color of the material
00166     void SetDiffuseColor(const LColor3 &color);
00167     // sets the specular color of the material
00168     void SetSpecularColor(const LColor3 &color);
00169     // sets the shininess of the material
00170     void SetShininess(float value);
00171     // sets the transparency of the material
00172     void SetTransparency(float value);
00173     // sets the shading type
00174     void SetShadingType(LShading shading);
00175 protected:
00176     // the unique material ID
00177     int m_id;
00178     // the first texture map
00179     LMap m_texMap1;
00180     // the second texture map
00181     LMap m_texMap2;
00182     // the opacity map
00183     LMap m_opacMap;
00184     // the reflection map
00185     LMap m_reflMap;
00186     // the bump map
00187     LMap m_bumpMap;
00188     // specular map
00189     LMap m_specMap;
00190     // material ambient color
00191     LColor3 m_ambient;
00192     // material diffuse color
00193     LColor3 m_diffuse;
00194     // material specular color
00195     LColor3 m_specular;
00196     // shininess
00197     float m_shininess;
00198     // transparency
00199     float m_transparency;
00200     // the shading type for the material
00201     LShading m_shading;
00202 };
00203 
00204 //------------------------------------------------
00205 
00206 class LMesh : public LObject
00207 {
00208 public:
00209     // the default constructor
00210     LMesh();
00211     // the destructor
00212     virtual ~LMesh();
00213     // clears the mesh, deleteing all data
00214     void Clear();
00215     // returns the number of vertices in the mesh
00216     uint GetVertexCount();
00217     // sets the the size fo the vertex array - for internal use
00218     void SetVertexArraySize(uint value);
00219     // returns the number of triangles in the mesh
00220     uint GetTriangleCount();
00221     // sets the size of the triangle array - for internal use
00222     void SetTriangleArraySize(uint value);
00223     // returns given vertex    
00224     const LVector4& GetVertex(uint index);
00225     // returns the given normal
00226     const LVector3& GetNormal(uint index);
00227     // returns the given texture coordinates vector
00228     const LVector2& GetUV(uint index);
00229     // returns the pointer to the array of tangents
00230     const LVector3& GetTangent(uint index);
00231     // returns the pointer to the array of binormals
00232     const LVector3& GetBinormal(uint index);
00233     // sets the vertex at a given index to "vec" - for internal use    
00234     void SetVertex(const LVector4 &vec, uint index);
00235     // sets the normal at a given index to "vec" - for internal use    
00236     void SetNormal(const LVector3 &vec, uint index);
00237     // sets the texture coordinates vector at a given index to "vec" - for internal use    
00238     void SetUV(const LVector2 &vec, uint index);
00239     // sets the tangent at a given index to "vec" - for internal use    
00240     void SetTangent(const LVector3 &vec, uint index);
00241     // sets the binormal at a given index to "vec" - for internal use    
00242     void SetBinormal(const LVector3 &vec, uint index);
00243     // returns the triangle with a given index
00244     const LTriangle& GetTriangle(uint index);
00245     // returns the triangle with a given index, see LTriangle2 structure description
00246     LTriangle2 GetTriangle2(uint index);
00247     // returns the mesh matrix, should be identity matrix after loading    
00248     LMatrix4 GetMatrix();
00249     // sets the mesh matrix to a given matrix - for internal use
00250     void SetMatrix(LMatrix4 m);
00251     // optimizises the mesh using a given optimization level
00252     void Optimize(LOptimizationLevel value);
00253     // sets an internal triangle structure with index "index" - for internal use only
00254     void SetTri(const LTri &tri, uint index);
00255     // returns the pointer to the internal triangle structure - for internal use only
00256     LTri& GetTri(uint index);
00257     // returns the material id with a given index for the mesh
00258     uint GetMaterial(uint index);
00259     // adds a material to the mesh and returns its index - for internal use
00260     uint AddMaterial(uint id);
00261     // returns the number of materials used in the mesh
00262     uint GetMaterialCount();
00263 protected:
00264     // the vertices, normals, etc.
00265     std::vector<LVector4> m_vertices;
00266     std::vector<LVector3> m_normals;
00267     std::vector<LVector3> m_binormals;
00268     std::vector<LVector3> m_tangents;
00269     std::vector<LVector2> m_uv;
00270     
00271     // triangles
00272     std::vector<LTriangle> m_triangles;
00273 
00274     //used internally
00275     std::vector<LTri> m_tris;
00276 
00277     // the transformation matrix.
00278     LMatrix4 m_matrix;
00279 
00280     // the material ID array
00281     std::vector<uint> m_materials;
00282 
00283     // calculates the normals, either using the smoothing groups information or not
00284     void CalcNormals(bool useSmoothingGroups);
00285     // calculates the texture(tangent) space for each vertex
00286     void CalcTextureSpace();
00287     // transforms the vertices by the mesh matrix
00288     void TransformVertices();
00289 };
00290 
00291 //------------------------------------------------
00292 
00293 class LLight : public LObject
00294 {
00295 public:
00296     // the default constructor
00297     LLight();
00298     // the destructor
00299     virtual ~LLight();
00300     // clears the data the class holds    
00301     void Clear();
00302     // sets the position of the light source - for internal use
00303     void SetPosition(LVector3 vec);
00304     // returns the position of the light source
00305     LVector3 GetPosition();
00306     // sets the color of the light - for internal use
00307     void SetColor(LColor3 color);
00308     // returns the color of the light
00309     LColor3 GetColor();
00310     // sets whether the light is a spotlight or not - internal use
00311     void SetSpotlight(bool value);
00312     // returns true if the light is a spotlight
00313     bool GetSpotlight();
00314     // sets the target of the light - internal use
00315     void SetTarget(LVector3 target);
00316     // returns the target of the spotlight
00317     LVector3 GetTarget();
00318     // sets the hotspot - internal use    
00319     void SetHotspot(float value);
00320     // returns the hotspot
00321     float GetHotspot();
00322     // sets falloff - internal use
00323     void SetFalloff(float value);
00324     // returns falloff
00325     float GetFalloff();
00326 protected:
00327     LVector3 m_pos;
00328     LColor3 m_color;
00329     bool m_spotlight;
00330     LVector3 m_target;
00331     float m_hotspot;
00332     float m_falloff;
00333 };
00334 
00335 //------------------------------------------------
00336 
00337 class LImporter
00338 {
00339 public:
00340     // the default constructor
00341     LImporter();
00342     // the destructor
00343     virtual ~LImporter();
00344     // reads the model from a file, must be overriden by the child classes
00345     virtual bool LoadFile(const char *filename) = 0;
00346     // returns the number of meshes in the scene
00347     uint GetMeshCount();
00348     // returns the number of lights in the scene
00349     uint GetLightCount();
00350     // returns the number of materials in the scene
00351     uint GetMaterialCount();
00352     // returns a pointer to a mesh
00353     LMesh& GetMesh(uint index);
00354     // returns a pointer to a light at a given index
00355     LLight& GetLight(uint index);
00356     // returns the pointer to the material
00357     LMaterial& GetMaterial(uint index);
00358     // returns the pointer to the material with a given name, or NULL if the material was not found
00359     LMaterial* FindMaterial(const std::string &name);
00360     // returns the pointer to the mesh with a given name, or NULL if the mesh with such name 
00361     // is not present in the scene
00362     LMesh* FindMesh(const std::string &name);
00363     // returns the pointer to the light with a given name, or NULL if not found
00364     LLight* FindLight(const std::string &name);
00365     // sets the optimization level to a given value
00366     void SetOptimizationLevel(LOptimizationLevel value);
00367     // returns the current optimization level
00368     LOptimizationLevel GetOptimizationLevel();
00369 protected:
00370     // the lights found in the scene
00371     std::vector<LLight> m_lights;
00372     // triangular meshes
00373     std::vector<LMesh> m_meshes;
00374     // the materials in the scene
00375     std::vector<LMaterial> m_materials;
00376     // level of optimization to perform on the meshes
00377     LOptimizationLevel m_optLevel;
00378     // clears all data.
00379     virtual void Clear();
00380 };
00381 //------------------------------------------------
00382 
00383 class L3DS : public LImporter
00384 {
00385 public:
00386     // the default contructor
00387     L3DS();
00388     // constructs the object and loads the file
00389     L3DS(const char *filename);
00390     // destructor
00391     virtual ~L3DS();
00392     // load 3ds file 
00393     virtual bool LoadFile(const char *filename);
00394 protected:
00395     // used internally for reading
00396     char m_objName[100];
00397     // true if end of file is reached
00398     bool m_eof;
00399     // buffer for loading, used for speedup
00400     unsigned char *m_buffer;
00401     // the size of the buffer
00402     uint m_bufferSize;
00403     // the current cursor position in the buffer
00404     uint m_pos;
00405 
00406     // reads a short value from the buffer
00407     short ReadShort();
00408     // reads an int value from the buffer
00409     int ReadInt();
00410     // reads a char from the buffer
00411     char ReadChar();
00412     //reada a floatvalue from the buffer
00413     float ReadFloat();
00414     //reads an unsigned byte from the buffer
00415     byte ReadByte();
00416     //reads an asciiz string 
00417     int ReadASCIIZ(char *buf, int max_count);
00418     // seek wihtin the buffer
00419     void Seek(int offset, int origin);
00420     // returns the position of the cursor
00421     uint Pos();
00422 
00423     // read the chunk and return it.
00424     LChunk ReadChunk();
00425     // read until given chunk is found
00426     bool FindChunk(LChunk &target, const LChunk &parent);
00427     // skip to the end of chunk "chunk"
00428     void SkipChunk(const LChunk &chunk);
00429     // goes to the beginning of the data in teh given chunk
00430     void GotoChunk(const LChunk &chunk);
00431 
00432     // the function read the color chunk (any of the color chunks)
00433     LColor3 ReadColor(const LChunk &chunk);
00434     // the function that read the percentage chunk and returns a float from 0 to 1
00435     float ReadPercentage(const LChunk &chunk);
00436     // this is where 3ds file is being read
00437     bool Read3DS();
00438     // read a light chunk 
00439     void ReadLight(const LChunk &parent);
00440     // read a trimesh chunk
00441     void ReadMesh(const LChunk &parent);
00442     // reads the face list, face materials, smoothing groups... and fill rthe information into the mesh
00443     void ReadFaceList(const LChunk &chunk, LMesh &mesh);
00444     // reads the material
00445     void ReadMaterial(const LChunk &parent);
00446     // reads the map info and fills the given map with this information
00447     void ReadMap(const LChunk &chunk, LMap& map);
00448     // reads keyframer data of the OBJECT_NODE_TAG chunk
00449     void ReadKeyframeData(const LChunk &parent);
00450     // reads the keyheader structure from the current offset and returns the frame number
00451     long ReadKeyheader();
00452 };
00453 
00454 //---------------------------------------------------------

Generated on Thu Jun 19 21:55:40 2003 for Debris by doxygen 1.3.2