00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef CFileLoaderOBJH
00024 #define CFileLoaderOBJH
00025
00026 #include "math/CMatrix3d.h"
00027 #include "math/CVector3d.h"
00028 #include "graphics/CVertex.h"
00029 #include "graphics/CTriangle.h"
00030 #include "graphics/CMaterial.h"
00031 #include "graphics/CTexture2D.h"
00032 #include "scenegraph/CMesh.h"
00033 #include "scenegraph/CWorld.h"
00034 #include "scenegraph/CLight.h"
00035 #include <string>
00036 #include <stdio.h>
00037 #include <map>
00038
00039
00040
00048
00049
00050
00051
00052
00053
00059 bool cLoadFileOBJ(cMesh* iMesh, const string& iFileName);
00060
00067 extern bool g_objLoaderShouldGenerateExtraVertices;
00068
00069
00070
00071 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00072
00073
00074
00075
00076
00077
00079 struct vertexIndexSet
00080 {
00081 int vIndex;
00082 int nIndex;
00083 int tIndex;
00084
00085 vertexIndexSet()
00086 {
00087 vIndex = nIndex = tIndex = 0;
00088 }
00089 vertexIndexSet(int vIndex, int nIndex, int tIndex)
00090 {
00091 this->vIndex = vIndex;
00092 this->nIndex = nIndex;
00093 this->tIndex = tIndex;
00094 }
00095 vertexIndexSet(int vIndex)
00096 {
00097 this->vIndex = vIndex;
00098 nIndex = tIndex = 0;
00099 }
00100 };
00101
00102
00103
00104 struct ltVertexIndexSet
00105 {
00106 bool operator()(vertexIndexSet v1, vertexIndexSet v2) const
00107 {
00108 if (v1.vIndex < v2.vIndex) return 1;
00109 if (v2.vIndex < v1.vIndex) return 0;
00110 if (v1.nIndex < v2.nIndex) return 1;
00111 if (v2.nIndex < v1.nIndex) return 0;
00112 if (v1.tIndex < v2.tIndex) return 1;
00113 return 0;
00114 }
00115 };
00116
00117
00118
00119 typedef std::map<vertexIndexSet,unsigned int,ltVertexIndexSet> vertexIndexSet_uint_map;
00120
00121
00122
00123
00124
00125
00126
00127
00128 #define CHAI_OBJ_VERTEX_ID "v"
00129 #define CHAI_OBJ_TEXCOORD_ID "vt"
00130 #define CHAI_OBJ_NORMAL_ID "vn"
00131 #define CHAI_OBJ_FACE_ID "f"
00132 #define CHAI_OBJ_COMMENT_ID "#"
00133 #define CHAI_OBJ_MTL_LIB_ID "mtllib"
00134 #define CHAI_OBJ_USE_MTL_ID "usemtl"
00135 #define CHAI_OBJ_NAME_ID "g"
00136
00137
00138 #define CHAI_OBJ_NEW_MTL_ID "newmtl"
00139 #define CHAI_OBJ_MTL_TEXTURE_ID "map_Kd"
00140 #define CHAI_OBJ_MTL_AMBIENT_ID "Ka"
00141 #define CHAI_OBJ_MTL_DIFFUSE_ID "Kd"
00142 #define CHAI_OBJ_MTL_SPECULAR_ID "Ks"
00143 #define CHAI_OBJ_MTL_SHININESS_ID "Ns"
00144 #define CHAI_OBJ_MTL_ALPHA_ID "Tr"
00145 #define CHAI_OBJ_MTL_ALPHA_ID_ALT "d"
00146
00147
00148 #define CHAI_OBJ_MAX_STR_SIZE 1024
00149
00150
00151 #define CHAI_OBJ_MAX_VERTICES 256
00152
00153
00154 struct cOBJFileInfo
00155 {
00156 unsigned int m_vertexCount;
00157 unsigned int m_texCoordCount;
00158 unsigned int m_normalCount;
00159 unsigned int m_faceCount;
00160 unsigned int m_materialCount;
00161 };
00162
00163
00164 struct cFace
00165 {
00166 unsigned int m_numVertices;
00167 unsigned int m_materialIndex;
00168
00169
00170 int m_groupIndex;
00171
00172 int *m_pVertexIndices;
00173 cVector3d *m_pVertices;
00174 int *m_pNormalIndices;
00175 cVector3d *m_pNormals;
00176 int *m_pTextureIndices;
00177 cVector3d *m_pTexCoords;
00178 };
00179
00180
00181 struct cMaterialInfo
00182 {
00183 char m_name[1024];
00184 char m_texture[CHAI_SIZE_PATH];
00185 int m_textureID;
00186 float m_diffuse[3];
00187 float m_ambient[3];
00188 float m_specular[3];
00189 float m_emmissive[3];
00190 float m_alpha;
00191 float m_shininess;
00192
00193 cMaterialInfo() {
00194 m_name[0] = '\0';
00195 m_texture[0] = '\0';
00196 m_textureID = -1;
00197 m_diffuse[0] = m_diffuse[1] = m_diffuse[2] = 0.8f;
00198 m_ambient[0] = m_ambient[1] = m_ambient[2] = 0.8f;
00199 m_specular[0] = m_specular[1] = m_specular[2] = 0.3f;
00200 m_emmissive[0] = m_emmissive[1] = m_emmissive[2] = 0.0f;
00201 m_shininess = 0;
00202 m_alpha = 1.0f;
00203 }
00204 };
00205
00206
00207
00215
00216 class cOBJModel
00217 {
00218 public:
00219
00220
00221
00222
00223
00225 cOBJModel();
00226
00228 ~cOBJModel();
00229
00230
00231
00232
00233
00235 bool LoadModel(const char szFileName[]);
00236
00237
00238
00239
00240
00241
00243 cVector3d* m_pVertices;
00244
00246 cFace* m_pFaces;
00247
00249 cVector3d* m_pNormals;
00250
00252 cVector3d* m_pTexCoords;
00253
00255 cMaterialInfo* m_pMaterials;
00256
00258 cOBJFileInfo m_OBJInfo;
00259
00261 vector<char*> m_groupNames;
00262
00263
00264 private:
00265
00266
00267
00268
00270 void readNextString(char a_string[], FILE *hStream);
00271
00273 void getTokenParameter(char a_string[], const unsigned int a_strSize, FILE *a_hFile);
00274
00276 void makePath(char a_fileAndPath[]);
00277
00279 bool loadMaterialLib(const char a_fFileName[], cMaterialInfo *a_pMaterials,
00280 unsigned int *a_curMaterialIndex, char a_basePath[]);
00281
00283 void parseFaceString(char a_faceString[], cFace *a_faceOut, const cVector3d *a_pVertices,
00284 const cVector3d *a_pNormals, const cVector3d *a_pTexCoords, const unsigned int a_materialIndex);
00285
00287 void getFileInfo(FILE *a_hStream, cOBJFileInfo *a_stat, const char a_constBasePath[]);
00288 };
00289
00291 unsigned int getVertexIndex(cMesh* a_Mesh,
00292 cOBJModel* a_model,
00293 vertexIndexSet_uint_map* a_VertexMap,
00294 vertexIndexSet& vis);
00295
00296
00297 #endif // DOXYGEN_SHOULD_SKIP_THIS
00298
00299 #endif
00300
00301
00302