Fix: 模型包围盒计算错误

TaoZhang-Branch
wuyize 2023-03-10 23:24:19 +08:00
parent ca8d68fe93
commit 4bfdb0d9f6
6 changed files with 53 additions and 62 deletions

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <QOpenGLFunctions_4_5_Core> #include <QOpenGLFunctions_4_5_Core>
#include <QString> #include <QString>
#include <QVector> #include <vector>
#include <QVector2D> #include <QVector2D>
#include <QVector3D> #include <QVector3D>
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
@ -38,8 +38,8 @@ namespace Renderer
class Mesh : public Drawable class Mesh : public Drawable
{ {
public: public:
QVector<Vertex> vertices; std::vector<Vertex> vertices;
QVector<unsigned int> indices; std::vector<unsigned int> indices;
//QVector<Texture*> textures; //QVector<Texture*> textures;
GLuint textureBasecolor = 0; GLuint textureBasecolor = 0;
GLuint textureMetallicRoughness = 0; GLuint textureMetallicRoughness = 0;

View File

@ -86,7 +86,9 @@ void Renderer::Model::loadModel(QString path)
maxY = std::numeric_limits<float>::min(); maxY = std::numeric_limits<float>::min();
minZ = std::numeric_limits<float>::max(); minZ = std::numeric_limits<float>::max();
maxZ = std::numeric_limits<float>::min(); maxZ = std::numeric_limits<float>::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, minZ));
AABB.push_back(QVector3D(minX, minY, maxZ)); AABB.push_back(QVector3D(minX, minY, maxZ));
AABB.push_back(QVector3D(minX, maxY, minZ)); AABB.push_back(QVector3D(minX, maxY, minZ));
@ -121,36 +123,35 @@ std::unique_ptr<Drawable> Model::processMesh(aiMesh* mesh, const aiScene* scene,
aiString str; aiString str;
material->GetTexture(aiTextureType_BASE_COLOR, 0, &str); material->GetTexture(aiTextureType_BASE_COLOR, 0, &str);
if (auto iter = paintingMap.find(std::string(str.C_Str())); paintingProgram != nullptr && iter != paintingMap.end()) std::vector<Vertex> vertices;
{
auto& [paintingPath, leftBottom, rightTop] = iter->second;
qDebug() << str.C_Str() << "Replaced";
// 初始化网格
auto m_mesh = std::make_unique<PaintingMesh>(glFunc, paintingProgram, shadowProgram, modelQ);
// 遍历网格的每个顶点
for (unsigned int i = 0; i < mesh->mNumVertices; i++) for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{ {
if (mesh->mNormals && mesh->mTextureCoords[0]) if (mesh->mNormals && mesh->mTextureCoords[0])
{ {
Vertex vertex(mesh->mVertices[i], mesh->mNormals[i], mesh->mTextureCoords[0][i]); auto pos = mesh->mVertices[i];
minX = std::min(minX, vertex.Position.x()); vertices.push_back(Vertex(pos, mesh->mNormals[i], mesh->mTextureCoords[0][i]));
maxX = std::max(maxX, vertex.Position.x()); auto worldPos = model * pos;
minY = std::min(minY, vertex.Position.y()); minX = std::min(minX, worldPos.x);
maxY = std::max(maxY, vertex.Position.y()); maxX = std::max(maxX, worldPos.x);
minZ = std::min(minZ, vertex.Position.z()); minY = std::min(minY, worldPos.y);
maxZ = std::max(maxZ, vertex.Position.z()); maxY = std::max(maxY, worldPos.y);
m_mesh->vertices.push_back(vertex); minZ = std::min(minZ, worldPos.z);
maxZ = std::max(maxZ, worldPos.z);
} }
} }
std::vector<unsigned int> indices;
for (auto face = mesh->mFaces; face < mesh->mFaces + mesh->mNumFaces; face++)
indices.insert(indices.end(), face->mIndices, face->mIndices + face->mNumIndices);
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
if (auto iter = paintingMap.find(std::string(str.C_Str())); paintingProgram != nullptr && iter != paintingMap.end())
{ {
aiFace face = mesh->mFaces[i]; auto& [paintingPath, leftBottom, rightTop] = iter->second;
// 将所有面的索引数据添加到索引数组中 qDebug() << str.C_Str() << "Replaced";
for (unsigned int j = 0; j < face.mNumIndices; j++) {
m_mesh->indices.push_back(face.mIndices[j]); auto m_mesh = std::make_unique<PaintingMesh>(glFunc, paintingProgram, shadowProgram, modelQ);
} m_mesh->vertices = vertices;
} m_mesh->indices = indices;
m_mesh->paintingId = loadPainting(paintingPath); m_mesh->paintingId = loadPainting(paintingPath);
auto& handle = vtManager->getPaintingHandle(m_mesh->paintingId); auto& handle = vtManager->getPaintingHandle(m_mesh->paintingId);
@ -161,28 +162,9 @@ std::unique_ptr<Drawable> Model::processMesh(aiMesh* mesh, const aiScene* scene,
} }
else else
{ {
// 初始化网格
auto m_mesh = std::make_unique<Mesh>(glFunc, shaderProgram, shadowProgram, modelQ); auto m_mesh = std::make_unique<Mesh>(glFunc, shaderProgram, shadowProgram, modelQ);
// 遍历网格的每个顶点 m_mesh->vertices = vertices;
for (unsigned int i = 0; i < mesh->mNumVertices; i++) m_mesh->indices = indices;
{
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);
// ´¦Àí²ÄÖÊ // ´¦Àí²ÄÖÊ
if (!(m_mesh->textureBasecolor = loadMaterialTextures(material, aiTextureType_BASE_COLOR))) if (!(m_mesh->textureBasecolor = loadMaterialTextures(material, aiTextureType_BASE_COLOR)))

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <QOpenGLFunctions_4_5_Core> #include <QOpenGLFunctions_4_5_Core>
#include <QString> #include <QString>
#include <QVector> #include <vector>
#include <QVector2D> #include <QVector2D>
#include <QVector3D> #include <QVector3D>
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
@ -18,8 +18,8 @@ namespace Renderer
class PaintingMesh : public Drawable class PaintingMesh : public Drawable
{ {
public: public:
QVector<Vertex> vertices; std::vector<Vertex> vertices;
QVector<GLuint> indices; std::vector<GLuint> indices;
GLuint textureBasecolor; GLuint textureBasecolor;
GLuint textureMetallicRoughness; GLuint textureMetallicRoughness;
QMatrix4x4 model; QMatrix4x4 model;

View File

@ -73,17 +73,14 @@ void RendererGLWidget::stopTimer()
timerId = -1; timerId = -1;
} }
void RendererGLWidget::setModel() void RendererGLWidget::setModel(QString path)
{ {
makeCurrent(); makeCurrent();
model->loadModel("Models/Sponza/Sponza.gltf"); //model->loadModel("Models/Sponza/Sponza.gltf");
//model = new Model("E:\\3D Objects\\gallery_gltf\\gallery_gltf.gltf", context(), modelProgramPtr, paintingProgramPtr, shadowProgramPtr, paintingHelper); //model->loadModel("E:\\3D Objects\\Gate\\gltf\\Gate.gltf");
model->loadModel(path);
light.model = model; light.model = model;
qDebug() << model->AABB; qDebug() << model->AABB;
//paintingHelper->allocateBuffers();
//paintingCompProgramPtr->bind();
//paintingHelper->bindPaintingBuffers();
//paintingCompProgramPtr->release();
doneCurrent(); doneCurrent();
} }

View File

@ -26,7 +26,7 @@ namespace Renderer
void startTimer(); void startTimer();
void stopTimer(); void stopTimer();
public slots: public slots:
void setModel(); void setModel(QString path);
void setMainLightPitch(float pitch); void setMainLightPitch(float pitch);
void setMainLightYaw(float yaw); void setMainLightYaw(float yaw);
void setExposure(float exposure); void setExposure(float exposure);

View File

@ -1,6 +1,7 @@
#include "RendererWidget.h" #include "RendererWidget.h"
#include "RendererGLWidget.h" #include "RendererGLWidget.h"
#include "../FluentMenu.h" #include "../FluentMenu.h"
#include <QFileDialog>
Renderer::RendererWidget::RendererWidget(QWidget* parent) Renderer::RendererWidget::RendererWidget(QWidget* parent)
: QWidget(parent) : QWidget(parent)
@ -10,8 +11,10 @@ Renderer::RendererWidget::RendererWidget(QWidget* parent)
FluentMenu* menu = new FluentMenu(this); FluentMenu* menu = new FluentMenu(this);
auto openAction = new QAction(QStringLiteral("´ò¿ª"), menu); auto openAction = new QAction(QStringLiteral("´ò¿ª"), menu);
auto saveAction = new QAction(QStringLiteral("±£´æ"), menu); auto saveAction = new QAction(QStringLiteral("±£´æ"), menu);
auto testAction = new QAction(QStringLiteral("²âÊÔ"), menu);
menu->addAction(openAction); menu->addAction(openAction);
menu->addAction(saveAction); menu->addAction(saveAction);
menu->addAction(testAction);
ui.openButton->setHaloVisible(false); ui.openButton->setHaloVisible(false);
ui.openButton->setOverlayStyle(::Material::TintedOverlay); ui.openButton->setOverlayStyle(::Material::TintedOverlay);
@ -34,7 +37,16 @@ Renderer::RendererWidget::RendererWidget(QWidget* parent)
QObject::connect(ui.exposureSlider, &QSlider::valueChanged, [&](int value) { QObject::connect(ui.exposureSlider, &QSlider::valueChanged, [&](int value) {
ui.openGLWidget->setExposure(value / 100.f); 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->setValue(105);
ui.horizontalSlider_2->setValue(80); ui.horizontalSlider_2->setValue(80);