[2747] | 1 | #include "BinaryLoader.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"
|
---|
[2755] | 7 | //#include "gzstream.h"
|
---|
[2747] | 8 |
|
---|
| 9 |
|
---|
| 10 | using namespace std;
|
---|
| 11 |
|
---|
| 12 |
|
---|
[2776] | 13 | namespace CHCDemoEngine
|
---|
[2755] | 14 | {
|
---|
[2747] | 15 |
|
---|
[2755] | 16 |
|
---|
[2781] | 17 | BinaryLoader::~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 |
|
---|
[2756] | 47 | //SceneEntity *BinaryLoader::LoadSceneEntity(igzstream &str)
|
---|
| 48 | SceneEntity *BinaryLoader::LoadSceneEntity(ifstream &str)
|
---|
[2747] | 49 | {
|
---|
[2764] | 50 | // shape
|
---|
| 51 | int shapeId;
|
---|
| 52 | str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
[2747] | 53 |
|
---|
[2764] | 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 |
|
---|
[2756] | 73 | SceneEntity *sceneGeom = new SceneEntity(geom, mat, trafo);
|
---|
[2747] | 74 |
|
---|
[2755] | 75 | return sceneGeom;
|
---|
[2747] | 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
[2757] | 79 | void 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 |
|
---|
[2764] | 89 | //str.read(reinterpret_cast<char *>(&id), sizeof(int));
|
---|
[2757] | 90 | str.read(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
| 91 |
|
---|
| 92 | char *texname = new char[texnameSize];
|
---|
| 93 | str.read(texname, sizeof(char) * texnameSize);
|
---|
| 94 |
|
---|
[2760] | 95 | //cout << "loading texture " << texname << " with len " << texnameSize << " id: " << id << endl;
|
---|
[2784] | 96 | Texture *tex = new Texture(model_path + texname);
|
---|
[2757] | 97 |
|
---|
[2764] | 98 | mTextureTable[i] = tex;
|
---|
[2757] | 99 | }
|
---|
[2764] | 100 |
|
---|
| 101 | cout << "loaded " << mTextureTable.size() << " textures" << endl;
|
---|
[2757] | 102 | }
|
---|
| 103 |
|
---|
| 104 |
|
---|
[2764] | 105 | void 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 |
|
---|
[2755] | 123 | //Appearance *BinaryLoader::LoadMaterial(igzstream &str)
|
---|
| 124 | Material *BinaryLoader::LoadMaterial(ifstream &str)
|
---|
[2747] | 125 | {
|
---|
[2756] | 126 | // default material
|
---|
[2755] | 127 | Material *mat = new Material();
|
---|
[2756] | 128 |
|
---|
[2747] | 129 | // texture
|
---|
[2757] | 130 | int texId;
|
---|
| 131 | str.read(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
[2747] | 132 |
|
---|
[2757] | 133 | if (texId >= 0)
|
---|
| 134 | mat->SetTexture(mTextureTable[texId]);
|
---|
[2764] | 135 |
|
---|
[2769] | 136 | str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool));
|
---|
[2747] | 137 |
|
---|
| 138 | // material
|
---|
[2769] | 139 | bool hasMaterial;
|
---|
| 140 | str.read(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
|
---|
[2747] | 141 |
|
---|
| 142 | if (hasMaterial)
|
---|
| 143 | {
|
---|
[2769] | 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));
|
---|
[2747] | 148 | }
|
---|
| 149 |
|
---|
[2755] | 150 | return mat;
|
---|
[2747] | 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
[2755] | 154 | //Geometry *BinaryLoader::LoadGeometry(igzstream &str)
|
---|
| 155 | Geometry *BinaryLoader::LoadGeometry(ifstream &str)
|
---|
[2747] | 156 | {
|
---|
[2756] | 157 | Vector3 *vertices;
|
---|
| 158 | Vector3 *normals;
|
---|
| 159 | float *texcoords;
|
---|
[2747] | 160 |
|
---|
| 161 |
|
---|
[2756] | 162 | //////////////
|
---|
| 163 | //-- read in vertices
|
---|
[2747] | 164 |
|
---|
[2756] | 165 | int vertexCount;
|
---|
| 166 | str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
[2747] | 167 |
|
---|
| 168 | // end of file reached
|
---|
| 169 | if (str.eof())
|
---|
| 170 | return NULL;
|
---|
| 171 |
|
---|
[2757] | 172 | //cout << "vertexcount: " << vertexCount << endl;
|
---|
[2747] | 173 |
|
---|
[2756] | 174 | vertices = new Vector3[vertexCount];
|
---|
| 175 | str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 176 |
|
---|
[2756] | 177 | normals = new Vector3[vertexCount];
|
---|
| 178 | str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount);
|
---|
[2747] | 179 |
|
---|
[2756] | 180 | int texCoordCount;
|
---|
| 181 | str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
[2747] | 182 |
|
---|
[2756] | 183 | if (texCoordCount)
|
---|
[2747] | 184 | {
|
---|
[2757] | 185 | //cout << "loading texcoords of size " << texCoordCount << endl;
|
---|
[2756] | 186 | texcoords = new float[texCoordCount * 2];
|
---|
| 187 | str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2);
|
---|
[2747] | 188 | }
|
---|
[2756] | 189 | else
|
---|
| 190 | texcoords = NULL;
|
---|
[2747] | 191 |
|
---|
[2786] | 192 | return new Geometry(vertices, normals, texcoords, vertexCount, true);
|
---|
[2747] | 193 | }
|
---|
| 194 |
|
---|
| 195 |
|
---|
[2764] | 196 | void BinaryLoader::LoadSceneEntities(ifstream &str, SceneEntityContainer &entities)
|
---|
[2747] | 197 | {
|
---|
[2764] | 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 |
|
---|
| 213 | bool BinaryLoader::Load(const std::string &filename, SceneEntityContainer &entities)
|
---|
| 214 | {
|
---|
[2756] | 215 | //clear_textures(); // clear the texture table
|
---|
[2747] | 216 |
|
---|
[2756] | 217 | //igzstream str(filename.c_str());//, ios::binary);
|
---|
| 218 | ifstream istr(filename.c_str(), ios::binary);
|
---|
[2747] | 219 |
|
---|
[2756] | 220 | if (!istr.is_open())
|
---|
[2747] | 221 | return false;
|
---|
| 222 |
|
---|
[2764] | 223 | cout << "loading textures" << endl;
|
---|
| 224 |
|
---|
[2757] | 225 | // load the texture table
|
---|
| 226 | LoadTextures(istr);
|
---|
| 227 |
|
---|
[2764] | 228 | cout << "loading shapes (geometry + materials)" << endl;
|
---|
[2747] | 229 |
|
---|
[2764] | 230 | // load the shapees
|
---|
| 231 | LoadShapes(istr);
|
---|
[2762] | 232 |
|
---|
[2764] | 233 | cout << "loading scene entites" << endl;
|
---|
| 234 | LoadSceneEntities(istr, entities);
|
---|
[2763] | 235 |
|
---|
[2756] | 236 | cout << "bin loading finished" << endl;
|
---|
[2747] | 237 |
|
---|
[2756] | 238 | return true;
|
---|
[2747] | 239 | }
|
---|
| 240 |
|
---|
| 241 | }
|
---|