[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 "Vector3.h"
|
---|
[2840] | 9 | #include "Transform3.h"
|
---|
[2842] | 10 | #include "Shape.h"
|
---|
[2844] | 11 | #include "LODInfo.h"
|
---|
[3034] | 12 | #include "RenderState.h"
|
---|
| 13 | #include "ShaderProgram.h"
|
---|
[3057] | 14 | #include "ShaderManager.h"
|
---|
[2747] | 15 |
|
---|
| 16 |
|
---|
[3021] | 17 | #ifdef _CRT_SET
|
---|
| 18 | #define _CRTDBG_MAP_ALLOC
|
---|
| 19 | #include <stdlib.h>
|
---|
| 20 | #include <crtdbg.h>
|
---|
| 21 |
|
---|
| 22 | // redefine new operator
|
---|
| 23 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
| 24 | #define new DEBUG_NEW
|
---|
| 25 | #endif
|
---|
| 26 |
|
---|
| 27 |
|
---|
[2747] | 28 | using namespace std;
|
---|
| 29 |
|
---|
| 30 |
|
---|
[2776] | 31 | namespace CHCDemoEngine
|
---|
[2755] | 32 | {
|
---|
[2747] | 33 |
|
---|
[3038] | 34 |
|
---|
[3037] | 35 | // only instance of the resource manager
|
---|
| 36 | ResourceManager *ResourceManager::sResourceManager = NULL;
|
---|
[2755] | 37 |
|
---|
[3038] | 38 |
|
---|
| 39 | ResourceManager::ResourceManager()
|
---|
| 40 | {
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
[2795] | 44 | ResourceManager::~ResourceManager()
|
---|
[2781] | 45 | {
|
---|
[2795] | 46 | // delete all resources.
|
---|
| 47 | CLEAR_CONTAINER(mSceneEntities);
|
---|
| 48 | CLEAR_CONTAINER(mGeometry);
|
---|
| 49 | CLEAR_CONTAINER(mMaterials);
|
---|
| 50 | CLEAR_CONTAINER(mTextures);
|
---|
| 51 | CLEAR_CONTAINER(mTrafos);
|
---|
[2842] | 52 | CLEAR_CONTAINER(mShapes);
|
---|
[3070] | 53 | //CLEAR_CONTAINER(mLODs);
|
---|
[2781] | 54 | }
|
---|
| 55 |
|
---|
[2795] | 56 |
|
---|
[3052] | 57 | void ResourceManager::DelSingleton()
|
---|
| 58 | {
|
---|
| 59 | DEL_PTR(sResourceManager);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
[2795] | 63 | SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str)
|
---|
[2747] | 64 | {
|
---|
[2764] | 65 | bool hasTrafo;
|
---|
| 66 | str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
| 67 |
|
---|
[2823] | 68 | SceneEntity *sceneGeom;
|
---|
[2957] | 69 | Transform3 *trafo;
|
---|
[2823] | 70 |
|
---|
[2764] | 71 | if (!hasTrafo)
|
---|
| 72 | {
|
---|
[2961] | 73 | // identity
|
---|
[2957] | 74 | trafo = new Transform3();
|
---|
[2764] | 75 | }
|
---|
| 76 | else
|
---|
| 77 | {
|
---|
[2957] | 78 | Matrix4x4 m;
|
---|
| 79 | str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
|
---|
| 80 |
|
---|
| 81 | trafo = new Transform3(m);
|
---|
[2764] | 82 | }
|
---|
| 83 |
|
---|
[2840] | 84 | mTrafos.push_back(trafo);
|
---|
[2844] | 85 |
|
---|
[2839] | 86 | sceneGeom = new SceneEntity(trafo);
|
---|
[2825] | 87 |
|
---|
[2852] | 88 |
|
---|
[2844] | 89 | ///////////////
|
---|
[3048] | 90 | //-- load LOD levels
|
---|
[2842] | 91 |
|
---|
[2853] | 92 | // note: lods must be ordered from smallest distance to largest
|
---|
[2844] | 93 | int numLODs;
|
---|
| 94 | str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
[2842] | 95 |
|
---|
[2844] | 96 | for (int i = 0; i < numLODs; ++ i)
|
---|
| 97 | {
|
---|
[3048] | 98 | // the distance until the next LOD level is used
|
---|
[2853] | 99 | float dist;
|
---|
| 100 | str.read(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
[2844] | 101 |
|
---|
| 102 | int numShapes;
|
---|
| 103 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
| 104 |
|
---|
[3070] | 105 | //LODLevel *lodLevel = new LODLevel(dist);
|
---|
| 106 | LODLevel lodLevel(dist);
|
---|
[2844] | 107 |
|
---|
| 108 | for (int j = 0; j < numShapes; ++ j)
|
---|
| 109 | {
|
---|
| 110 | int shapeId;
|
---|
| 111 | str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
| 112 |
|
---|
| 113 | Geometry *geom = mGeometryTable[shapeId];
|
---|
| 114 | Material *mat = mMaterialTable[shapeId];
|
---|
| 115 |
|
---|
| 116 | // create shape
|
---|
[3071] | 117 | Shape *shape = new Shape(geom, mat);
|
---|
[3041] | 118 |
|
---|
[2844] | 119 | mShapes.push_back(shape);
|
---|
| 120 |
|
---|
| 121 | sceneGeom->AddShape(shape);
|
---|
[3070] | 122 | lodLevel.AddShape(shape);
|
---|
[2844] | 123 | }
|
---|
| 124 |
|
---|
[3070] | 125 | //mLODs.push_back(lodLevel);
|
---|
[2844] | 126 | sceneGeom->AddLODLevel(lodLevel);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[3057] | 129 |
|
---|
[3113] | 130 | static ShaderProgram *sTreeAnimationProgram =
|
---|
| 131 | ShaderManager::GetSingleton()->GetShaderProgram("treeAnimation");
|
---|
| 132 | static ShaderProgram *sTreeAnimationProgramMrt =
|
---|
| 133 | ShaderManager::GetSingleton()->GetShaderProgram("treeAnimationMrt");
|
---|
| 134 |
|
---|
| 135 |
|
---|
[3041] | 136 | ///////////
|
---|
[3047] | 137 | //-- hack: add tree animation (should be done per material script!)
|
---|
[3041] | 138 |
|
---|
[3050] | 139 | if (numLODs > 1)
|
---|
[3041] | 140 | {
|
---|
[3047] | 141 | ShapeContainer::iterator sstart, send;
|
---|
| 142 |
|
---|
| 143 | // only use shader for the first two lod levels (the non-billboards)
|
---|
| 144 | for (int i = 0; i < min(numLODs, 2); ++ i)
|
---|
| 145 | //for (int i = 0; i < numLODs; ++ i)
|
---|
[3041] | 146 | {
|
---|
| 147 | sceneGeom->GetLODLevel(i, sstart, send);
|
---|
| 148 |
|
---|
| 149 | for (ShapeContainer::iterator it = sstart; it != send; ++ it)
|
---|
| 150 | {
|
---|
| 151 | Shape *shape = *it;
|
---|
[3045] | 152 |
|
---|
[3041] | 153 | Material *mat = shape->GetMaterial();
|
---|
| 154 |
|
---|
[3045] | 155 | // add the vertex animation program
|
---|
[3050] | 156 | for (int i = 0; i < 3; ++ i)
|
---|
[3045] | 157 | {
|
---|
| 158 | Technique *tech = mat->GetTechnique(i);
|
---|
[3041] | 159 |
|
---|
[3045] | 160 | GPUProgramParameters *vtxParams = tech->GetVertexProgramParameters();
|
---|
[3041] | 161 |
|
---|
[3046] | 162 | if (i == 0)
|
---|
| 163 | {
|
---|
| 164 | tech->SetVertexProgram(sTreeAnimationProgram);
|
---|
| 165 | vtxParams->SetLightDirParam(5);
|
---|
| 166 | }
|
---|
| 167 | else
|
---|
| 168 | {
|
---|
| 169 | tech->SetVertexProgram(sTreeAnimationProgramMrt);
|
---|
[3115] | 170 | vtxParams->SetOldTimerParam(5);
|
---|
[3046] | 171 | }
|
---|
| 172 |
|
---|
| 173 | /// use a timer to simulate the moving of the tree in the wind
|
---|
[3045] | 174 | vtxParams->SetTimerParam(0);
|
---|
[3113] | 175 |
|
---|
[3045] | 176 | // wind direction
|
---|
| 177 | static Vector3 windDir = Normalize(Vector3(0.8f, 0.2f, 0.0f));
|
---|
| 178 | vtxParams->SetValue3f(1, windDir.x, windDir.y, windDir.z);
|
---|
| 179 | // amplitude
|
---|
| 180 | vtxParams->SetValue1f(2, 0.3f);
|
---|
| 181 |
|
---|
| 182 | AxisAlignedBox3 box = sceneGeom->GetBoundingBox();
|
---|
| 183 | vtxParams->SetValue2f(3, box.Min().z, box.Max().z);
|
---|
| 184 | // frequency
|
---|
| 185 | vtxParams->SetValue1f(4, 0.1f);
|
---|
| 186 | }
|
---|
[3041] | 187 | }
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[2755] | 191 | return sceneGeom;
|
---|
[2747] | 192 | }
|
---|
| 193 |
|
---|
| 194 |
|
---|
[2795] | 195 | void ResourceManager::LoadTextures(igzstream &str)
|
---|
[2757] | 196 | {
|
---|
| 197 | int numTextures;
|
---|
| 198 | str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
|
---|
[2961] | 199 |
|
---|
[2757] | 200 | for (int i = 0; i < numTextures; ++ i)
|
---|
| 201 | {
|
---|
[2961] | 202 | // load texture name
|
---|
[2757] | 203 | int texnameSize;
|
---|
| 204 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
| 205 |
|
---|
| 206 | char *texname = new char[texnameSize];
|
---|
| 207 | str.read(texname, sizeof(char) * texnameSize);
|
---|
| 208 |
|
---|
[2784] | 209 | Texture *tex = new Texture(model_path + texname);
|
---|
[2757] | 210 |
|
---|
[2846] | 211 | int boundS, boundT;
|
---|
| 212 |
|
---|
| 213 | str.read(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
| 214 | str.read(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
| 215 |
|
---|
| 216 | tex->SetBoundaryModeS(boundS);
|
---|
| 217 | tex->SetBoundaryModeT(boundT);
|
---|
| 218 |
|
---|
| 219 | tex->Create();
|
---|
| 220 |
|
---|
[2764] | 221 | mTextureTable[i] = tex;
|
---|
[2795] | 222 | mTextures.push_back(tex);
|
---|
[3021] | 223 |
|
---|
| 224 | delete [] texname;
|
---|
[2757] | 225 | }
|
---|
[2764] | 226 |
|
---|
[2800] | 227 | cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
|
---|
[2757] | 228 | }
|
---|
| 229 |
|
---|
| 230 |
|
---|
[2795] | 231 | void ResourceManager::LoadShapes(igzstream &str)
|
---|
[2764] | 232 | {
|
---|
| 233 | int numShapes;
|
---|
| 234 | str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
|
---|
| 235 |
|
---|
| 236 | for (int i = 0; i < numShapes; ++ i)
|
---|
| 237 | {
|
---|
| 238 | Geometry *geom = LoadGeometry(str);
|
---|
| 239 | Material *mat = LoadMaterial(str);
|
---|
| 240 |
|
---|
| 241 | mGeometryTable[i] = geom;
|
---|
[2842] | 242 | mMaterialTable[i] = mat;
|
---|
[2795] | 243 |
|
---|
| 244 | mGeometry.push_back(geom);
|
---|
| 245 | mMaterials.push_back(mat);
|
---|
[2764] | 246 | }
|
---|
| 247 |
|
---|
[2800] | 248 | cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
|
---|
[2764] | 249 | }
|
---|
| 250 |
|
---|
| 251 |
|
---|
[2795] | 252 | Material *ResourceManager::LoadMaterial(igzstream &str)
|
---|
[2747] | 253 | {
|
---|
[2756] | 254 | // default material
|
---|
[2755] | 255 | Material *mat = new Material();
|
---|
[2756] | 256 |
|
---|
[3042] | 257 | // set default fragment + vertex programs
|
---|
| 258 | Technique *tech = mat->GetDefaultTechnique();
|
---|
| 259 |
|
---|
[2747] | 260 | // texture
|
---|
[2757] | 261 | int texId;
|
---|
| 262 | str.read(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
[2747] | 263 |
|
---|
[2982] | 264 | if (texId >= 0)
|
---|
[3042] | 265 | tech->SetTexture(mTextureTable[texId]);
|
---|
[2764] | 266 |
|
---|
[3042] | 267 | str.read(reinterpret_cast<char *>(&tech->mAlphaTestEnabled), sizeof(bool));
|
---|
| 268 | str.read(reinterpret_cast<char *>(&tech->mCullFaceEnabled), sizeof(bool));
|
---|
[2844] | 269 |
|
---|
[3034] | 270 | // gl material
|
---|
| 271 | bool hasGlMaterial;
|
---|
| 272 | str.read(reinterpret_cast<char *>(&hasGlMaterial), sizeof(bool));
|
---|
[3042] | 273 |
|
---|
[3034] | 274 | if (hasGlMaterial)
|
---|
[2747] | 275 | {
|
---|
[2769] | 276 | // only write rgb part of the material
|
---|
[3042] | 277 | str.read(reinterpret_cast<char *>(&tech->mAmbientColor), sizeof(Vector3));
|
---|
| 278 | str.read(reinterpret_cast<char *>(&tech->mDiffuseColor), sizeof(Vector3));
|
---|
| 279 | str.read(reinterpret_cast<char *>(&tech->mEmmisiveColor), sizeof(Vector3));
|
---|
| 280 | str.read(reinterpret_cast<char *>(&tech->mSpecularColor), sizeof(Vector3));
|
---|
[2747] | 281 | }
|
---|
| 282 |
|
---|
[3045] | 283 |
|
---|
| 284 | ///////////////
|
---|
| 285 | //-- add technique for deferred shading
|
---|
| 286 |
|
---|
[3057] | 287 | static ShaderProgram *sDefaultFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentMrt");
|
---|
| 288 | static ShaderProgram *sDefaultFragmentTexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultFragmentTexMrt");
|
---|
| 289 | static ShaderProgram *sDefaultVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultVertexMrt");
|
---|
| 290 |
|
---|
[3045] | 291 | Technique *deferred = new Technique(*tech);
|
---|
| 292 |
|
---|
| 293 | if (deferred->GetTexture())
|
---|
[3034] | 294 | {
|
---|
[3057] | 295 | deferred->SetFragmentProgram(sDefaultFragmentTexProgramMrt);
|
---|
[3045] | 296 | deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
|
---|
| 297 | deferred->GetFragmentProgramParameters()->SetTexture(1, tech->GetTexture()->GetId());
|
---|
[3034] | 298 | }
|
---|
| 299 | else
|
---|
| 300 | {
|
---|
[3058] | 301 | deferred->SetFragmentProgram(sDefaultFragmentProgramMrt);
|
---|
[3045] | 302 | deferred->GetFragmentProgramParameters()->SetViewMatrixParam(0);
|
---|
[3034] | 303 | }
|
---|
| 304 |
|
---|
[3057] | 305 | deferred->SetVertexProgram(sDefaultVertexProgramMrt);
|
---|
[3113] | 306 |
|
---|
[3114] | 307 | deferred->GetVertexProgramParameters()->SetModelMatrixParam(1);
|
---|
| 308 | deferred->GetVertexProgramParameters()->SetOldModelMatrixParam(2);
|
---|
[3113] | 309 |
|
---|
[3050] | 310 | mat->AddTechnique(deferred);
|
---|
[3041] | 311 |
|
---|
[3048] | 312 |
|
---|
| 313 | ///////////
|
---|
| 314 | //-- add depth pass
|
---|
| 315 |
|
---|
| 316 | Technique *depthPass = new Technique(*tech);
|
---|
[3114] | 317 |
|
---|
[3048] | 318 | depthPass->SetColorWriteEnabled(false);
|
---|
| 319 | depthPass->SetLightingEnabled(false);
|
---|
| 320 | mat->AddTechnique(depthPass);
|
---|
[3045] | 321 |
|
---|
[2755] | 322 | return mat;
|
---|
[2747] | 323 | }
|
---|
| 324 |
|
---|
| 325 |
|
---|
[2795] | 326 | Geometry *ResourceManager::LoadGeometry(igzstream &str)
|
---|
[2747] | 327 | {
|
---|
[2756] | 328 | Vector3 *vertices;
|
---|
| 329 | Vector3 *normals;
|
---|
[2980] | 330 | Texcoord2 *texcoords;
|
---|
[2747] | 331 |
|
---|
| 332 |
|
---|
[2756] | 333 | //////////////
|
---|
| 334 | //-- read in vertices
|
---|
[2747] | 335 |
|
---|
[2756] | 336 | int vertexCount;
|
---|
| 337 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
[2747] | 338 |
|
---|
| 339 | // end of file reached
|
---|
[2961] | 340 | if (str.eof()) return NULL;
|
---|
[2747] | 341 |
|
---|
[2757] | 342 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
[2747] | 343 |
|
---|
[2756] | 344 | vertices = new Vector3[vertexCount];
|
---|
| 345 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 346 |
|
---|
[2756] | 347 | normals = new Vector3[vertexCount];
|
---|
| 348 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 349 |
|
---|
[2756] | 350 | int texCoordCount;
|
---|
| 351 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
[2747] | 352 |
|
---|
[2756] | 353 | if (texCoordCount)
|
---|
[2747] | 354 | {
|
---|
[2980] | 355 | texcoords = new Texcoord2[texCoordCount];
|
---|
| 356 | str.read(reinterpret_cast<char *>(texcoords), sizeof(Texcoord2) * texCoordCount);
|
---|
[2747] | 357 | }
|
---|
[2756] | 358 | else
|
---|
[2811] | 359 | {
|
---|
[2756] | 360 | texcoords = NULL;
|
---|
[2811] | 361 | }
|
---|
[2747] | 362 |
|
---|
[2982] | 363 | return new Geometry(vertices, normals, texcoords, vertexCount, true);
|
---|
| 364 | //return new Geometry(vertices, normals, texcoords, vertexCount, false);
|
---|
[2747] | 365 | }
|
---|
| 366 |
|
---|
| 367 |
|
---|
[3046] | 368 | void ResourceManager::LoadSceneEntities(igzstream &str,
|
---|
| 369 | SceneEntityContainer &entities)
|
---|
[2747] | 370 | {
|
---|
[2764] | 371 | int entityCount;
|
---|
| 372 | str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
| 373 |
|
---|
[2963] | 374 | entities.reserve(entityCount);
|
---|
[2764] | 375 |
|
---|
| 376 | for (int i = 0; i < entityCount; ++ i)
|
---|
| 377 | {
|
---|
| 378 | SceneEntity *ent = LoadSceneEntity(str);
|
---|
[2842] | 379 |
|
---|
[2963] | 380 | // return loaded entities
|
---|
| 381 | entities.push_back(ent);
|
---|
| 382 | // also store internally
|
---|
[2795] | 383 | mSceneEntities.push_back(ent);
|
---|
[2764] | 384 | }
|
---|
| 385 |
|
---|
| 386 | cout << "loaded " << entityCount << " scene entities" << endl;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 |
|
---|
[3046] | 390 | bool ResourceManager::Load(const std::string &filename,
|
---|
| 391 | SceneEntityContainer &entities)
|
---|
[2764] | 392 | {
|
---|
[2795] | 393 | igzstream istr(filename.c_str());
|
---|
[2747] | 394 |
|
---|
[2756] | 395 | if (!istr.is_open())
|
---|
[2747] | 396 | return false;
|
---|
| 397 |
|
---|
[2764] | 398 | cout << "loading textures" << endl;
|
---|
| 399 |
|
---|
[2757] | 400 | // load the texture table
|
---|
| 401 | LoadTextures(istr);
|
---|
| 402 |
|
---|
[2764] | 403 | cout << "loading shapes (geometry + materials)" << endl;
|
---|
[2747] | 404 |
|
---|
[2764] | 405 | // load the shapees
|
---|
| 406 | LoadShapes(istr);
|
---|
[2762] | 407 |
|
---|
[2764] | 408 | cout << "loading scene entites" << endl;
|
---|
| 409 | LoadSceneEntities(istr, entities);
|
---|
[2763] | 410 |
|
---|
[2756] | 411 | cout << "bin loading finished" << endl;
|
---|
[2747] | 412 |
|
---|
[2795] | 413 | // clean up
|
---|
| 414 | mTextureTable.clear();
|
---|
| 415 | mGeometryTable.clear();
|
---|
| 416 | mMaterialTable.clear();
|
---|
| 417 |
|
---|
[2756] | 418 | return true;
|
---|
[2747] | 419 | }
|
---|
| 420 |
|
---|
[3070] | 421 |
|
---|
| 422 | void ResourceManager::AddSceneEntity(SceneEntity *ent)
|
---|
| 423 | {
|
---|
| 424 | mSceneEntities.push_back(ent);
|
---|
[3036] | 425 | }
|
---|
| 426 |
|
---|
[3070] | 427 |
|
---|
| 428 | Transform3 *ResourceManager::CreateTransform(const Matrix4x4 &m)
|
---|
| 429 | {
|
---|
| 430 | Transform3 *t = new Transform3(m);
|
---|
| 431 |
|
---|
| 432 | mTrafos.push_back(t);
|
---|
| 433 | return t;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | } |
---|