OSG3DSLoader.h
Go to the documentation of this file.00001
00002
00003
00004 #ifndef L3DS_H
00005 #define L3DS_H
00006
00007
00008 #include <vector>
00009 #include <string>
00010 #include <iostream>
00011
00012
00013
00014 typedef unsigned long ulong;
00015 typedef unsigned int uint;
00016 typedef unsigned char byte;
00017
00018 enum LShading {sWireframe, sFlat, sGouraud, sPhong, sMetal};
00019
00020 enum LOptimizationLevel {oNone, oSimple, oFull};
00021
00022
00023
00024
00025
00026
00027
00028
00029 struct LVector4
00030 {
00031 float x;
00032 float y;
00033 float z;
00034 float w;
00035 };
00036
00037 struct LVector3
00038 {
00039 float x;
00040 float y;
00041 float z;
00042 };
00043
00044 struct LVector2
00045 {
00046 float x;
00047 float y;
00048 };
00049
00050 struct LColor3
00051 {
00052 float r;
00053 float g;
00054 float b;
00055 };
00056
00057
00058
00059
00060
00061 struct LChunk
00062 {
00063 unsigned short id;
00064 uint start;
00065 uint end;
00066 };
00067
00068 struct LTri
00069 {
00070 unsigned short a;
00071 unsigned short b;
00072 unsigned short c;
00073 ulong smoothingGroups;
00074 LVector3 normal;
00075 LVector3 tangent;
00076 LVector3 binormal;
00077 uint materialId;
00078 };
00079
00080 struct LTriangle
00081 {
00082 unsigned short a;
00083 unsigned short b;
00084 unsigned short c;
00085 };
00086
00087 struct LMatrix4
00088 {
00089 float _11, _12, _13, _14;
00090 float _21, _22, _23, _24;
00091 float _31, _32, _33, _34;
00092 float _41, _42, _43, _44;
00093 };
00094
00095 struct LTriangle2
00096 {
00097 LVector4 vertices[3];
00098 LVector3 vertexNormals[3];
00099 LVector2 textureCoords[3];
00100 LVector3 faceNormal;
00101 uint materialId;
00102 };
00103
00104
00105 struct LMap
00106 {
00107
00108 float strength;
00109
00110 char mapName[255];
00111 float uScale;
00112 float vScale;
00113 float uOffset;
00114 float vOffset;
00115 float angle;
00116 short tiling;
00117 };
00118
00119
00120
00121 class LObject
00122 {
00123 public:
00124
00125 LObject();
00126
00127 virtual ~LObject();
00128
00129 virtual const std::string& GetName();
00130
00131
00132
00133
00134
00135 virtual void SetName(const std::string& value);
00136
00137 bool IsObject(const std::string &name);
00138 protected:
00139
00140 std::string m_name;
00141 };
00142
00143
00144
00145 class LMaterial : public LObject
00146 {
00147 public:
00148
00149 LMaterial();
00150
00151 virtual ~LMaterial();
00152
00153 uint GetID();
00154
00155 LMap& GetTextureMap1();
00156
00157 LMap& GetTextureMap2();
00158
00159 LMap& GetOpacityMap();
00160
00161 LMap& GetSpecularMap();
00162
00163 LMap& GetBumpMap();
00164
00165 LMap& GetReflectionMap();
00166
00167 LColor3 GetAmbientColor();
00168
00169 LColor3 GetDiffuseColor();
00170
00171 LColor3 GetSpecularColor();
00172
00173 float GetShininess();
00174
00175 float GetTransparency();
00176
00177 LShading GetShadingType();
00178
00179
00180
00181
00182
00183 void SetID(uint value);
00184
00185 void SetAmbientColor(const LColor3 &color);
00186
00187 void SetDiffuseColor(const LColor3 &color);
00188
00189 void SetSpecularColor(const LColor3 &color);
00190
00191 void SetShininess(float value);
00192
00193 void SetTransparency(float value);
00194
00195 void SetShadingType(LShading shading);
00196 protected:
00197
00198 int m_id;
00199
00200 LMap m_texMap1;
00201
00202 LMap m_texMap2;
00203
00204 LMap m_opacMap;
00205
00206 LMap m_reflMap;
00207
00208 LMap m_bumpMap;
00209
00210 LMap m_specMap;
00211
00212 LColor3 m_ambient;
00213
00214 LColor3 m_diffuse;
00215
00216 LColor3 m_specular;
00217
00218 float m_shininess;
00219
00220 float m_transparency;
00221
00222 LShading m_shading;
00223 };
00224
00225
00226
00227 class LMesh : public LObject
00228 {
00229 public:
00230
00231 LMesh();
00232
00233 virtual ~LMesh();
00234
00235 void Clear();
00236
00237 uint GetVertexCount();
00238
00239 void SetVertexArraySize(uint value);
00240
00241 uint GetTriangleCount();
00242
00243 void SetTriangleArraySize(uint value);
00244
00245 const LVector4& GetVertex(uint index);
00246
00247 const LVector3& GetNormal(uint index);
00248
00249 const LVector2& GetUV(uint index);
00250
00251 const LVector3& GetTangent(uint index);
00252
00253 const LVector3& GetBinormal(uint index);
00254
00255 void SetVertex(const LVector4 &vec, uint index);
00256
00257 void SetNormal(const LVector3 &vec, uint index);
00258
00259 void SetUV(const LVector2 &vec, uint index);
00260
00261 void SetTangent(const LVector3 &vec, uint index);
00262
00263 void SetBinormal(const LVector3 &vec, uint index);
00264
00265 const LTriangle& GetTriangle(uint index);
00266
00267 LTriangle2 GetTriangle2(uint index);
00268
00269 LMatrix4 GetMatrix();
00270
00271 void SetMatrix(LMatrix4 m);
00272
00273 void Optimize(LOptimizationLevel value);
00274
00275 void SetTri(const LTri &tri, uint index);
00276
00277 LTri& GetTri(uint index);
00278
00279 uint GetMaterial(uint index);
00280
00281 uint AddMaterial(uint id);
00282
00283 uint GetMaterialCount();
00284 protected:
00285
00286 std::vector<LVector4> m_vertices;
00287 std::vector<LVector3> m_normals;
00288 std::vector<LVector3> m_binormals;
00289 std::vector<LVector3> m_tangents;
00290 std::vector<LVector2> m_uv;
00291
00292
00293 std::vector<LTriangle> m_triangles;
00294
00295
00296 std::vector<LTri> m_tris;
00297
00298
00299 LMatrix4 m_matrix;
00300
00301
00302 std::vector<uint> m_materials;
00303
00304
00305 void CalcNormals(bool useSmoothingGroups);
00306
00307 void CalcTextureSpace();
00308
00309 void TransformVertices();
00310 };
00311
00312
00313
00314 class LCamera : public LObject
00315 {
00316 public:
00317
00318 LCamera();
00319
00320 virtual ~LCamera();
00321
00322 void Clear();
00323
00324 void SetPosition(LVector3 vec);
00325
00326 LVector3 GetPosition();
00327
00328 void SetTarget(LVector3 target);
00329
00330 LVector3 GetTarget();
00331
00332 void SetFOV(float value);
00333
00334 float GetFOV();
00335
00336 void SetBank(float value);
00337
00338 float GetBank();
00339
00340 void SetNearplane(float value);
00341
00342 float GetNearplane();
00343
00344 void SetFarplane(float value);
00345
00346 float GetFarplane();
00347 protected:
00348 LVector3 m_pos;
00349 LVector3 m_target;
00350 float m_bank;
00351 float m_fov;
00352 float m_near;
00353 float m_far;
00354 };
00355
00356
00357
00358 class LLight : public LObject
00359 {
00360 public:
00361
00362 LLight();
00363
00364 virtual ~LLight();
00365
00366 void Clear();
00367
00368 void SetPosition(LVector3 vec);
00369
00370 LVector3 GetPosition();
00371
00372 void SetColor(LColor3 color);
00373
00374 LColor3 GetColor();
00375
00376 void SetSpotlight(bool value);
00377
00378 bool GetSpotlight();
00379
00380 void SetTarget(LVector3 target);
00381
00382 LVector3 GetTarget();
00383
00384 void SetHotspot(float value);
00385
00386 float GetHotspot();
00387
00388 void SetFalloff(float value);
00389
00390 float GetFalloff();
00391
00392 void SetAttenuationstart(float value);
00393
00394 float GetAttenuationstart();
00395
00396 void SetAttenuationend(float value);
00397
00398 float GetAttenuationend();
00399 protected:
00400 LVector3 m_pos;
00401 LColor3 m_color;
00402 bool m_spotlight;
00403 LVector3 m_target;
00404 float m_hotspot;
00405 float m_falloff;
00406 float m_attenuationstart;
00407 float m_attenuationend;
00408 };
00409
00410
00411
00412 class LImporter
00413 {
00414 public:
00415
00416 LImporter();
00417
00418 virtual ~LImporter();
00419
00420 virtual bool Load(std::istream &is) = 0;
00421 virtual bool Load(const char *filename) = 0;
00422
00423 uint GetMeshCount();
00424
00425 uint GetLightCount();
00426
00427 uint GetMaterialCount();
00428
00429 uint GetCameraCount();
00430
00431 LMesh& GetMesh(uint index);
00432
00433 LCamera& GetCamera(uint index);
00434
00435 LLight& GetLight(uint index);
00436
00437 LMaterial& GetMaterial(uint index);
00438
00439 LMaterial* FindMaterial(const std::string &name);
00440
00441
00442 LMesh* FindMesh(const std::string &name);
00443
00444 LLight* FindCamera(const std::string &name);
00445
00446 LLight* FindLight(const std::string &name);
00447
00448 void SetOptimizationLevel(LOptimizationLevel value);
00449
00450 LOptimizationLevel GetOptimizationLevel();
00451 protected:
00452
00453 std::vector<LCamera> m_cameras;
00454
00455 std::vector<LLight> m_lights;
00456
00457 std::vector<LMesh> m_meshes;
00458
00459 std::vector<LMaterial> m_materials;
00460
00461 LOptimizationLevel m_optLevel;
00462
00463 virtual void Clear();
00464 };
00465
00466
00467 class L3DS : public LImporter
00468 {
00469 public:
00470
00471 L3DS();
00472
00473 L3DS(const char *filename);
00474
00475 virtual ~L3DS();
00476
00477 virtual bool Load(std::istream &is);
00478 virtual bool Load(const char *filename);
00479 protected:
00480
00481 char m_objName[100];
00482
00483 bool m_eof;
00484
00485 unsigned char *m_buffer;
00486
00487 uint m_bufferSize;
00488
00489 uint m_pos;
00490
00491
00492 short ReadShort();
00493
00494 int ReadInt();
00495
00496 char ReadChar();
00497
00498 float ReadFloat();
00499
00500 byte ReadByte();
00501
00502 int ReadASCIIZ(char *buf, int max_count);
00503
00504 void Seek(int offset, int origin);
00505
00506 uint Pos();
00507
00508
00509 LChunk ReadChunk();
00510
00511 bool FindChunk(LChunk &target, const LChunk &parent);
00512
00513 void SkipChunk(const LChunk &chunk);
00514
00515 void GotoChunk(const LChunk &chunk);
00516
00517
00518 LColor3 ReadColor(const LChunk &chunk);
00519
00520 float ReadPercentage(const LChunk &chunk);
00521
00522 bool Read3DS();
00523
00524 void ReadLight(const LChunk &parent);
00525
00526 void ReadCamera(const LChunk &parent);
00527
00528 void ReadMesh(const LChunk &parent);
00529
00530 void ReadFaceList(const LChunk &chunk, LMesh &mesh);
00531
00532 void ReadMaterial(const LChunk &parent);
00533
00534 void ReadMap(const LChunk &chunk, LMap& map);
00535
00536 void ReadKeyframeData(const LChunk &parent);
00537
00538 long ReadKeyheader();
00539 };
00540
00541
00542
00543 #endif