From ed7d72e5948db0cc3e186e043a1ec3462cd18789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Wed, 11 Dec 2019 22:27:44 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/libs@681 a8b55f48-bf90-11e4-a774-851b48703e85 --- qglengine/formats/loader_assimp.cpp | 57 +++++++++++++++++++++-- qglengine/qglview_test/qglview_window.cpp | 1 - qglengine/widgets/materials_editor.ui | 5 +- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/qglengine/formats/loader_assimp.cpp b/qglengine/formats/loader_assimp.cpp index 2974021..4fd4e92 100644 --- a/qglengine/formats/loader_assimp.cpp +++ b/qglengine/formats/loader_assimp.cpp @@ -81,6 +81,46 @@ Mesh * assimpMesh(const aiMesh * m) { } +QColor aiMatColor(const aiMaterial * m, const char * key, uint s0, uint s1, const QColor & def = Qt::white) { + aiColor4D col; + aiReturn r = m->Get(key, s0, s1, col); + //qDebug() << key << r << col.r << col.g << col.b; + if (r != aiReturn_SUCCESS) return def; + return QColor::fromRgbF(col.r, col.g, col.b); +} +float aiMatFloat(const aiMaterial * m, const char * key, uint s0, uint s1, float def = 0.f) { + float ret; + aiReturn r = m->Get(key, s0, s1, ret); + if (r != aiReturn_SUCCESS) return def; + return ret; +} +QString aiMatMap(const aiMaterial * m, aiTextureType texture) { + aiString p; + aiReturn r = const_cast(m)->GetTexture(texture, 0, &p); + if (r != aiReturn_SUCCESS) return QString(p.C_Str()); + return QString(); +} +Material * assimpMaterial(const aiMaterial * m) { + if (!m) return 0; + Material * ret = new Material(); + ret->name = const_cast(m)->GetName().C_Str(); + //qDebug() << "mat" << ret->name; + //for (int i = 0; i < m->mNumProperties; ++i) + // qDebug()<< m->mProperties[i]->mKey.C_Str(); + ret->color_diffuse = aiMatColor(m, AI_MATKEY_COLOR_DIFFUSE); + ret->color_specular = aiMatColor(m, AI_MATKEY_COLOR_SPECULAR); + ret->color_emission = aiMatColor(m, AI_MATKEY_COLOR_EMISSIVE); + ret->map_diffuse .bitmap_path = aiMatMap(m, aiTextureType_DIFFUSE); + ret->map_normal .bitmap_path = aiMatMap(m, aiTextureType_NORMALS); + ret->map_specular .bitmap_path = aiMatMap(m, aiTextureType_SPECULAR); + ret->map_roughness.bitmap_path = aiMatMap(m, aiTextureType_DIFFUSE_ROUGHNESS); + ret->map_emission .bitmap_path = aiMatMap(m, aiTextureType_EMISSIVE); + ret->transparency = 1.f - aiMatFloat(m, AI_MATKEY_OPACITY, 1.f); + ret->setTypes(); + return ret; +} + + Light * assimpLight(const aiLight * l) { if (!l) return 0; Light * ret = new Light(); @@ -102,7 +142,9 @@ Light * assimpLight(const aiLight * l) { } -ObjectBase * assimpObject(const aiNode * n, const QVector & meshes, const QMap & light_by_name, QVector & out_lights) { +ObjectBase * assimpObject(const aiNode * n, const QVector & meshes, aiMesh ** ai_meshes, + const QVector & materials, + const QMap & light_by_name, QVector & out_lights) { if (!n) return 0; ObjectBase * ret = 0; QString name = n->mName.C_Str(); @@ -120,8 +162,12 @@ ObjectBase * assimpObject(const aiNode * n, const QVector & meshes, con for (uint i = 0; i < n->mNumMeshes; ++i) { int mi = n->mMeshes[i]; if (meshes[mi]) { - if (!ret->mesh()) + if (!ret->mesh()) { ret->setMesh(new Mesh()); + int mati = ai_meshes[mi]->mMaterialIndex; + if (mati >= 0 || mati < materials.size()) + ret->setMaterial(materials[mati]); + } ret->mesh()->append(meshes[mi]); //ret->setMesh(meshes[mi]); //qDebug() << "set mesh" << mi << ret->mesh(); @@ -130,7 +176,7 @@ ObjectBase * assimpObject(const aiNode * n, const QVector & meshes, con } } for (uint i = 0; i < n->mNumChildren; ++i) { - ObjectBase * co = assimpObject(n->mChildren[i], meshes, light_by_name, out_lights); + ObjectBase * co = assimpObject(n->mChildren[i], meshes, ai_meshes, materials, light_by_name, out_lights); if (co) ret->addChild(co); } @@ -154,6 +200,9 @@ Scene * loadScene(const QString & filepath) { QVector meshes; for (uint i = 0; i < ais->mNumMeshes; ++i) meshes << assimpMesh(ais->mMeshes[i]); + QVector materials; + for (uint i = 0; i < ais->mNumMaterials; ++i) + materials << assimpMaterial(ais->mMaterials[i]); QVector lights; for (uint i = 0; i < ais->mNumLights; ++i) lights << assimpLight(ais->mLights[i]); @@ -162,7 +211,7 @@ Scene * loadScene(const QString & filepath) { light_by_name[l->name()] = l; QVector out_lights; - ObjectBase * root = assimpObject(ais->mRootNode, meshes, light_by_name, out_lights); + ObjectBase * root = assimpObject(ais->mRootNode, meshes, ais->mMeshes, materials, light_by_name, out_lights); if (!root) return 0; ObjectBaseList rcl = root->children(true); diff --git a/qglengine/qglview_test/qglview_window.cpp b/qglengine/qglview_test/qglview_window.cpp index 869c54f..fd1b38c 100644 --- a/qglengine/qglview_test/qglview_window.cpp +++ b/qglengine/qglview_test/qglview_window.cpp @@ -82,7 +82,6 @@ QGLViewWindow::QGLViewWindow(QWidget * parent): QMainWindow(parent), Ui::QGLView view->view()->start(-1); startTimer(1000/60); - connect(view->view(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged())); connect(view->view(), SIGNAL(keyEvent(Qt::Key, Qt::KeyboardModifiers)), this, SLOT(view_keyEvent(Qt::Key, Qt::KeyboardModifiers))); //connect(matEditor, SIGNAL(changed()), this, SLOT(materialChanged())); sceneTree->assignQGLView(view->view()); diff --git a/qglengine/widgets/materials_editor.ui b/qglengine/widgets/materials_editor.ui index e81227f..aa9fb13 100644 --- a/qglengine/widgets/materials_editor.ui +++ b/qglengine/widgets/materials_editor.ui @@ -176,7 +176,7 @@ 0 0 386 - 434 + 440 @@ -189,9 +189,6 @@ 0 - - 0 - 0