summaryrefslogtreecommitdiff
path: root/MapTool/t3d/objreader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'MapTool/t3d/objreader.cpp')
-rw-r--r--MapTool/t3d/objreader.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/MapTool/t3d/objreader.cpp b/MapTool/t3d/objreader.cpp
new file mode 100644
index 0000000..3a07323
--- /dev/null
+++ b/MapTool/t3d/objreader.cpp
@@ -0,0 +1,80 @@
+#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]];
+ }
+}