[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"
|
---|
[2747] | 8 |
|
---|
| 9 |
|
---|
| 10 | using namespace std;
|
---|
| 11 |
|
---|
| 12 |
|
---|
[2776] | 13 | namespace CHCDemoEngine
|
---|
[2755] | 14 | {
|
---|
[2747] | 15 |
|
---|
[2755] | 16 |
|
---|
[2795] | 17 | ResourceManager::~ResourceManager()
|
---|
[2781] | 18 | {
|
---|
[2795] | 19 | // delete all resources.
|
---|
| 20 | CLEAR_CONTAINER(mSceneEntities);
|
---|
| 21 | CLEAR_CONTAINER(mGeometry);
|
---|
| 22 | CLEAR_CONTAINER(mMaterials);
|
---|
| 23 | CLEAR_CONTAINER(mTextures);
|
---|
| 24 | CLEAR_CONTAINER(mTrafos);
|
---|
[2781] | 25 | }
|
---|
| 26 |
|
---|
[2795] | 27 |
|
---|
| 28 | SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
|
---|
[2747] | 29 | {
|
---|
[2764] | 30 | // shape
|
---|
| 31 | int shapeId;
|
---|
| 32 | str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
[2747] | 33 |
|
---|
[2764] | 34 | Geometry *geom = mGeometryTable[shapeId];
|
---|
| 35 | Material *mat = mMaterialTable[shapeId];
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | bool hasTrafo;
|
---|
| 39 | str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
| 40 |
|
---|
| 41 | Matrix4x4 *trafo;
|
---|
| 42 |
|
---|
| 43 | if (!hasTrafo)
|
---|
| 44 | {
|
---|
| 45 | trafo = NULL;
|
---|
| 46 | }
|
---|
| 47 | else
|
---|
| 48 | {
|
---|
| 49 | trafo = new Matrix4x4();
|
---|
| 50 | str.read(reinterpret_cast<char *>(trafo->x), sizeof(Matrix4x4));
|
---|
[2795] | 51 | mTrafos.push_back(trafo);
|
---|
[2764] | 52 | }
|
---|
| 53 |
|
---|
[2756] | 54 | SceneEntity *sceneGeom = new SceneEntity(geom, mat, trafo);
|
---|
[2747] | 55 |
|
---|
[2755] | 56 | return sceneGeom;
|
---|
[2747] | 57 | }
|
---|
| 58 |
|
---|
| 59 |
|
---|
[2795] | 60 | void ResourceManager::LoadTextures(igzstream &str)
|
---|
[2757] | 61 | {
|
---|
| 62 | int numTextures;
|
---|
| 63 | str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
|
---|
| 64 |
|
---|
| 65 | for (int i = 0; i < numTextures; ++ i)
|
---|
| 66 | {
|
---|
| 67 | // texture
|
---|
| 68 | int texnameSize;
|
---|
| 69 |
|
---|
[2764] | 70 | //str.read(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
[2757] | 71 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
| 72 |
|
---|
| 73 | char *texname = new char[texnameSize];
|
---|
| 74 | str.read(texname, sizeof(char) * texnameSize);
|
---|
| 75 |
|
---|
[2760] | 76 | //cout << "loading texture " << texname << " with len " << texnameSize << " id: " << id << endl;
|
---|
[2784] | 77 | Texture *tex = new Texture(model_path + texname);
|
---|
[2757] | 78 |
|
---|
[2764] | 79 | mTextureTable[i] = tex;
|
---|
[2795] | 80 | mTextures.push_back(tex);
|
---|
[2757] | 81 | }
|
---|
[2764] | 82 |
|
---|
[2800] | 83 | cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
|
---|
[2757] | 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
[2795] | 87 | void ResourceManager::LoadShapes(igzstream &str)
|
---|
[2764] | 88 | {
|
---|
| 89 | int numShapes;
|
---|
| 90 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
| 91 |
|
---|
| 92 | for (int i = 0; i < numShapes; ++ i)
|
---|
| 93 | {
|
---|
| 94 | Geometry *geom = LoadGeometry(str);
|
---|
| 95 | Material *mat = LoadMaterial(str);
|
---|
| 96 |
|
---|
| 97 | mGeometryTable[i] = geom;
|
---|
| 98 | mMaterialTable[i] = mat;
|
---|
[2795] | 99 |
|
---|
| 100 | mGeometry.push_back(geom);
|
---|
| 101 | mMaterials.push_back(mat);
|
---|
[2764] | 102 | }
|
---|
| 103 |
|
---|
[2800] | 104 | cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
|
---|
[2764] | 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
[2795] | 108 | Material *ResourceManager::LoadMaterial(igzstream &str)
|
---|
[2747] | 109 | {
|
---|
[2756] | 110 | // default material
|
---|
[2755] | 111 | Material *mat = new Material();
|
---|
[2756] | 112 |
|
---|
[2747] | 113 | // texture
|
---|
[2757] | 114 | int texId;
|
---|
| 115 | str.read(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
[2747] | 116 |
|
---|
[2757] | 117 | if (texId >= 0)
|
---|
| 118 | mat->SetTexture(mTextureTable[texId]);
|
---|
[2764] | 119 |
|
---|
[2769] | 120 | str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool));
|
---|
[2747] | 121 |
|
---|
| 122 | // material
|
---|
[2769] | 123 | bool hasMaterial;
|
---|
| 124 | str.read(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
[2747] | 125 |
|
---|
| 126 | if (hasMaterial)
|
---|
| 127 | {
|
---|
[2769] | 128 | // only write rgb part of the material
|
---|
| 129 | str.read(reinterpret_cast<char *>(&mat->mAmbientColor), sizeof(Vector3));
|
---|
| 130 | str.read(reinterpret_cast<char *>(&mat->mDiffuseColor), sizeof(Vector3));
|
---|
[2795] | 131 | str.read(reinterpret_cast<char *>(&mat->mEmmisiveColor), sizeof(Vector3));
|
---|
[2769] | 132 | str.read(reinterpret_cast<char *>(&mat->mSpecularColor), sizeof(Vector3));
|
---|
[2747] | 133 | }
|
---|
| 134 |
|
---|
[2755] | 135 | return mat;
|
---|
[2747] | 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
[2795] | 139 | Geometry *ResourceManager::LoadGeometry(igzstream &str)
|
---|
[2747] | 140 | {
|
---|
[2756] | 141 | Vector3 *vertices;
|
---|
| 142 | Vector3 *normals;
|
---|
| 143 | float *texcoords;
|
---|
[2747] | 144 |
|
---|
| 145 |
|
---|
[2756] | 146 | //////////////
|
---|
| 147 | //-- read in vertices
|
---|
[2747] | 148 |
|
---|
[2756] | 149 | int vertexCount;
|
---|
| 150 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
[2747] | 151 |
|
---|
| 152 | // end of file reached
|
---|
| 153 | if (str.eof())
|
---|
| 154 | return NULL;
|
---|
| 155 |
|
---|
[2757] | 156 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
[2747] | 157 |
|
---|
[2756] | 158 | vertices = new Vector3[vertexCount];
|
---|
| 159 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 160 |
|
---|
[2756] | 161 | normals = new Vector3[vertexCount];
|
---|
| 162 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 163 |
|
---|
[2756] | 164 | int texCoordCount;
|
---|
| 165 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
[2747] | 166 |
|
---|
[2756] | 167 | if (texCoordCount)
|
---|
[2747] | 168 | {
|
---|
[2756] | 169 | texcoords = new float[texCoordCount * 2];
|
---|
| 170 | str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
|
---|
[2747] | 171 | }
|
---|
[2756] | 172 | else
|
---|
[2811] | 173 | {
|
---|
[2756] | 174 | texcoords = NULL;
|
---|
[2811] | 175 | }
|
---|
[2747] | 176 |
|
---|
[2811] | 177 | for (int i = 0; i < vertexCount; i += 3)
|
---|
| 178 | {
|
---|
| 179 | Triangle3 tri(vertices[i], vertices[i + 1], vertices[i + 2]);
|
---|
| 180 | Vector3 n = tri.GetNormal();
|
---|
| 181 | normals[i + 0] = n;
|
---|
| 182 | normals[i + 1] = n;
|
---|
| 183 | normals[i + 2] = n;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[2786] | 186 | return new Geometry(vertices, normals, texcoords, vertexCount, true);
|
---|
[2747] | 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
[2795] | 190 | void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities)
|
---|
[2747] | 191 | {
|
---|
[2764] | 192 | int entityCount;
|
---|
| 193 | str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
| 194 |
|
---|
| 195 | entities.resize(entityCount);
|
---|
| 196 |
|
---|
| 197 | for (int i = 0; i < entityCount; ++ i)
|
---|
| 198 | {
|
---|
| 199 | SceneEntity *ent = LoadSceneEntity(str);
|
---|
| 200 | entities[i] = ent;
|
---|
[2795] | 201 | mSceneEntities.push_back(ent);
|
---|
[2764] | 202 | }
|
---|
| 203 |
|
---|
| 204 | cout << "loaded " << entityCount << " scene entities" << endl;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
[2795] | 208 | bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities)
|
---|
[2764] | 209 | {
|
---|
[2795] | 210 | igzstream istr(filename.c_str());
|
---|
[2747] | 211 |
|
---|
[2756] | 212 | if (!istr.is_open())
|
---|
[2747] | 213 | return false;
|
---|
| 214 |
|
---|
[2764] | 215 | cout << "loading textures" << endl;
|
---|
| 216 |
|
---|
[2757] | 217 | // load the texture table
|
---|
| 218 | LoadTextures(istr);
|
---|
| 219 |
|
---|
[2764] | 220 | cout << "loading shapes (geometry + materials)" << endl;
|
---|
[2747] | 221 |
|
---|
[2764] | 222 | // load the shapees
|
---|
| 223 | LoadShapes(istr);
|
---|
[2762] | 224 |
|
---|
[2764] | 225 | cout << "loading scene entites" << endl;
|
---|
| 226 | LoadSceneEntities(istr, entities);
|
---|
[2763] | 227 |
|
---|
[2756] | 228 | cout << "bin loading finished" << endl;
|
---|
[2747] | 229 |
|
---|
[2795] | 230 | // clean up
|
---|
| 231 | mTextureTable.clear();
|
---|
| 232 | mGeometryTable.clear();
|
---|
| 233 | mMaterialTable.clear();
|
---|
| 234 |
|
---|
[2756] | 235 | return true;
|
---|
[2747] | 236 | }
|
---|
| 237 |
|
---|
| 238 | }
|
---|