From 4bfdb0d9f65c20b7c2c46afcce55654dbbb50547 Mon Sep 17 00:00:00 2001 From: wuyize Date: Fri, 10 Mar 2023 23:24:19 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20=E6=A8=A1=E5=9E=8B=E5=8C=85=E5=9B=B4?= =?UTF-8?q?=E7=9B=92=E8=AE=A1=E7=AE=97=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Renderer/Mesh.h | 6 +- .../src/Renderer/Model.cpp | 76 +++++++------------ .../src/Renderer/PaintingMesh.h | 6 +- .../src/Renderer/RendererGLWidget.cpp | 11 +-- .../src/Renderer/RendererGLWidget.h | 2 +- .../src/Renderer/RendererWidget.cpp | 14 +++- 6 files changed, 53 insertions(+), 62 deletions(-) diff --git a/ArchitectureColoredPainting/src/Renderer/Mesh.h b/ArchitectureColoredPainting/src/Renderer/Mesh.h index d7bc6b7..dd1d705 100644 --- a/ArchitectureColoredPainting/src/Renderer/Mesh.h +++ b/ArchitectureColoredPainting/src/Renderer/Mesh.h @@ -1,7 +1,7 @@ #pragma once #include #include -#include +#include #include #include #include @@ -38,8 +38,8 @@ namespace Renderer class Mesh : public Drawable { public: - QVector vertices; - QVector indices; + std::vector vertices; + std::vector indices; //QVector textures; GLuint textureBasecolor = 0; GLuint textureMetallicRoughness = 0; diff --git a/ArchitectureColoredPainting/src/Renderer/Model.cpp b/ArchitectureColoredPainting/src/Renderer/Model.cpp index 0cd402c..a22be70 100644 --- a/ArchitectureColoredPainting/src/Renderer/Model.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Model.cpp @@ -86,7 +86,9 @@ void Renderer::Model::loadModel(QString path) maxY = std::numeric_limits::min(); minZ = std::numeric_limits::max(); maxZ = std::numeric_limits::min(); - processNode(scene->mRootNode, scene); + aiMatrix4x4 transform; + aiMatrix4x4::Scaling(aiVector3D(1 / 0.008), transform); + processNode(scene->mRootNode, scene, transform * scene->mRootNode->mTransformation); AABB.push_back(QVector3D(minX, minY, minZ)); AABB.push_back(QVector3D(minX, minY, maxZ)); AABB.push_back(QVector3D(minX, maxY, minZ)); @@ -121,36 +123,35 @@ std::unique_ptr Model::processMesh(aiMesh* mesh, const aiScene* scene, aiString str; material->GetTexture(aiTextureType_BASE_COLOR, 0, &str); + std::vector vertices; + for (unsigned int i = 0; i < mesh->mNumVertices; i++) + { + if (mesh->mNormals && mesh->mTextureCoords[0]) + { + auto pos = mesh->mVertices[i]; + vertices.push_back(Vertex(pos, mesh->mNormals[i], mesh->mTextureCoords[0][i])); + auto worldPos = model * pos; + minX = std::min(minX, worldPos.x); + maxX = std::max(maxX, worldPos.x); + minY = std::min(minY, worldPos.y); + maxY = std::max(maxY, worldPos.y); + minZ = std::min(minZ, worldPos.z); + maxZ = std::max(maxZ, worldPos.z); + } + } + std::vector indices; + for (auto face = mesh->mFaces; face < mesh->mFaces + mesh->mNumFaces; face++) + indices.insert(indices.end(), face->mIndices, face->mIndices + face->mNumIndices); + + if (auto iter = paintingMap.find(std::string(str.C_Str())); paintingProgram != nullptr && iter != paintingMap.end()) { auto& [paintingPath, leftBottom, rightTop] = iter->second; qDebug() << str.C_Str() << "Replaced"; - // 初始化网格 - auto m_mesh = std::make_unique(glFunc, paintingProgram, shadowProgram, modelQ); - // 遍历网格的每个顶点 - for (unsigned int i = 0; i < mesh->mNumVertices; i++) - { - if (mesh->mNormals && mesh->mTextureCoords[0]) - { - Vertex vertex(mesh->mVertices[i], mesh->mNormals[i], mesh->mTextureCoords[0][i]); - minX = std::min(minX, vertex.Position.x()); - maxX = std::max(maxX, vertex.Position.x()); - minY = std::min(minY, vertex.Position.y()); - maxY = std::max(maxY, vertex.Position.y()); - minZ = std::min(minZ, vertex.Position.z()); - maxZ = std::max(maxZ, vertex.Position.z()); - m_mesh->vertices.push_back(vertex); - } - } - for (unsigned int i = 0; i < mesh->mNumFaces; i++) - { - aiFace face = mesh->mFaces[i]; - // 将所有面的索引数据添加到索引数组中 - for (unsigned int j = 0; j < face.mNumIndices; j++) { - m_mesh->indices.push_back(face.mIndices[j]); - } - } + auto m_mesh = std::make_unique(glFunc, paintingProgram, shadowProgram, modelQ); + m_mesh->vertices = vertices; + m_mesh->indices = indices; m_mesh->paintingId = loadPainting(paintingPath); auto& handle = vtManager->getPaintingHandle(m_mesh->paintingId); @@ -161,28 +162,9 @@ std::unique_ptr Model::processMesh(aiMesh* mesh, const aiScene* scene, } else { - // 初始化网格 auto m_mesh = std::make_unique(glFunc, shaderProgram, shadowProgram, modelQ); - // 遍历网格的每个顶点 - for (unsigned int i = 0; i < mesh->mNumVertices; i++) - { - if (mesh->mNormals && mesh->mTextureCoords[0]) - { - Vertex vertex(mesh->mVertices[i], mesh->mNormals[i], mesh->mTextureCoords[0][i]); - minX = std::min(minX, vertex.Position.x()); - maxX = std::max(maxX, vertex.Position.x()); - minY = std::min(minY, vertex.Position.y()); - maxY = std::max(maxY, vertex.Position.y()); - minZ = std::min(minZ, vertex.Position.z()); - maxZ = std::max(maxZ, vertex.Position.z()); - m_mesh->vertices.push_back(vertex); - } - } - - // 将所有面的索引数据添加到索引数组中 - for (auto face = mesh->mFaces; face < mesh->mFaces + mesh->mNumFaces; face++) - for (auto indice = face->mIndices; indice < face->mIndices + face->mNumIndices; indice++) - m_mesh->indices.push_back(*indice); + m_mesh->vertices = vertices; + m_mesh->indices = indices; // 处理材质 if (!(m_mesh->textureBasecolor = loadMaterialTextures(material, aiTextureType_BASE_COLOR))) diff --git a/ArchitectureColoredPainting/src/Renderer/PaintingMesh.h b/ArchitectureColoredPainting/src/Renderer/PaintingMesh.h index 595c9ea..773a992 100644 --- a/ArchitectureColoredPainting/src/Renderer/PaintingMesh.h +++ b/ArchitectureColoredPainting/src/Renderer/PaintingMesh.h @@ -1,7 +1,7 @@ #pragma once #include #include -#include +#include #include #include #include @@ -18,8 +18,8 @@ namespace Renderer class PaintingMesh : public Drawable { public: - QVector vertices; - QVector indices; + std::vector vertices; + std::vector indices; GLuint textureBasecolor; GLuint textureMetallicRoughness; QMatrix4x4 model; diff --git a/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.cpp b/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.cpp index 5caa9e6..679bfcb 100644 --- a/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.cpp +++ b/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.cpp @@ -73,17 +73,14 @@ void RendererGLWidget::stopTimer() timerId = -1; } -void RendererGLWidget::setModel() +void RendererGLWidget::setModel(QString path) { makeCurrent(); - model->loadModel("Models/Sponza/Sponza.gltf"); - //model = new Model("E:\\3D Objects\\gallery_gltf\\gallery_gltf.gltf", context(), modelProgramPtr, paintingProgramPtr, shadowProgramPtr, paintingHelper); + //model->loadModel("Models/Sponza/Sponza.gltf"); + //model->loadModel("E:\\3D Objects\\Gate\\gltf\\Gate.gltf"); + model->loadModel(path); light.model = model; qDebug() << model->AABB; - //paintingHelper->allocateBuffers(); - //paintingCompProgramPtr->bind(); - //paintingHelper->bindPaintingBuffers(); - //paintingCompProgramPtr->release(); doneCurrent(); } diff --git a/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.h b/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.h index e590cf7..5565f46 100644 --- a/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.h +++ b/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.h @@ -26,7 +26,7 @@ namespace Renderer void startTimer(); void stopTimer(); public slots: - void setModel(); + void setModel(QString path); void setMainLightPitch(float pitch); void setMainLightYaw(float yaw); void setExposure(float exposure); diff --git a/ArchitectureColoredPainting/src/Renderer/RendererWidget.cpp b/ArchitectureColoredPainting/src/Renderer/RendererWidget.cpp index 27bc6c8..5e777a4 100644 --- a/ArchitectureColoredPainting/src/Renderer/RendererWidget.cpp +++ b/ArchitectureColoredPainting/src/Renderer/RendererWidget.cpp @@ -1,6 +1,7 @@ #include "RendererWidget.h" #include "RendererGLWidget.h" #include "../FluentMenu.h" +#include Renderer::RendererWidget::RendererWidget(QWidget* parent) : QWidget(parent) @@ -10,8 +11,10 @@ Renderer::RendererWidget::RendererWidget(QWidget* parent) FluentMenu* menu = new FluentMenu(this); auto openAction = new QAction(QStringLiteral("打开"), menu); auto saveAction = new QAction(QStringLiteral("保存"), menu); + auto testAction = new QAction(QStringLiteral("测试"), menu); menu->addAction(openAction); menu->addAction(saveAction); + menu->addAction(testAction); ui.openButton->setHaloVisible(false); ui.openButton->setOverlayStyle(::Material::TintedOverlay); @@ -34,7 +37,16 @@ Renderer::RendererWidget::RendererWidget(QWidget* parent) QObject::connect(ui.exposureSlider, &QSlider::valueChanged, [&](int value) { ui.openGLWidget->setExposure(value / 100.f); }); - QObject::connect(openAction, &QAction::triggered, ui.openGLWidget, &Renderer::RendererGLWidget::setModel); + + QObject::connect(openAction, &QAction::triggered, [&] { + QString fileName = QFileDialog::getOpenFileName(this, QStringLiteral("打开模型"), QString(), QStringLiteral("glTF 2.0 (*.gltf)")); + qDebug() << fileName; + if (fileName != QString()) + ui.openGLWidget->setModel(fileName); + }); + QObject::connect(testAction, &QAction::triggered, [&] { + ui.openGLWidget->setModel("Models/Sponza/Sponza.gltf"); + }); ui.horizontalSlider->setValue(105); ui.horizontalSlider_2->setValue(80);