//! creates/loads an animated mesh from the file. //! \return Pointer to the created mesh. Returns 0 if loading failed. //! If you no longer need the mesh, you should call IAnimatedMesh::drop(). //! See IReferenceCounted::drop() for more information. IAnimatedMesh* CSTLMeshFileLoader::createMesh(io::IReadFile* file) { const long filesize = file->getSize(); if (filesize < 6) // we need a header return 0; const u32 WORD_BUFFER_LENGTH = 512; SMesh* mesh = new SMesh(); SMeshBuffer* meshBuffer = new SMeshBuffer(); mesh->addMeshBuffer(meshBuffer); meshBuffer->drop(); core::vector3df vertex[3]; core::vector3df normal; c8 buffer[WORD_BUFFER_LENGTH]; bool binary = false; file->read(buffer, 5); if (strncmp("solid", buffer, 5)) binary = true; // read/skip header u32 binFaceCount = 0; if (binary) { file->seek(80); file->read(&binFaceCount, 4); #ifdef __BIG_ENDIAN__ binFaceCount = os::Byteswap::byteswap(binFaceCount); #endif } else goNextLine(file); u16 attrib=0; core::stringc token; token.reserve(32); while (file->getPos() < filesize) { if (!binary) { if (getNextToken(file, token) != "facet") { if (token=="endsolid") break; mesh->drop(); return 0; } if (getNextToken(file, token) != "normal") { mesh->drop(); return 0; } } getNextVector(file, normal, binary); if (!binary) { if (getNextToken(file, token) != "outer") { mesh->drop(); return 0; } if (getNextToken(file, token) != "loop") { mesh->drop(); return 0; } } for (u32 i=0; i<3; ++i) { if (!binary) { if (getNextToken(file, token) != "vertex") { mesh->drop(); return 0; } } getNextVector(file, vertex[i], binary); } if (!binary) { if (getNextToken(file, token) != "endloop") { mesh->drop(); return 0; } if (getNextToken(file, token) != "endfacet") { mesh->drop(); return 0; } } else { file->read(&attrib, 2); #ifdef __BIG_ENDIAN__ attrib = os::Byteswap::byteswap(attrib); #endif } SMeshBuffer* mb = reinterpret_cast<SMeshBuffer*>(mesh->getMeshBuffer(mesh->getMeshBufferCount()-1)); u32 vCount = mb->getVertexCount(); video::SColor color(0xffffffff); if (attrib & 0x8000) color = video::A1R5G5B5toA8R8G8B8(attrib); if (normal==core::vector3df()) normal=core::plane3df(vertex[2],vertex[1],vertex[0]).Normal; mb->Vertices.push_back(video::S3DVertex(vertex[2],normal,color, core::vector2df())); mb->Vertices.push_back(video::S3DVertex(vertex[1],normal,color, core::vector2df())); mb->Vertices.push_back(video::S3DVertex(vertex[0],normal,color, core::vector2df())); mb->Indices.push_back(vCount); mb->Indices.push_back(vCount+1); mb->Indices.push_back(vCount+2); } // end while (file->getPos() < filesize) mesh->getMeshBuffer(0)->recalculateBoundingBox(); // Create the Animated mesh if there's anything in the mesh SAnimatedMesh* pAM = 0; if ( 0 != mesh->getMeshBufferCount() ) { mesh->recalculateBoundingBox(); pAM = new SAnimatedMesh(); pAM->Type = EAMT_OBJ; pAM->addMesh(mesh); pAM->recalculateBoundingBox(); } mesh->drop(); return pAM; }
void meshReader(char *filename, int sign) { float x, y, z, len; int i; char prefix[2]; char letter; point v1, v2, crossP; int *normCount; int fvx, fvy, fvz, ftx, fty, ftz, fnx, fny, fnz; FILE *fp; fp = fopen(filename, "r"); if (fp == NULL) { printf("Cannot open %s\n!", filename); return; } // Count the number of vertices and faces while (!feof(fp)) { fscanf(fp, "%s %f %f %f\n", prefix, &x, &y, &z); if (prefix[0] == 'v') { if (prefix[1] == ' ') { verts++; } else if (prefix[1] == 'n') { norms++; } else if (prefix[1] == 't') { texCoords++; } } else if(prefix[0] == 'f') { faces++; } } fclose(fp); printf("verts : %d\n", verts); printf("faces : %d\n", faces); // Dynamic allocation of vertex and face lists faceList = (faceStruct *)malloc(sizeof(faceStruct)*faces); vertList = (point *)malloc(sizeof(point)*verts); normList = (point *)malloc(sizeof(point)*norms); texList = (point *)malloc(sizeof(point)*texCoords); fp = fopen(filename, "r"); i = 0; // Read the veritces while (!feof(fp)) { fscanf(fp, "%c", prefix); if (prefix[0] == '#') {//its commented line goNextLine(fp); } else if (prefix[0] == 'v') { fscanf(fp, "%c %f %f %f\n", &prefix[1], &x, &y, &z); if (prefix[0] == 'v' && prefix[1] == ' ') { vertList[i].x = x; vertList[i].y = y; vertList[i].z = z; } else if (prefix[0] == 'v' && prefix[1] == 'n') { normList[i].x = x; normList[i].y = y; normList[i].z = z; } else if (prefix[0] == 'v' && prefix[1] == 't') { texList[i].x = x; texList[i].y = y; texList[i].z = z; } } else if (prefix[0] == 'f') { fscanf(fp, "%c %d/%d/%d/%d/%d/%d/%d/%d/%d/\n", prefix, &fvx, &fnx, &ftx, &fvy, &fny, &fty, &fvz, &fnz, &ftz); faceList[i].v1 = fvx - 1; faceList[i].v2 = fvy - 1; faceList[i].v3 = fvz - 1; } } fclose(fp); }