ResourceModel::ResourceModel( const std::string& filename ): ResourceGeneric( filename ), mFlipAxis(true) { struct TMFHEADER { uint8_t id[4]; // should be TMF uint16_t version; // version, multiplied by 100 uint16_t nobj; // number of geometry objects uint16_t nhelpers; // number of helper objects } TMFHeader; memset(&TMFHeader, 0, sizeof(TMFHEADER)); LoadMaterialLibrary(filename); uint32_t filesize = 0; FILE* fp = 0; fopen_s(&fp, filename.c_str(), "rb"); if (!fp) return; // Check if it is a TMF file fread(TMFHeader.id, sizeof(uint8_t), 3, fp); if (TMFHeader.id[0] != 'T' || TMFHeader.id[1] != 'M' || TMFHeader.id[2] != 'F') return; fread(&TMFHeader.version, sizeof(uint16_t), 1, fp); assert( TMFHeader.version == MODEL_VERSION ); fread(&TMFHeader.nobj, sizeof(uint16_t), 1, fp); fread(&TMFHeader.nhelpers, sizeof(uint16_t), 1, fp); for (int i = 0; i < TMFHeader.nhelpers; i++) { AddTMFDummy(fp); } for (int i = 0; i < TMFHeader.nobj; i++) { AddTMFObject( fp ); } fclose(fp); }
void OBJLoader::Load(const std::string& filename, bool invertTexCoords) { assert(!filename.empty()); std::string binaryFilename = ".\\/Assets\\/" + filename + ".bin"; std::ifstream binaryInFile; binaryInFile.open(binaryFilename, std::ios::in | std::ios::binary); auto pGraphics = static_cast<IGraphics*>(ResourceManagerInstance()->GetService("graphics")); assert(pGraphics != nullptr); // TODO: Disabled binary load until it can r/w subobjects and material if (true /*!binaryInFile.good()*/) { std::ifstream inFile; inFile.open(".\\/Assets\\/" + filename); if (!inFile.good()) { TI_LOG_E("Couldn't open obj file: " << filename); return; } else { //Start new subobject list for this file _meshSubObjs[filename]; //Temp subobject SubObject* pSubObject = nullptr; //Vectors to store the vertex positions, normals and texture coordinates. Need to use vectors since they're resizeable and we have //no way of knowing ahead of time how large these meshes will be std::vector<Vector3> verts; std::vector<Vector3> normals; std::vector<Vector2> texCoords; //DirectX uses 1 index buffer, OBJ is optimized for storage and not rendering and so uses 3 smaller index buffers.....great... //We'll have to merge this into 1 index buffer which we'll do after loading in all of the required data. std::vector<unsigned short> vertIndices; std::vector<unsigned short> normalIndices; std::vector<unsigned short> textureIndices; std::string input; UINT indexCounter = 0; Vector3 vert; Vector2 texCoord; Vector3 normal; unsigned short vInd[3]; //indices for the vertex position unsigned short tInd[3]; //indices for the texture coordinate unsigned short nInd[3]; //indices for the normal std::string beforeFirstSlash; std::string afterFirstSlash; std::string afterSecondSlash; std::string matLib; while (!inFile.eof()) //While we have yet to reach the end of the file... { inFile >> input; //Get the next input from the file //Check what type of input it was, we are only interested in vertex positions, texture coordinates, normals, indices, groups, and materials. if (input.compare("mtllib") == 0)//Material library, calls readMaterial() { inFile >> matLib; LoadMaterialLibrary(matLib); } else if (input.compare("g") == 0)//Group(SubObject) name { if (pSubObject != nullptr) _meshSubObjs[filename].push_back(pSubObject); pSubObject = new SubObject(); inFile >> pSubObject->name; pSubObject->vertexEnd = 0; pSubObject->vertexStart = UINT_MAX; pSubObject->indexEnd = 0; pSubObject->indexStart = UINT_MAX; } else if (input.compare("v") == 0) //Vertex position