Skip to content

Commit

Permalink
Expose more light parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Jul 2, 2023
1 parent 899b6e6 commit e720005
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
5 changes: 5 additions & 0 deletions doc/source/doxygen-docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
\page changelog Change Log

# Version 2.10.0: UNRELEASED
- Changes in libraries:
- \ref mrpt_opengl_grp
- Move the parameter eyeDistance2lightShadowExtension from TRenderMatrices to mrpt::opengl::TLightParameters so it can be changed from user code (ABI change).

# Version 2.9.4: Released July 1st, 2023
- Python:
- pymrpt now ships stub `.pyi` files, for IDEs to autocomplete MRPT Python programs.
Expand Down
7 changes: 7 additions & 0 deletions libs/opengl/include/mrpt/opengl/TLightParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ struct TLightParameters
float shadow_bias = 1e-5, shadow_bias_cam2frag = 1e-5,
shadow_bias_normal = 1e-4;

/** Multiplier from eye distance to the length size of the squared area in
* which to evaluate shadow casting by unidirectional light.
* Unitless (meter/meter).
* \note (New in MRPT 2.10.0)
*/
double eyeDistance2lightShadowExtension = 2.0;

void writeToStream(mrpt::serialization::CArchive& out) const;
void readFromStream(mrpt::serialization::CArchive& in);

Expand Down
8 changes: 2 additions & 6 deletions libs/opengl/include/mrpt/opengl/TRenderMatrices.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <mrpt/img/TCamera.h>
#include <mrpt/math/CMatrixFixed.h>
#include <mrpt/math/TPoint3D.h>
#include <mrpt/opengl/opengl_frwds.h>

namespace mrpt::opengl
{
Expand Down Expand Up @@ -84,11 +85,6 @@ struct TRenderMatrices
double azimuth = .0, elev = .0;
double eyeDistance = 1.0f;

/** Multiplier from eye distance to the length size of the squared area in
* which to evaluate shadow casting by unidirectional light.
*/
double eyeDistance2lightShadowExtension = 1.5;

/** In pixels. This may be smaller than the total render window. */
uint32_t viewport_width = 640, viewport_height = 480;

Expand Down Expand Up @@ -121,7 +117,7 @@ struct TRenderMatrices

/** Updates light_pv */
void computeLightProjectionMatrix(
float zmin, float zmax, const mrpt::math::TVector3Df& direction);
float zmin, float zmax, const TLightParameters& lp);

/** Especial case for custom parameters of Orthographic projection.
* Equivalent to `p_matrix = ortho(...);`.
Expand Down
6 changes: 3 additions & 3 deletions libs/opengl/src/TLightParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace std;

void TLightParameters::writeToStream(mrpt::serialization::CArchive& out) const
{
const uint8_t version = 2;
const uint8_t version = 3;
out << version;

out << diffuse << ambient << specular << direction << color;
Expand All @@ -44,11 +44,11 @@ void TLightParameters::readFromStream(mrpt::serialization::CArchive& in)
break;
case 1:
case 2:
case 3:
in >> diffuse >> ambient >> specular >> direction >> color;
if (version >= 2)
{
in >> shadow_bias >> shadow_bias_cam2frag >> shadow_bias_normal;
}
if (version >= 3) in >> eyeDistance2lightShadowExtension;
break;
default: MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
};
Expand Down
9 changes: 5 additions & 4 deletions libs/opengl/src/TRenderMatrices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <mrpt/containers/yaml.h>
#include <mrpt/math/geometry.h> // crossProduct3D()
#include <mrpt/math/ops_containers.h> // dotProduct()
#include <mrpt/opengl/TLightParameters.h>
#include <mrpt/opengl/TRenderMatrices.h>

#include <Eigen/Dense>
Expand Down Expand Up @@ -151,26 +152,26 @@ static void azimuthElevationFromDirection(
}

void TRenderMatrices::computeLightProjectionMatrix(
float zmin, float zmax, const mrpt::math::TVector3Df& direction)
float zmin, float zmax, const TLightParameters& lp)
{
m_last_light_z_near = zmin;
m_last_light_z_far = zmax;

float dist = eyeDistance * eyeDistance2lightShadowExtension;
float dist = eyeDistance * lp.eyeDistance2lightShadowExtension;
light_p = OrthoProjectionMatrix(-dist, dist, -dist, dist, zmin, zmax);

// "up" vector from elevation:

float azim = 0, elevation = 0;
azimuthElevationFromDirection(direction, elevation, azim);
azimuthElevationFromDirection(lp.direction, elevation, azim);

const auto lightUp = mrpt::math::TVector3Df(
-cos(azim) * sin(elevation), // x
-sin(azim) * sin(elevation), // y
cos(elevation) // z
);

light_v = LookAt(pointing - direction * zmax * 0.5, pointing, lightUp);
light_v = LookAt(pointing - lp.direction * zmax * 0.5, pointing, lightUp);

light_pv.asEigen() = light_p.asEigen() * light_v.asEigen();

Expand Down
2 changes: 1 addition & 1 deletion libs/opengl/src/Viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ void Viewport::updateMatricesFromCamera(const CCamera& myCamera) const

// Compute the directional light projection matrix (light_pv)
_.computeLightProjectionMatrix(
m_lightShadowClipMin, m_lightShadowClipMax, m_light.direction);
m_lightShadowClipMin, m_lightShadowClipMax, m_light);

_.initialized = true;
}
Expand Down

0 comments on commit e720005

Please sign in to comment.