summaryrefslogtreecommitdiffstats
path: root/model.cpp
blob: 20c91ba9fbe3870031732138173b04c18eac8267 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "model.h"

#include <QFile>
#include <QSet>
#include <QTextStream>
#include <QVarLengthArray>

#include "GL/gl.h"

static Point3d readPoint(QTextStream &ts)
{
    Point3d p;
    ts >> p.x >> p.y >> p.z;
    return p;
}

struct Edge
{
    int pointA;
    int pointB;
};

uint qHash(const Edge &edge)
{
    return qHash(edge.pointA) ^ qHash(edge.pointB);
}

bool operator==(const Edge &a, const Edge &b)
{
    return (a.pointA == b.pointA && a.pointB == b.pointB)
        || (a.pointA == b.pointB && a.pointB == b.pointA);
}

Model::Model(const QString &filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly))
        return;

    QSet<Edge> edges;

    Point3d min( 1e9, 1e9, 1e9);
    Point3d max(-1e9,-1e9,-1e9);

    QVector<Point3d> pointData;
    QVector<Point3d> normalData;

    QVector<int> pointIndices;
    QVector<int> normalIndices;

    QTextStream in(&file);
    while (!in.atEnd()) {
        QString input = in.readLine();

        if (input.isEmpty() || input[0] == '#')
            continue;

        QTextStream ts(&input);
        QString id;
        ts >> id;

        if (id == "v") {
            Point3d p = readPoint(ts);
            p.y = -p.y;
            pointData << p;

            min.x = qMin(min.x, p.x);
            min.y = qMin(min.y, p.y);
            min.z = qMin(min.z, p.z);

            max.x = qMax(max.x, p.x);
            max.y = qMax(max.y, p.y);
            max.z = qMax(max.z, p.z);
        } else if (id == "f" || id == "fo") {
            QVarLengthArray<int, 4> p;

            while (!ts.atEnd()) {
                QString vertex;
                ts >> vertex;

                int index;
                QTextStream vertexStream(&vertex);
                vertexStream >> index;
                if (vertexStream.status() == QTextStream::Ok)
                    p.append(index - 1);
            }

            for (int i = 0; i < p.size(); ++i) {
                Edge edge = { p[i], p[(i + 1) % p.size()] };
                edges << edge;
            }

            for (int i = 0; i < 3; ++i)
                pointIndices << p[i];

            if (p.size() == 4)
                for (int i = 0; i < 3; ++i)
                    pointIndices << p[(i + 2) % 4];
        }
    }

    Point3d bounds = max - min;
    qreal scale = 1 / qMax(bounds.x, qMax(bounds.y, bounds.z));

    for (int i = 0; i < pointData.size(); ++i) {
        Point3d &p = pointData[i];

        p.x -= min.x + bounds.x * 0.5;
        p.y -= min.y + bounds.y * 0.5;
        p.z -= min.z + bounds.z * 0.5;

        p.x *= scale;
        p.y *= scale;
        p.z *= scale;
    }

    for (int i = 0; i < pointIndices.size(); ++i)
        m_points << pointData.at(pointIndices.at(i));

    foreach(const Edge &edge, QVector<Edge>::fromList(edges.toList()))
        m_edges << pointData.at(edge.pointA) << pointData.at(edge.pointB);
}

void Model::render(bool wireframe) const
{
    glEnableClientState(GL_VERTEX_ARRAY);
    if (wireframe) {
        glVertexPointer(3, GL_FLOAT, 0, m_edges.data());
        glDrawArrays(GL_LINES, 0, m_edges.size());
    } else {
        glVertexPointer(3, GL_FLOAT, 0, m_points.data());
        glDrawArrays(GL_TRIANGLES, 0, m_points.size());
    }
    glDisableClientState(GL_VERTEX_ARRAY);
}