#include "BinaryLoader.h" #include "Matrix4x4.h" #include "Geometry.h" #include "SceneEntity.h" #include "Material.h" #include "Texture.h" //#include "gzstream.h" using namespace std; namespace CHCDemoEngine { BinaryLoader::~BinaryLoader() { /** delete all maps. */ std::map::iterator it, it_end = mTextureTable.end(); for (it = mTextureTable.begin(); it != it_end; ++ it) { Texture *tex = (*it).second; delete tex; } std::map::iterator nit, nit_end = mMaterialTable.end(); for (nit = mMaterialTable.begin(); nit != nit_end; ++ nit) { Material *mat = (*nit).second; delete mat; } std::map::iterator git, git_end = mGeometryTable.end(); for (git = mGeometryTable.begin(); git != git_end; ++git) { Geometry *geom = (*git).second; delete geom; } } //SceneEntity *BinaryLoader::LoadSceneEntity(igzstream &str) SceneEntity *BinaryLoader::LoadSceneEntity(ifstream &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(); //*trafo = IdentityMatrix(); str.read(reinterpret_cast(trafo->x), sizeof(Matrix4x4)); //str.read(reinterpret_cast(trafo), sizeof(Matrix4x4)); //cout << "m:\n" << *trafo<(&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; } cout << "loaded " << mTextureTable.size() << " textures" << endl; } void BinaryLoader::LoadShapes(ifstream &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; } cout << "loaded " << mGeometryTable.size() << " shapes" << endl; } //Appearance *BinaryLoader::LoadMaterial(igzstream &str) Material *BinaryLoader::LoadMaterial(ifstream &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->mSpecularColor), sizeof(Vector3)); } return mat; } //Geometry *BinaryLoader::LoadGeometry(igzstream &str) Geometry *BinaryLoader::LoadGeometry(ifstream &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) { //cout << "loading texcoords of size " << texCoordCount << endl; texcoords = new float[texCoordCount * 2]; str.read(reinterpret_cast(texcoords), sizeof(float) * vertexCount * 2); } else texcoords = NULL; return new Geometry(vertices, normals, texcoords, vertexCount, true); } void BinaryLoader::LoadSceneEntities(ifstream &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); ent->id = i; entities[i] = ent; } cout << "loaded " << entityCount << " scene entities" << endl; } bool BinaryLoader::Load(const std::string &filename, SceneEntityContainer &entities) { //clear_textures(); // clear the texture table //igzstream str(filename.c_str());//, ios::binary); ifstream istr(filename.c_str(), ios::binary); 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; return true; } }