[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 |
|
---|
[2823] | 39 | SceneEntity *sceneGeom;
|
---|
[2957] | 40 | Transform3 *trafo;
|
---|
[2823] | 41 |
|
---|
[2764] | 42 | if (!hasTrafo)
|
---|
| 43 | {
|
---|
[2961] | 44 | // identity
|
---|
[2957] | 45 | trafo = new Transform3();
|
---|
[2764] | 46 | }
|
---|
| 47 | else
|
---|
| 48 | {
|
---|
[2957] | 49 | Matrix4x4 m;
|
---|
| 50 | str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
|
---|
| 51 |
|
---|
| 52 | trafo = new Transform3(m);
|
---|
[2764] | 53 | }
|
---|
| 54 |
|
---|
[2840] | 55 | mTrafos.push_back(trafo);
|
---|
[2844] | 56 |
|
---|
[2839] | 57 | sceneGeom = new SceneEntity(trafo);
|
---|
[2825] | 58 |
|
---|
[2852] | 59 |
|
---|
[2844] | 60 | ///////////////
|
---|
| 61 | //-- load lod levels
|
---|
[2842] | 62 |
|
---|
[2853] | 63 | // note: lods must be ordered from smallest distance to largest
|
---|
[2844] | 64 | int numLODs;
|
---|
| 65 | str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
[2842] | 66 |
|
---|
[2844] | 67 | for (int i = 0; i < numLODs; ++ i)
|
---|
| 68 | {
|
---|
[2853] | 69 | float dist;
|
---|
| 70 | str.read(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
[2844] | 71 |
|
---|
| 72 | int numShapes;
|
---|
| 73 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
| 74 |
|
---|
[2961] | 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 |
|
---|
| 95 | sceneGeom->AddLODLevel(lodLevel);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[2755] | 98 | return sceneGeom;
|
---|
[2747] | 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
[2795] | 102 | void ResourceManager::LoadTextures(igzstream &str)
|
---|
[2757] | 103 | {
|
---|
| 104 | int numTextures;
|
---|
| 105 | str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
|
---|
[2961] | 106 |
|
---|
[2757] | 107 | for (int i = 0; i < numTextures; ++ i)
|
---|
| 108 | {
|
---|
[2961] | 109 | // load texture name
|
---|
[2757] | 110 | int texnameSize;
|
---|
| 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
|
---|
[2961] | 204 | if (str.eof()) return NULL;
|
---|
[2747] | 205 |
|
---|
[2757] | 206 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
[2747] | 207 |
|
---|
[2756] | 208 | vertices = new Vector3[vertexCount];
|
---|
| 209 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 210 |
|
---|
[2756] | 211 | normals = new Vector3[vertexCount];
|
---|
| 212 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 213 |
|
---|
[2756] | 214 | int texCoordCount;
|
---|
| 215 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
[2747] | 216 |
|
---|
[2756] | 217 | if (texCoordCount)
|
---|
[2747] | 218 | {
|
---|
[2756] | 219 | texcoords = new float[texCoordCount * 2];
|
---|
[2961] | 220 | str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * texCoordCount * 2);
|
---|
[2747] | 221 | }
|
---|
[2756] | 222 | else
|
---|
[2811] | 223 | {
|
---|
[2756] | 224 | texcoords = NULL;
|
---|
[2811] | 225 | }
|
---|
[2833] | 226 |
|
---|
[2747] | 227 |
|
---|
[2833] | 228 | return new Geometry(vertices, normals, texcoords, vertexCount, true);
|
---|
[2747] | 229 | }
|
---|
| 230 |
|
---|
| 231 |
|
---|
[2795] | 232 | void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities)
|
---|
[2747] | 233 | {
|
---|
[2764] | 234 | int entityCount;
|
---|
| 235 | str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
| 236 |
|
---|
[2963] | 237 | entities.reserve(entityCount);
|
---|
[2764] | 238 |
|
---|
| 239 | for (int i = 0; i < entityCount; ++ i)
|
---|
| 240 | {
|
---|
| 241 | SceneEntity *ent = LoadSceneEntity(str);
|
---|
[2842] | 242 |
|
---|
[2963] | 243 | // return loaded entities
|
---|
| 244 | entities.push_back(ent);
|
---|
| 245 | // also store internally
|
---|
[2795] | 246 | mSceneEntities.push_back(ent);
|
---|
[2764] | 247 | }
|
---|
| 248 |
|
---|
| 249 | cout << "loaded " << entityCount << " scene entities" << endl;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 |
|
---|
[2795] | 253 | bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities)
|
---|
[2764] | 254 | {
|
---|
[2795] | 255 | igzstream istr(filename.c_str());
|
---|
[2747] | 256 |
|
---|
[2756] | 257 | if (!istr.is_open())
|
---|
[2747] | 258 | return false;
|
---|
| 259 |
|
---|
[2764] | 260 | cout << "loading textures" << endl;
|
---|
| 261 |
|
---|
[2757] | 262 | // load the texture table
|
---|
| 263 | LoadTextures(istr);
|
---|
| 264 |
|
---|
[2764] | 265 | cout << "loading shapes (geometry + materials)" << endl;
|
---|
[2747] | 266 |
|
---|
[2764] | 267 | // load the shapees
|
---|
| 268 | LoadShapes(istr);
|
---|
[2762] | 269 |
|
---|
[2764] | 270 | cout << "loading scene entites" << endl;
|
---|
| 271 | LoadSceneEntities(istr, entities);
|
---|
[2763] | 272 |
|
---|
[2756] | 273 | cout << "bin loading finished" << endl;
|
---|
[2747] | 274 |
|
---|
[2795] | 275 | // clean up
|
---|
| 276 | mTextureTable.clear();
|
---|
| 277 | mGeometryTable.clear();
|
---|
| 278 | mMaterialTable.clear();
|
---|
| 279 |
|
---|
[2756] | 280 | return true;
|
---|
[2747] | 281 | }
|
---|
| 282 |
|
---|
| 283 | }
|
---|