- Main Page
- Modules
- Classes
- Files

- File List
- File Members

# D:/CHAI3D/build-2-0-0/2.0.0/win32/src/files/CFileLoader3DS.h

Go to the documentation of this file.

    00001 //===========================================================================
    00002 /*
    00003     This file is part of the CHAI 3D visualization and haptics libraries.
    00004     Copyright (C) 2003-2009 by CHAI 3D. All rights reserved.
    00005
    00006     This library is free software; you can redistribute it and/or modify
    00007     it under the terms of the GNU General Public License("GPL") version 2
    00008     as published by the Free Software Foundation.
    00009
    00010     For using the CHAI 3D libraries with software that can not be combined
    00011     with the GNU GPL, and for taking advantage of the additional benefits
    00012     of our support services, please contact CHAI 3D about acquiring a
    00013     Professional Edition License.
    00014
    00015     \author    <http://www.chai3d.org>
    00016     \author    Lev Povalahev
    00017     \author    Dan Morris
    00018     \version   2.0.0 $Rev: 251 $
    00019 */
    00020 //===========================================================================
    00021
    00022 //---------------------------------------------------------------------------
    00023 #ifndef CFileLoader3DSH
    00024 #define CFileLoader3DSH
    00025 //---------------------------------------------------------------------------
    00026 #include "math/CMatrix3d.h"
    00027 #include "math/CVector3d.h"
    00028 #include "graphics/CVertex.h"
    00029 #include "graphics/CTriangle.h"
    00030 #include "scenegraph/CMesh.h"
    00031 #include "graphics/CMaterial.h"
    00032 #include "graphics/CTexture2D.h"
    00033 #include "scenegraph/CWorld.h"
    00034 #include "scenegraph/CLight.h"
    00035 #include <string>
    00036 #include <vector>
    00037 #include <stdio.h>
    00038 //---------------------------------------------------------------------------
    00039 using std::string;
    00040 using std::vector;
    00041 //---------------------------------------------------------------------------
    00042
    00043 //===========================================================================
    00051 //===========================================================================
    00052
    00053 //---------------------------------------------------------------------------
    00054 // GLOBAL UTILITY FUNCTIONS:
    00055 //---------------------------------------------------------------------------
    00056
    00062 bool cLoadFile3DS(cMesh* iMesh, const string& iFileName);
    00063
    00071 extern bool g_3dsLoaderShouldGenerateExtraVertices;
    00072
    00073
    00074 //---------------------------------------------------------------------------
    00075 #ifndef DOXYGEN_SHOULD_SKIP_THIS
    00076 //---------------------------------------------------------------------------
    00077
    00078 //===========================================================================
    00079 /*
    00080   The remainder of this file comes from Lev Povalahev's l3ds.h
    00081   copyright (c) 2001-2002 Lev Povalahev
    00082 */
    00083 //===========================================================================
    00084
    00085 #if defined(_MSVC)
    00086 // Tell VC++ not to pad any structures
    00087 #pragma pack(push)
    00088 #pragma pack(1)
    00089 #endif
    00090
    00091 //===========================================================================
    00092 // copyright (c) 2001-2002 Lev Povalahev
    00093 // this is a 3ds importer version 2
    00094 //
    00095 // Modified by Dan Morris, 2004
    00096 //===========================================================================
    00097
    00098 typedef unsigned int uint;
    00099 typedef unsigned char byte;
    00100
    00101 enum LShading {sWireframe, sFlat, sGouraud, sPhong, sMetal};
    00102
    00103 enum LOptimizationLevel {oNone, oSimple, oFull};
    00104
    00105 // for internal use
    00106 struct LChunk;
    00107 struct LTri;
    00108
    00109 //---------------------------------------------------------------------------
    00110
    00111 struct LVector4
    00112 {
    00113     float x;
    00114     float y;
    00115     float z;
    00116     float w;
    00117 };
    00118
    00119 //---------------------------------------------------------------------------
    00120
    00121 struct LVector3
    00122 {
    00123     float x;
    00124     float y;
    00125     float z;
    00126     LVector3()
    00127     {
    00128         x = y = z = 0;
    00129     }
    00130     LVector3(float x, float y, float z)
    00131     {
    00132         this->x = x; this->y = y; this->z = z;
    00133     }
    00134
    00135 };
    00136
    00137 //---------------------------------------------------------------------------
    00138
    00139 struct LVector2
    00140 {
    00141     float x;
    00142     float y;
    00143 };
    00144
    00145 //---------------------------------------------------------------------------
    00146
    00147 struct LColor3
    00148 {
    00149     float r;
    00150     float g;
    00151     float b;
    00152 };
    00153
    00154 //---------------------------------------------------------------------------
    00155
    00156 struct LChunk
    00157 {
    00158     unsigned short id;
    00159     uint start;
    00160     uint end;
    00161 };
    00162
    00163 //---------------------------------------------------------------------------
    00164
    00165 struct LTri
    00166 {
    00167     unsigned short a;
    00168     unsigned short b;
    00169     unsigned short c;
    00170     unsigned long smoothingGroups;
    00171     LVector3 normal;
    00172     LVector3 tangent;
    00173     LVector3 binormal;
    00174     uint materialId;
    00175     LTri()
    00176     {
    00177       a = b = c = 0;
    00178       smoothingGroups = 0;
    00179       materialId = 0;
    00180     }
    00181 };
    00182
    00183 //---------------------------------------------------------------------------
    00184
    00185 struct LTriangle
    00186 {
    00187     unsigned short a;
    00188     unsigned short b;
    00189     unsigned short c;
    00190 };
    00191
    00192 //---------------------------------------------------------------------------
    00193
    00194 struct LMatrix4
    00195 {
    00196     float _11, _12, _13, _14;
    00197     float _21, _22, _23, _24;
    00198     float _31, _32, _33, _34;
    00199     float _41, _42, _43, _44;
    00200 };
    00201
    00202 //---------------------------------------------------------------------------
    00203
    00204 struct LTriangle2
    00205 {
    00206     LVector4 vertices[3];
    00207     LVector3 vertexNormals[3];
    00208     LVector2 textureCoords[3];
    00209     LVector3 faceNormal;
    00210     LColor3 color[3];
    00211     uint materialId;
    00212 };
    00213
    00214 //---------------------------------------------------------------------------
    00215
    00216 // a structure for a texture map
    00217 struct LMap
    00218 {
    00219     // the strength of the texture map
    00220     float strength;
    00221     // the file name of the map. only 8.3 format in 3ds files :(
    00222     char mapName[255];
    00223     float uScale;
    00224     float vScale;
    00225     float uOffset;
    00226     float vOffset;
    00227     float angle;
    00228 };
    00229
    00230 //---------------------------------------------------------------------------
    00231
    00232 class LObject
    00233 {
    00234   public:
    00235     // the default constructor, initializes the m_name here
    00236     LObject();
    00237
    00238     // the destructor frees memory (m_name)
    00239     virtual ~LObject();
    00240
    00241     // call this to get the name of the object
    00242     virtual const string& GetName();
    00243
    00244     // this methods should not be used by the "user", they're used internally to fill the class
    00245     // with valid data when reading from file. If you're about to add an importer for another format you'LL
    00246     // have to use these methods
    00247     // call this to set the name of the object
    00248     virtual void SetName(const string& value);
    00249
    00250     // returns true if the object's name is the name passed as parameter
    00251     bool IsObject(const string &name);
    00252
    00253   protected:
    00254     // the name of the object
    00255     string m_name;
    00256 };
    00257
    00258 //---------------------------------------------------------------------------
    00259
    00260 class LMaterial : public LObject
    00261 {
    00262   public:
    00263     // the default constructor, does the initialization
    00264     LMaterial();
    00265
    00266     // the destructor
    00267     virtual ~LMaterial();
    00268
    00269     // returns the material ID
    00270     uint GetID();
    00271
    00272     // returns the pointer to the texture map 1
    00273     LMap& GetTextureMap1();
    00274
    00275     // returns the pointer to the texture map 2
    00276     LMap& GetTextureMap2();
    00277
    00278     // returns the pointer to the opacity map
    00279     LMap& GetOpacityMap();
    00280
    00281     // returns the pointer to the specular (gloss) map
    00282     LMap& GetSpecularMap();
    00283
    00284     // returns the pointer to the bump map
    00285     LMap& GetBumpMap();
    00286
    00287     // returns the pointer to the reflection map
    00288     LMap& GetReflectionMap();
    00289
    00290     // returns the ambient color of the material
    00291     LColor3 GetAmbientColor();
    00292
    00293     // returns the diffuse color of the material
    00294     LColor3 GetDiffuseColor();
    00295
    00296     // returns the specular color of the material
    00297     LColor3 GetSpecularColor();
    00298
    00299     // returns the shininess of the material, ranging from 0(matte) to 1(shiny)
    00300     float GetShininess();
    00301
    00302     // returns the transparency of the material, ranging from 1(fully transparent) to 0(opaque)
    00303     float GetTransparency();
    00304
    00305     // returns the type of shading, see LShading type
    00306     LShading GetShadingType();
    00307
    00308     // this methods should not be used by the "user", they're used internally to fill the class
    00309     // with valid data when reading from file. If you're about to add an importer for another format you'LL
    00310     // have to use these methods
    00311     // sets the material ID to "value"
    00312     void SetID(uint value);
    00313
    00314     // call this to set the ambient color of the material
    00315     void SetAmbientColor(const LColor3 &color);
    00316
    00317     // sets the diffuse color of the material
    00318     void SetDiffuseColor(const LColor3 &color);
    00319
    00320     // sets the specular color of the material
    00321     void SetSpecularColor(const LColor3 &color);
    00322
    00323     // sets the shininess of the material
    00324     void SetShininess(float value);
    00325
    00326     // sets the transparency of the material
    00327     void SetTransparency(float value);
    00328
    00329     // sets the shading type
    00330     void SetShadingType(LShading shading);
    00331
    00332   protected:
    00333     // the unique material ID
    00334     int m_id;
    00335
    00336     // the first texture map
    00337     LMap m_texMap1;
    00338
    00339     // the second texture map
    00340     LMap m_texMap2;
    00341
    00342     // the opacity map
    00343     LMap m_opacMap;
    00344
    00345     // the reflection map
    00346     LMap m_reflMap;
    00347
    00348     // the bump map
    00349     LMap m_bumpMap;
    00350
    00351     // specular map
    00352     LMap m_specMap;
    00353
    00354     // material ambient color
    00355     LColor3 m_ambient;
    00356
    00357     // material diffuse color
    00358     LColor3 m_diffuse;
    00359
    00360     // material specular color
    00361     LColor3 m_specular;
    00362
    00363     // shininess
    00364     float m_shininess;
    00365
    00366     // transparency
    00367     float m_transparency;
    00368
    00369     // the shading type for the material
    00370     LShading m_shading;
    00371 };
    00372
    00373 //---------------------------------------------------------------------------
    00374
    00375 class LMesh : public LObject
    00376 {
    00377   public:
    00378     // the default constructor
    00379     LMesh();
    00380
    00381     // the destructor
    00382     virtual ~LMesh();
    00383
    00384     // clears the mesh, deleting all data
    00385     void Clear();
    00386
    00387     // returns the number of vertices in the mesh
    00388     uint GetVertexCount();
    00389
    00390     // sets the the size of the vertex array - for internal use
    00391     void SetVertexArraySize(uint value);
    00392
    00393     // returns the number of triangles in the mesh
    00394     uint GetTriangleCount();
    00395
    00396     // sets the size of the triangle array - for internal use
    00397     void SetTriangleArraySize(uint value);
    00398
    00399     // returns given vertex
    00400     const LVector4& GetVertex(uint index);
    00401
    00402     // returns the given normal
    00403     const LVector3& GetNormal(uint index);
    00404
    00405     // returns the given texture coordinates vector
    00406     const LVector2& GetUV(uint index);
    00407
    00408     // return the color given the index
    00409     LColor3& GetColor(uint index);
    00410
    00411     // returns the pointer to the array of tangents
    00412     const LVector3& GetTangent(uint index);
    00413
    00414     // returns the pointer to the array of binormals
    00415     const LVector3& GetBinormal(uint index);
    00416
    00417     // sets the vertex at a given index to "vec" - for internal use
    00418     void SetVertex(const LVector4 &vec, uint index);
    00419
    00420     // sets the normal at a given index to "vec" - for internal use
    00421     void SetNormal(const LVector3 &vec, uint index);
    00422
    00423     // sets the texture coordinates vector at a given index to "vec" - for internal use
    00424     void SetUV(const LVector2 &vec, uint index);
    00425
    00426     // sets the color
    00427     void SetColor(const LColor3 &vec, uint index);
    00428
    00429     // sets the tangent at a given index to "vec" - for internal use
    00430     void SetTangent(const LVector3 &vec, uint index);
    00431
    00432     // sets the binormal at a given index to "vec" - for internal use
    00433     void SetBinormal(const LVector3 &vec, uint index);
    00434
    00435     // returns the triangle with a given index
    00436     const LTriangle& GetTriangle(uint index);
    00437
    00438     // returns the triangle with a given index, see LTriangle2 structure description
    00439     LTriangle2 GetTriangle2(uint index);
    00440
    00441     // returns the mesh matrix, should be identity matrix after loading
    00442     LMatrix4 GetMatrix();
    00443
    00444     // sets the mesh matrix to a given matrix - for internal use
    00445     void SetMatrix(LMatrix4 m);
    00446
    00447     // optimizises the mesh using a given optimization level
    00448     void Optimize(LOptimizationLevel value);
    00449
    00450     // sets an internal triangle structure with index "index" - for internal use only
    00451     void SetTri(const LTri &tri, uint index);
    00452
    00453     // returns the pointer to the internal triangle structure - for internal use only
    00454     LTri& GetTri(uint index);
    00455
    00456     // returns the material id with a given index for the mesh
    00457     uint GetMaterial(uint index);
    00458
    00459     // adds a material to the mesh and returns its index - for internal use
    00460     uint AddMaterial(uint id);
    00461
    00462     // returns the number of materials used in the mesh
    00463     uint GetMaterialCount();
    00464
    00465     // the vertices, normals, etc.
    00466     vector<LVector4> m_vertices;
    00467     vector<LVector3> m_normals;
    00468     vector<LVector3> m_binormals;
    00469     vector<LVector3> m_tangents;
    00470     vector<LVector2> m_uv;
    00471     vector<LColor3> m_colors;
    00472
    00473     // triangles
    00474     vector<LTriangle> m_triangles;
    00475
    00476     //used internally
    00477     vector<LTri> m_tris;
    00478
    00479     // the transformation matrix.
    00480     LMatrix4 m_matrix;
    00481
    00482     // the material ID array
    00483     vector<uint> m_materials;
    00484
    00485     // calculates the normals, either using the smoothing groups information or not
    00486     void CalcNormals(bool useSmoothingGroups);
    00487
    00488     // calculates the texture(tangent) space for each vertex
    00489     void CalcTextureSpace();
    00490
    00491     // transforms the vertices by the mesh matrix
    00492     void TransformVertices();
    00493 };
    00494
    00495 //---------------------------------------------------------------------------
    00496
    00497 class LCamera : public LObject
    00498 {
    00499   public:
    00500     // the default constructor
    00501     LCamera();
    00502
    00503     // the destructor
    00504     virtual ~LCamera();
    00505
    00506     // clears the data the class holds
    00507     void Clear();
    00508
    00509     // sets the position of the camera - for internal use
    00510     void SetPosition(LVector3 vec);
    00511
    00512     // returns the position of the camera
    00513     LVector3 GetPosition();
    00514
    00515     // sets the target of the camera - internal use
    00516     void SetTarget(LVector3 target);
    00517
    00518     // returns the target of the camera
    00519     LVector3 GetTarget();
    00520
    00521     // sets the fov - internal use
    00522     void SetFOV(float value);
    00523
    00524     // returns the fov
    00525     float GetFOV();
    00526
    00527     // sets the bank - internal use
    00528     void SetBank(float value);
    00529
    00530     // returns the bank
    00531     float GetBank();
    00532
    00533     // sets the near plane - internal use
    00534     void SetNearplane(float value);
    00535
    00536     // returns the near plane distance
    00537     float GetNearplane();
    00538
    00539     // sets the far plane - internal use
    00540     void SetFarplane(float value);
    00541
    00542     // returns the far plane distance
    00543     float GetFarplane();
    00544
    00545   protected:
    00546     LVector3 m_pos;
    00547     LVector3 m_target;
    00548         float m_bank;
    00549     float m_fov;
    00550         float m_near;
    00551         float m_far;
    00552 };
    00553
    00554 //---------------------------------------------------------------------------
    00555
    00556 class LLight : public LObject
    00557 {
    00558   public:
    00559     // the default constructor
    00560     LLight();
    00561
    00562     // the destructor
    00563     virtual ~LLight();
    00564
    00565     // clears the data the class holds
    00566     void Clear();
    00567
    00568     // sets the position of the light source - for internal use
    00569     void SetPosition(LVector3 vec);
    00570
    00571     // returns the position of the light source
    00572     LVector3 GetPosition();
    00573
    00574     // sets the color of the light - for internal use
    00575     void SetColor(LColor3 color);
    00576
    00577     // returns the color of the light
    00578     LColor3 GetColor();
    00579
    00580     // sets whether the light is a spotlight or not - internal use
    00581     void SetSpotlight(bool value);
    00582
    00583     // returns true if the light is a spotlight
    00584     bool GetSpotlight();
    00585
    00586     // sets the target of the light - internal use
    00587     void SetTarget(LVector3 target);
    00588
    00589     // returns the target of the spotlight
    00590     LVector3 GetTarget();
    00591
    00592     // sets the hotspot - internal use
    00593     void SetHotspot(float value);
    00594
    00595     // returns the hotspot
    00596     float GetHotspot();
    00597
    00598     // sets falloff - internal use
    00599     void SetFalloff(float value);
    00600
    00601     // returns falloff
    00602     float GetFalloff();
    00603
    00604     // sets attenuationstart - internal use
    00605     void SetAttenuationstart(float value);
    00606
    00607     // returns attenuationstart
    00608     float GetAttenuationstart();
    00609
    00610     // sets attenuationend - internal use
    00611     void SetAttenuationend(float value);
    00612
    00613     // returns attenuationend
    00614     float GetAttenuationend();
    00615
    00616   protected:
    00617     LVector3 m_pos;
    00618     LColor3 m_color;
    00619     bool m_spotlight;
    00620     LVector3 m_target;
    00621     float m_hotspot;
    00622     float m_falloff;
    00623         float m_attenuationstart;
    00624         float m_attenuationend;
    00625 };
    00626
    00627 //---------------------------------------------------------------------------
    00628
    00629 class LImporter
    00630 {
    00631   public:
    00632     // the default constructor
    00633     LImporter();
    00634
    00635     // the destructor
    00636     virtual ~LImporter();
    00637
    00638     // reads the model from a file, must be overriden by the child classes
    00639     virtual bool LoadFile(const char *filename) = 0;
    00640
    00641     // returns the number of meshes in the scene
    00642     uint GetMeshCount();
    00643
    00644     // returns the number of lights in the scene
    00645     uint GetLightCount();
    00646
    00647     // returns the number of materials in the scene
    00648     uint GetMaterialCount();
    00649
    00650         // returns the number of cameras in the scene
    00651         uint GetCameraCount();
    00652
    00653     // returns a pointer to a mesh
    00654     LMesh& GetMesh(uint index);
    00655
    00656     // returns a pointer to a camera at a given index
    00657     LCamera& GetCamera(uint index);
    00658
    00659     // returns a pointer to a light at a given index
    00660     LLight& GetLight(uint index);
    00661
    00662     // returns the pointer to the material
    00663     LMaterial& GetMaterial(uint index);
    00664
    00665     // returns the pointer to the material with a given name, or NULL if the material was not found
    00666     LMaterial* FindMaterial(const string &name);
    00667
    00668     // returns the pointer to the mesh with a given name, or NULL if the mesh with such name
    00669     // is not present in the scene
    00670     LMesh* FindMesh(const string &name);
    00671
    00672     // returns the pointer to the camera with a given name, or NULL if not found
    00673     LLight* FindCamera(const string &name);
    00674
    00675     // returns the pointer to the light with a given name, or NULL if not found
    00676     LLight* FindLight(const string &name);
    00677
    00678     // sets the optimization level to a given value
    00679     void SetOptimizationLevel(LOptimizationLevel value);
    00680
    00681     // returns the current optimization level
    00682     LOptimizationLevel GetOptimizationLevel();
    00683
    00684  protected:
    00685     // the cameras found in the scene
    00686     vector<LCamera> m_cameras;
    00687
    00688     // the lights found in the scene
    00689     vector<LLight> m_lights;
    00690
    00691     // triangular meshes
    00692     vector<LMesh> m_meshes;
    00693
    00694     // the materials in the scene
    00695     vector<LMaterial> m_materials;
    00696
    00697     // level of optimization to perform on the meshes
    00698     LOptimizationLevel m_optLevel;
    00699
    00700     // clears all data.
    00701     virtual void Clear();
    00702 };
    00703
    00704 //---------------------------------------------------------------------------
    00705
    00706 class L3DS : public LImporter
    00707 {
    00708   public:
    00709     // the default constructor
    00710     L3DS();
    00711
    00712     // constructs the object and loads the file
    00713     L3DS(const char *filename);
    00714
    00715     // destructor
    00716     virtual ~L3DS();
    00717
    00718     // load 3ds file
    00719     virtual bool LoadFile(const char *filename);
    00720
    00721   protected:
    00722     // used internally for reading
    00723     char m_objName[100];
    00724
    00725     // true if end of file is reached
    00726     bool m_eof;
    00727
    00728     // buffer for loading, used for speedup
    00729     unsigned char *m_buffer;
    00730
    00731     // the size of the buffer
    00732     uint m_bufferSize;
    00733
    00734     // the current cursor position in the buffer
    00735     uint m_pos;
    00736
    00737     // reads a short value from the buffer
    00738     short ReadShort();
    00739
    00740     // reads an int value from the buffer
    00741     int ReadInt();
    00742
    00743     // reads a char from the buffer
    00744     char ReadChar();
    00745
    00746     // reads a float value from the buffer
    00747     float ReadFloat();
    00748
    00749     // reads an unsigned byte from the buffer
    00750     byte ReadByte();
    00751
    00752     // reads an asciiz string
    00753     int ReadASCIIZ(char *buf, int max_count);
    00754
    00755     // seek within the buffer
    00756     void Seek(int offset, int origin);
    00757
    00758     // returns the position of the cursor
    00759     uint Pos();
    00760
    00761     // read the chunk and return it.
    00762     LChunk ReadChunk();
    00763
    00764     // read until given chunk is found
    00765     bool FindChunk(LChunk &target, const LChunk &parent);
    00766
    00767     // skip to the end of chunk "chunk"
    00768     void SkipChunk(const LChunk &chunk);
    00769
    00770     // goes to the beginning of the data in the given chunk
    00771     void GotoChunk(const LChunk &chunk);
    00772
    00773     // the function read the color chunk (any of the color chunks)
    00774     LColor3 ReadColor(const LChunk &chunk);
    00775
    00776     // the function that read the percentage chunk and returns a float from 0 to 1
    00777     float ReadPercentage(const LChunk &chunk);
    00778
    00779     // this is where 3ds file is being read
    00780     bool Read3DS();
    00781
    00782     // read a light chunk
    00783     void ReadLight(const LChunk &parent);
    00784
    00785         // read a camera chunk
    00786         void ReadCamera(const LChunk &parent);
    00787
    00788     // read a trimesh chunk
    00789     void ReadMesh(const LChunk &parent);
    00790
    00791     // reads the face list, face materials, smoothing groups... and fill the information into the mesh
    00792     void ReadFaceList(const LChunk &chunk, LMesh &mesh);
    00793
    00794     // reads the material
    00795     void ReadMaterial(const LChunk &parent);
    00796
    00797     // reads the map info and fills the given map with this information
    00798     void ReadMap(const LChunk &chunk, LMap& map);
    00799
    00800     // reads keyframer data of the OBJECT_NODE_TAG chunk
    00801     void ReadKeyframeData(const LChunk &parent);
    00802
    00803     // reads the keyheader structure from the current offset and returns the frame number
    00804     long ReadKeyheader();
    00805 };
    00806
    00807 #if defined(_MSVC)
    00808 #pragma pack(pop)
    00809 #endif
    00810
    00811 //---------------------------------------------------------------------------
    00812 #endif  // DOXYGEN_SHOULD_SKIP_THIS
    00813 //---------------------------------------------------------------------------
    00814 #endif
    00815 //---------------------------------------------------------------------------
    00816
    00817

CHAI3D 2.0.0 documentation
Please address any questions to support@chai3d.org
(C) 2003-2009 - CHAI 3D
All Rights Reserved.