[2795] | 1 | #include "ResourceManager.h"
|
---|
[2755] | 2 | #include "Matrix4x4.h"
|
---|
| 3 | #include "Geometry.h"
|
---|
[2756] | 4 | #include "SceneEntity.h"
|
---|
[2755] | 5 | #include "Material.h"
|
---|
[2756] | 6 | #include "Texture.h"
|
---|
[2795] | 7 | #include "gzstream.h"
|
---|
[2823] | 8 | #include "Matrix4x4.h"
|
---|
| 9 | #include "Vector3.h"
|
---|
[2840] | 10 | #include "Transform3.h"
|
---|
[2842] | 11 | #include "Shape.h"
|
---|
[2844] | 12 | #include "LODInfo.h"
|
---|
[2747] | 13 |
|
---|
| 14 |
|
---|
| 15 | using namespace std;
|
---|
| 16 |
|
---|
| 17 |
|
---|
[2776] | 18 | namespace CHCDemoEngine
|
---|
[2755] | 19 | {
|
---|
[2747] | 20 |
|
---|
[2755] | 21 |
|
---|
[2795] | 22 | ResourceManager::~ResourceManager()
|
---|
[2781] | 23 | {
|
---|
[2795] | 24 | // delete all resources.
|
---|
| 25 | CLEAR_CONTAINER(mSceneEntities);
|
---|
| 26 | CLEAR_CONTAINER(mGeometry);
|
---|
| 27 | CLEAR_CONTAINER(mMaterials);
|
---|
| 28 | CLEAR_CONTAINER(mTextures);
|
---|
| 29 | CLEAR_CONTAINER(mTrafos);
|
---|
[2842] | 30 | CLEAR_CONTAINER(mShapes);
|
---|
[3019] | 31 | CLEAR_CONTAINER(mLODs);
|
---|
[2781] | 32 | }
|
---|
| 33 |
|
---|
[2795] | 34 |
|
---|
| 35 | SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
|
---|
[2747] | 36 | {
|
---|
[2764] | 37 | bool hasTrafo;
|
---|
| 38 | str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
| 39 |
|
---|
[2823] | 40 | SceneEntity *sceneGeom;
|
---|
[2957] | 41 | Transform3 *trafo;
|
---|
[2823] | 42 |
|
---|
[2764] | 43 | if (!hasTrafo)
|
---|
| 44 | {
|
---|
[2961] | 45 | // identity
|
---|
[2957] | 46 | trafo = new Transform3();
|
---|
[2764] | 47 | }
|
---|
| 48 | else
|
---|
| 49 | {
|
---|
[2957] | 50 | Matrix4x4 m;
|
---|
| 51 | str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
|
---|
| 52 |
|
---|
| 53 | trafo = new Transform3(m);
|
---|
[2764] | 54 | }
|
---|
| 55 |
|
---|
[2840] | 56 | mTrafos.push_back(trafo);
|
---|
[2844] | 57 |
|
---|
[2839] | 58 | sceneGeom = new SceneEntity(trafo);
|
---|
[2825] | 59 |
|
---|
[2852] | 60 |
|
---|
[2844] | 61 | ///////////////
|
---|
| 62 | //-- load lod levels
|
---|
[2842] | 63 |
|
---|
[2853] | 64 | // note: lods must be ordered from smallest distance to largest
|
---|
[2844] | 65 | int numLODs;
|
---|
| 66 | str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
[2842] | 67 |
|
---|
[2844] | 68 | for (int i = 0; i < numLODs; ++ i)
|
---|
| 69 | {
|
---|
[2853] | 70 | float dist;
|
---|
| 71 | str.read(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
[2844] | 72 |
|
---|
| 73 | int numShapes;
|
---|
| 74 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
| 75 |
|
---|
[2853] | 76 | LODLevel *lodLevel = new LODLevel(dist);
|
---|
[2844] | 77 |
|
---|
| 78 | for (int j = 0; j < numShapes; ++ j)
|
---|
| 79 | {
|
---|
| 80 | int shapeId;
|
---|
| 81 | str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
| 82 |
|
---|
| 83 | Geometry *geom = mGeometryTable[shapeId];
|
---|
| 84 | Material *mat = mMaterialTable[shapeId];
|
---|
| 85 |
|
---|
| 86 | // create shape
|
---|
| 87 | Shape *shape = new Shape(geom, mat, sceneGeom);
|
---|
| 88 |
|
---|
| 89 | mShapes.push_back(shape);
|
---|
| 90 |
|
---|
| 91 | sceneGeom->AddShape(shape);
|
---|
[2848] | 92 | lodLevel->AddShape(shape);
|
---|
[2844] | 93 | }
|
---|
| 94 |
|
---|
[3019] | 95 | mLODs.push_back(lodLevel);
|
---|
[2844] | 96 | sceneGeom->AddLODLevel(lodLevel);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[2755] | 99 | return sceneGeom;
|
---|
[2747] | 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
[2795] | 103 | void ResourceManager::LoadTextures(igzstream &str)
|
---|
[2757] | 104 | {
|
---|
| 105 | int numTextures;
|
---|
| 106 | str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
|
---|
[2961] | 107 |
|
---|
[2757] | 108 | for (int i = 0; i < numTextures; ++ i)
|
---|
| 109 | {
|
---|
[2961] | 110 | // load texture name
|
---|
[2757] | 111 | int texnameSize;
|
---|
| 112 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
| 113 |
|
---|
| 114 | char *texname = new char[texnameSize];
|
---|
| 115 | str.read(texname, sizeof(char) * texnameSize);
|
---|
| 116 |
|
---|
[2784] | 117 | Texture *tex = new Texture(model_path + texname);
|
---|
[2757] | 118 |
|
---|
[2846] | 119 | int boundS, boundT;
|
---|
| 120 |
|
---|
| 121 | str.read(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
| 122 | str.read(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
| 123 |
|
---|
| 124 | tex->SetBoundaryModeS(boundS);
|
---|
| 125 | tex->SetBoundaryModeT(boundT);
|
---|
| 126 |
|
---|
| 127 | tex->Create();
|
---|
| 128 |
|
---|
[2764] | 129 | mTextureTable[i] = tex;
|
---|
[2795] | 130 | mTextures.push_back(tex);
|
---|
[2757] | 131 | }
|
---|
[2764] | 132 |
|
---|
[2800] | 133 | cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
|
---|
[2757] | 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
[2795] | 137 | void ResourceManager::LoadShapes(igzstream &str)
|
---|
[2764] | 138 | {
|
---|
| 139 | int numShapes;
|
---|
| 140 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
| 141 |
|
---|
| 142 | for (int i = 0; i < numShapes; ++ i)
|
---|
| 143 | {
|
---|
| 144 | Geometry *geom = LoadGeometry(str);
|
---|
| 145 | Material *mat = LoadMaterial(str);
|
---|
| 146 |
|
---|
| 147 | mGeometryTable[i] = geom;
|
---|
[2842] | 148 | mMaterialTable[i] = mat;
|
---|
[2795] | 149 |
|
---|
| 150 | mGeometry.push_back(geom);
|
---|
| 151 | mMaterials.push_back(mat);
|
---|
[2764] | 152 | }
|
---|
| 153 |
|
---|
[2800] | 154 | cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
|
---|
[2764] | 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
[2795] | 158 | Material *ResourceManager::LoadMaterial(igzstream &str)
|
---|
[2747] | 159 | {
|
---|
[2756] | 160 | // default material
|
---|
[2755] | 161 | Material *mat = new Material();
|
---|
[2756] | 162 |
|
---|
[2747] | 163 | // texture
|
---|
[2757] | 164 | int texId;
|
---|
| 165 | str.read(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
[2747] | 166 |
|
---|
[2982] | 167 | if (texId >= 0)
|
---|
[2757] | 168 | mat->SetTexture(mTextureTable[texId]);
|
---|
[2764] | 169 |
|
---|
[2769] | 170 | str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool));
|
---|
[2747] | 171 |
|
---|
[2845] | 172 | str.read(reinterpret_cast<char *>(&mat->mCullFaceEnabled), sizeof(bool));
|
---|
[2844] | 173 |
|
---|
[2747] | 174 | // material
|
---|
[2769] | 175 | bool hasMaterial;
|
---|
| 176 | str.read(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
[2747] | 177 |
|
---|
| 178 | if (hasMaterial)
|
---|
| 179 | {
|
---|
[2769] | 180 | // only write rgb part of the material
|
---|
| 181 | str.read(reinterpret_cast<char *>(&mat->mAmbientColor), sizeof(Vector3));
|
---|
| 182 | str.read(reinterpret_cast<char *>(&mat->mDiffuseColor), sizeof(Vector3));
|
---|
[2795] | 183 | str.read(reinterpret_cast<char *>(&mat->mEmmisiveColor), sizeof(Vector3));
|
---|
[2769] | 184 | str.read(reinterpret_cast<char *>(&mat->mSpecularColor), sizeof(Vector3));
|
---|
[2747] | 185 | }
|
---|
| 186 |
|
---|
[2755] | 187 | return mat;
|
---|
[2747] | 188 | }
|
---|
| 189 |
|
---|
| 190 |
|
---|
[2795] | 191 | Geometry *ResourceManager::LoadGeometry(igzstream &str)
|
---|
[2747] | 192 | {
|
---|
[2756] | 193 | Vector3 *vertices;
|
---|
| 194 | Vector3 *normals;
|
---|
[2980] | 195 | Texcoord2 *texcoords;
|
---|
[2747] | 196 |
|
---|
| 197 |
|
---|
[2756] | 198 | //////////////
|
---|
| 199 | //-- read in vertices
|
---|
[2747] | 200 |
|
---|
[2756] | 201 | int vertexCount;
|
---|
| 202 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
[2747] | 203 |
|
---|
| 204 | // end of file reached
|
---|
[2961] | 205 | if (str.eof()) return NULL;
|
---|
[2747] | 206 |
|
---|
[2757] | 207 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
[2747] | 208 |
|
---|
[2756] | 209 | vertices = new Vector3[vertexCount];
|
---|
| 210 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 211 |
|
---|
[2756] | 212 | normals = new Vector3[vertexCount];
|
---|
| 213 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 214 |
|
---|
[2756] | 215 | int texCoordCount;
|
---|
| 216 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
[2747] | 217 |
|
---|
[2756] | 218 | if (texCoordCount)
|
---|
[2747] | 219 | {
|
---|
[2980] | 220 | texcoords = new Texcoord2[texCoordCount];
|
---|
| 221 | str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
|
---|
[2747] | 222 | }
|
---|
[2756] | 223 | else
|
---|
[2811] | 224 | {
|
---|
[2756] | 225 | texcoords = NULL;
|
---|
[2811] | 226 | }
|
---|
[2747] | 227 |
|
---|
[2982] | 228 | return new Geometry(vertices, normals, texcoords, vertexCount, true);
|
---|
| 229 | //return new Geometry(vertices, normals, texcoords, vertexCount, false);
|
---|
[2747] | 230 | }
|
---|
| 231 |
|
---|
| 232 |
|
---|
[2795] | 233 | void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities)
|
---|
[2747] | 234 | {
|
---|
[2764] | 235 | int entityCount;
|
---|
| 236 | str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
| 237 |
|
---|
[2963] | 238 | entities.reserve(entityCount);
|
---|
[2764] | 239 |
|
---|
| 240 | for (int i = 0; i < entityCount; ++ i)
|
---|
| 241 | {
|
---|
| 242 | SceneEntity *ent = LoadSceneEntity(str);
|
---|
[2842] | 243 |
|
---|
[2963] | 244 | // return loaded entities
|
---|
| 245 | entities.push_back(ent);
|
---|
| 246 | // also store internally
|
---|
[2795] | 247 | mSceneEntities.push_back(ent);
|
---|
[2764] | 248 | }
|
---|
| 249 |
|
---|
| 250 | cout << "loaded " << entityCount << " scene entities" << endl;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 |
|
---|
[2795] | 254 | bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities)
|
---|
[2764] | 255 | {
|
---|
[2795] | 256 | igzstream istr(filename.c_str());
|
---|
[2747] | 257 |
|
---|
[2756] | 258 | if (!istr.is_open())
|
---|
[2747] | 259 | return false;
|
---|
| 260 |
|
---|
[2764] | 261 | cout << "loading textures" << endl;
|
---|
| 262 |
|
---|
[2757] | 263 | // load the texture table
|
---|
| 264 | LoadTextures(istr);
|
---|
| 265 |
|
---|
[2764] | 266 | cout << "loading shapes (geometry + materials)" << endl;
|
---|
[2747] | 267 |
|
---|
[2764] | 268 | // load the shapees
|
---|
| 269 | LoadShapes(istr);
|
---|
[2762] | 270 |
|
---|
[2764] | 271 | cout << "loading scene entites" << endl;
|
---|
| 272 | LoadSceneEntities(istr, entities);
|
---|
[2763] | 273 |
|
---|
[2756] | 274 | cout << "bin loading finished" << endl;
|
---|
[2747] | 275 |
|
---|
[2795] | 276 | // clean up
|
---|
| 277 | mTextureTable.clear();
|
---|
| 278 | mGeometryTable.clear();
|
---|
| 279 | mMaterialTable.clear();
|
---|
| 280 |
|
---|
[2756] | 281 | return true;
|
---|
[2747] | 282 | }
|
---|
| 283 |
|
---|
| 284 | }
|
---|