172 lines
5.3 KiB
C++
172 lines
5.3 KiB
C++
/*
|
|
QGLView
|
|
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "loader_assimp.h"
|
|
#include "glscene.h"
|
|
#include "glmesh.h"
|
|
#include "glmaterial.h"
|
|
#include "globject.h"
|
|
#include <assimp/Importer.hpp>
|
|
#include <assimp/postprocess.h>
|
|
#include <assimp/scene.h>
|
|
#include <assimp/mesh.h>
|
|
#include <assimp/material.h>
|
|
|
|
QVector3D fromAiVector3D(const aiVector3D & v) {return QVector3D(v.x, v.y, v.z);}
|
|
Vector3i fromAiFace (const aiFace & v) {return Vector3i(v.mIndices[0], v.mIndices[1], v.mIndices[2]);}
|
|
QMatrix4x4 fromAiMatrix4D(const aiMatrix4x4 & v) {return QMatrix4x4(v.a1, v.a2, v.a3, v.a4,
|
|
v.b1, v.b2, v.b3, v.b4,
|
|
v.c1, v.c2, v.c3, v.c4,
|
|
v.d1, v.d2, v.d3, v.d4);}
|
|
bool isAiMeshTriangles(const aiMesh * m) {return (m->mPrimitiveTypes & aiPrimitiveType_TRIANGLE) == aiPrimitiveType_TRIANGLE;}
|
|
|
|
|
|
Mesh * assimpMesh(const aiMesh * m) {
|
|
if (!m) return 0;
|
|
if (!isAiMeshTriangles(m)) return 0;
|
|
Mesh * ret = new Mesh();
|
|
int vcnt = m->mNumVertices, tcnt = m->mNumFaces;
|
|
|
|
QVector<QVector3D> & v(ret->vertices()); v.resize(vcnt);
|
|
QVector<QVector2D> & t(ret->texcoords()); t.resize(vcnt);
|
|
QVector< Vector3i> & ind(ret->indicesTriangles());
|
|
|
|
for (int i = 0; i < vcnt; ++i)
|
|
v[i] = fromAiVector3D(m->mVertices[i]);
|
|
|
|
if (m->HasNormals()) {
|
|
QVector<QVector3D> & n(ret->normals());
|
|
n.resize(vcnt);
|
|
for (int i = 0; i < vcnt; ++i)
|
|
n[i] = fromAiVector3D(m->mNormals[i]);
|
|
}
|
|
|
|
if (m->HasTextureCoords(0)) {
|
|
for (int i = 0; i < vcnt; ++i)
|
|
t[i] = fromAiVector3D(m->mTextureCoords[0][i]).toVector2D();
|
|
}
|
|
|
|
if (m->HasFaces()) {
|
|
ind.resize(tcnt);
|
|
for (int i = 0; i < tcnt; ++i)
|
|
ind[i] = fromAiFace(m->mFaces[i]);
|
|
} else {
|
|
tcnt = vcnt / 3;
|
|
ind.resize(tcnt);
|
|
int si = 0;
|
|
for (int i = 0; i < tcnt; ++i) {
|
|
si = i+i+i;
|
|
ind[i] = Vector3i(si, si+1, si+2);
|
|
}
|
|
}
|
|
|
|
//qDebug() << "add mesh" << v.size() << ret->normals().size() << ret->texcoords().size() << ret->indices().size();
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
Light * assimpLight(const aiLight * l) {
|
|
if (!l) return 0;
|
|
Light * ret = new Light();
|
|
ret->setName(l->mName.C_Str());
|
|
ret->setPos(fromAiVector3D(l->mPosition));
|
|
ret->direction = fromAiVector3D(l->mDirection);
|
|
ret->decay_const = l->mAttenuationConstant ;
|
|
ret->decay_linear = l->mAttenuationLinear ;
|
|
ret->decay_quadratic = l->mAttenuationQuadratic;
|
|
ret->angle_start = l->mAngleInnerCone * rad2deg;
|
|
ret->angle_end = l->mAngleOuterCone * rad2deg;
|
|
if (l->mType == aiLightSource_SPOT)
|
|
ret->light_type = Light::Cone;
|
|
QVector3D col(l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b);
|
|
ret->intensity = col.length();
|
|
col /= ret->intensity;
|
|
ret->setColor(QColor::fromRgbF(col[0], col[1], col[2]));
|
|
return ret;
|
|
}
|
|
|
|
|
|
ObjectBase * assimpObject(const aiNode * n, const QVector<Mesh * > & meshes, const QMap<QString, Light * > & light_by_name) {
|
|
if (!n) return 0;
|
|
ObjectBase * ret = 0;
|
|
QString name = n->mName.C_Str();
|
|
Light * light = light_by_name.value(name, 0);
|
|
if (light)
|
|
ret = light->clone();
|
|
else
|
|
ret = new ObjectBase();
|
|
ret->setName(name);
|
|
ret->setTransform(fromAiMatrix4D(n->mTransformation));
|
|
//qDebug() << "add object" << ret << ret->name();
|
|
if (!light) {
|
|
for (uint i = 0; i < n->mNumMeshes; ++i) {
|
|
int mi = n->mMeshes[i];
|
|
if (meshes[mi]) {
|
|
ret->setMesh(meshes[mi]);
|
|
//qDebug() << "set mesh" << mi << ret->mesh();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
for (uint i = 0; i < n->mNumChildren; ++i) {
|
|
ObjectBase * co = assimpObject(n->mChildren[i], meshes, light_by_name);
|
|
if (co) ret->addChild(co);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
Scene * loadScene(const QString & filepath) {
|
|
if (filepath.isEmpty()) return 0;
|
|
qDebug() << "[Loader Assimp] Import" << filepath << "...";
|
|
Assimp::Importer importer;
|
|
const aiScene * ais = importer.ReadFile(filepath.toUtf8(), aiProcess_Triangulate |
|
|
aiProcess_SortByPType |
|
|
aiProcess_GenUVCoords |
|
|
aiProcess_TransformUVCoords);
|
|
if (!ais) {
|
|
qDebug() << "[Loader Assimp] Error: \"" + QString(importer.GetErrorString()) + "\"";
|
|
return 0;
|
|
}
|
|
qDebug() << "[Loader Assimp] Imported" << ais->mNumMeshes << "meshes";
|
|
QVector<Mesh * > meshes;
|
|
for (uint i = 0; i < ais->mNumMeshes; ++i)
|
|
meshes << assimpMesh(ais->mMeshes[i]);
|
|
QVector<Light * > lights;
|
|
for (uint i = 0; i < ais->mNumLights; ++i)
|
|
lights << assimpLight(ais->mLights[i]);
|
|
QMap<QString, Light * > light_by_name;
|
|
foreach (Light * l, lights)
|
|
light_by_name[l->name()] = l;
|
|
|
|
ObjectBase * root = assimpObject(ais->mRootNode, meshes, light_by_name);
|
|
if (!root) return 0;
|
|
|
|
Scene * scene = new Scene();
|
|
scene->setName(root->name());
|
|
foreach (ObjectBase * o, root->children())
|
|
scene->addObject(o);
|
|
//foreach (ObjectBase * o, lights)
|
|
// scene->addObject(o);
|
|
qDeleteAll(lights);
|
|
|
|
return scene;
|
|
}
|