Changeset 2756 for GTP/trunk/App/Demos/Vis/CHC_revisited/BinaryLoader.cpp
- Timestamp:
- 06/13/08 18:06:32 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/CHC_revisited/BinaryLoader.cpp
r2755 r2756 2 2 #include "Matrix4x4.h" 3 3 #include "Geometry.h" 4 #include "SceneEntity.h" 4 5 #include "Material.h" 6 #include "Texture.h" 5 7 //#include "gzstream.h" 6 8 … … 66 68 67 69 68 //SceneEntity *BinaryLoader::LoadS hape(igzstream &str)69 SceneEntity *BinaryLoader::LoadS hape(ifstream &str)70 //SceneEntity *BinaryLoader::LoadSceneEntity(igzstream &str) 71 SceneEntity *BinaryLoader::LoadSceneEntity(ifstream &str) 70 72 { 71 73 Geometry *geom = LoadGeometry(str); 72 74 Material *mat = LoadMaterial(str); 73 74 Matrix4x4 *m; 75 SceneEntity *sceneGeom = new SceneEntity(geom, m , mat);75 Matrix4x4 *trafo = NULL;//IdentityMatrix(); 76 77 SceneEntity *sceneGeom = new SceneEntity(geom, mat, trafo); 76 78 77 79 return sceneGeom; … … 82 84 Material *BinaryLoader::LoadMaterial(ifstream &str) 83 85 { 86 // default material 84 87 Material *mat = new Material(); 85 /*88 86 89 // texture 87 90 int texnameSize; … … 91 94 if (texnameSize) 92 95 { 93 TextureAttributes *texAttr;94 96 char *texname = new char[texnameSize]; 95 str.read(texname, sizeof(char) * texnameSize); 96 97 Texture *tex = get_texture(texname, texAttr); 98 app->setTexture(tex); 99 app->setTextureAttributes(texAttr); 100 } 97 str.read(texname, sizeof(char) *texnameSize); 98 99 100 cout << "loading texture " << texname << " with len " << texnameSize << endl; 101 Texture *tex = new Texture(texname); 102 103 mat->SetTexture(tex); 104 } 105 else 106 cout << "no texture " << texnameSize << endl; 101 107 102 108 // material 103 char hasMaterial;109 /*char hasMaterial; 104 110 str.read(&hasMaterial, sizeof(char)); 105 111 … … 137 143 Geometry *BinaryLoader::LoadGeometry(ifstream &str) 138 144 { 139 /* 145 Vector3 *vertices; 146 Vector3 *normals; 147 float *texcoords; 148 149 150 ////////////// 151 //-- read in vertices 152 140 153 int vertexCount; 141 int stripCount; 142 143 Point3f *vertices; 144 Vector3f *normals = NULL; 145 Point2f *texCoords = NULL; 146 147 int *stripIndexCounts; 148 int *indices; 149 int *normalIndices; 150 151 // todo 152 int colorCount = 0; 153 int normalCount = 0; 154 int texCount = 0; 155 156 int format; 157 158 // read in format 159 str.read(reinterpret_cast<char *>(&format), sizeof(int)); 160 161 format = IndexedTriangleStripArray::COORDINATES 162 | IndexedTriangleStripArray::NORMALS 163 ; 164 165 //OUT1("new format: " << format); 166 154 str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int)); 155 167 156 // end of file reached 168 157 if (str.eof()) 169 158 return NULL; 170 159 171 172 ////////////// 173 // read in vertices 174 175 str.read(reinterpret_cast<char *>(&vertexCount), sizeof(int)); 176 177 vertices = new Point3f[vertexCount]; 178 str.read(reinterpret_cast<char *>(vertices), sizeof(Point3f) * vertexCount); 179 180 181 ///////////////// 182 // read in strip counters 183 184 str.read(reinterpret_cast<char *>(&stripCount), sizeof(int)); 185 186 stripIndexCounts = new int[stripCount]; 187 str.read(reinterpret_cast<char *>(stripIndexCounts), sizeof(int) * stripCount); 188 189 190 ////////// 191 // read in indices 192 193 int indexCount; 194 str.read(reinterpret_cast<char *>(&indexCount), sizeof(int)); 195 indices = new int[indexCount]; 196 str.read(reinterpret_cast<char *>(indices), sizeof(int) * indexCount); 197 198 199 /////////////// 200 // read in normals 201 202 str.read(reinterpret_cast<char *>(&normalCount), sizeof(int)); 203 204 if (normalCount) 205 { 206 normals = new Vector3f[normalCount]; 207 str.read(reinterpret_cast<char *>(normals), sizeof(Vector3f) * normalCount); 208 normalIndices = new int[indexCount]; 209 str.read(reinterpret_cast<char *>(normalIndices), sizeof(int) * indexCount); 210 } 211 212 213 ////////////// 214 // read in texCoords 215 216 str.read(reinterpret_cast<char *>(&texCount), sizeof(int)); 217 if (texCount) 218 { 219 texCoords = new Point2f[texCount]; 220 str.read(reinterpret_cast<char *>(texCoords), sizeof(Point2f) * texCount); 221 } 222 223 IndexedTriangleStripArray *geom = 224 new IndexedTriangleStripArray(vertexCount, 225 normalCount, 226 texCount, 227 colorCount, 228 format, 229 indexCount, 230 stripIndexCounts, 231 stripCount); 232 233 geom->setCoordinates(0, vertices, vertexCount); 234 geom->setCoordinateIndices(0, indices, indexCount); 235 236 237 if (texCount) 238 { 239 geom->setTextureCoordinateIndices(0, indices, indexCount); 240 geom->setTextureCoordinates(0, texCoords, texCount); 241 } 242 243 if (normalCount) 244 { 245 geom->setNormals(0, normals, normalCount); 246 geom->setNormalIndices(0, normalIndices, indexCount); 247 } 248 249 250 /////// 251 //-- cleanup 252 253 DELAPTR(stripIndexCounts); 254 DELAPTR(vertices); 255 DELAPTR(normals); 256 DELAPTR(texCoords); 257 DELAPTR(indices); 258 DELAPTR(normalIndices); 259 260 261 return geom; 262 */ 263 return 0; 160 cout << "vertexcount: " << vertexCount << endl; 161 162 vertices = new Vector3[vertexCount]; 163 str.read(reinterpret_cast<char *>(vertices), sizeof(Vector3) * vertexCount); 164 165 normals = new Vector3[vertexCount]; 166 str.read(reinterpret_cast<char *>(normals), sizeof(Vector3) * vertexCount); 167 168 int texCoordCount; 169 str.read(reinterpret_cast<char *>(&texCoordCount), sizeof(int)); 170 171 if (texCoordCount) 172 { 173 cout << "loading texcoords of size " << texCoordCount << endl; 174 texcoords = new float[texCoordCount * 2]; 175 str.read(reinterpret_cast<char *>(texcoords), sizeof(float) * vertexCount * 2); 176 } 177 else 178 texcoords = NULL; 179 180 for (int i = 0; i < vertexCount; i += 3) 181 { 182 Triangle3 tri(vertices[i], vertices[i + 1], vertices[i + 2]); 183 Vector3 n = tri.GetNormal(); 184 185 normals[i + 0] = n; 186 normals[i + 1] = n; 187 normals[i + 2] = n; 188 } 189 190 return new Geometry(vertices, normals, texcoords, vertexCount); 264 191 } 265 192 … … 267 194 bool BinaryLoader::Load(const std::string &filename, SceneEntityContainer &geometry) 268 195 { 269 #if 0 270 clear_textures(); // clear the texture table 271 272 BranchGroup *root = new BranchGroup(); 273 //root->setName("start"); 274 igzstream str(filename.c_str());//, ios::binary); 275 276 if (!str.is_open()) 196 //clear_textures(); // clear the texture table 197 198 //igzstream str(filename.c_str());//, ios::binary); 199 ifstream istr(filename.c_str(), ios::binary); 200 201 if (!istr.is_open()) 277 202 return false; 278 203 279 204 // #shapes 280 205 int shapeCount; 281 str.read(reinterpret_cast<char *>(&shapeCount), sizeof(int));206 istr.read(reinterpret_cast<char *>(&shapeCount), sizeof(int)); 282 207 283 208 int progress = 0; 284 209 285 GeometryProcessor geomConv(500000, NULL);286 287 210 for (int i = 0; i < shapeCount; ++ i) 288 211 { 289 Shape3D *shape = LoadShape(str); 290 291 vector<Shape3D *> newShapes; 292 ShapePair shapePair = ShapePair(shape, -1); 293 294 // check, if this node actually contains any geometry 295 CalcNumTrianglesVisitor triangleVisitor; 296 triangleVisitor.Apply(shape); 297 const int numTriangles = triangleVisitor.GetNumTriangles(); 298 299 // the shape should be subdivided => replace old shape 300 if (geomConv.SubdivideShape(newShapes, shapePair, numTriangles)) 301 { 302 OUT1("triangleCount: " << numTriangles << " => subdividing to " << newShapes.size() << " shapes"); 303 delete shape;//shape->unref(); 304 } 305 else 306 { 307 newShapes.push_back(shape); 308 } 309 310 VisibilityGroup * visGrp = new VisibilityGroup(); 311 root->addChild(visGrp); 312 313 for (size_t j = 0; j < newShapes.size(); ++ j) 314 { 315 Shape3D *sh = newShapes[j]; 316 visGrp->addChild(sh); 317 } 318 319 int p = i * 100 / shapeCount; 212 SceneEntity *ent = LoadSceneEntity(istr); 213 geometry.push_back(ent); 214 215 int p = (i + 1) * 100 / shapeCount; 320 216 321 217 if (p >= progress) 322 218 { 323 OUT1("% loaded: " << p);219 cout << "% loaded: " << p << endl; 324 220 progress += 10; 325 221 } 326 222 } 327 223 328 OUT1("bin loading finished"); 329 330 return root; 331 #endif 332 } 333 334 } 224 cout << "bin loading finished" << endl; 225 226 return true; 227 } 228 229 }
Note: See TracChangeset
for help on using the changeset viewer.