#include "objreader.h" T3DObjReader::T3DObjReader(T3DModel &model) { m_model = model; } T3DObjReader::parseLine(QString line) { line = line.trimmed(); if (line.isEmpty() || line.at(0) == '#') return; QStringList params = line.split(' ', QString::SkipEmptyParts); QString cmd = params[0]; if (cmd == "v") { float x, y, z; x = params[1].toFloat(); y = params[2].toFloat(); z = params[3].toFloat(); known_vertices.append(Vec(x, y, z)); } if (cmd == "vt") { float x, y; x = params[1].toFloat(); y = params[2].toFloat(); known_texcoords.append(Vec2(x, y)); } if (cmd == "vn") { float x, y, z; x = params[1].toFloat(); y = params[2].toFloat(); z = params[3].toFloat(); known_normals.append(Vec(x, y, z)); } if (cmd == "f") { int vtxCount = params.count() - 1; if (vtxCount < 3 || vtxCount > 4) return; int vtxID[4], tcID[4], nrmID[4]; for (int i = 0; i < vtxCount; i++) { QString face = params[i+1]; QStringList ids = face.split('/'); vtxID[i] = ids[0].isEmpty() ? 0 : ids[0].toInt(); tcID[i] = ids[1].isEmpty() ? 0 : ids[1].toInt(); nrmID[i] = ids[2].isEmpty() ? 0 : ids[2].toInt(); } if (vtxCount == 3) { Triangle tri; for (int i = 0; i < 3; i++) { tri.vertex[i] = known_vertices[vtxID[i]]; tri.texcoord[i] = known_texcoords[tcID[i]]; tri.normal[i] = known_normals[nrmID[i]]; } m_currentMesh->triangles.append(tri); } if (vtxCount == 4) { Quad q; for (int i = 0; i < 4; i++) { q.vertex[i] = known_vertices[vtxID[i]]; q.texcoord[i] = known_texcoords[tcID[i]]; q.normal[i] = known_normals[nrmID[i]]; } m_currentMesh->quads.append(q); } } if (cmd == "usemtl") { // treat this as "new shape" m_currentMesh = new T3DMesh(); m_model->meshes.append(m_currentMesh); m_currentMesh->material = materials[params[1]]; } }