相机远近平面随模型自适应,方向键上下调整相机移动速度

main
wuyize 2024-01-08 15:43:48 +08:00
parent 20114158be
commit 34524a4319
7 changed files with 35 additions and 7 deletions

View File

@ -5,6 +5,7 @@
#include <QJsonDocument>
#include <QJsonObject>
#include "../util/PaintingUtil.h"
#include <QStackedwidget>
using namespace Library;
@ -20,8 +21,6 @@ LibraryWidget::LibraryWidget(QWidget* parent)
connect(ui.classComboBox, &QComboBox::textActivated, this, &LibraryWidget::onClassChanged);
Renderer::PaintingRenderer::instance();
onClassChanged(ui.classComboBox->currentText());
}
QSize LibraryWidget::getPaintingAdjustedSize(const QString& path)
@ -103,3 +102,13 @@ void Library::LibraryWidget::onClassChanged(const QString& name)
loadingThread = std::jthread([this](std::stop_token stop) {loadIcons(stop); });
}
void Library::LibraryWidget::currentTabChanged(int index)
{
static bool firstChange = true;
if (qobject_cast<QStackedWidget*>(sender())->currentWidget() == this && firstChange)
{
onClassChanged(ui.classComboBox->currentText());
firstChange = false;
}
}

View File

@ -29,6 +29,9 @@ namespace Library
LibraryWidget(QWidget* parent = nullptr);
public slots:
void currentTabChanged(int index);
signals:
void openPaintingFile(QString path);

View File

@ -21,6 +21,7 @@ CentralWidget::CentralWidget(QWidget* parent) : QWidget(parent)
ui.gridLayout->addWidget(navigationBarWidget, 0, 0, 1, 1, Qt::AlignTop | Qt::AlignHCenter);
QObject::connect(navigationBarWidget->tabs, &QtMaterialTabs::currentChanged, ui.stackedWidget, &QStackedWidget::setCurrentIndex);
QObject::connect(ui.stackedWidget, &QStackedWidget::currentChanged, ui.rendererWidget, &Renderer::RendererWidget::currentTabChanged);
QObject::connect(ui.stackedWidget, &QStackedWidget::currentChanged, ui.libraryWidget, &Library::LibraryWidget::currentTabChanged);
QObject::connect(ui.rendererWidget, &Renderer::RendererWidget::openPaintingFile, [&, navigationBarWidget](QString path) {
navigationBarWidget->tabs->setCurrentTab(0);
ui.stackedWidget->setCurrentWidget(ui.editorWidget);

View File

@ -5,6 +5,12 @@ Light::Light(Camera* camera)
: camera(camera)
//, shadowCascadeLevels{ camera->FarPlane / 25.0f, camera->FarPlane / 12.0f, camera->FarPlane / 6.0f, camera->FarPlane / 3.0f }
{
updateShadowCascadeLevels();
}
void Renderer::Light::updateShadowCascadeLevels()
{
shadowCascadeLevels.clear();
const float levelCount = 5;
const float lambda = 0.5;
for (int i = 1; i < levelCount; i++)

View File

@ -20,6 +20,7 @@ namespace Renderer
std::vector<float> frustumSizes;
Model* model = nullptr;
Light(Camera* camera);
void updateShadowCascadeLevels();
std::vector<QVector4D> getFrustumCornersWorldSpace(const QMatrix4x4& projview);
std::vector<QVector4D> getFrustumCornersWorldSpace(const QMatrix4x4& proj, const QMatrix4x4& view);
QMatrix4x4 getLightSpaceMatrix(const float nearPlane, const float farPlane);

View File

@ -19,6 +19,8 @@ namespace Renderer
std::vector<QVector3D> AABB;
glm::vec3 minPos = glm::vec3(std::numeric_limits<float>::max());
glm::vec3 maxPos = glm::vec3(std::numeric_limits<float>::min());
private:
QOpenGLContext* context = nullptr;
QOpenGLFunctions_4_5_Core* glFunc = nullptr;
@ -39,8 +41,6 @@ namespace Renderer
QDir directory; /// 模型所在路径
QString name; /// 模型文件名
glm::vec3 minPos = glm::vec3(std::numeric_limits<float>::max());
glm::vec3 maxPos = glm::vec3(std::numeric_limits<float>::min());
/// 递归遍历结点
void processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4 = aiMatrix4x4());

View File

@ -84,8 +84,12 @@ void RendererGLWidget::setModel(QString path)
//model->loadModel("Models/Sponza/Sponza.gltf");
//model->loadModel("E:\\3D Objects\\Gate\\gltf\\Gate.gltf");
model->loadModel(path);
light.model = model;
qDebug() << model->AABB;
light.model = model;
auto extent = model->maxPos - model->minPos;
camera.FarPlane = glm::sqrt(extent.x * extent.x + extent.y * extent.y + extent.z * extent.z);
camera.NearPlane = camera.FarPlane / 500.f;
light.updateShadowCascadeLevels();
doneCurrent();
}
@ -689,6 +693,10 @@ void RendererGLWidget::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Escape)
clearFocus();
else if (event->key() == Qt::Key_Up)
camera.MovementSpeed += 50;
else if (event->key() == Qt::Key_Down)
camera.MovementSpeed = glm::max(50.f, camera.MovementSpeed - 50);
else if (!event->isAutoRepeat())
pressedKeys.insert(event->key());
QOpenGLWidget::keyPressEvent(event);