Skip to content

Commit

Permalink
update sky renderer to use configurable field of view
Browse files Browse the repository at this point in the history
  • Loading branch information
nstlaurent committed Jan 9, 2024
1 parent 5bc7097 commit 45139af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ public void Dispose()

private static mat4 CalculateMvp(RenderInfo renderInfo)
{
// Note that this means we've hard coded the sky to always render
// the same regardless of the field of view.
float w = renderInfo.Viewport.Width;
float h = renderInfo.Viewport.Height;
float aspectRatio = w / h;
float fovY = OldCamera.FieldOfViewXToY((float)MathHelper.HalfPi, aspectRatio);

// We want the sky sphere to not be touching the NDC edges because
// we'll be doing some translating which could push it outside of
// the clipping box. Therefore we shrink the unit sphere from r = 1
Expand All @@ -83,7 +76,8 @@ private static mat4 CalculateMvp(RenderInfo renderInfo)

// Our projection far plane only goes as far as the scaled sphere
// radius.
mat4 projection = mat4.PerspectiveFov(fovY, w, h, 0.0f, 0.5f);
var fovInfo = Renderer.GetFieldOfViewInfo(renderInfo);
mat4 projection = mat4.PerspectiveFov(fovInfo.FovY, fovInfo.Width, fovInfo.Height, 0.0f, 0.5f);

return projection * view * model;
}
Expand Down
19 changes: 13 additions & 6 deletions Core/Render/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace Helion.Render;

public record struct FieldOfViewInfo(float Width, float Height, float FovY);

public class Renderer : IDisposable
{
const float ZNearMin = 0.2f;
Expand Down Expand Up @@ -124,18 +126,23 @@ public static Vec3F GetColorMix(Entity viewerEntity, OldCamera camera)

public static mat4 CalculateMvpMatrix(RenderInfo renderInfo, bool onlyXY = false)
{
float w = renderInfo.Viewport.Width;
float h = renderInfo.Viewport.Height * 0.825f;
// Default FOV is 63.2. Config default is 90 so we need to convert. (90 - 63.2 = 26.8).
float fovY = (float)MathHelper.ToRadians(renderInfo.Config.FieldOfView - 26.8);

var fovInfo = GetFieldOfViewInfo(renderInfo);
mat4 model = mat4.Identity;
mat4 view = renderInfo.Camera.CalculateViewMatrix(onlyXY);

mat4 projection = mat4.PerspectiveFov(fovY, w, h, GetZNear(renderInfo), 65536.0f);
mat4 projection = mat4.PerspectiveFov(fovInfo.FovY, fovInfo.Width, fovInfo.Height, GetZNear(renderInfo), 65536.0f);
return projection * view * model;
}

public static FieldOfViewInfo GetFieldOfViewInfo(RenderInfo renderInfo)
{
float w = renderInfo.Viewport.Width;
float h = renderInfo.Viewport.Height * 0.825f;
// Default FOV is 63.2. Config default is 90 so we need to convert. (90 - 63.2 = 26.8).
float fovY = (float)MathHelper.ToRadians(renderInfo.Config.FieldOfView - 26.8);
return new(w, h, fovY);
}

private static float GetZNear(RenderInfo renderInfo)
{
// Optimially this should be handled in the shader. Setting this variable and using it for a low zNear is good enough for now.
Expand Down

0 comments on commit 45139af

Please sign in to comment.