From 4ddec7e10c473dfee6c7d85089039c9b61a2f1e3 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 23 Jun 2018 15:01:29 -1000 Subject: [PATCH] Make CLineRenderer supply verts in clip-space --- Runtime/Graphics/CGraphics.cpp | 6 +++ Runtime/Graphics/CGraphics.hpp | 1 + Runtime/Graphics/CLineRenderer.cpp | 67 ++++++++++++++++-------------- Runtime/Graphics/CLineRenderer.hpp | 2 + specter | 2 +- 5 files changed, 46 insertions(+), 32 deletions(-) diff --git a/Runtime/Graphics/CGraphics.cpp b/Runtime/Graphics/CGraphics.cpp index 9919661c7..a018df554 100644 --- a/Runtime/Graphics/CGraphics.cpp +++ b/Runtime/Graphics/CGraphics.cpp @@ -468,6 +468,12 @@ zeus::CVector3f CGraphics::ProjectModelPointToViewportSpace(const zeus::CVector3 return GetPerspectiveProjectionMatrix(true).multiplyOneOverW(pt); } +zeus::CVector3f CGraphics::ProjectModelPointToViewportSpace(const zeus::CVector3f& point, float& wOut) +{ + zeus::CVector3f pt = g_GXModelView * point; + return GetPerspectiveProjectionMatrix(true).multiplyOneOverW(pt, wOut); +} + void CGraphics::SetViewportResolution(const zeus::CVector2i& res) { g_Viewport.x8_width = res.x; diff --git a/Runtime/Graphics/CGraphics.hpp b/Runtime/Graphics/CGraphics.hpp index 7a0dde31b..7e8a7f0d7 100644 --- a/Runtime/Graphics/CGraphics.hpp +++ b/Runtime/Graphics/CGraphics.hpp @@ -290,6 +290,7 @@ public: static SClipScreenRect ClipScreenRectFromMS(const zeus::CVector3f& p1, const zeus::CVector3f& p2); static SClipScreenRect ClipScreenRectFromVS(const zeus::CVector3f& p1, const zeus::CVector3f& p2); static zeus::CVector3f ProjectModelPointToViewportSpace(const zeus::CVector3f& point); + static zeus::CVector3f ProjectModelPointToViewportSpace(const zeus::CVector3f& point, float& wOut); static void SetViewportResolution(const zeus::CVector2i& res); static void SetViewport(int leftOff, int bottomOff, int width, int height); diff --git a/Runtime/Graphics/CLineRenderer.cpp b/Runtime/Graphics/CLineRenderer.cpp index 54bda70c8..7f97d4432 100644 --- a/Runtime/Graphics/CLineRenderer.cpp +++ b/Runtime/Graphics/CLineRenderer.cpp @@ -133,13 +133,15 @@ void CLineRenderer::AddVertex(const zeus::CVector3f& position, const zeus::CColo return; float adjWidth = width / 480.f; - zeus::CVector3f projPt = CGraphics::ProjectModelPointToViewportSpace(position); + float w; + zeus::CVector3f projPt = CGraphics::ProjectModelPointToViewportSpace(position, w); if (m_mode == EPrimitiveMode::LineLoop) { if (m_nextVert == 0) { m_firstPos = projPt; + m_firstW = w; m_secondPos = projPt; m_firstUV = uv; m_firstColor = color; @@ -172,14 +174,14 @@ void CLineRenderer::AddVertex(const zeus::CVector3f& position, const zeus::CColo if (m_nextVert & 1) { g_StaticLineVertsTex.push_back(g_StaticLineVertsTex.back()); - g_StaticLineVertsTex.push_back({m_lastPos + dvb, m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(m_lastPos + dvb, m_lastW), m_lastColor, m_lastUV}); g_StaticLineVertsTex.push_back(g_StaticLineVertsTex.back()); - g_StaticLineVertsTex.push_back({m_lastPos - dvb, m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(m_lastPos - dvb, m_lastW), m_lastColor, m_lastUV}); } else { - g_StaticLineVertsTex.push_back({m_lastPos + dva, m_lastColor, m_lastUV}); - g_StaticLineVertsTex.push_back({m_lastPos - dva, m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(m_lastPos + dva, m_lastW), m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(m_lastPos - dva, m_lastW), m_lastColor, m_lastUV}); } } else @@ -192,8 +194,8 @@ void CLineRenderer::AddVertex(const zeus::CVector3f& position, const zeus::CColo m_lastPos.toVec2f() - dvb, projPt.toVec2f() - dvb); intersect2.z = m_lastPos.z; - g_StaticLineVertsTex.push_back({intersect1, m_lastColor, m_lastUV}); - g_StaticLineVertsTex.push_back({intersect2, m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(intersect1, m_lastW), m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(intersect2, m_lastW), m_lastColor, m_lastUV}); } } else @@ -203,14 +205,14 @@ void CLineRenderer::AddVertex(const zeus::CVector3f& position, const zeus::CColo if (m_nextVert & 1) { g_StaticLineVertsNoTex.push_back(g_StaticLineVertsNoTex.back()); - g_StaticLineVertsNoTex.push_back({m_lastPos + dvb, m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(m_lastPos + dvb, m_lastW), m_lastColor}); g_StaticLineVertsNoTex.push_back(g_StaticLineVertsNoTex.back()); - g_StaticLineVertsNoTex.push_back({m_lastPos - dvb, m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(m_lastPos - dvb, m_lastW), m_lastColor}); } else { - g_StaticLineVertsNoTex.push_back({m_lastPos + dva, m_lastColor}); - g_StaticLineVertsNoTex.push_back({m_lastPos - dva, m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(m_lastPos + dva, m_lastW), m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(m_lastPos - dva, m_lastW), m_lastColor}); } } else @@ -223,8 +225,8 @@ void CLineRenderer::AddVertex(const zeus::CVector3f& position, const zeus::CColo m_lastPos.toVec2f() - dvb, projPt.toVec2f() - dvb); intersect2.z = m_lastPos.z; - g_StaticLineVertsNoTex.push_back({intersect1, m_lastColor}); - g_StaticLineVertsNoTex.push_back({intersect2, m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(intersect1, m_lastW), m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(intersect2, m_lastW), m_lastColor}); } } } @@ -237,18 +239,19 @@ void CLineRenderer::AddVertex(const zeus::CVector3f& position, const zeus::CColo dv.x /= CGraphics::g_ProjAspect; if (m_textured) { - g_StaticLineVertsTex.push_back({m_lastPos + dv, m_lastColor, m_lastUV}); - g_StaticLineVertsTex.push_back({m_lastPos - dv, m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(m_lastPos + dv, m_lastW), m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(m_lastPos - dv, m_lastW), m_lastColor, m_lastUV}); } else { - g_StaticLineVertsNoTex.push_back({m_lastPos + dv, m_lastColor}); - g_StaticLineVertsNoTex.push_back({m_lastPos - dv, m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(m_lastPos + dv, m_lastW), m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(m_lastPos - dv, m_lastW), m_lastColor}); } } m_lastPos2 = m_lastPos; m_lastPos = projPt; + m_lastW = w; m_lastUV = uv; m_lastColor = color; m_lastWidth = adjWidth; @@ -284,13 +287,13 @@ void CLineRenderer::Render(const zeus::CColor& moduColor) if (m_textured) { - g_StaticLineVertsTex.push_back({intersect1, m_lastColor, m_lastUV}); - g_StaticLineVertsTex.push_back({intersect2, m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(intersect1, m_lastW), m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(intersect2, m_lastW), m_lastColor, m_lastUV}); } else { - g_StaticLineVertsNoTex.push_back({intersect1, m_lastColor}); - g_StaticLineVertsNoTex.push_back({intersect2, m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(intersect1, m_lastW), m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(intersect2, m_lastW), m_lastColor}); } } { @@ -316,13 +319,13 @@ void CLineRenderer::Render(const zeus::CColor& moduColor) if (m_textured) { - g_StaticLineVertsTex.push_back({intersect1, m_firstColor, m_firstUV}); - g_StaticLineVertsTex.push_back({intersect2, m_firstColor, m_firstUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(intersect1, m_firstW), m_firstColor, m_firstUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(intersect2, m_firstW), m_firstColor, m_firstUV}); } else { - g_StaticLineVertsNoTex.push_back({intersect1, m_firstColor}); - g_StaticLineVertsNoTex.push_back({intersect2, m_firstColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(intersect1, m_firstW), m_firstColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(intersect2, m_firstW), m_firstColor}); } } } @@ -339,8 +342,8 @@ void CLineRenderer::Render(const zeus::CColor& moduColor) {} else { - g_StaticLineVertsTex.push_back({m_lastPos + dv, m_lastColor, m_lastUV}); - g_StaticLineVertsTex.push_back({m_lastPos - dv, m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(m_lastPos + dv, m_lastW), m_lastColor, m_lastUV}); + g_StaticLineVertsTex.push_back({zeus::CVector4f::ToClip(m_lastPos - dv, m_lastW), m_lastColor, m_lastUV}); } } else @@ -349,8 +352,8 @@ void CLineRenderer::Render(const zeus::CColor& moduColor) {} else { - g_StaticLineVertsNoTex.push_back({m_lastPos + dv, m_lastColor}); - g_StaticLineVertsNoTex.push_back({m_lastPos - dv, m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(m_lastPos + dv, m_lastW), m_lastColor}); + g_StaticLineVertsNoTex.push_back({zeus::CVector4f::ToClip(m_lastPos - dv, m_lastW), m_lastColor}); } } } @@ -361,13 +364,15 @@ void CLineRenderer::Render(const zeus::CColor& moduColor) m_uniformBuf.access() = SDrawUniform{moduColor}; if (m_textured) { - memmove(m_vertBufTex.access(), g_StaticLineVertsTex.data(), sizeof(SDrawVertTex) * g_StaticLineVertsTex.size()); + memmove(m_vertBufTex.access(), g_StaticLineVertsTex.data(), + sizeof(SDrawVertTex) * g_StaticLineVertsTex.size()); CGraphics::SetShaderDataBinding(m_shaderBind); CGraphics::DrawArray(0, g_StaticLineVertsTex.size()); } else { - memmove(m_vertBufNoTex.access(), g_StaticLineVertsNoTex.data(), sizeof(SDrawVertNoTex) * g_StaticLineVertsNoTex.size()); + memmove(m_vertBufNoTex.access(), g_StaticLineVertsNoTex.data(), + sizeof(SDrawVertNoTex) * g_StaticLineVertsNoTex.size()); CGraphics::SetShaderDataBinding(m_shaderBind); CGraphics::DrawArray(0, g_StaticLineVertsNoTex.size()); } diff --git a/Runtime/Graphics/CLineRenderer.hpp b/Runtime/Graphics/CLineRenderer.hpp index a82d1a46b..a7d7bd79e 100644 --- a/Runtime/Graphics/CLineRenderer.hpp +++ b/Runtime/Graphics/CLineRenderer.hpp @@ -52,12 +52,14 @@ private: zeus::CVector2f m_firstUV; zeus::CColor m_firstColor; float m_firstWidth; + float m_firstW; zeus::CVector3f m_lastPos; zeus::CVector3f m_lastPos2; zeus::CVector2f m_lastUV; zeus::CColor m_lastColor; float m_lastWidth; + float m_lastW; static rstl::reserved_vector g_StaticLineVertsTex; static rstl::reserved_vector g_StaticLineVertsNoTex; diff --git a/specter b/specter index 88a4a52d8..1b3e19f74 160000 --- a/specter +++ b/specter @@ -1 +1 @@ -Subproject commit 88a4a52d82f0a60e46181d0bf60e4d4d2ceee02c +Subproject commit 1b3e19f74ccb55d4c20a8caf2f9c2a4ca06de335