source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BinaryLoader.cpp @ 2793

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