#include "ResourceManager.h" #include "Matrix4x4.h" #include "Geometry.h" #include "SceneEntity.h" #include "Material.h" #include "Texture.h" #include "gzstream.h" using namespace std; namespace CHCDemoEngine { ResourceManager::~ResourceManager() { // delete all resources. CLEAR_CONTAINER(mSceneEntities); CLEAR_CONTAINER(mGeometry); CLEAR_CONTAINER(mMaterials); CLEAR_CONTAINER(mTextures); CLEAR_CONTAINER(mTrafos); } SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str) { // shape int shapeId; str.read(reinterpret_cast(&shapeId), sizeof(int)); Geometry *geom = mGeometryTable[shapeId]; Material *mat = mMaterialTable[shapeId]; bool hasTrafo; str.read(reinterpret_cast(&hasTrafo), sizeof(bool)); Matrix4x4 *trafo; if (!hasTrafo) { trafo = NULL; } else { trafo = new Matrix4x4(); str.read(reinterpret_cast(trafo->x), sizeof(Matrix4x4)); mTrafos.push_back(trafo); } SceneEntity *sceneGeom = new SceneEntity(geom, mat, trafo); return sceneGeom; } void ResourceManager::LoadTextures(igzstream &str) { int numTextures; str.read(reinterpret_cast(&numTextures), sizeof(int)); for (int i = 0; i < numTextures; ++ i) { // texture int texnameSize; //str.read(reinterpret_cast(&id), sizeof(int)); str.read(reinterpret_cast(&texnameSize), sizeof(int)); char *texname = new char[texnameSize]; str.read(texname, sizeof(char) * texnameSize); //cout << "loading texture " << texname << " with len " << texnameSize << " id: " << id << endl; Texture *tex = new Texture(model_path + texname); mTextureTable[i] = tex; mTextures.push_back(tex); } cout << "loaded " << (int)mTextureTable.size() << " textures" << endl; } void ResourceManager::LoadShapes(igzstream &str) { int numShapes; str.read(reinterpret_cast(&numShapes), sizeof(int)); for (int i = 0; i < numShapes; ++ i) { Geometry *geom = LoadGeometry(str); Material *mat = LoadMaterial(str); mGeometryTable[i] = geom; mMaterialTable[i] = mat; mGeometry.push_back(geom); mMaterials.push_back(mat); } cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl; } Material *ResourceManager::LoadMaterial(igzstream &str) { // default material Material *mat = new Material(); // texture int texId; str.read(reinterpret_cast(&texId), sizeof(int)); if (texId >= 0) mat->SetTexture(mTextureTable[texId]); str.read(reinterpret_cast(&mat->mAlphaTestEnabled), sizeof(bool)); // material bool hasMaterial; str.read(reinterpret_cast(&hasMaterial), sizeof(bool)); if (hasMaterial) { // only write rgb part of the material str.read(reinterpret_cast(&mat->mAmbientColor), sizeof(Vector3)); str.read(reinterpret_cast(&mat->mDiffuseColor), sizeof(Vector3)); str.read(reinterpret_cast(&mat->mEmmisiveColor), sizeof(Vector3)); str.read(reinterpret_cast(&mat->mSpecularColor), sizeof(Vector3)); } return mat; } Geometry *ResourceManager::LoadGeometry(igzstream &str) { Vector3 *vertices; Vector3 *normals; float *texcoords; ////////////// //-- read in vertices int vertexCount; str.read(reinterpret_cast(&vertexCount), sizeof(int)); // end of file reached if (str.eof()) return NULL; //cout << "vertexcount: " << vertexCount << endl; vertices = new Vector3[vertexCount]; str.read(reinterpret_cast(vertices), sizeof(Vector3) * vertexCount); normals = new Vector3[vertexCount]; str.read(reinterpret_cast(normals), sizeof(Vector3) * vertexCount); int texCoordCount; str.read(reinterpret_cast(&texCoordCount), sizeof(int)); if (texCoordCount) { texcoords = new float[texCoordCount * 2]; str.read(reinterpret_cast(texcoords), sizeof(float) * vertexCount * 2); } else { texcoords = NULL; } for (int i = 0; i < vertexCount; i += 3) { Triangle3 tri(vertices[i], vertices[i + 1], vertices[i + 2]); Vector3 n = tri.GetNormal(); normals[i + 0] = n; normals[i + 1] = n; normals[i + 2] = n; } return new Geometry(vertices, normals, texcoords, vertexCount, true); } void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities) { int entityCount; str.read(reinterpret_cast(&entityCount), sizeof(int)); entities.resize(entityCount); for (int i = 0; i < entityCount; ++ i) { SceneEntity *ent = LoadSceneEntity(str); entities[i] = ent; mSceneEntities.push_back(ent); } cout << "loaded " << entityCount << " scene entities" << endl; } bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities) { igzstream istr(filename.c_str()); if (!istr.is_open()) return false; cout << "loading textures" << endl; // load the texture table LoadTextures(istr); cout << "loading shapes (geometry + materials)" << endl; // load the shapees LoadShapes(istr); cout << "loading scene entites" << endl; LoadSceneEntities(istr, entities); cout << "bin loading finished" << endl; // clean up mTextureTable.clear(); mGeometryTable.clear(); mMaterialTable.clear(); return true; } }