diff options
author | Treeki <treeki@gmail.com> | 2011-03-12 23:17:12 +0100 |
---|---|---|
committer | Treeki <treeki@gmail.com> | 2011-03-12 23:17:12 +0100 |
commit | 7d4e4c0b34a613dd3c0220475ae4e448197522c1 (patch) | |
tree | 4f5cee367de3fdef4f9a7c84af59ffe76a2bb1c3 /MapTool/t3d | |
download | kamek-7d4e4c0b34a613dd3c0220475ae4e448197522c1.tar.gz kamek-7d4e4c0b34a613dd3c0220475ae4e448197522c1.zip |
initial commit. now I can start playing with stuff!
Diffstat (limited to '')
-rw-r--r-- | MapTool/t3d/common.h | 24 | ||||
-rw-r--r-- | MapTool/t3d/material.cpp | 4 | ||||
-rw-r--r-- | MapTool/t3d/material.h | 9 | ||||
-rw-r--r-- | MapTool/t3d/mesh.cpp | 4 | ||||
-rw-r--r-- | MapTool/t3d/mesh.h | 18 | ||||
-rw-r--r-- | MapTool/t3d/model.cpp | 4 | ||||
-rw-r--r-- | MapTool/t3d/model.h | 16 | ||||
-rw-r--r-- | MapTool/t3d/objreader.cpp | 80 | ||||
-rw-r--r-- | MapTool/t3d/objreader.h | 30 |
9 files changed, 189 insertions, 0 deletions
diff --git a/MapTool/t3d/common.h b/MapTool/t3d/common.h new file mode 100644 index 0000000..15b65b8 --- /dev/null +++ b/MapTool/t3d/common.h @@ -0,0 +1,24 @@ +#ifndef T3DCOMMON_H +#define T3DCOMMON_H + +struct Vec { + float x, y, z; +}; + +struct Vec2 { + float x, y; +}; + +struct Triangle { + Vec vertex[3]; + Vec2 texcoord[3]; + Vec normal[3]; +}; + +struct Quad { + Vec vertex[4]; + Vec2 texcoord[4]; + Vec normal[4]; +}; + +#endif // T3DCOMMON_H diff --git a/MapTool/t3d/material.cpp b/MapTool/t3d/material.cpp new file mode 100644 index 0000000..c9f3a35 --- /dev/null +++ b/MapTool/t3d/material.cpp @@ -0,0 +1,4 @@ +#include "material.h" + +T3DMaterial::T3DMaterial() { +} diff --git a/MapTool/t3d/material.h b/MapTool/t3d/material.h new file mode 100644 index 0000000..cc4bab3 --- /dev/null +++ b/MapTool/t3d/material.h @@ -0,0 +1,9 @@ +#ifndef T3DMATERIAL_H +#define T3DMATERIAL_H + +class T3DMaterial { +public: + T3DMaterial(); +}; + +#endif // T3DMATERIAL_H diff --git a/MapTool/t3d/mesh.cpp b/MapTool/t3d/mesh.cpp new file mode 100644 index 0000000..3dc10b8 --- /dev/null +++ b/MapTool/t3d/mesh.cpp @@ -0,0 +1,4 @@ +#include "mesh.h" + +T3DMesh::T3DMesh() { +} diff --git a/MapTool/t3d/mesh.h b/MapTool/t3d/mesh.h new file mode 100644 index 0000000..2b9ce89 --- /dev/null +++ b/MapTool/t3d/mesh.h @@ -0,0 +1,18 @@ +#ifndef T3DMESH_H +#define T3DMESH_H + +#include "common.h" +#include "material.h" +#include <QtCore/QVector> + +class T3DMesh { +public: + T3DMesh(); + + QVector<Triangle> triangles; + QVector<Quad> quads; + + T3DMaterial *material; +}; + +#endif // T3DMESH_H diff --git a/MapTool/t3d/model.cpp b/MapTool/t3d/model.cpp new file mode 100644 index 0000000..c2dc6dc --- /dev/null +++ b/MapTool/t3d/model.cpp @@ -0,0 +1,4 @@ +#include "model.h" + +T3DModel::T3DModel() { +} diff --git a/MapTool/t3d/model.h b/MapTool/t3d/model.h new file mode 100644 index 0000000..a6535af --- /dev/null +++ b/MapTool/t3d/model.h @@ -0,0 +1,16 @@ +#ifndef T3DMODEL_H +#define T3DMODEL_H + +#include <QtCore/QList> +#include "mesh.h" + +class T3DModel { +public: + T3DModel(); + ~T3DModel(); + + QList<T3DMesh *> meshes; + +}; + +#endif // T3DMODEL_H 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]]; + } +} diff --git a/MapTool/t3d/objreader.h b/MapTool/t3d/objreader.h new file mode 100644 index 0000000..6e518e8 --- /dev/null +++ b/MapTool/t3d/objreader.h @@ -0,0 +1,30 @@ +#ifndef T3DOBJREADER_H +#define T3DOBJREADER_H + +#include <QtCore/QList> +#include <QtCore/QVector> +#include <QtCore/QString> +#include <QtCore/QStringList> +#include <QtCore/QHash> +#include "model.h" + +class T3DObjReader { +private: + T3DModel *m_model; + T3DMesh *m_currentMesh; + + QVector<Vec> known_vertices; + QVector<Vec2> known_texcoords; + QVector<Vec> known_normals; + +public: + T3DObjReader(T3DModel &model); + + void parseLine(QString line); + + T3DModel *model() { return m_model; } + + QHash<QString, T3DMaterial *> materials; +}; + +#endif // T3DOBJREADER_H |