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

View File

@ -86,7 +86,9 @@ void Renderer::Model::loadModel(QString path)
maxY = std::numeric_limits<float>::min();
minZ = std::numeric_limits<float>::max();
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, maxZ));
AABB.push_back(QVector3D(minX, maxY, minZ));
@ -121,36 +123,35 @@ std::unique_ptr<Drawable> Model::processMesh(aiMesh* mesh, const aiScene* scene,
aiString str;
material->GetTexture(aiTextureType_BASE_COLOR, 0, &str);
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<PaintingMesh>(glFunc, paintingProgram, shadowProgram, modelQ);
// 遍历网格的每个顶点
std::vector<Vertex> vertices;
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);
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<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];
// 将所有面的索引数据添加到索引数组中
for (unsigned int j = 0; j < face.mNumIndices; j++) {
m_mesh->indices.push_back(face.mIndices[j]);
}
}
auto& [paintingPath, leftBottom, rightTop] = iter->second;
qDebug() << str.C_Str() << "Replaced";
auto m_mesh = std::make_unique<PaintingMesh>(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<Drawable> Model::processMesh(aiMesh* mesh, const aiScene* scene,
}
else
{
// 初始化网格
auto m_mesh = std::make_unique<Mesh>(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)))

View File

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

View File

@ -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();
}

View File

@ -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);

View File

@ -1,6 +1,7 @@
#include "RendererWidget.h"
#include "RendererGLWidget.h"
#include "../FluentMenu.h"
#include <QFileDialog>
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);