source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp @ 2848

Revision 2848, 6.4 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[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
15using namespace std;
16
17
[2776]18namespace CHCDemoEngine
[2755]19{
[2747]20
[2755]21
[2795]22ResourceManager::~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
34SceneEntity *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
[2844]58        ///////////////
59        //-- load lod levels
[2842]60
[2844]61        int numLODs;
62        str.read(reinterpret_cast<char *>(&numLODs), sizeof(int));
[2842]63
[2844]64        //if (numLODs > 1) cout << "numlods: " << numLODs << endl;
65        for (int i = 0; i < numLODs; ++ i)
66        {
67                float distance;
68                str.read(reinterpret_cast<char *>(&distance), sizeof(float));
69
70                int numShapes;
71                str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
72
73                //if (numLODs > 1) cout << "dist: " << distance << " shapes: " << numShapes;
74
75                LODLevel *lodLevel = new LODLevel(distance);
76
77                for (int j = 0; j < numShapes; ++ j)
78                {
79                        int shapeId;
80                        str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
81
82                        //if (numLODs > 1) cout << " id " << shapeId;
83                       
84                        Geometry *geom = mGeometryTable[shapeId];
85                        Material *mat = mMaterialTable[shapeId];
86
87                        // create shape
88                        Shape *shape = new Shape(geom, mat, sceneGeom);
89                       
90                        mShapes.push_back(shape);
91
92                        sceneGeom->AddShape(shape);
[2848]93                        lodLevel->AddShape(shape);
[2844]94                }
95
96                //if (numLODs > 1) cout << endl;
97
98                sceneGeom->AddLODLevel(lodLevel);
99        }
100
[2755]101        return sceneGeom;
[2747]102}
103
104
[2795]105void ResourceManager::LoadTextures(igzstream &str)
[2757]106{
107        int numTextures;
108        str.read(reinterpret_cast<char *>(&numTextures), sizeof(int));
109
110        for (int i = 0; i < numTextures; ++ i)
111        {
112                // texture
113                int texnameSize;
114
[2764]115                //str.read(reinterpret_cast<char *>(&id), sizeof(int));
[2757]116                str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
117
118                char *texname = new char[texnameSize];
119                str.read(texname, sizeof(char) * texnameSize);
120
[2784]121                Texture *tex = new Texture(model_path + texname);
[2757]122
[2846]123                int boundS, boundT;
124
125                str.read(reinterpret_cast<char *>(&boundS), sizeof(int));
126                str.read(reinterpret_cast<char *>(&boundT), sizeof(int));
127
128                tex->SetBoundaryModeS(boundS);
129                tex->SetBoundaryModeT(boundT);
130
131                //tex->SetBoundaryModeS(Texture::CLAMP);
132                //tex->SetBoundaryModeT(Texture::CLAMP);
133
134                tex->Create();
135
[2764]136                mTextureTable[i] = tex;
[2795]137                mTextures.push_back(tex);
[2757]138        }
[2764]139
[2800]140        cout << "loaded " << (int)mTextureTable.size() << " textures" << endl;
[2757]141}
142
143
[2795]144void ResourceManager::LoadShapes(igzstream &str)
[2764]145{
146        int numShapes;
147        str.read(reinterpret_cast<char *>(&numShapes), sizeof(int));
148
149        for (int i = 0; i < numShapes; ++ i)
150        {
151                Geometry *geom = LoadGeometry(str);
152                Material *mat = LoadMaterial(str);
153
154                mGeometryTable[i] = geom;
[2842]155                mMaterialTable[i] = mat;               
[2795]156
157                mGeometry.push_back(geom);
158                mMaterials.push_back(mat);
[2764]159        }
160
[2800]161        cout << "loaded " << (int)mGeometryTable.size() << " shapes" << endl;
[2764]162}
163
164
[2795]165Material *ResourceManager::LoadMaterial(igzstream &str)
[2747]166{
[2756]167        // default material
[2755]168        Material *mat = new Material();
[2756]169       
[2747]170        // texture
[2757]171        int texId;
172        str.read(reinterpret_cast<char *>(&texId), sizeof(int));
[2747]173
[2757]174        if (texId >= 0)
175                mat->SetTexture(mTextureTable[texId]);
[2764]176       
[2769]177        str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool));
[2747]178
[2845]179        str.read(reinterpret_cast<char *>(&mat->mCullFaceEnabled), sizeof(bool));
[2844]180
[2747]181        // material
[2769]182        bool hasMaterial;
183        str.read(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
[2747]184       
185        if (hasMaterial)
186        {
[2769]187                // only write rgb part of the material
188                str.read(reinterpret_cast<char *>(&mat->mAmbientColor), sizeof(Vector3));
189                str.read(reinterpret_cast<char *>(&mat->mDiffuseColor), sizeof(Vector3));
[2795]190                str.read(reinterpret_cast<char *>(&mat->mEmmisiveColor), sizeof(Vector3));
[2769]191                str.read(reinterpret_cast<char *>(&mat->mSpecularColor), sizeof(Vector3));
[2747]192        }
193
[2755]194        return mat;
[2747]195}
196
197
[2795]198Geometry *ResourceManager::LoadGeometry(igzstream &str)
[2747]199{
[2756]200        Vector3 *vertices;
201        Vector3 *normals;
202        float *texcoords;
[2747]203
204
[2756]205        //////////////
206        //-- read in vertices
[2747]207
[2756]208        int vertexCount;
209        str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
[2747]210       
211        // end of file reached
212        if (str.eof())
213                return NULL;
214
[2757]215        //cout << "vertexcount: " << vertexCount << endl;
[2747]216
[2756]217        vertices = new Vector3[vertexCount];
218    str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
[2747]219       
[2756]220        normals = new Vector3[vertexCount];
221        str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
[2747]222
[2756]223        int texCoordCount;
224        str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
[2747]225
[2756]226        if (texCoordCount)
[2747]227        {
[2756]228                texcoords = new float[texCoordCount * 2];
229                str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
[2747]230        }
[2756]231        else
[2811]232        {
[2756]233                texcoords = NULL;
[2811]234        }
[2833]235       
[2747]236
[2833]237        return new Geometry(vertices, normals, texcoords, vertexCount, true);
[2747]238}
239
240
[2795]241void ResourceManager::LoadSceneEntities(igzstream &str, SceneEntityContainer &entities)
[2747]242{
[2764]243        int entityCount;
244        str.read(reinterpret_cast<char *>(&entityCount), sizeof(int));
245
246        entities.resize(entityCount);
247
248        for (int i = 0; i < entityCount; ++ i)
249        {
250                SceneEntity *ent = LoadSceneEntity(str);
251                entities[i] = ent;
[2842]252
[2795]253                mSceneEntities.push_back(ent);
[2764]254        }
255
256        cout << "loaded " << entityCount << " scene entities" << endl;
257}
258
259
[2795]260bool ResourceManager::Load(const std::string &filename, SceneEntityContainer &entities)
[2764]261{
[2795]262        igzstream istr(filename.c_str());
[2747]263       
[2756]264        if (!istr.is_open())
[2747]265                return false;
266
[2764]267        cout << "loading textures" << endl;
268       
[2757]269        // load the texture table
270        LoadTextures(istr);
271
[2764]272        cout << "loading shapes (geometry + materials)" << endl;
[2747]273
[2764]274        // load the shapees
275        LoadShapes(istr);
[2762]276
[2764]277        cout << "loading scene entites" << endl;
278        LoadSceneEntities(istr, entities);
[2763]279       
[2756]280        cout << "bin loading finished" << endl;
[2747]281
[2795]282        // clean up
283        mTextureTable.clear();
284        mGeometryTable.clear();
285        mMaterialTable.clear();
[2842]286        //mShapesTable.clear();
[2795]287
[2756]288        return true;
[2747]289}
290
291}
Note: See TracBrowser for help on using the repository browser.