Skip to content

Commit

Permalink
RENDERING: z-far value stored for reuse in projection to mitigate pre…
Browse files Browse the repository at this point in the history
…cision error when reconstructing far plane from matrix (fixed typo)
  • Loading branch information
roalyr committed Jan 20, 2024
1 parent 1f3a419 commit 27fe14e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ This fork is regularly synchronized with [4.x branch](https://github.com/godoten
Available for Windows x86-64, Linux 32-bit and x86-64, Linux armv8 (64 bit), Android armv7 and armv8.

Implemented tweaks:
* Far plane (z-far) upper limit is set to 9e18 meters.
* [Large World Coordinates](https://docs.godotengine.org/en/stable/tutorials/physics/large_world_coordinates.html) are used when compiling (double precision floats).
* Increased editor zoom out distance to galactic scale (depth buffer must be adjusted for such scales, see below).
* Increased editor zoom increment for faster zooming.
* This 4.x fork build DOES NOT implement hard-coded logarithmic depth buffer. It is implemented in 3.x version of GF3DOW.
* This 4.x fork build DOES NOT implement hard-coded logarithmic depth buffer.
The reason to not to implement this solution is to make it possible to incorporate possible future options for
either logarithmic or reverse linear depth buffer, which are being discussed by dev team and are on the milestone.

Expand Down
47 changes: 19 additions & 28 deletions core/math/projection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#include "core/math/transform_3d.h"
#include "core/string/ustring.h"

// Store for future use in the methods.
real_t PZFarStorage::p_z_far_stored = 1.0;

float Projection::determinant() const {
return columns[0][3] * columns[1][2] * columns[2][1] * columns[3][0] - columns[0][2] * columns[1][3] * columns[2][1] * columns[3][0] -
columns[0][3] * columns[1][1] * columns[2][2] * columns[3][0] + columns[0][1] * columns[1][3] * columns[2][2] * columns[3][0] +
Expand Down Expand Up @@ -183,13 +186,7 @@ Plane Projection::get_projection_plane(Planes p_plane) const {
return new_plane;
}
case PLANE_FAR: {
Plane new_plane = Plane(matrix[3] - matrix[2],
matrix[7] - matrix[6],
matrix[11] - matrix[10],
matrix[15] - matrix[14]);

new_plane.normal = -new_plane.normal;
new_plane.normalize();
Plane new_plane = Plane(0.0, 0.0, -1.0, PZFarStorage::p_z_far_stored);
return new_plane;
}
case PLANE_LEFT: {
Expand Down Expand Up @@ -266,6 +263,9 @@ void Projection::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t
cotangent = Math::cos(radians) / sine;

set_identity();

// Store the value in the struct for it to be used in Plane definition.
PZFarStorage::p_z_far_stored = p_z_far;

columns[0][0] = cotangent / p_aspect;
columns[1][1] = cotangent;
Expand Down Expand Up @@ -305,6 +305,9 @@ void Projection::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t
}

set_frustum(left, right, -ymax, ymax, p_z_near, p_z_far);

// Store the value in the struct for it to be used in Plane definition.
PZFarStorage::p_z_far_stored = p_z_far;

// translate matrix by (modeltranslation, 0.0, 0.0)
Projection cm;
Expand All @@ -328,6 +331,9 @@ void Projection::set_for_hmd(int p_eye, real_t p_aspect, real_t p_intraocular_di

// always apply KEEP_WIDTH aspect ratio
f3 /= p_aspect;

// Store the value in the struct for it to be used in Plane definition.
PZFarStorage::p_z_far_stored = p_z_far;

switch (p_eye) {
case 1: { // left eye
Expand All @@ -343,6 +349,9 @@ void Projection::set_for_hmd(int p_eye, real_t p_aspect, real_t p_intraocular_di

void Projection::set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar) {
set_identity();

// Store the value in the struct for it to be used in Plane definition.
PZFarStorage::p_z_far_stored = p_zfar;

columns[0][0] = 2.0 / (p_right - p_left);
columns[3][0] = -((p_right + p_left) / (p_right - p_left));
Expand Down Expand Up @@ -402,15 +411,7 @@ void Projection::set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, r
}

real_t Projection::get_z_far() const {
const real_t *matrix = (const real_t *)columns;
Plane new_plane = Plane(matrix[3] - matrix[2],
matrix[7] - matrix[6],
matrix[11] - matrix[10],
matrix[15] - matrix[14]);

new_plane.normalize();

return new_plane.d;
return PZFarStorage::p_z_far_stored;
}

real_t Projection::get_z_near() const {
Expand Down Expand Up @@ -455,11 +456,7 @@ Vector2 Projection::get_viewport_half_extents() const {
Vector2 Projection::get_far_plane_half_extents() const {
const real_t *matrix = (const real_t *)columns;
///////--- Far Plane ---///////
Plane far_plane = Plane(matrix[3] - matrix[2],
matrix[7] - matrix[6],
matrix[11] - matrix[10],
-matrix[15] + matrix[14]);
far_plane.normalize();
Plane far_plane = Plane(0.0, 0.0, -1.0, PZFarStorage::p_z_far_stored);

///////--- Right Plane ---///////
Plane right_plane = Plane(matrix[3] - matrix[0],
Expand Down Expand Up @@ -532,13 +529,7 @@ Vector<Plane> Projection::get_projection_planes(const Transform3D &p_transform)
planes.write[0] = p_transform.xform(new_plane);

///////--- Far Plane ---///////
new_plane = Plane(matrix[3] - matrix[2],
matrix[7] - matrix[6],
matrix[11] - matrix[10],
matrix[15] - matrix[14]);

new_plane.normal = -new_plane.normal;
new_plane.normalize();
new_plane = Plane(0.0, 0.0, -1.0, PZFarStorage::p_z_far_stored);

planes.write[1] = p_transform.xform(new_plane);

Expand Down
6 changes: 6 additions & 0 deletions core/math/projection.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ struct Rect2;
struct Transform3D;
struct Vector2;

// Store for future use in the methods of Projection.
class PZFarStorage {
public:
static real_t p_z_far_stored;
};

struct _NO_DISCARD_ Projection {
enum Planes {
PLANE_NEAR,
Expand Down
6 changes: 3 additions & 3 deletions rebuild_linux_32_buildroot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ scons -j2 platform=linuxbsd precision=double arch=x86_32 2>&1 | tee ./logs/godot

# Template(s)
scons -j2 platform=linuxbsd precision=double target=template_debug arch=x86_32 2>&1 | tee ./logs/godot_4_scons_linuxbsd_86_32_template_build.txt;
scons -j2 platform=linuxbsd precision=double target=template_release arch=x86_32 2>&1 | tee ./logs/godot_4_scons_linuxbsd_86_32_template_release_build.txt;
#scons -j2 platform=linuxbsd precision=double target=template_release arch=x86_32 2>&1 | tee ./logs/godot_4_scons_linuxbsd_86_32_template_release_build.txt;

# Removing debug symbols
strip ./bin/godot.linuxbsd.editor.double.x86_32
#strip ./bin/godot.linuxbsd.editor.double.x86_32
#strip ./bin/godot.linuxbsd.template_debug.double.x86_32
strip ./bin/godot.linuxbsd.template_release.double.x86_32
#strip ./bin/godot.linuxbsd.template_release.double.x86_32

echo
echo " ==============================================="
Expand Down
6 changes: 3 additions & 3 deletions rebuild_linux_64_buildroot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ scons -j2 platform=linuxbsd precision=double 2>&1 | tee ./logs/godot_4_scons_lin

# Template(s)
scons -j2 platform=linuxbsd precision=double target=template_debug arch=x86_64 2>&1 | tee ./logs/godot_4_scons_linuxbsd_86_64_template_build.txt;
scons -j2 platform=linuxbsd precision=double target=template_release arch=x86_64 2>&1 | tee ./logs/godot_4_scons_linuxbsd_86_64_template_release_build.txt;
#scons -j2 platform=linuxbsd precision=double target=template_release arch=x86_64 2>&1 | tee ./logs/godot_4_scons_linuxbsd_86_64_template_release_build.txt;


# Removing debug symbols
strip ./bin/godot.linuxbsd.editor.double.x86_64
#strip ./bin/godot.linuxbsd.editor.double.x86_64
#strip ./bin/godot.linuxbsd.template_debug.double.x86_64
strip ./bin/godot.linuxbsd.template_release.double.x86_64
#strip ./bin/godot.linuxbsd.template_release.double.x86_64

echo
echo " ==============================================="
Expand Down
2 changes: 1 addition & 1 deletion rebuild_windows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ scons -j2 platform=windows precision=double 2>&1 | tee ./logs/godot_4_scons_wind

# Template(s)
scons -j2 platform=windows precision=double target=template_debug arch=x86_64 2>&1 | tee ./logs/godot_4_scons_windows_64_template_build.txt;
scons -j2 platform=windows precision=double target=template_release arch=x86_64 2>&1 | tee ./logs/godot_4_scons_windows_64_template_release_build.txt;
# scons -j2 platform=windows precision=double target=template_release arch=x86_64 2>&1 | tee ./logs/godot_4_scons_windows_64_template_release_build.txt;

# Removing debug symbols
#strip ./bin/godot.windows.opt.tools.64.exe
Expand Down

0 comments on commit 27fe14e

Please sign in to comment.