Optln

OBJ Loader Header

Mar 31st, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #ifndef OBJ2SMS_H
  2. #define OBJ2SMS_H
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <fstream>
  7. #include <vector>
  8. #include <rapidxml/rapidxml.hpp>
  9. #include <rapidxml/rapidxml_print.hpp>
  10. #include "SheqMath.h"
  11.  
  12. using SheqTech::Vector2;
  13. using SheqTech::Vector3;
  14. using SheqTech::Vector4;
  15.  
  16. enum MaterialFlags
  17. {
  18.     BE_MF_HAS_DIFFUSE_MAP = 0x00000001,
  19.     BE_MF_HAS_SPECULAR_MAP = 0x0000002,
  20.     BE_MF_HAS_NORMAL_MAP = 0x00000004,
  21.     BE_MF_HAS_AMBIENT_MAP = 0x00000008,
  22.     BE_MF_HAS_ALPHA_MAP = 0x00000010
  23. };
  24.  
  25. struct CacheEntry
  26. {
  27.     unsigned int index;
  28.     CacheEntry* next;
  29. };
  30.  
  31. struct OBJMaterial
  32. {
  33.     OBJMaterial():  name("Default"),
  34.         illumModel(2),
  35.         ambientFactor(Vector3(0.2f, 0.2f, 0.2f)),
  36.         diffuseFactor(Vector3(0.3f, 0.3f, 0.3f)),
  37.         specularFactor(Vector3(0.2f, 0.2f, 0.2f)),
  38.         emissiveFactor(Vector3(0.0f, 0.0f, 0.0f)),
  39.         shininess(0.0f),
  40.         transparency(0.0f),
  41.         hasDiffuseMap(false),
  42.         hasSpecularMap(false),
  43.         hasNormalMap(false),
  44.         hasAlphaMap(false),
  45.         hasAmbientMap(false)
  46.  
  47.     {}
  48.  
  49.     std::string name;
  50.     int         illumModel;
  51.     Vector3     ambientFactor;
  52.     Vector3     diffuseFactor;
  53.     Vector3     specularFactor;
  54.     Vector3     emissiveFactor;
  55.     float       shininess;
  56.     float       transparency;
  57.     std::string ambientMap;
  58.     std::string diffuseMap;
  59.     std::string specularMap;
  60.     std::string normalMap;
  61.     std::string alphaMap;
  62.     bool        hasDiffuseMap;
  63.     bool        hasSpecularMap;
  64.     bool        hasNormalMap;
  65.     bool        hasAlphaMap;
  66.     bool        hasAmbientMap;
  67. };
  68.  
  69. struct OBJVertex
  70. {
  71.     Vector3 position;
  72.     Vector4 tangent;
  73.     Vector3 bitangent;
  74.     Vector3 normal;
  75.     Vector2 texCoord;
  76. };
  77.  
  78. struct OBJFace
  79. {
  80.     unsigned int position;
  81.     unsigned int texCoord;
  82.     unsigned int normal;
  83. };
  84.  
  85. struct SMSMaterial
  86. {
  87.     char    name[128];
  88.     int     flags;
  89.     Vector3 ambientFactor;
  90.     Vector3 diffuseFactor;
  91.     Vector3 specularFactor;
  92.     Vector3 emissiveFactor;
  93.     float   shininess;
  94.     float   transparency;
  95.     char    ambientMap[128];
  96.     char    diffuseMap[128];
  97.     char    specularMap[128];
  98.     char    normalMap[128];
  99.     char    alphaMap[128];
  100. };
  101.  
  102. struct SMSVertex
  103. {
  104.     Vector3 position;
  105.     Vector4 tangent;
  106.     Vector3 normal;
  107.     Vector2 texCoord;
  108. };
  109.  
  110. struct Submesh
  111. {
  112.     SMSMaterial*                material;
  113.     std::vector<OBJVertex>      vertexData;
  114.     std::vector<unsigned short> indexData;
  115.     std::vector<CacheEntry*>    vertexCache;
  116. };
  117.  
  118. class OBJ2SMS
  119. {
  120. public:
  121.     OBJ2SMS();
  122.     ~OBJ2SMS();
  123.  
  124.     bool load(std::string filename);
  125.     bool writeSMS(std::string filename);
  126.     void writeMaterials();
  127.  
  128. private:
  129.     void            loadMTL(std::string filename);
  130.     void            getDirectory(std::string filename);
  131.     unsigned int    addVertex(unsigned int subsetNumber, unsigned int hash, OBJVertex* vertex);
  132.     void            generateTangents();
  133.     void            deleteCache();
  134.  
  135. private:
  136.     std::string                 m_directoryPath;
  137.     std::vector<Vector3>        m_vertexList;
  138.     std::vector<Vector2>        m_texCoordList;
  139.     std::vector<Vector3>        m_normalList;
  140.     std::vector<OBJMaterial>    m_materialLibrary;
  141.  
  142.     Submesh* m_submeshes;
  143.     unsigned int m_submeshCount;
  144. };
  145.  
  146. #endif // OBJ2SMS_H
Advertisement
Add Comment
Please, Sign In to add comment