Add a dialog for configuring unprojection parameters

This commit is contained in:
baldurk
2025-12-04 16:07:51 +00:00
parent b0b14c1b7b
commit 5b1b57d703
3 changed files with 425 additions and 0 deletions
@@ -0,0 +1,179 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2019-2025 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "ProjectionGuessDialog.h"
#include <float.h>
#include "Code/QRDUtils.h"
#include "ui_ProjectionGuessDialog.h"
ProjectionGuessDialog::ProjectionGuessDialog(ICaptureContext &Ctx,
const ProjectionGuessParameters &params, QWidget *parent)
: QDialog(parent), m_Ctx(Ctx), ui(new Ui::ProjectionGuessDialog)
{
ui->setupUi(this);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
m_Params = params;
if(m_Params.orthographic)
ui->matrixType->setCurrentIndex(1);
else
ui->matrixType->setCurrentIndex(0);
ui->fov->setValue(m_Params.fov);
if(m_Params.aspect <= 0.0)
ui->aspectRatio->setText(QString());
else
ui->aspectRatio->setText(QFormatStr("%1").arg(m_Params.aspect));
if(m_Params.nearPlane <= 0.0)
ui->nearPlane->setText(QString());
else
ui->nearPlane->setText(QFormatStr("%1").arg(m_Params.nearPlane));
if(m_Params.farPlane == FLT_MAX)
ui->infiniteFar->setChecked(true);
else if(m_Params.farPlane <= 0.0)
ui->farPlane->setText(QString());
else
ui->farPlane->setText(QFormatStr("%1").arg(m_Params.farPlane));
connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
&ProjectionGuessDialog::validateParameters);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}
void ProjectionGuessDialog::on_infiniteFar_toggled()
{
if(ui->infiniteFar->isChecked())
{
ui->farPlane->setEnabled(false);
ui->farPlane->setText(tr("Infinite - Reverse Z"));
}
else
{
ui->farPlane->setEnabled(true);
ui->farPlane->setText(QString());
}
}
void ProjectionGuessDialog::on_matrixType_currentIndexChanged(int idx)
{
const bool ortho = (idx == 1);
// not used for orthographic projection
ui->aspectRatio->setEnabled(!ortho);
ui->fov->setEnabled(!ortho);
}
void ProjectionGuessDialog::validateParameters()
{
QString str;
bool ok = false;
m_Params.orthographic = (ui->matrixType->currentIndex() == 1);
if(m_Params.orthographic)
{
m_Params.aspect = 0.0f;
m_Params.fov = 90.0f;
}
else
{
str = ui->aspectRatio->text();
if(str.isEmpty())
{
m_Params.aspect = 0.0f;
}
else
{
m_Params.aspect = str.toFloat(&ok);
if(!ok)
{
RDDialog::critical(this, tr("Aspect ratio error"),
tr("Unrecognised aspect ratio %1, expected decimal number.").arg(str));
return;
}
}
m_Params.fov = (float)ui->fov->value();
}
str = ui->nearPlane->text();
if(str.isEmpty())
{
m_Params.nearPlane = 0.0f;
}
else
{
m_Params.nearPlane = str.toFloat(&ok);
if(!ok)
{
RDDialog::critical(
this, tr("Near plane error"),
tr("Unrecognised near plane %1, expected decimal number if specified.").arg(str));
return;
}
}
if(ui->infiniteFar->isChecked())
{
m_Params.farPlane = FLT_MAX;
}
else
{
str = ui->farPlane->text();
if(str.isEmpty())
{
m_Params.farPlane = 0.0f;
}
else
{
m_Params.farPlane = str.toFloat(&ok);
if(!ok)
{
RDDialog::critical(
this, tr("Far plane error"),
tr("Unrecognised far plane %1, expected decimal number if specified.").arg(str));
return;
}
}
}
if(m_Params.nearPlane > 0 && m_Params.farPlane > 0 && m_Params.nearPlane >= m_Params.farPlane)
{
RDDialog::critical(this, tr("Invalid near & far plane"),
tr("Near plane %1 must be less than far plane %2.")
.arg(m_Params.nearPlane)
.arg(m_Params.farPlane));
return;
}
accept();
}
ProjectionGuessDialog::~ProjectionGuessDialog()
{
delete ui;
}
@@ -0,0 +1,66 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2019-2025 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#pragma once
#include <QDialog>
#include "Code/Interface/QRDInterface.h"
struct ProjectionGuessParameters
{
bool orthographic = false;
float fov = 90.0f;
float aspect = 0.0f;
float nearPlane = 0.0f;
float farPlane = 0.0f;
};
namespace Ui
{
class ProjectionGuessDialog;
}
struct ICaptureContext;
class ProjectionGuessDialog : public QDialog
{
Q_OBJECT
public:
explicit ProjectionGuessDialog(ICaptureContext &Ctx, const ProjectionGuessParameters &params,
QWidget *parent = 0);
const ProjectionGuessParameters &getParameters() { return m_Params; }
~ProjectionGuessDialog();
public slots:
void on_infiniteFar_toggled();
void on_matrixType_currentIndexChanged(int idx);
private slots:
void validateParameters();
private:
Ui::ProjectionGuessDialog *ui;
ICaptureContext &m_Ctx;
ProjectionGuessParameters m_Params;
};
@@ -0,0 +1,180 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ProjectionGuessDialog</class>
<widget class="QDialog" name="ProjectionGuessDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>296</width>
<height>356</height>
</rect>
</property>
<property name="windowTitle">
<string>Vertex Projection Configuration</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>10</number>
</property>
<property name="topMargin">
<number>10</number>
</property>
<property name="rightMargin">
<number>10</number>
</property>
<property name="bottomMargin">
<number>10</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Configure your application's projection matrix used for final vertex transform, which will be used to unproject rasterized vertices into world space.&lt;/p&gt;&lt;p&gt;Some parameters can be empty, which will use an estimate/guess based on the current mesh data.&lt;/p&gt;&lt;p&gt;For reverse-Z you can enable an infinite far plane.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QGridLayout" name="gridLayout_5">
<property name="bottomMargin">
<number>10</number>
</property>
<item row="2" column="1">
<widget class="QLineEdit" name="aspectRatio">
<property name="placeholderText">
<string>Auto</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="nearPlane">
<property name="placeholderText">
<string>Auto</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="fov">
<property name="decimals">
<number>1</number>
</property>
<property name="minimum">
<double>1.000000000000000</double>
</property>
<property name="maximum">
<double>179.000000000000000</double>
</property>
<property name="value">
<double>90.000000000000000</double>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Far Plane:</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Aspect Ratio:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="farPlane">
<property name="placeholderText">
<string>Auto</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Matrix type</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Near Plane:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>FOV:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="matrixType">
<item>
<property name="text">
<string>Perspective</string>
</property>
</item>
<item>
<property name="text">
<string>Orthographic</string>
</property>
</item>
</widget>
</item>
<item row="5" column="1">
<widget class="QCheckBox" name="infiniteFar">
<property name="text">
<string>Infinite</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
<property name="centerButtons">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
<tabstop>matrixType</tabstop>
<tabstop>fov</tabstop>
<tabstop>aspectRatio</tabstop>
<tabstop>nearPlane</tabstop>
<tabstop>farPlane</tabstop>
<tabstop>infiniteFar</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>