#include "ResourceManager.h" #include "Matrix4x4.h" #include "Geometry.h" #include "SceneEntity.h" #include "Material.h" #include "Texture.h" #include "gzstream.h" #include "Matrix4x4.h" #include "Vector3.h" #include "Transform3.h" #include "Shape.h" #include "LODInfo.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); CLEAR_CONTAINER(mShapes); } SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str) { bool hasTrafo; str.read(reinterpret_cast(&hasTrafo), sizeof(bool)); Matrix4x4 *m; SceneEntity *sceneGeom; if (!hasTrafo) { m = NULL; } else { m = new Matrix4x4(); str.read(reinterpret_cast(m->x), sizeof(Matrix4x4)); } Transform3 *trafo = new Transform3(m); mTrafos.push_back(trafo); sceneGeom = new SceneEntity(trafo); /////////////// //-- load lod levels int numLODs; str.read(reinterpret_cast(&numLODs), sizeof(int)); //if (numLODs > 1) cout << "numlods: " << numLODs << endl; for (int i = 0; i < numLODs; ++ i) { float distance; str.read(reinterpret_cast(&distance), sizeof(float)); // hack distance = i * 20; int numShapes; str.read(reinterpret_cast(&numShapes), sizeof(int)); //if (numLODs > 1) cout << "dist: " << distance << " shapes: " << numShapes; LODLevel *lodLevel = new LODLevel(distance); for (int j = 0; j < numShapes; ++ j) { int shapeId; str.read(reinterpret_cast(&shapeId), sizeof(int)); Geometry *geom = mGeometryTable[shapeId]; Material *mat = mMaterialTable[shapeId]; // create shape Shape *shape = new Shape(geom, mat, sceneGeom); mShapes.push_back(shape); sceneGeom->AddShape(shape); lodLevel->AddShape(shape); } //if (numLODs > 1) cout << endl; sceneGeom->AddLODLevel(lodLevel); } 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); Texture *tex = new Texture(model_path + texname); int boundS, boundT; str.read(reinterpret_cast(&boundS), sizeof(int)); str.read(reinterpret_cast(&boundT), sizeof(int)); tex->SetBoundaryModeS(boundS); tex->SetBoundaryModeT(boundT); tex->Create(); 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)); str.read(reinterpret_cast(&mat->mCullFaceEnabled), 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; } 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(); //mShapesTable.clear(); return true; } }