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