00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00081
00082
00083
00084
00085 #if defined(_MSVC)
00086
00087 #pragma pack(push)
00088 #pragma pack(1)
00089 #endif
00090
00091
00092
00093
00094
00095
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
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
00217 struct LMap
00218 {
00219
00220 float strength;
00221
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
00236 LObject();
00237
00238
00239 virtual ~LObject();
00240
00241
00242 virtual const string& GetName();
00243
00244
00245
00246
00247
00248 virtual void SetName(const string& value);
00249
00250
00251 bool IsObject(const string &name);
00252
00253 protected:
00254
00255 string m_name;
00256 };
00257
00258
00259
00260 class LMaterial : public LObject
00261 {
00262 public:
00263
00264 LMaterial();
00265
00266
00267 virtual ~LMaterial();
00268
00269
00270 uint GetID();
00271
00272
00273 LMap& GetTextureMap1();
00274
00275
00276 LMap& GetTextureMap2();
00277
00278
00279 LMap& GetOpacityMap();
00280
00281
00282 LMap& GetSpecularMap();
00283
00284
00285 LMap& GetBumpMap();
00286
00287
00288 LMap& GetReflectionMap();
00289
00290
00291 LColor3 GetAmbientColor();
00292
00293
00294 LColor3 GetDiffuseColor();
00295
00296
00297 LColor3 GetSpecularColor();
00298
00299
00300 float GetShininess();
00301
00302
00303 float GetTransparency();
00304
00305
00306 LShading GetShadingType();
00307
00308
00309
00310
00311
00312 void SetID(uint value);
00313
00314
00315 void SetAmbientColor(const LColor3 &color);
00316
00317
00318 void SetDiffuseColor(const LColor3 &color);
00319
00320
00321 void SetSpecularColor(const LColor3 &color);
00322
00323
00324 void SetShininess(float value);
00325
00326
00327 void SetTransparency(float value);
00328
00329
00330 void SetShadingType(LShading shading);
00331
00332 protected:
00333
00334 int m_id;
00335
00336
00337 LMap m_texMap1;
00338
00339
00340 LMap m_texMap2;
00341
00342
00343 LMap m_opacMap;
00344
00345
00346 LMap m_reflMap;
00347
00348
00349 LMap m_bumpMap;
00350
00351
00352 LMap m_specMap;
00353
00354
00355 LColor3 m_ambient;
00356
00357
00358 LColor3 m_diffuse;
00359
00360
00361 LColor3 m_specular;
00362
00363
00364 float m_shininess;
00365
00366
00367 float m_transparency;
00368
00369
00370 LShading m_shading;
00371 };
00372
00373
00374
00375 class LMesh : public LObject
00376 {
00377 public:
00378
00379 LMesh();
00380
00381
00382 virtual ~LMesh();
00383
00384
00385 void Clear();
00386
00387
00388 uint GetVertexCount();
00389
00390
00391 void SetVertexArraySize(uint value);
00392
00393
00394 uint GetTriangleCount();
00395
00396
00397 void SetTriangleArraySize(uint value);
00398
00399
00400 const LVector4& GetVertex(uint index);
00401
00402
00403 const LVector3& GetNormal(uint index);
00404
00405
00406 const LVector2& GetUV(uint index);
00407
00408
00409 LColor3& GetColor(uint index);
00410
00411
00412 const LVector3& GetTangent(uint index);
00413
00414
00415 const LVector3& GetBinormal(uint index);
00416
00417
00418 void SetVertex(const LVector4 &vec, uint index);
00419
00420
00421 void SetNormal(const LVector3 &vec, uint index);
00422
00423
00424 void SetUV(const LVector2 &vec, uint index);
00425
00426
00427 void SetColor(const LColor3 &vec, uint index);
00428
00429
00430 void SetTangent(const LVector3 &vec, uint index);
00431
00432
00433 void SetBinormal(const LVector3 &vec, uint index);
00434
00435
00436 const LTriangle& GetTriangle(uint index);
00437
00438
00439 LTriangle2 GetTriangle2(uint index);
00440
00441
00442 LMatrix4 GetMatrix();
00443
00444
00445 void SetMatrix(LMatrix4 m);
00446
00447
00448 void Optimize(LOptimizationLevel value);
00449
00450
00451 void SetTri(const LTri &tri, uint index);
00452
00453
00454 LTri& GetTri(uint index);
00455
00456
00457 uint GetMaterial(uint index);
00458
00459
00460 uint AddMaterial(uint id);
00461
00462
00463 uint GetMaterialCount();
00464
00465
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
00474 vector<LTriangle> m_triangles;
00475
00476
00477 vector<LTri> m_tris;
00478
00479
00480 LMatrix4 m_matrix;
00481
00482
00483 vector<uint> m_materials;
00484
00485
00486 void CalcNormals(bool useSmoothingGroups);
00487
00488
00489 void CalcTextureSpace();
00490
00491
00492 void TransformVertices();
00493 };
00494
00495
00496
00497 class LCamera : public LObject
00498 {
00499 public:
00500
00501 LCamera();
00502
00503
00504 virtual ~LCamera();
00505
00506
00507 void Clear();
00508
00509
00510 void SetPosition(LVector3 vec);
00511
00512
00513 LVector3 GetPosition();
00514
00515
00516 void SetTarget(LVector3 target);
00517
00518
00519 LVector3 GetTarget();
00520
00521
00522 void SetFOV(float value);
00523
00524
00525 float GetFOV();
00526
00527
00528 void SetBank(float value);
00529
00530
00531 float GetBank();
00532
00533
00534 void SetNearplane(float value);
00535
00536
00537 float GetNearplane();
00538
00539
00540 void SetFarplane(float value);
00541
00542
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
00560 LLight();
00561
00562
00563 virtual ~LLight();
00564
00565
00566 void Clear();
00567
00568
00569 void SetPosition(LVector3 vec);
00570
00571
00572 LVector3 GetPosition();
00573
00574
00575 void SetColor(LColor3 color);
00576
00577
00578 LColor3 GetColor();
00579
00580
00581 void SetSpotlight(bool value);
00582
00583
00584 bool GetSpotlight();
00585
00586
00587 void SetTarget(LVector3 target);
00588
00589
00590 LVector3 GetTarget();
00591
00592
00593 void SetHotspot(float value);
00594
00595
00596 float GetHotspot();
00597
00598
00599 void SetFalloff(float value);
00600
00601
00602 float GetFalloff();
00603
00604
00605 void SetAttenuationstart(float value);
00606
00607
00608 float GetAttenuationstart();
00609
00610
00611 void SetAttenuationend(float value);
00612
00613
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
00633 LImporter();
00634
00635
00636 virtual ~LImporter();
00637
00638
00639 virtual bool LoadFile(const char *filename) = 0;
00640
00641
00642 uint GetMeshCount();
00643
00644
00645 uint GetLightCount();
00646
00647
00648 uint GetMaterialCount();
00649
00650
00651 uint GetCameraCount();
00652
00653
00654 LMesh& GetMesh(uint index);
00655
00656
00657 LCamera& GetCamera(uint index);
00658
00659
00660 LLight& GetLight(uint index);
00661
00662
00663 LMaterial& GetMaterial(uint index);
00664
00665
00666 LMaterial* FindMaterial(const string &name);
00667
00668
00669
00670 LMesh* FindMesh(const string &name);
00671
00672
00673 LLight* FindCamera(const string &name);
00674
00675
00676 LLight* FindLight(const string &name);
00677
00678
00679 void SetOptimizationLevel(LOptimizationLevel value);
00680
00681
00682 LOptimizationLevel GetOptimizationLevel();
00683
00684 protected:
00685
00686 vector<LCamera> m_cameras;
00687
00688
00689 vector<LLight> m_lights;
00690
00691
00692 vector<LMesh> m_meshes;
00693
00694
00695 vector<LMaterial> m_materials;
00696
00697
00698 LOptimizationLevel m_optLevel;
00699
00700
00701 virtual void Clear();
00702 };
00703
00704
00705
00706 class L3DS : public LImporter
00707 {
00708 public:
00709
00710 L3DS();
00711
00712
00713 L3DS(const char *filename);
00714
00715
00716 virtual ~L3DS();
00717
00718
00719 virtual bool LoadFile(const char *filename);
00720
00721 protected:
00722
00723 char m_objName[100];
00724
00725
00726 bool m_eof;
00727
00728
00729 unsigned char *m_buffer;
00730
00731
00732 uint m_bufferSize;
00733
00734
00735 uint m_pos;
00736
00737
00738 short ReadShort();
00739
00740
00741 int ReadInt();
00742
00743
00744 char ReadChar();
00745
00746
00747 float ReadFloat();
00748
00749
00750 byte ReadByte();
00751
00752
00753 int ReadASCIIZ(char *buf, int max_count);
00754
00755
00756 void Seek(int offset, int origin);
00757
00758
00759 uint Pos();
00760
00761
00762 LChunk ReadChunk();
00763
00764
00765 bool FindChunk(LChunk &target, const LChunk &parent);
00766
00767
00768 void SkipChunk(const LChunk &chunk);
00769
00770
00771 void GotoChunk(const LChunk &chunk);
00772
00773
00774 LColor3 ReadColor(const LChunk &chunk);
00775
00776
00777 float ReadPercentage(const LChunk &chunk);
00778
00779
00780 bool Read3DS();
00781
00782
00783 void ReadLight(const LChunk &parent);
00784
00785
00786 void ReadCamera(const LChunk &parent);
00787
00788
00789 void ReadMesh(const LChunk &parent);
00790
00791
00792 void ReadFaceList(const LChunk &chunk, LMesh &mesh);
00793
00794
00795 void ReadMaterial(const LChunk &parent);
00796
00797
00798 void ReadMap(const LChunk &chunk, LMap& map);
00799
00800
00801 void ReadKeyframeData(const LChunk &parent);
00802
00803
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