Correctly scale screen space sprites according to screen aspect ratio

This commit is contained in:
Sezz 2023-02-20 18:55:51 +11:00
parent e96d981f7f
commit 68b16dba43
3 changed files with 39 additions and 23 deletions

View file

@ -26,6 +26,17 @@ namespace TEN::Hud
void HudController::Draw() const void HudController::Draw() const
{ {
// ----------------Debug
static short orient2D = 0;
orient2D += ANGLE(1.0f);
auto pos = SCREEN_SPACE_RES / 3;
g_Renderer.DrawSpriteInScreenSpace(
ID_BINOCULAR_GRAPHIC,
pos, orient2D, Vector4(1, 1, 1, 0.5f), 200.0f);
//------------------
this->PickupSummary.Draw(); this->PickupSummary.Draw();
} }

View file

@ -550,7 +550,6 @@ namespace TEN::Renderer
ID3D11DepthStencilView* depthTarget); ID3D11DepthStencilView* depthTarget);
void DrawShockwaves(RenderView& view); void DrawShockwaves(RenderView& view);
void DrawRipples(RenderView& view); void DrawRipples(RenderView& view);
void DrawSpriteInScreenSpace(RendererSprite* sprite, const Vector2& pos, short orient2D, const Vector4& color, float scale);
void DrawFullScreenQuad(ID3D11ShaderResourceView* texture, Vector3 color, bool fit = true); void DrawFullScreenQuad(ID3D11ShaderResourceView* texture, Vector3 color, bool fit = true);
void DrawSmokeParticles(RenderView& view); void DrawSmokeParticles(RenderView& view);
void DrawSparkParticles(RenderView& view); void DrawSparkParticles(RenderView& view);
@ -691,6 +690,8 @@ namespace TEN::Renderer
Vector2i GetScreenResolution() const; Vector2i GetScreenResolution() const;
Vector2 GetScreenSpacePosition(const Vector3& pos) const; Vector2 GetScreenSpacePosition(const Vector3& pos) const;
Vector3 GetAbsEntityBonePosition(int itemNumber, int jointIndex, const Vector3& relOffset = Vector3::Zero); Vector3 GetAbsEntityBonePosition(int itemNumber, int jointIndex, const Vector3& relOffset = Vector3::Zero);
void DrawSpriteInScreenSpace(unsigned int spriteID, const Vector2& pos, short orient2D, const Vector4& color, float scale);
}; };
extern Renderer11 g_Renderer; extern Renderer11 g_Renderer;

View file

