From bf7b10850e8a19bf40d44f7f80e415740f5caedf Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Wed, 11 Dec 2024 11:51:55 +0100 Subject: [PATCH] Terrain rendering hack --- Engine/Graphics/3DRenderer.cpp | 32 ++++++++++++++++++++++++++------ Engine/Graphics/Camera.hpp | 2 ++ Engine/Graphics/DebugUI.cpp | 2 +- Engine/Utils/3DMaths.hpp | 2 +- Engine/Utils/3DMaths_bs.inl | 2 +- Engine/Utils/3DMaths_mat.inl | 2 +- 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Engine/Graphics/3DRenderer.cpp b/Engine/Graphics/3DRenderer.cpp index d02bd9b..e8d3de1 100644 --- a/Engine/Graphics/3DRenderer.cpp +++ b/Engine/Graphics/3DRenderer.cpp @@ -1,5 +1,7 @@ #include "3DRenderer.hpp" +#include + #include "../World/DbgCube.hpp" #include "../World/Tank.hpp" @@ -99,12 +101,33 @@ void Graphic3DRenderer::Draw(sf::RenderTexture& context) { // Load main matrices M3D_MATRIX viewMat = mMainCamera->GetView(); + M3D_MATRIX invViewMat = M3D_MInverse(viewMat); // aka. camMat M3D_MATRIX projMat = mMainCamera->GetProj(); M3D_MATRIX viewProjMat = viewMat * projMat; // Create the frustrum "box" M3D_BoundingFrustum camFrustrum(projMat, false); - camFrustrum.Transform(camFrustrum, M3D_MInverse(viewMat)); + camFrustrum.Transform(camFrustrum, invViewMat); + + const float sgRatio = std::tan(mMainCamera->GetLook3f().y); + // -= Draw the sky =- + // To avoid unfilled pixels on screen, the "sky-plane" will be rendered + // all over the screen. + // It's will be useless to use and compute a specific rectangle from the + // size of the screen! + // The sky have an infinite z-depth (any objects will be rendered over). + context.clear(sf::Color::Cyan); + + // -= Draw the ground =- + // A simple rectangle shape is used to draw the ground over the sky-plane. + // The ground is draw after the sky, and before any other object. + // Depending of the camera pitch, the ratio sky/ground on screen vary. + // Like the sky, the ground have an infinite z-depth (any objects will + // be rendered over). + sf::RectangleShape gndRect(sf::Vector2f(1280, mRTSize.y * (0.5f - sgRatio))); + gndRect.setPosition(sf::Vector2f(0, mRTSize.y * (0.5f + sgRatio))); + gndRect.setFillColor(sf::Color::Green); + context.draw(gndRect, sRS); // Process scene's objects for (auto& obj : mRenderList) { @@ -120,7 +143,7 @@ void Graphic3DRenderer::Draw(sf::RenderTexture& context) { { size_t vCount = obj->GetObjectVerticesCount(); auto& oMesh = obj->GetObjectMesh(); - auto* projVertices = new M3D_F4[vCount]; + M3D_F4 projVertices[vCount] = {}; // Vertices homogeneous clip space transformation M3D_V3Transform( @@ -131,7 +154,7 @@ void Graphic3DRenderer::Draw(sf::RenderTexture& context) { ); // Draw the object indice triangles if visible or partially clipped - auto* v_tri = new sf::Vertex[4]; + sf::Vertex v_tri[4]; for (auto& objPt : oMesh.parts) { auto indicePtr = static_cast(objPt.indices.data()); @@ -188,9 +211,6 @@ void Graphic3DRenderer::Draw(sf::RenderTexture& context) { } } } - - delete[] v_tri; - delete[] projVertices; } } } diff --git a/Engine/Graphics/Camera.hpp b/Engine/Graphics/Camera.hpp index cf9bbc5..01dc1fe 100644 --- a/Engine/Graphics/Camera.hpp +++ b/Engine/Graphics/Camera.hpp @@ -15,6 +15,8 @@ public: M3D_VECTOR GetPos() const { return M3D_V4LoadF3(&mPos); } M3D_F3 GetPos3f() const { return mPos; } + M3D_VECTOR GetLook() const { return M3D_V4LoadF3(&mLook); } + M3D_F3 GetLook3f() const { return mLook; } M3D_MATRIX GetView() const { return M3D_V4LoadF4x4(&mViewMat); } const M3D_F4X4& GetView4x4f() const { return mViewMat; } M3D_MATRIX GetProj() const { return M3D_V4LoadF4x4(&mProjMat); } diff --git a/Engine/Graphics/DebugUI.cpp b/Engine/Graphics/DebugUI.cpp index 9bf3788..6354ab2 100644 --- a/Engine/Graphics/DebugUI.cpp +++ b/Engine/Graphics/DebugUI.cpp @@ -28,7 +28,7 @@ void DebugUI::DrawDebugData(std::shared_ptr context) { outStrStream << std::fixed; outStrStream << std::setprecision(0) << "CPU: " << gDbgStats.cpu_usage << "% (cycle: " << std::setprecision(3) << gDbgStats.cpu_time << "/" << gDbgStats.cycle_time << "ms)" << std::endl; - outStrStream << std::setprecision(1) << "RAM: " << gDbgStats.ram_virt_usage << "MB(V) / " << gDbgStats.ram_phys_usage << "MB(P)" << std::endl; + outStrStream << std::setprecision(1) << "RAM: " << gDbgStats.ram_virt_usage << "MB(V)/" << gDbgStats.ram_phys_usage << "MB(P)" << std::endl; outStrStream << std::setprecision(0) << "FPS: " << gDbgStats.fps; gDbgText.setString(outStrStream.str()); diff --git a/Engine/Utils/3DMaths.hpp b/Engine/Utils/3DMaths.hpp index 51eb4c8..e745ec7 100644 --- a/Engine/Utils/3DMaths.hpp +++ b/Engine/Utils/3DMaths.hpp @@ -555,7 +555,7 @@ M3D_MATRIX M3D_MMultiply(M3D_MATRIX M1, M3D_MATRIX& M2) noexcept; M3D_MATRIX M3D_MTranspose(M3D_MATRIX M) noexcept; M3D_MATRIX M3D_MInverse(M3D_MATRIX M) noexcept; -M3D_VECTOR M3D_QRotationMatrix(M3D_MATRIX M) noexcept; +M3D_VECTOR M3D_QRotationFromMatrix(M3D_MATRIX M) noexcept; // diff --git a/Engine/Utils/3DMaths_bs.inl b/Engine/Utils/3DMaths_bs.inl index 24ade29..5dd5217 100644 --- a/Engine/Utils/3DMaths_bs.inl +++ b/Engine/Utils/3DMaths_bs.inl @@ -178,7 +178,7 @@ INLINE_AVX_FIX void M3D_BoundingFrustum::Transform(M3D_BoundingFrustum& Out, M3D nM.rows[1] = M3D_V3Normalize(M.rows[1]); nM.rows[2] = M3D_V3Normalize(M.rows[2]); nM.rows[3] = M3D_MIdentityR3; - M3D_VECTOR Rotation = M3D_QRotationMatrix(nM); + M3D_VECTOR Rotation = M3D_QRotationFromMatrix(nM); vOrientation = M3D_QMultiply(vOrientation, Rotation); // Transform the center. diff --git a/Engine/Utils/3DMaths_mat.inl b/Engine/Utils/3DMaths_mat.inl index 13a8350..f22d9f6 100644 --- a/Engine/Utils/3DMaths_mat.inl +++ b/Engine/Utils/3DMaths_mat.inl @@ -591,7 +591,7 @@ INLINE_AVX_FIX M3D_MATRIX M3D_MInverse(M3D_MATRIX M) noexcept { /* -------------------------------------------------------------------------------------------------------------------------- */ -INLINE_AVX_FIX M3D_VECTOR M3D_QRotationMatrix(M3D_MATRIX M) noexcept { +INLINE_AVX_FIX M3D_VECTOR M3D_QRotationFromMatrix(M3D_MATRIX M) noexcept { #ifdef DISABLE_INTRINSICS M3D_V4F32 q; float r22 = M.mat[2][2];