1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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]];
}
}
|