Archived
1
Fork 0

Insert float epsilon to prevent NaN and other artifacts in infinite perspectives

This commit is contained in:
redstrate 2020-08-18 00:46:16 -04:00
parent d798d1328e
commit e1205662ec
3 changed files with 3 additions and 5 deletions

View file

@ -31,9 +31,9 @@ Matrix4x4 transform::infinite_perspective(const float field_of_view, const float
Matrix4x4 result(0.0f);
result[0][0] = (2.0f * z_near) / (right - left);
result[1][1] = (2.0f * z_near) / (top - bottom);
result[2][2] = 1.0f;
result[2][2] = 1.0f - std::numeric_limits<float>::epsilon(); // prevent NaN
result[2][3] = 1.0f;
result[3][2] = -2.0 * z_near;
result[3][2] = -(2.0 - std::numeric_limits<float>::epsilon()) * z_near;
return result;
}

View file

@ -683,7 +683,6 @@ void Renderer::create_mesh_pipeline(Material& material) {
pipelineInfo.render_pass = offscreenRenderPass;
pipelineInfo.depth.depth_mode = GFXDepthMode::Less;
pipelineInfo.rasterization.culling_mode = GFXCullingMode::Backface;
//pipelineInfo.blending.enable_blending = true;
pipelineInfo.blending.src_rgb = GFXBlendFactor::SrcAlpha;
pipelineInfo.blending.dst_rgb = GFXBlendFactor::OneMinusSrcAlpha;

View file

@ -193,8 +193,7 @@ void CommonEditor::update(float deltaTime) {
Vector4 ray_start = view_proj_inverse * Vector4(n.x, n.y, 0.0f, 1.0f);
ray_start *= 1.0f / ray_start.w;
// note: we take the lowest visible floating point below 1.0 because we use infinite perspective martrix. if we simply inserted 1.0, then it would return NaN
Vector4 ray_end = view_proj_inverse * Vector4(n.x, n.y, 1.0f - std::numeric_limits<float>::epsilon(), 1.0f);
Vector4 ray_end = view_proj_inverse * Vector4(n.x, n.y, 1.0f, 1.0f);
ray_end *= 1.0f / ray_end.w;
Ray camera_ray;