Gui: Add 3 point lighting

This commit is contained in:
Kacper Donat
2025-01-25 20:14:04 +01:00
parent 2aaf67029b
commit 7f984811e8
13 changed files with 856 additions and 999 deletions

View File

@@ -24,12 +24,11 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <Inventor/draggers/SoDirectionalLightDragger.h>
#include <Inventor/events/SoEvent.h>
#include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/nodes/SoEventCallback.h>
#include <Inventor/nodes/SoOrthographicCamera.h>
#include <Inventor/nodes/SoPickStyle.h>
#include <Inventor/nodes/SoComplexity.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoSphere.h>
@@ -37,7 +36,10 @@
#include "DlgSettingsLightSources.h"
#include "ui_DlgSettingsLightSources.h"
#include <App/Application.h>
#include <Utilities.h>
#include <Base/Builder3D.h>
#include <Base/Tools.h>
#include <Gui/View3DInventorViewer.h>
#include <Gui/View3DSettings.h>
@@ -46,273 +48,305 @@ using namespace Gui::Dialog;
/* TRANSLATOR Gui::Dialog::DlgSettingsLightSources */
DlgSettingsLightSources::DlgSettingsLightSources(QWidget* parent)
: PreferencePage(parent)
, ui(new Ui_DlgSettingsLightSources)
{
ui->setupUi(this);
view = ui->viewer;
createViewer();
}
static inline
SbVec3f getDirectionVector(const SbRotation &rotation)
static inline SbVec3f getDirectionVector(const SbRotation& rotation)
{
SbVec3f dir {0.0f, 0.0f, -1.0f};
rotation.multVec(dir, dir);
return dir;
}
static inline
void setLightDirection(const SbRotation &rotation, Gui::View3DInventorViewer *viewer)
DlgSettingsLightSources::DlgSettingsLightSources(QWidget* parent)
: PreferencePage(parent)
, ui(new Ui_DlgSettingsLightSources)
{
viewer->getHeadlight()->direction = getDirectionVector(rotation);
ui->setupUi(this);
view = ui->viewer;
configureViewer();
const auto connectLightEvents = [&](QuantitySpinBox* horizontalAngleSpinBox,
QuantitySpinBox* verticalAngleSpinBox,
QSpinBox* intensitySpinBox,
ColorButton* colorButton,
QCheckBox* enabledCheckbox,
auto updateLightFunction) {
connect(horizontalAngleSpinBox,
qOverload<double>(&QuantitySpinBox::valueChanged),
this,
updateLightFunction);
connect(verticalAngleSpinBox,
qOverload<double>(&QuantitySpinBox::valueChanged),
this,
updateLightFunction);
connect(intensitySpinBox,
qOverload<int>(&QSpinBox::valueChanged),
this,
updateLightFunction);
connect(colorButton, &ColorButton::changed, this, updateLightFunction);
connect(enabledCheckbox, &QCheckBox::stateChanged, this, updateLightFunction);
};
const auto updateLight = [&](SoDirectionalLight* light,
QuantitySpinBox* horizontalAngleSpinBox,
QuantitySpinBox* verticalAngleSpinBox,
QSpinBox* intensitySpinBox,
ColorButton* colorButton,
QCheckBox* enabledCheckbox,
std::function<void(bool)> setLightEnabled) {
light->color = Base::convertTo<SbColor>(colorButton->color());
light->intensity = intensitySpinBox->value() / 100.F;
light->direction =
Base::convertTo<SbVec3f>(azimuthElevationToDirection(horizontalAngleSpinBox->rawValue(),
verticalAngleSpinBox->rawValue()));
setLightEnabled(enabledCheckbox->isChecked());
};
const auto updateHeadLight = [&] {
updateLight(view->getHeadlight(),
ui->mainLightHorizontalAngle,
ui->mainLightVerticalAngle,
ui->mainLightIntensitySpinBox,
ui->mainLightColor,
ui->mainLightEnable,
[&](bool enabled) {
view->setHeadlightEnabled(enabled);
});
};
const auto updateFillLight = [&] {
updateLight(view->getFillLight(),
ui->fillLightHorizontalAngle,
ui->fillLightVerticalAngle,
ui->fillLightIntensitySpinBox,
ui->fillLightColor,
ui->fillLightEnable,
[&](bool enabled) {
view->setFillLightEnabled(enabled);
});
};
const auto updateBackLight = [&] {
updateLight(view->getBacklight(),
ui->backLightHorizontalAngle,
ui->backLightVerticalAngle,
ui->backLightIntensitySpinBox,
ui->backLightColor,
ui->backLightEnable,
[&](bool enabled) {
view->setBacklightEnabled(enabled);
});
};
connectLightEvents(ui->mainLightHorizontalAngle,
ui->mainLightVerticalAngle,
ui->mainLightIntensitySpinBox,
ui->mainLightColor,
ui->mainLightEnable,
updateHeadLight);
connectLightEvents(ui->backLightHorizontalAngle,
ui->backLightVerticalAngle,
ui->backLightIntensitySpinBox,
ui->backLightColor,
ui->backLightEnable,
updateBackLight);
connectLightEvents(ui->fillLightHorizontalAngle,
ui->fillLightVerticalAngle,
ui->fillLightIntensitySpinBox,
ui->fillLightColor,
ui->fillLightEnable,
updateFillLight);
const auto updateAmbientLight = [&] {
view->getEnvironment()->ambientColor =
Base::convertTo<SbColor>(ui->ambientLightColor->color());
view->getEnvironment()->ambientIntensity =
ui->ambientLightIntensitySpinBox->value() / 100.F;
};
connect(ui->ambientLightIntensitySpinBox,
qOverload<int>(&QSpinBox::valueChanged),
this,
updateAmbientLight);
connect(ui->ambientLightColor, &ColorButton::changed, this, updateAmbientLight);
connect(ui->zoomInButton, &QToolButton::clicked, this, &DlgSettingsLightSources::zoomIn);
connect(ui->zoomOutButton, &QToolButton::clicked, this, &DlgSettingsLightSources::zoomOut);
DlgSettingsLightSources::loadSettings();
}
static inline
void setLightDraggerDirection(const SbRotation &rotation, SoDirectionalLightDragger *light_dragger)
static inline SoMaterial* createMaterial(void)
{
light_dragger->rotation = rotation;
}
const QColor ambientColor {0xff333333}, diffuseColor {0xffd2d2ff}, emissiveColor {0xff000000},
specularColor {0xffcccccc};
static inline
void setValueSilently(QDoubleSpinBox *spn, const float val)
{
Q_ASSERT_X(spn, "setValueSilently", "QDoubleSpinBox has been deleted");
auto material = new SoMaterial();
spn->blockSignals(true);
spn->setValue(val);
spn->blockSignals(false);
}
void DlgSettingsLightSources::dragMotionCallback(void *data, SoDragger *drag)
{
auto lightdrag = dynamic_cast <SoDirectionalLightDragger *> (drag);
auto self = static_cast<DlgSettingsLightSources*>(data);
const SbRotation rotation = lightdrag->rotation.getValue();
setLightDirection(rotation, self->view);
setValueSilently(self->ui->q0_spnBox, rotation[0]);
setValueSilently(self->ui->q1_spnBox, rotation[1]);
setValueSilently(self->ui->q2_spnBox, rotation[2]);
setValueSilently(self->ui->q3_spnBox, rotation[3]);
const SbVec3f dir = getDirectionVector(rotation);
setValueSilently(self->ui->x_spnBox, dir[0]);
setValueSilently(self->ui->y_spnBox, dir[1]);
setValueSilently(self->ui->z_spnBox, dir[2]);
}
static inline
SoMaterial *createMaterial(void)
{
const QColor ambientColor {0xff333333},
diffuseColor {0xffd2d2ff},
emissiveColor {0xff000000},
specularColor {0xffcccccc};
auto material = new SoMaterial ();
material->ambientColor.setValue (ambientColor.redF(), ambientColor.greenF(), ambientColor.blueF());
material->diffuseColor.setValue (diffuseColor.redF(), diffuseColor.greenF(), diffuseColor.blueF());
material->emissiveColor.setValue(emissiveColor.redF(), emissiveColor.greenF(), emissiveColor.blueF());
material->specularColor.setValue(specularColor.redF(), specularColor.greenF(), specularColor.blueF());
material->ambientColor.setValue(Base::convertTo<SbColor>(ambientColor));
material->diffuseColor.setValue(Base::convertTo<SbColor>(diffuseColor));
material->emissiveColor.setValue(Base::convertTo<SbColor>(emissiveColor));
material->specularColor.setValue(Base::convertTo<SbColor>(specularColor));
material->shininess = 0.9f;
return material;
}
static inline
SoSphere *createSphere(void)
static inline SoSphere* createSphere(void)
{
auto sphere = new SoSphere();
sphere->radius = 2;
sphere->radius = 3;
return sphere;
}
void DlgSettingsLightSources::createViewer()
static inline SoComplexity* createGoodComplexity()
{
const QColor default_bg_color {180, 180, 180};
const SbVec3f default_view_direction {1.0f, 1.0f, -5.0f};
auto complexity = new SoComplexity();
complexity->value = 1.0;
return complexity;
}
void DlgSettingsLightSources::configureViewer()
{
const SbVec3f defaultViewDirection {0.0f, 1.0f, 0.3f};
View3DSettings(hGrp, view).applySettings();
// NOLINTBEGIN
view->setRedirectToSceneGraph(true);
view->setViewing(true);
view->setPopupMenuEnabled(false);
view->setBackgroundColor(default_bg_color);
view->setGradientBackground(Gui::View3DInventorViewer::NoGradient);
view->setEnabledNaviCube(false);
auto root = static_cast<SoSeparator*>(view->getSceneGraph());
root->addChild(createDragger());
const auto root = static_cast<SoSeparator*>(view->getSceneGraph());
root->addChild(createGoodComplexity());
root->addChild(createMaterial());
root->addChild(createSphere());
auto callback = new SoEventCallback();
const auto callback = new SoEventCallback();
root->addChild(callback);
callback->addEventCallback(SoEvent::getClassTypeId(),
[] (void* ud, SoEventCallback* cb) {
Q_UNUSED(ud)
cb->setHandled();
});
[]([[maybe_unused]] void* ud, SoEventCallback* cb) {
cb->setHandled();
});
view->setCameraType(SoOrthographicCamera::getClassTypeId());
view->setViewDirection(default_view_direction);
view->setViewDirection(defaultViewDirection);
view->viewAll();
camera = dynamic_cast <SoOrthographicCamera *> (view->getCamera());
const float camera_height = camera->height.getValue() * 2.0f;
camera->height = camera_height;
cam_step = camera_height / 14.0f;
// NOLINTEND
camera = dynamic_cast<SoOrthographicCamera*>(view->getCamera());
const float cameraHeight = camera->height.getValue() * 2.0f;
camera->height = cameraHeight;
zoomStep = cameraHeight / 14.0f;
}
SoDirectionalLightDragger* DlgSettingsLightSources::createDragger()
Base::Vector3d DlgSettingsLightSources::azimuthElevationToDirection(double azimuth,
double elevation)
{
// NOLINTBEGIN
lightDragger = new SoDirectionalLightDragger();
if (SoDragger* translator = dynamic_cast<SoDragger *>(lightDragger->getPart("translator", false))) {
translator->setPartAsDefault("xTranslator.translatorActive", nullptr);
translator->setPartAsDefault("yTranslator.translatorActive", nullptr);
translator->setPartAsDefault("zTranslator.translatorActive", nullptr);
translator->setPartAsDefault("xTranslator.translator", nullptr);
translator->setPartAsDefault("yTranslator.translator", nullptr);
translator->setPartAsDefault("zTranslator.translator", nullptr);
SoNode* node = translator->getPart("yzTranslator.translator", false);
if (node && node->isOfType(SoGroup::getClassTypeId())) {
auto ps = new SoPickStyle();
ps->style = SoPickStyle::UNPICKABLE;
static_cast<SoGroup*>(node)->insertChild(ps, 0);
}
}
azimuth = Base::toRadians(azimuth);
elevation = Base::toRadians(elevation);
lightDragger->addMotionCallback(dragMotionCallback, this);
return lightDragger;
// NOLINTEND
auto direction = Base::Vector3d {std::sin(azimuth) * std::cos(elevation),
std::cos(azimuth) * std::cos(elevation),
std::sin(elevation)};
direction.Normalize();
return direction;
}
std::pair<double, double>
DlgSettingsLightSources::directionToAzimuthElevation(Base::Vector3d direction)
{
const auto azimuth = std::atan2(direction[0], direction[1]);
const auto elevation =
std::atan2(direction[2],
std::sqrt(direction[1] * direction[1] + direction[0] * direction[0]));
return {Base::toDegrees(azimuth), Base::toDegrees(elevation)};
}
void DlgSettingsLightSources::saveSettings()
{
ui->checkBoxLight1->onSave();
ui->light1Color->onSave();
ui->sliderIntensity1->onSave();
saveDirection();
for (const auto& widget : findChildren<QWidget*>()) {
if (const auto pref = dynamic_cast<PrefWidget*>(widget)) {
pref->onSave();
}
}
const auto saveAngles = [&](QuantitySpinBox* horizontalAngleSpinBox,
QuantitySpinBox* verticalAngleSpinBox,
const char* parameter) {
try {
const auto direction = azimuthElevationToDirection(horizontalAngleSpinBox->rawValue(),
verticalAngleSpinBox->rawValue());
hGrp->SetASCII(parameter,
Base::vectorToString(Base::convertTo<Base::Vector3f>(direction)));
}
catch (...) {
}
};
saveAngles(ui->mainLightHorizontalAngle, ui->mainLightVerticalAngle, "HeadlightDirection");
saveAngles(ui->backLightHorizontalAngle, ui->backLightVerticalAngle, "BacklightDirection");
saveAngles(ui->fillLightHorizontalAngle, ui->fillLightVerticalAngle, "FillLightDirection");
}
void DlgSettingsLightSources::loadSettings()
{
ui->checkBoxLight1->onRestore();
ui->light1Color->onRestore();
ui->sliderIntensity1->onRestore();
loadDirection();
lightColor();
for (const auto& widget : findChildren<QWidget*>()) {
if (const auto pref = dynamic_cast<PrefWidget*>(widget)) {
pref->onRestore();
}
}
const auto loadAngles = [&](QuantitySpinBox* horizontalAngleSpinBox,
QuantitySpinBox* verticalAngleSpinBox,
const char* parameter) {
try {
const auto direction = Base::stringToVector(hGrp->GetASCII(parameter));
const auto [azimuth, elevation] =
directionToAzimuthElevation(Base::convertTo<Base::Vector3d>(direction));
horizontalAngleSpinBox->setValue(azimuth);
verticalAngleSpinBox->setValue(elevation);
}
catch (...) {
}
};
loadAngles(ui->mainLightHorizontalAngle, ui->mainLightVerticalAngle, "HeadlightDirection");
loadAngles(ui->backLightHorizontalAngle, ui->backLightVerticalAngle, "BacklightDirection");
loadAngles(ui->fillLightHorizontalAngle, ui->fillLightVerticalAngle, "FillLightDirection");
}
void DlgSettingsLightSources::resetSettingsToDefaults()
{
ParameterGrp::handle grp = ui->sliderIntensity1->getWindowParameter();
grp->SetFloat("HeadlightRotationX", 0.0);
grp->SetFloat("HeadlightRotationY", 0.0);
grp->SetFloat("HeadlightRotationZ", 0.0);
grp->SetFloat("HeadlightRotationW", 1.0);
grp->SetASCII("HeadlightDirection", "(0.0,0.0,-1.0)");
PreferencePage::resetSettingsToDefaults();
loadSettings();
configureViewer();
}
void DlgSettingsLightSources::saveDirection()
void DlgSettingsLightSources::zoomIn() const
{
if (lightDragger) {
const SbRotation rotation = lightDragger->rotation.getValue();
const SbVec3f dir = getDirectionVector(rotation);
const QString headlightDir = QStringLiteral("(%1,%2,%3)").arg(dir[0]).arg(dir[1]).arg(dir[2]);
ParameterGrp::handle grp = ui->sliderIntensity1->getWindowParameter();
grp->SetFloat("HeadlightRotationX", rotation[0]);
grp->SetFloat("HeadlightRotationY", rotation[1]);
grp->SetFloat("HeadlightRotationZ", rotation[2]);
grp->SetFloat("HeadlightRotationW", rotation[3]);
grp->SetASCII("HeadlightDirection", qPrintable(headlightDir));
}
}
void DlgSettingsLightSources::loadDirection()
{
ParameterGrp::handle grp = ui->sliderIntensity1->getWindowParameter();
SbRotation rotation = lightDragger->rotation.getValue();
auto get_q = [&grp](const char *name, const float def){return static_cast <float> (grp->GetFloat(name, def));};
const float q0 = get_q("HeadlightRotationX", rotation[0]),
q1 = get_q("HeadlightRotationY", rotation[1]),
q2 = get_q("HeadlightRotationZ", rotation[2]),
q3 = get_q("HeadlightRotationW", rotation[3]);
rotation.setValue(q0, q1, q2, q3);
setLightDirection(rotation, ui->viewer);
setLightDraggerDirection(rotation, lightDragger);
setValueSilently(ui->q0_spnBox, rotation[0]);
setValueSilently(ui->q1_spnBox, rotation[1]);
setValueSilently(ui->q2_spnBox, rotation[2]);
setValueSilently(ui->q3_spnBox, rotation[3]);
const SbVec3f dir = getDirectionVector(rotation);
setValueSilently(ui->x_spnBox, dir[0]);
setValueSilently(ui->y_spnBox, dir[1]);
setValueSilently(ui->z_spnBox, dir[2]);
}
void DlgSettingsLightSources::toggleLight(bool on)
{
if (view) {
view->setHeadlightEnabled(on);
}
}
void DlgSettingsLightSources::lightIntensity(int value)
{
if (view) {
view->getHeadlight()->intensity = static_cast <float> (value) / 100.0f;
}
}
void DlgSettingsLightSources::lightColor()
{
if (view) {
const QColor color = ui->light1Color->color();
view->getHeadlight()->color.setValue(color.redF(),
color.greenF(),
color.blueF());
}
}
void DlgSettingsLightSources::pushIn(void)
{
if (camera == nullptr)
if (camera == nullptr) {
return;
}
camera->height = camera->height.getValue() - cam_step;
camera->height = camera->height.getValue() - zoomStep;
}
void DlgSettingsLightSources::pullOut(void)
void DlgSettingsLightSources::zoomOut() const
{
if (camera == nullptr)
if (camera == nullptr) {
return;
}
camera->height = camera->height.getValue() + cam_step;
camera->height = camera->height.getValue() + zoomStep;
}
void DlgSettingsLightSources::changeEvent(QEvent* event)
@@ -323,41 +357,4 @@ void DlgSettingsLightSources::changeEvent(QEvent* event)
PreferencePage::changeEvent(event);
}
void DlgSettingsLightSources::updateDraggerQS()
{
const float q0 = ui->q0_spnBox->value(),
q1 = ui->q1_spnBox->value(),
q2 = ui->q2_spnBox->value(),
q3 = ui->q3_spnBox->value();
const SbRotation rotation {q0, q1, q2, q3};
setLightDirection(rotation, view);
setLightDraggerDirection(rotation, lightDragger);
const SbVec3f dir = getDirectionVector(rotation);
setValueSilently(ui->x_spnBox, dir[0]);
setValueSilently(ui->y_spnBox, dir[1]);
setValueSilently(ui->z_spnBox, dir[2]);
}
void DlgSettingsLightSources::updateDraggerXYZ()
{
const float x = ui->x_spnBox->value(),
y = ui->y_spnBox->value(),
z = ui->z_spnBox->value();
const SbRotation rotation {SbVec3f{0.0f, 0.0f, -1.0f}, SbVec3f{x, y, z}};
setLightDirection(rotation, view);
setLightDraggerDirection(rotation, lightDragger);
setValueSilently(ui->q0_spnBox, rotation[0]);
setValueSilently(ui->q1_spnBox, rotation[1]);
setValueSilently(ui->q2_spnBox, rotation[2]);
setValueSilently(ui->q3_spnBox, rotation[3]);
}
#include "moc_DlgSettingsLightSources.cpp"