diff --git a/parse.cpp b/parse.cpp new file mode 100644 index 0000000..b15416b --- /dev/null +++ b/parse.cpp @@ -0,0 +1,111 @@ +```cpp +void parse(const char* filename, std::vector &vBuffer, std::vector &tBuffer, std::vector &meshList) { + + std::ifstream file(filename); + if (!file) { + + return; + } + parse(file, vBuffer, tBuffer, meshList); + } + + void parse(std::istream& file, std::vector &vBuffer, std::vector &tBuffer, std::vector &meshList) { + std::string mtlPath; + std::string line; + subMesh currMesh; + + while (std::getline(file, line)) { + std::stringstream ss(line); + + //reads until white space + ss.unsetf(std::ios_base::skipws); + ss >> std::ws; + + // If the line is empty + if (ss.eof()) { + continue; + } + + if (ss.peek() == '#') { + // Comment line in file + continue; + } + + std::string keyword; + ss >> keyword; + + //In the case of a vertex coordinate + if (keyword == "v") { + vec3d v; + ss >> std::ws >> v.x >> std::ws >> v.y >> std::ws >> v.z >> std::ws; + vBuffer.push_back(v); + + } + //In the case of a texture coordinate + else if (keyword == "vt") { + vec2d t; + ss >> std::ws >> t.u >> std::ws >> t.v >> std::ws; + //t.u = 1.0f - t.u; + //t.v = 1.0f - t.v; + tBuffer.push_back(t); + } + + //In the case of a face + else if (keyword == "f") { + triangle currFace; + std::vector parts; + std::vector vertIndices; + std::vector texIndices; + + while (!ss.eof()) { + //Read each section is as a part of a list + int pointcount = 0; + std::string part; + ss >> std::ws >> part >> std::ws; + parts.push_back(part); + + for (unsigned int i = 0; i < parts.size(); i++) { + int vert; + int tex; + + std::stringstream partSS(parts[i]); + partSS >> vert; + + if (!partSS.eof() && (partSS.peek() == '/')) { + char slash1; + partSS >> slash1; + + partSS >> tex; + currFace.texs[pointcount] = tex; + currFace.texs[pointcount] = currFace.texs[pointcount] - 1; + } + + currFace.verts[pointcount++] = vert; + currFace.verts[pointcount - 1] = currFace.verts[pointcount - 1] - 1; + } + } + currMesh.triGroup.push_back(currFace); + } + else if (keyword == "o") { + if (currMesh.name.empty()) { + ss >> std::ws >> currMesh.name; + } + else { + meshList.push_back(currMesh); + currMesh = {}; + ss >> std::ws >> currMesh.name; + } + } + else if (keyword == "mtllib") { + mtlPath = line.substr(7); + } + else if (keyword == "usemtl") { + std::string material; + ss >> std::ws >> material; + getTexturePath(material, mtlPath, currMesh); + } + + } + meshList.push_back(currMesh); + } +``` \ No newline at end of file