Skip to content

Commit

Permalink
Merge pull request #88270 from TokageItLab/avoid-near-far
Browse files Browse the repository at this point in the history
Avoid unqualified-id "near" and "far" in Node3DEditor/Viewport
  • Loading branch information
akien-mga committed Feb 13, 2024
2 parents fbe8a1d + 67e3870 commit dfe226b
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 75 deletions.
19 changes: 7 additions & 12 deletions core/math/aabb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ AABB AABB::intersection(const AABB &p_aabb) const {
return AABB(min, max - min);
}

#ifdef MINGW_ENABLED
#undef near
#undef far
#endif

bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
Expand All @@ -130,8 +125,8 @@ bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *
#endif
Vector3 c1, c2;
Vector3 end = position + size;
real_t near = -1e20;
real_t far = 1e20;
real_t depth_near = -1e20;
real_t depth_far = 1e20;
int axis = 0;

for (int i = 0; i < 3; i++) {
Expand All @@ -146,14 +141,14 @@ bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *
if (c1[i] > c2[i]) {
SWAP(c1, c2);
}
if (c1[i] > near) {
near = c1[i];
if (c1[i] > depth_near) {
depth_near = c1[i];
axis = i;
}
if (c2[i] < far) {
far = c2[i];
if (c2[i] < depth_far) {
depth_far = c2[i];
}
if ((near > far) || (far < 0)) {
if ((depth_near > depth_far) || (depth_far < 0)) {
return false;
}
}
Expand Down
12 changes: 6 additions & 6 deletions editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,13 +943,13 @@ void Node3DEditorViewport::_select_region() {
}
}

Plane near(-_get_camera_normal(), cam_pos);
near.d -= get_znear();
frustum.push_back(near);
Plane near_plane = Plane(-_get_camera_normal(), cam_pos);
near_plane.d -= get_znear();
frustum.push_back(near_plane);

Plane far = -near;
far.d += get_zfar();
frustum.push_back(far);
Plane far_plane = -near_plane;
far_plane.d += get_zfar();
frustum.push_back(far_plane);

if (spatial_editor->get_single_selected_node()) {
Node3D *single_selected = spatial_editor->get_single_selected_node();
Expand Down
16 changes: 8 additions & 8 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Projection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static Projection CreateForHmd(int eye, real_t aspect, real_t intraocular
/// <param name="near">The near clipping distance.</param>
/// <param name="far">The far clipping distance.</param>
/// <returns>The created projection.</returns>
public static Projection CreateFrustum(real_t left, real_t right, real_t bottom, real_t top, real_t near, real_t far)
public static Projection CreateFrustum(real_t left, real_t right, real_t bottom, real_t top, real_t depth_near, real_t depth_far)
{
if (right <= left)
{
Expand All @@ -264,18 +264,18 @@ public static Projection CreateFrustum(real_t left, real_t right, real_t bottom,
{
throw new ArgumentException("top is less or equal to bottom.");
}
if (far <= near)
if (depth_far <= depth_near)
{
throw new ArgumentException("far is less or equal to near.");
}

real_t x = 2 * near / (right - left);
real_t y = 2 * near / (top - bottom);
real_t x = 2 * depth_near / (right - left);
real_t y = 2 * depth_near / (top - bottom);

real_t a = (right + left) / (right - left);
real_t b = (top + bottom) / (top - bottom);
real_t c = -(far + near) / (far - near);
real_t d = -2 * far * near / (far - near);
real_t c = -(depth_far + depth_near) / (depth_far - depth_near);
real_t d = -2 * depth_far * depth_near / (depth_far - depth_near);

return new Projection(
new Vector4(x, 0, 0, 0),
Expand All @@ -297,13 +297,13 @@ public static Projection CreateFrustum(real_t left, real_t right, real_t bottom,
/// <param name="far">The far clipping distance.</param>
/// <param name="flipFov">If the field of view is flipped over the projection's diagonal.</param>
/// <returns>The created projection.</returns>
public static Projection CreateFrustumAspect(real_t size, real_t aspect, Vector2 offset, real_t near, real_t far, bool flipFov)
public static Projection CreateFrustumAspect(real_t size, real_t aspect, Vector2 offset, real_t depth_near, real_t depth_far, bool flipFov)
{
if (!flipFov)
{
size *= aspect;
}
return CreateFrustum(-size / 2 + offset.X, +size / 2 + offset.X, -size / aspect / 2 + offset.Y, +size / aspect / 2 + offset.Y, near, far);
return CreateFrustum(-size / 2 + offset.X, +size / 2 + offset.X, -size / aspect / 2 + offset.Y, +size / aspect / 2 + offset.Y, depth_near, depth_far);
}

/// <summary>
Expand Down
13 changes: 4 additions & 9 deletions scene/debugger/scene_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ void SceneDebugger::deinitialize() {
}
}

#ifdef MINGW_ENABLED
#undef near
#undef far
#endif

#ifdef DEBUG_ENABLED
Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
SceneTree *scene_tree = SceneTree::get_singleton();
Expand Down Expand Up @@ -124,12 +119,12 @@ Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Arra
Transform3D transform = p_args[0];
bool is_perspective = p_args[1];
float size_or_fov = p_args[2];
float near = p_args[3];
float far = p_args[4];
float depth_near = p_args[3];
float depth_far = p_args[4];
if (is_perspective) {
scene_tree->get_root()->set_camera_3d_override_perspective(size_or_fov, near, far);
scene_tree->get_root()->set_camera_3d_override_perspective(size_or_fov, depth_near, depth_far);
} else {
scene_tree->get_root()->set_camera_3d_override_orthogonal(size_or_fov, near, far);
scene_tree->get_root()->set_camera_3d_override_orthogonal(size_or_fov, depth_near, depth_far);
}
scene_tree->get_root()->set_camera_3d_override_transform(transform);
#endif // _3D_DISABLED
Expand Down
4 changes: 2 additions & 2 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,13 +896,13 @@ void Viewport::_process_picking() {
if (camera_3d) {
Vector3 from = camera_3d->project_ray_origin(pos);
Vector3 dir = camera_3d->project_ray_normal(pos);
real_t far = camera_3d->get_far();
real_t depth_far = camera_3d->get_far();

PhysicsDirectSpaceState3D *space = PhysicsServer3D::get_singleton()->space_get_direct_state(find_world_3d()->get_space());
if (space) {
PhysicsDirectSpaceState3D::RayParameters ray_params;
ray_params.from = from;
ray_params.to = from + dir * far;
ray_params.to = from + dir * depth_far;
ray_params.collide_with_areas = true;
ray_params.pick_ray = true;

Expand Down
13 changes: 4 additions & 9 deletions scene/resources/camera_attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,6 @@ real_t CameraAttributesPhysical::get_fov() const {
return frustum_fov;
}

#ifdef MINGW_ENABLED
#undef near
#undef far
#endif

void CameraAttributesPhysical::_update_frustum() {
//https://en.wikipedia.org/wiki/Circle_of_confusion#Circle_of_confusion_diameter_limit_based_on_d/1500
Vector2i sensor_size = Vector2i(36, 24); // Matches high-end DSLR, could be made variable if there is demand.
Expand All @@ -393,12 +388,12 @@ void CameraAttributesPhysical::_update_frustum() {
// that it is not picked up by the camera sensors.
// To be properly physically-based, we would run the DoF shader at all depths. To be efficient, we are only running it where the CoC
// will be visible, this introduces some value shifts in the near field that we have to compensate for below.
float near = ((hyperfocal_length * u) / (hyperfocal_length + (u - frustum_focal_length))) / 1000.0; // In meters.
float far = ((hyperfocal_length * u) / (hyperfocal_length - (u - frustum_focal_length))) / 1000.0; // In meters.
float depth_near = ((hyperfocal_length * u) / (hyperfocal_length + (u - frustum_focal_length))) / 1000.0; // In meters.
float depth_far = ((hyperfocal_length * u) / (hyperfocal_length - (u - frustum_focal_length))) / 1000.0; // In meters.
float scale = (frustum_focal_length / (u - frustum_focal_length)) * (frustum_focal_length / exposure_aperture);

bool use_far = (far < frustum_far) && (far > 0.0);
bool use_near = near > frustum_near;
bool use_far = (depth_far < frustum_far) && (depth_far > 0.0);
bool use_near = depth_near > frustum_near;
#ifdef DEBUG_ENABLED
if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
// Force disable DoF in editor builds to suppress warnings.
Expand Down
41 changes: 18 additions & 23 deletions servers/rendering/renderer_scene_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ void RendererSceneRender::CameraData::set_camera(const Transform3D p_transform,
taa_jitter = p_taa_jitter;
}

#ifdef MINGW_ENABLED
#undef near
#undef far
#endif

void RendererSceneRender::CameraData::set_multiview_camera(uint32_t p_view_count, const Transform3D *p_transforms, const Projection *p_projections, bool p_is_orthogonal, bool p_vaspect) {
ERR_FAIL_COND_MSG(p_view_count != 2, "Incorrect view count for stereoscopic view");

Expand Down Expand Up @@ -90,47 +85,47 @@ void RendererSceneRender::CameraData::set_multiview_camera(uint32_t p_view_count

// 5. figure out far plane, this could use some improvement, we may have our far plane too close like this, not sure if this matters
Vector3 far_center = (planes[0][Projection::PLANE_FAR].get_center() + planes[1][Projection::PLANE_FAR].get_center()) * 0.5;
Plane far(-z, far_center);
Plane far_plane = Plane(-z, far_center);

/////////////////////////////////////////////////////////////////////////////
// Figure out our top/bottom planes

// 6. Intersect far and left planes with top planes from both eyes, save the point with highest y as top_left.
Vector3 top_left, other;
ERR_FAIL_COND_MSG(
!far.intersect_3(planes[0][Projection::PLANE_LEFT], planes[0][Projection::PLANE_TOP], &top_left), "Can't determine left camera far/left/top vector");
!far_plane.intersect_3(planes[0][Projection::PLANE_LEFT], planes[0][Projection::PLANE_TOP], &top_left), "Can't determine left camera far/left/top vector");
ERR_FAIL_COND_MSG(
!far.intersect_3(planes[1][Projection::PLANE_LEFT], planes[1][Projection::PLANE_TOP], &other), "Can't determine right camera far/left/top vector");
!far_plane.intersect_3(planes[1][Projection::PLANE_LEFT], planes[1][Projection::PLANE_TOP], &other), "Can't determine right camera far/left/top vector");
if (y.dot(top_left) < y.dot(other)) {
top_left = other;
}

// 7. Intersect far and left planes with bottom planes from both eyes, save the point with lowest y as bottom_left.
Vector3 bottom_left;
ERR_FAIL_COND_MSG(
!far.intersect_3(planes[0][Projection::PLANE_LEFT], planes[0][Projection::PLANE_BOTTOM], &bottom_left), "Can't determine left camera far/left/bottom vector");
!far_plane.intersect_3(planes[0][Projection::PLANE_LEFT], planes[0][Projection::PLANE_BOTTOM], &bottom_left), "Can't determine left camera far/left/bottom vector");
ERR_FAIL_COND_MSG(
!far.intersect_3(planes[1][Projection::PLANE_LEFT], planes[1][Projection::PLANE_BOTTOM], &other), "Can't determine right camera far/left/bottom vector");
!far_plane.intersect_3(planes[1][Projection::PLANE_LEFT], planes[1][Projection::PLANE_BOTTOM], &other), "Can't determine right camera far/left/bottom vector");
if (y.dot(other) < y.dot(bottom_left)) {
bottom_left = other;
}

// 8. Intersect far and right planes with top planes from both eyes, save the point with highest y as top_right.
Vector3 top_right;
ERR_FAIL_COND_MSG(
!far.intersect_3(planes[0][Projection::PLANE_RIGHT], planes[0][Projection::PLANE_TOP], &top_right), "Can't determine left camera far/right/top vector");
!far_plane.intersect_3(planes[0][Projection::PLANE_RIGHT], planes[0][Projection::PLANE_TOP], &top_right), "Can't determine left camera far/right/top vector");
ERR_FAIL_COND_MSG(
!far.intersect_3(planes[1][Projection::PLANE_RIGHT], planes[1][Projection::PLANE_TOP], &other), "Can't determine right camera far/right/top vector");
!far_plane.intersect_3(planes[1][Projection::PLANE_RIGHT], planes[1][Projection::PLANE_TOP], &other), "Can't determine right camera far/right/top vector");
if (y.dot(top_right) < y.dot(other)) {
top_right = other;
}

// 9. Intersect far and right planes with bottom planes from both eyes, save the point with lowest y as bottom_right.
Vector3 bottom_right;
ERR_FAIL_COND_MSG(
!far.intersect_3(planes[0][Projection::PLANE_RIGHT], planes[0][Projection::PLANE_BOTTOM], &bottom_right), "Can't determine left camera far/right/bottom vector");
!far_plane.intersect_3(planes[0][Projection::PLANE_RIGHT], planes[0][Projection::PLANE_BOTTOM], &bottom_right), "Can't determine left camera far/right/bottom vector");
ERR_FAIL_COND_MSG(
!far.intersect_3(planes[1][Projection::PLANE_RIGHT], planes[1][Projection::PLANE_BOTTOM], &other), "Can't determine right camera far/right/bottom vector");
!far_plane.intersect_3(planes[1][Projection::PLANE_RIGHT], planes[1][Projection::PLANE_BOTTOM], &other), "Can't determine right camera far/right/bottom vector");
if (y.dot(other) < y.dot(bottom_right)) {
bottom_right = other;
}
Expand All @@ -145,29 +140,29 @@ void RendererSceneRender::CameraData::set_multiview_camera(uint32_t p_view_count
// Figure out our near plane points

// 12. Create a near plane using -camera z and the eye further along in that axis.
Plane near;
Plane near_plane;
Vector3 neg_z = -z;
if (neg_z.dot(p_transforms[1].origin) < neg_z.dot(p_transforms[0].origin)) {
near = Plane(neg_z, p_transforms[0].origin);
near_plane = Plane(neg_z, p_transforms[0].origin);
} else {
near = Plane(neg_z, p_transforms[1].origin);
near_plane = Plane(neg_z, p_transforms[1].origin);
}

// 13. Intersect near plane with bottm/left planes, to obtain min_vec then top/right to obtain max_vec
Vector3 min_vec;
ERR_FAIL_COND_MSG(
!near.intersect_3(bottom, planes[0][Projection::PLANE_LEFT], &min_vec), "Can't determine left camera near/left/bottom vector");
!near_plane.intersect_3(bottom, planes[0][Projection::PLANE_LEFT], &min_vec), "Can't determine left camera near/left/bottom vector");
ERR_FAIL_COND_MSG(
!near.intersect_3(bottom, planes[1][Projection::PLANE_LEFT], &other), "Can't determine right camera near/left/bottom vector");
!near_plane.intersect_3(bottom, planes[1][Projection::PLANE_LEFT], &other), "Can't determine right camera near/left/bottom vector");
if (x.dot(other) < x.dot(min_vec)) {
min_vec = other;
}

Vector3 max_vec;
ERR_FAIL_COND_MSG(
!near.intersect_3(top, planes[0][Projection::PLANE_RIGHT], &max_vec), "Can't determine left camera near/right/top vector");
!near_plane.intersect_3(top, planes[0][Projection::PLANE_RIGHT], &max_vec), "Can't determine left camera near/right/top vector");
ERR_FAIL_COND_MSG(
!near.intersect_3(top, planes[1][Projection::PLANE_RIGHT], &other), "Can't determine right camera near/right/top vector");
!near_plane.intersect_3(top, planes[1][Projection::PLANE_RIGHT], &other), "Can't determine right camera near/right/top vector");
if (x.dot(max_vec) < x.dot(other)) {
max_vec = other;
}
Expand All @@ -177,8 +172,8 @@ void RendererSceneRender::CameraData::set_multiview_camera(uint32_t p_view_count
Vector3 local_max_vec = main_transform_inv.xform(max_vec);

// 15. get x and y from these to obtain left, top, right bottom for the frustum. Get the distance from near plane to camera origin to obtain near, and the distance from the far plane to the camer origin to obtain far.
float z_near = -near.distance_to(main_transform.origin);
float z_far = -far.distance_to(main_transform.origin);
float z_near = -near_plane.distance_to(main_transform.origin);
float z_far = -far_plane.distance_to(main_transform.origin);

// 16. Use this to build the combined camera matrix.
main_projection.set_frustum(local_min_vec.x, local_max_vec.x, local_min_vec.y, local_max_vec.y, z_near, z_far);
Expand Down
12 changes: 6 additions & 6 deletions tests/scene/test_camera_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ TEST_CASE("[SceneTree][Camera3D] Getters and setters") {
}

SUBCASE("Camera frustum properties") {
constexpr float near = 0.2f;
constexpr float far = 995.0f;
constexpr float depth_near = 0.2f;
constexpr float depth_far = 995.0f;
constexpr float fov = 120.0f;
constexpr float size = 7.0f;
constexpr float h_offset = 1.1f;
constexpr float v_offset = -1.6f;
const Vector2 frustum_offset(5, 7);
test_camera->set_near(near);
CHECK(test_camera->get_near() == near);
test_camera->set_far(far);
CHECK(test_camera->get_far() == far);
test_camera->set_near(depth_near);
CHECK(test_camera->get_near() == depth_near);
test_camera->set_far(depth_far);
CHECK(test_camera->get_far() == depth_far);
test_camera->set_fov(fov);
CHECK(test_camera->get_fov() == fov);
test_camera->set_size(size);
Expand Down

0 comments on commit dfe226b

Please sign in to comment.