From 7a1f1124cb4526c444c028548dbe8c796bb58f74 Mon Sep 17 00:00:00 2001 From: Ymihere03 Date: Mon, 12 Aug 2024 18:40:33 -0600 Subject: [PATCH 1/5] Add function to allow the FrameBuffer to be updated on the fly Update the FrameBuffer during the Window Resize event --- src/game/CAvaraApp.cpp | 1 + src/render/AbstractRenderer.h | 5 +++++ src/render/ModernOpenGLRenderer.cpp | 11 +++++++++++ src/render/ModernOpenGLRenderer.h | 1 + src/render/NullRenderer.h | 1 + 5 files changed, 19 insertions(+) diff --git a/src/game/CAvaraApp.cpp b/src/game/CAvaraApp.cpp index 47aff359..f1804014 100755 --- a/src/game/CAvaraApp.cpp +++ b/src/game/CAvaraApp.cpp @@ -201,6 +201,7 @@ void CAvaraAppImpl::RenderContents() { void CAvaraAppImpl::WindowResized(int width, int height) { gRenderer->UpdateViewRect(width, height, mPixelRatio); + gRenderer->ApplyFrameBuffer(); //performLayout(); } diff --git a/src/render/AbstractRenderer.h b/src/render/AbstractRenderer.h index 563e35a5..89262b8c 100644 --- a/src/render/AbstractRenderer.h +++ b/src/render/AbstractRenderer.h @@ -39,6 +39,11 @@ class AbstractRenderer { */ virtual void ApplyProjection() = 0; + /** + * Update the frame buffer with the currently configured resolution and FOV. + */ + virtual void ApplyFrameBuffer() = 0; + /** * Reset the renderer's state back to its defaults. */ diff --git a/src/render/ModernOpenGLRenderer.cpp b/src/render/ModernOpenGLRenderer.cpp index 0b11a7f7..e32ce64c 100644 --- a/src/render/ModernOpenGLRenderer.cpp +++ b/src/render/ModernOpenGLRenderer.cpp @@ -252,6 +252,9 @@ void ModernOpenGLRenderer::ApplyProjection() { SDL_GL_GetDrawableSize(this->window, &resolution[0], &resolution[1]); + //viewParams->viewPixelDimensions.h = resolution[0]; + //viewParams->viewPixelDimensions.v = resolution[1]; + glm::mat4 proj = glm::scale( glm::perspective( glm::radians(fov), @@ -275,6 +278,14 @@ void ModernOpenGLRenderer::ApplyProjection() glCheckErrors(); } +void ModernOpenGLRenderer::ApplyFrameBuffer() +{ + GLsizei w, h; + SDL_GL_GetDrawableSize(window, &w, &h); + MakeFramebuffer(0, w, h); + MakeFramebuffer(1, w, h); +} + void ModernOpenGLRenderer::LevelReset() { dynamicWorld->DisposeParts(); diff --git a/src/render/ModernOpenGLRenderer.h b/src/render/ModernOpenGLRenderer.h index 5bc65a6a..ca813916 100644 --- a/src/render/ModernOpenGLRenderer.h +++ b/src/render/ModernOpenGLRenderer.h @@ -19,6 +19,7 @@ class ModernOpenGLRenderer final: public AbstractRenderer { virtual void AddPart(CBSPPart *part) override; virtual void ApplyLights() override; virtual void ApplyProjection() override; + virtual void ApplyFrameBuffer() override; virtual void LevelReset() override; virtual std::unique_ptr NewVertexDataInstance() override; virtual void OverheadPoint(Fixed *pt, Fixed *extent) override; diff --git a/src/render/NullRenderer.h b/src/render/NullRenderer.h index 4d5cb23f..a73e7b81 100644 --- a/src/render/NullRenderer.h +++ b/src/render/NullRenderer.h @@ -14,6 +14,7 @@ class NullRenderer final: public AbstractRenderer { virtual void AddPart(CBSPPart *part) override {}; virtual void ApplyLights() override {}; virtual void ApplyProjection() override {}; + virtual void ApplyFrameBuffer() override {}; virtual std::unique_ptr NewVertexDataInstance() override { return nullptr; }; From a0a5e3f5493e69430423095f4a37dddc28c62c97 Mon Sep 17 00:00:00 2001 From: Ymihere03 Date: Mon, 12 Aug 2024 18:46:32 -0600 Subject: [PATCH 2/5] Remove comment lines --- src/render/ModernOpenGLRenderer.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/render/ModernOpenGLRenderer.cpp b/src/render/ModernOpenGLRenderer.cpp index e32ce64c..a4b4a68d 100644 --- a/src/render/ModernOpenGLRenderer.cpp +++ b/src/render/ModernOpenGLRenderer.cpp @@ -252,9 +252,6 @@ void ModernOpenGLRenderer::ApplyProjection() { SDL_GL_GetDrawableSize(this->window, &resolution[0], &resolution[1]); - //viewParams->viewPixelDimensions.h = resolution[0]; - //viewParams->viewPixelDimensions.v = resolution[1]; - glm::mat4 proj = glm::scale( glm::perspective( glm::radians(fov), From c3f2bfb0eade1f2b9af428089dc4f63ad2a3ea0f Mon Sep 17 00:00:00 2001 From: Ymihere03 Date: Tue, 13 Aug 2024 00:56:39 -0600 Subject: [PATCH 3/5] Refactored window resize handling to prevent memory leaks when updating the GL FBO Window Resize event is ignored if the resolution didn't actually change The HUD bump magnitude is a lot lower when damage taken is low --- src/game/CAbstractPlayer.cpp | 17 +++++++---------- src/game/CAvaraApp.cpp | 5 +++-- src/render/AbstractRenderer.h | 7 +------ src/render/ModernOpenGLRenderer.cpp | 19 ++++++++++++------- src/render/ModernOpenGLRenderer.h | 4 ++-- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/game/CAbstractPlayer.cpp b/src/game/CAbstractPlayer.cpp index b433a0f7..831a53c5 100644 --- a/src/game/CAbstractPlayer.cpp +++ b/src/game/CAbstractPlayer.cpp @@ -562,7 +562,7 @@ void CAbstractPlayer::LoadDashboardParts() { dashboardSpinSpeed = ToFixed(100); dashboardSpinHeading = 0; - int layout = itsGame->itsApp->Get(kHUDPreset); + layout = itsGame->itsApp->Get(kHUDPreset); //float alpha = itsGame->itsApp->Get(kHUDAlpha); //Fixed hudAlpha = FIX1 * alpha; @@ -718,21 +718,22 @@ void CAbstractPlayer::RenderDashboard() { // Lastly set relativeImpulse based on the impact location of the hit to bump the HUD Fixed hitAngle = FOneArcTan2(dSpeed[2], dSpeed[0]); Fixed angleDiff = hitAngle - viewYaw; + float magnitude = ToFloat(VectorLength(3, dSpeed)); if (angleDiff > 0) { // Hit from the right side - relativeImpulse[0] = FIX(1.0); + relativeImpulse[0] = FIX(1.0*magnitude); } else if (angleDiff < 0) { // Hit from the left side - relativeImpulse[0] = FIX(-1.0); + relativeImpulse[0] = FIX(-1.0*magnitude); } if (dSpeed[1] > FIX(.5)) { // Hit from the top - relativeImpulse[1] = FIX(1.0); + relativeImpulse[1] = FIX(1.0*magnitude); } else if (dSpeed[1] < FIX(-.5)) { // Hit from the bottom - relativeImpulse[1] = FIX(-1.0); + relativeImpulse[1] = FIX(-1.0*magnitude); } pidReset(&pMotionX); @@ -924,11 +925,7 @@ void CAbstractPlayer::DashboardPosition(CScaledBSP *part, bool autoRot, float x, // X/Y Coordinates on the screen are roughly described as a percentage of the screen away from the bottom and the left // (-1.0, -1.0) is the bottom left of the screen // (1.0, 1.0) is the top right of the screen - - - // TODO: Adjust these until it looks good, then go multiply - // all the DashboardPosition parameters by these numbers, and - // then delete these + float scale_x = 11.12; float scale_y = 8.23; Fixed hud_dist = (FIX3(6000) * 25)/8; diff --git a/src/game/CAvaraApp.cpp b/src/game/CAvaraApp.cpp index f1804014..d2965776 100755 --- a/src/game/CAvaraApp.cpp +++ b/src/game/CAvaraApp.cpp @@ -200,8 +200,9 @@ void CAvaraAppImpl::RenderContents() { } void CAvaraAppImpl::WindowResized(int width, int height) { - gRenderer->UpdateViewRect(width, height, mPixelRatio); - gRenderer->ApplyFrameBuffer(); + // Only update if the resolution is actually changing + if (gRenderer->viewParams->viewPixelDimensions.h != width || gRenderer->viewParams->viewPixelDimensions.v != height) + gRenderer->UpdateViewRect(width, height, mPixelRatio); //performLayout(); } diff --git a/src/render/AbstractRenderer.h b/src/render/AbstractRenderer.h index 89262b8c..97d9ced1 100644 --- a/src/render/AbstractRenderer.h +++ b/src/render/AbstractRenderer.h @@ -39,11 +39,6 @@ class AbstractRenderer { */ virtual void ApplyProjection() = 0; - /** - * Update the frame buffer with the currently configured resolution and FOV. - */ - virtual void ApplyFrameBuffer() = 0; - /** * Reset the renderer's state back to its defaults. */ @@ -108,7 +103,7 @@ class AbstractRenderer { * @param height The height in pixels. * @param pixelRatio The pixel ratio. */ - void UpdateViewRect(int width, int height, float pixelRatio); + virtual void UpdateViewRect(int width, int height, float pixelRatio) = 0; protected: float fov = 50.0f; }; diff --git a/src/render/ModernOpenGLRenderer.cpp b/src/render/ModernOpenGLRenderer.cpp index a4b4a68d..488eacb1 100644 --- a/src/render/ModernOpenGLRenderer.cpp +++ b/src/render/ModernOpenGLRenderer.cpp @@ -177,9 +177,6 @@ ModernOpenGLRenderer::ModernOpenGLRenderer(SDL_Window *window) : AbstractRendere glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); - MakeFramebuffer(0, w, h); - MakeFramebuffer(1, w, h); - // Configure alpha blending. glEnable(GL_BLEND); glBlendFuncSeparate(GL_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE); @@ -275,12 +272,15 @@ void ModernOpenGLRenderer::ApplyProjection() glCheckErrors(); } -void ModernOpenGLRenderer::ApplyFrameBuffer() +void ModernOpenGLRenderer::UpdateViewRect(int width, int height, float pixelRatio) { + AbstractRenderer::UpdateViewRect(width, height, pixelRatio); + GLsizei w, h; SDL_GL_GetDrawableSize(window, &w, &h); - MakeFramebuffer(0, w, h); - MakeFramebuffer(1, w, h); + + AdjustFramebuffer(0, w, h); + AdjustFramebuffer(1, w, h); } void ModernOpenGLRenderer::LevelReset() @@ -575,8 +575,13 @@ std::unique_ptr ModernOpenGLRenderer::LoadShader(const std::string return std::make_unique(*vertPath, *fragPath); } -void ModernOpenGLRenderer::MakeFramebuffer(short index, GLsizei width, GLsizei height) +void ModernOpenGLRenderer::AdjustFramebuffer(short index, GLsizei width, GLsizei height) { + // Remove previous bound objects + glDeleteTextures(1, &texture[index]); + glDeleteFramebuffers(1, &fbo[index]); + glDeleteRenderbuffers(1, &rbo[index]); + // Create a framebuffer, texture, and renderbuffer for the HUD. glGenFramebuffers(1, &fbo[index]); glBindFramebuffer(GL_FRAMEBUFFER, fbo[index]); diff --git a/src/render/ModernOpenGLRenderer.h b/src/render/ModernOpenGLRenderer.h index ca813916..42470304 100644 --- a/src/render/ModernOpenGLRenderer.h +++ b/src/render/ModernOpenGLRenderer.h @@ -19,7 +19,6 @@ class ModernOpenGLRenderer final: public AbstractRenderer { virtual void AddPart(CBSPPart *part) override; virtual void ApplyLights() override; virtual void ApplyProjection() override; - virtual void ApplyFrameBuffer() override; virtual void LevelReset() override; virtual std::unique_ptr NewVertexDataInstance() override; virtual void OverheadPoint(Fixed *pt, Fixed *extent) override; @@ -27,6 +26,7 @@ class ModernOpenGLRenderer final: public AbstractRenderer { virtual void RemoveHUDPart(CBSPPart *part) override; virtual void RemovePart(CBSPPart *part) override; virtual void RenderFrame() override; + virtual void UpdateViewRect(int width, int height, float pixelRatio) override; private: SDL_Window *window; @@ -55,6 +55,6 @@ class ModernOpenGLRenderer final: public AbstractRenderer { void Draw(OpenGLShader &shader, const CBSPPart &part, float defaultAmbient, bool useAlphaBuffer = false); void IgnoreDirectionalLights(OpenGLShader &shader, bool yn); std::unique_ptr LoadShader(const std::string &vertFile, const std::string &fragFile); - void MakeFramebuffer(short index, GLsizei width, GLsizei height); + void AdjustFramebuffer(short index, GLsizei width, GLsizei height); void SetTransforms(const CBSPPart &part); }; From b8b233a4d78cc837f2be612168f8d8b1a084ec14 Mon Sep 17 00:00:00 2001 From: Ymihere03 Date: Tue, 13 Aug 2024 01:12:39 -0600 Subject: [PATCH 4/5] Fix test class --- src/render/NullRenderer.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/render/NullRenderer.h b/src/render/NullRenderer.h index a73e7b81..4d5cb23f 100644 --- a/src/render/NullRenderer.h +++ b/src/render/NullRenderer.h @@ -14,7 +14,6 @@ class NullRenderer final: public AbstractRenderer { virtual void AddPart(CBSPPart *part) override {}; virtual void ApplyLights() override {}; virtual void ApplyProjection() override {}; - virtual void ApplyFrameBuffer() override {}; virtual std::unique_ptr NewVertexDataInstance() override { return nullptr; }; From c11e5f518c3e84a49fadf0eab74e72fdfb0a30f4 Mon Sep 17 00:00:00 2001 From: Ymihere03 Date: Tue, 13 Aug 2024 01:18:42 -0600 Subject: [PATCH 5/5] Fix test class --- src/render/NullRenderer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/render/NullRenderer.h b/src/render/NullRenderer.h index 4d5cb23f..21f5bea5 100644 --- a/src/render/NullRenderer.h +++ b/src/render/NullRenderer.h @@ -32,4 +32,5 @@ class NullRenderer final: public AbstractRenderer { virtual void RemoveHUDPart(CBSPPart *part) override {}; virtual void RemovePart(CBSPPart *part) override {}; virtual void RenderFrame() override {}; + virtual void UpdateViewRect(int width, int height, float pixelRatio) override {}; };