@ -186,26 +186,14 @@ namespace TEN::Renderer
DrawFullScreenQuad(m_whiteTexture.ShaderResourceView.Get(), flashColor); DrawFullScreenQuad(m_whiteTexture.ShaderResourceView.Get(), flashColor);
} }
/*if (CurrentLevel == 0) if (CurrentLevel == 0)
return; return;
if (!BinocularRange && !SpotcamOverlay) if (!BinocularRange && !SpotcamOverlay)
return;*/ return;
SetBlendMode(BLENDMODE_ALPHABLEND); SetBlendMode(BLENDMODE_ALPHABLEND);
// -----------------Debug
static short orient2D = 0;
orient2D += ANGLE(1.0f);
auto pos = SCREEN_SPACE_RES / 3;
DrawSpriteInScreenSpace(
&m_sprites[Objects[ID_DEFAULT_SPRITES].meshIndex],
pos, orient2D, Vector4(1, 1, 1, 0.5f), 200.0f);
//------------------
if (BinocularRange && !LaserSight) if (BinocularRange && !LaserSight)
{ {
DrawFullScreenQuad(m_sprites[Objects[ID_BINOCULAR_GRAPHIC].meshIndex].Texture->ShaderResourceView.Get(), Vector3::One, true); DrawFullScreenQuad(m_sprites[Objects[ID_BINOCULAR_GRAPHIC].meshIndex].Texture->ShaderResourceView.Get(), Vector3::One, true);
@ -336,10 +324,10 @@ namespace TEN::Renderer
m_context->RSSetViewports(1, &m_viewport); m_context->RSSetViewports(1, &m_viewport);
ResetScissor(); ResetScissor();
DrawFullScreenQuad(texture, Vector3(fade, fade, fade), true); DrawFullScreenQuad(texture, Vector3(fade), true);
} }
void Renderer11::DrawSpriteInScreenSpace(RendererSprite* sprite, const Vector2& pos, short orient2D, const Vector4& color, float scale) void Renderer11::DrawSpriteInScreenSpace(unsigned int spriteID, const Vector2& pos, short orient2D, const Vector4& color, float scale)
{ {
constexpr auto VERTEX_COUNT = 4; constexpr auto VERTEX_COUNT = 4;
constexpr auto UV_RANGE = std::pair<Vector2, Vector2>(Vector2(0.0f, 0.0f), Vector2(1.0f, 1.0f)); constexpr auto UV_RANGE = std::pair<Vector2, Vector2>(Vector2(0.0f, 0.0f), Vector2(1.0f, 1.0f));
@ -351,19 +339,32 @@ namespace TEN::Renderer
Vector2(-SQRT_2, -SQRT_2) Vector2(-SQRT_2, -SQRT_2)
}; };
// Calculate screen space positions. // Calculate relative screen space vertex positions.
auto rotMatrix = Matrix::CreateRotationZ(TO_RAD(orient2D)); auto rotMatrix = Matrix::CreateRotationZ(TO_RAD(orient2D));
auto vertexPoints = std::array<Vector2, VERTEX_COUNT> auto vertexPoints = std::array<Vector2, VERTEX_COUNT>
{ {
pos + Vector2::Transform(VERTEX_POINTS_DEFAULT[0] * (scale / 2), rotMatrix), pos + Vector2::Transform(VERTEX_POINTS_DEFAULT[0] * (scale / 2), rotMatrix),
pos + Vector2::Transform(VERTEX_POINTS_DEFAULT[1] * (scale / 2), rotMatrix), pos + Vector2::Transform(VERTEX_POINTS_DEFAULT[1] * (scale / 2), rotMatrix),
pos + Vector2::Transform(VERTEX_POINTS_DEFAULT[2] * (scale / 2), rotMatrix), pos + Vector2::Transform(VERTEX_POINTS_DEFAULT[2] * (scale / 2), rotMatrix),
pos + Vector2::Transform(VERTEX_POINTS_DEFAULT[3] * (scale / 2), rotMatrix) pos + Vector2::Transform(VERTEX_POINTS_DEFAULT[3] * (scale / 2), rotMatrix)
}; };
// Convert to NDC. auto screenRes = GetScreenResolution().ToVector2();
float aspectRatioDiff = (screenRes.x / screenRes.y) - (SCREEN_SPACE_RES.x / SCREEN_SPACE_RES.y);
for (auto& vertexPoint : vertexPoints) for (auto& vertexPoint : vertexPoints)
{
// Adjust vertex positions according to screen aspect ratio.
if (aspectRatioDiff > EPSILON)
vertexPoint.x *= 1.0f - (aspectRatioDiff / 2);
else if (aspectRatioDiff < -EPSILON)
vertexPoint.y *= 1.0f - (aspectRatioDiff / 2);
//vertexPoint += pos;
// Convert to NDC.
vertexPoint = TEN::Utils::ConvertScreenSpacePosToNDC(vertexPoint); vertexPoint = TEN::Utils::ConvertScreenSpacePosToNDC(vertexPoint);
}
// Define renderer vertices. // Define renderer vertices.
auto vertices = std::array<RendererVertex, VERTEX_COUNT>{}; auto vertices = std::array<RendererVertex, VERTEX_COUNT>{};
@ -392,10 +393,13 @@ namespace TEN::Renderer
vertices[3].UV.y = UV_RANGE.second.y; vertices[3].UV.y = UV_RANGE.second.y;
vertices[3].Color = color; vertices[3].Color = color;
SetBlendMode(BLENDMODE_ALPHABLEND);
m_context->VSSetShader(m_vsFullScreenQuad.Get(), nullptr, 0); m_context->VSSetShader(m_vsFullScreenQuad.Get(), nullptr, 0);
m_context->PSSetShader(m_psFullScreenQuad.Get(), nullptr, 0); m_context->PSSetShader(m_psFullScreenQuad.Get(), nullptr, 0);
auto* texturePtr = sprite->Texture->ShaderResourceView.Get(); const auto& spritePtr = m_sprites[Objects[spriteID].meshIndex];
auto* texturePtr = spritePtr.Texture->ShaderResourceView.Get();
m_context->PSSetShaderResources(0, 1, &texturePtr); m_context->PSSetShaderResources(0, 1, &texturePtr);
auto* sampler = m_states->AnisotropicClamp(); auto* sampler = m_states->AnisotropicClamp();
m_context->PSSetSamplers(0, 1, &sampler); m_context->PSSetSamplers(0, 1, &sampler);