From 16200bbeac6c7adae465348da67c747b8cdfe53a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Sat, 3 Jul 2021 13:21:23 +0200 Subject: [PATCH] Removed all dependency on interface rendering mode --- src/graphics/core/device.h | 3 +++ src/graphics/engine/engine.cpp | 27 +++++---------------------- src/graphics/engine/text.cpp | 1 - src/graphics/opengl/gl33device.cpp | 20 +++++++++++++++++++- src/graphics/opengl/gl33device.h | 2 ++ src/graphics/opengl/gl33renderers.cpp | 12 +++++------- src/graphics/opengl/gl33renderers.h | 6 +++++- src/ui/controls/button.cpp | 2 -- src/ui/controls/check.cpp | 2 -- src/ui/controls/color.cpp | 2 -- src/ui/controls/control.cpp | 10 ---------- src/ui/controls/edit.cpp | 2 -- src/ui/controls/group.cpp | 2 -- src/ui/controls/scroll.cpp | 2 -- src/ui/controls/shortcut.cpp | 2 -- src/ui/controls/slider.cpp | 4 ---- src/ui/controls/window.cpp | 8 -------- 17 files changed, 39 insertions(+), 68 deletions(-) diff --git a/src/graphics/core/device.h b/src/graphics/core/device.h index 7047593c..7c52b6ff 100644 --- a/src/graphics/core/device.h +++ b/src/graphics/core/device.h @@ -378,6 +378,9 @@ public: //! Returns UI renderer virtual CUIRenderer* GetUIRenderer() = 0; + //! Restores device rendering mode + virtual void Restore() = 0; + //! Sets the transform matrix of given type virtual void SetTransform(TransformType type, const Math::Matrix &matrix) = 0; diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index a085aba6..ff63a16e 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -359,7 +359,6 @@ bool CEngine::Create() auto renderer = m_device->GetUIRenderer(); renderer->SetProjection(0.0f, 1.0f, 0.0f, 1.0f); - m_device->SetRenderMode(RENDER_MODE_INTERFACE); Math::LoadOrthoProjectionMatrix(m_matProjInterface, 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); @@ -3651,8 +3650,6 @@ void CEngine::DrawCaptured3DScene() renderer->SetTexture(m_capturedWorldTexture); renderer->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, 4, vertices); - - m_device->SetRenderMode(RENDER_MODE_INTERFACE); } void CEngine::RenderDebugSphere(const Math::Sphere& sphere, const Math::Matrix& transform, const Gfx::Color& color) @@ -4114,7 +4111,7 @@ void CEngine::DrawObject(const EngineBaseObjDataTier& p4) void CEngine::DrawInterface() { - m_device->SetRenderMode(RENDER_MODE_INTERFACE); + m_device->SetRenderMode(RENDER_MODE_NORMAL); m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); m_device->SetRenderState(RENDER_STATE_LIGHTING, false); @@ -4219,7 +4216,7 @@ void CEngine::DrawInterface() m_device->SetRenderState(RENDER_STATE_LIGHTING, false); m_device->SetRenderState(RENDER_STATE_FOG, false); - m_device->SetRenderMode(RENDER_MODE_INTERFACE); + m_device->SetRenderMode(RENDER_MODE_NORMAL); SetInterfaceCoordinates(); } @@ -4750,7 +4747,7 @@ void CEngine::DrawShadowSpots() void CEngine::DrawBackground() { - m_device->SetRenderMode(RENDER_MODE_INTERFACE); + m_device->SetRenderMode(RENDER_MODE_NORMAL); if (m_cloud->GetLevel() != 0.0f) // clouds ? { @@ -4879,8 +4876,6 @@ void CEngine::DrawBackgroundImage() m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, vertices, 4); AddStatisticTriangle(2); - - m_device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } void CEngine::DrawPlanet() @@ -4927,7 +4922,7 @@ void CEngine::DrawForegroundImage() SetTexture(m_foregroundTex); SetState(ENG_RSTATE_CLAMP | ENG_RSTATE_TTEXTURE_BLACK); - m_device->SetRenderMode(RENDER_MODE_INTERFACE); + m_device->SetRenderMode(RENDER_MODE_NORMAL); m_device->SetTransform(TRANSFORM_VIEW, m_matViewInterface); m_device->SetTransform(TRANSFORM_PROJECTION, m_matProjInterface); @@ -4935,8 +4930,6 @@ void CEngine::DrawForegroundImage() m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, vertex, 4); AddStatisticTriangle(2); - - m_device->SetRenderMode(RENDER_MODE_NORMAL); } void CEngine::DrawOverColor() @@ -4955,7 +4948,7 @@ void CEngine::DrawOverColor() Color(0.0f, 0.0f, 0.0f, 0.0f) }; - m_device->SetRenderMode(RENDER_MODE_INTERFACE); + m_device->SetRenderMode(RENDER_MODE_NORMAL); SetState(m_overMode); @@ -5136,8 +5129,6 @@ void CEngine::DrawMouseSprite(Math::IntPoint pos, Math::IntPoint size, int icon) m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, vertex, 4); AddStatisticTriangle(2); - - m_device->SetRenderMode(RENDER_MODE_INTERFACE); } void CEngine::DrawStats() @@ -5241,8 +5232,6 @@ void CEngine::DrawStats() std::stringstream str; str << std::fixed << std::setprecision(2) << m_statisticPos.x << "; " << m_statisticPos.z; drawStatsLine( "Position", str.str(), ""); - - m_device->SetRenderMode(RENDER_MODE_INTERFACE); } void CEngine::DrawTimer() @@ -5251,8 +5240,6 @@ void CEngine::DrawTimer() Math::Point pos(0.98f, 0.98f-m_text->GetAscent(FONT_COMMON, 15.0f)); m_text->DrawText(m_timerText, FONT_COMMON, 15.0f, pos, 1.0f, TEXT_ALIGN_RIGHT, 0, Color(1.0f, 1.0f, 1.0f, 1.0f)); - - m_device->SetRenderMode(RENDER_MODE_INTERFACE); } void CEngine::AddBaseObjTriangles(int baseObjRank, const std::vector& triangles) @@ -5540,8 +5527,6 @@ void CEngine::SetInterfaceCoordinates() auto renderer = m_device->GetUIRenderer(); renderer->SetProjection(0.0f, 1.0f, 0.0f, 1.0f); - - m_device->SetRenderMode(RENDER_MODE_INTERFACE); } void CEngine::EnablePauseBlur() @@ -5575,8 +5560,6 @@ void CEngine::SetWindowCoordinates() auto renderer = m_device->GetUIRenderer(); renderer->SetProjection(0.0f, m_size.x, m_size.y, 0.0f); - - m_device->SetRenderMode(RENDER_MODE_INTERFACE); } void CEngine::SetUITexture(const std::string& name) diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index b8fad690..672a127d 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -157,7 +157,6 @@ public: } m_engine.GetDevice()->GetUIRenderer()->Flush(); - m_engine.GetDevice()->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); //const Vertex* vertices = m_quads.front().vertices; //m_engine.GetDevice()->DrawPrimitives(PRIMITIVE_TRIANGLE_STRIP, vertices, m_firsts.data(), diff --git a/src/graphics/opengl/gl33device.cpp b/src/graphics/opengl/gl33device.cpp index 82f79eb1..9c22fd3b 100644 --- a/src/graphics/opengl/gl33device.cpp +++ b/src/graphics/opengl/gl33device.cpp @@ -514,7 +514,7 @@ bool CGL33Device::Create() glUniform1f(uni.alphaReference, 1.0f); } - m_uiRenderer = std::make_unique(); + m_uiRenderer = std::make_unique(this); SetRenderMode(RENDER_MODE_NORMAL); @@ -609,6 +609,8 @@ void CGL33Device::BeginScene() void CGL33Device::EndScene() { + m_uiRenderer->Flush(); + #ifdef DEV_BUILD CheckGLErrors(); #endif @@ -655,6 +657,22 @@ CUIRenderer* CGL33Device::GetUIRenderer() return m_uiRenderer.get(); } +void CGL33Device::Restore() +{ + switch (m_mode) + { + case 0: + glUseProgram(m_normalProgram); + break; + case 1: + glUseProgram(m_interfaceProgram); + break; + case 2: + glUseProgram(m_shadowProgram); + break; + } +} + void CGL33Device::SetTransform(TransformType type, const Math::Matrix &matrix) { if (type == TRANSFORM_WORLD) diff --git a/src/graphics/opengl/gl33device.h b/src/graphics/opengl/gl33device.h index bbd05a01..209a3048 100644 --- a/src/graphics/opengl/gl33device.h +++ b/src/graphics/opengl/gl33device.h @@ -94,6 +94,8 @@ public: void SetRenderMode(RenderMode mode) override; CUIRenderer* GetUIRenderer() override; + void Restore() override; + void SetTransform(TransformType type, const Math::Matrix &matrix) override; void SetMaterial(const Material &material) override; diff --git a/src/graphics/opengl/gl33renderers.cpp b/src/graphics/opengl/gl33renderers.cpp index f61bbde9..7bfc08cb 100644 --- a/src/graphics/opengl/gl33renderers.cpp +++ b/src/graphics/opengl/gl33renderers.cpp @@ -1,5 +1,6 @@ #include "graphics/opengl/gl33renderers.h" +#include "graphics/opengl/gl33device.h" #include "graphics/opengl/glutil.h" #include "graphics/core/vertex.h" @@ -13,7 +14,8 @@ namespace Gfx { -CGL33UIRenderer::CGL33UIRenderer() +CGL33UIRenderer::CGL33UIRenderer(CGL33Device* device) + : m_device(device) { GLint shaders[2]; @@ -95,7 +97,7 @@ void CGL33UIRenderer::SetProjection(float left, float right, float bottom, float glUniformMatrix4fv(m_projectionMatrix, 1, GL_FALSE, glm::value_ptr(matrix)); - glUseProgram(0); + m_device->Restore(); } void CGL33UIRenderer::SetTexture(const Texture& texture) @@ -166,9 +168,6 @@ void CGL33UIRenderer::Flush() // Draw primitives by grouping by type for (size_t i = 0; i < m_types.size(); i++) { - //glDrawArrays(m_types[i], m_firsts[i], m_counts[i]); - - //* size_t count = 1; for (; i + count < m_types.size(); count++) @@ -180,7 +179,6 @@ void CGL33UIRenderer::Flush() glMultiDrawArrays(m_types[i], &m_firsts[i], &m_counts[i], count); i += count; - // */ } // Clear buffers @@ -189,7 +187,7 @@ void CGL33UIRenderer::Flush() m_firsts.clear(); m_counts.clear(); - glUseProgram(0); + m_device->Restore(); } } // namespace Gfx diff --git a/src/graphics/opengl/gl33renderers.h b/src/graphics/opengl/gl33renderers.h index 2ab940db..bdd26c89 100644 --- a/src/graphics/opengl/gl33renderers.h +++ b/src/graphics/opengl/gl33renderers.h @@ -35,10 +35,12 @@ namespace Gfx { +class CGL33Device; + class CGL33UIRenderer : public CUIRenderer { public: - CGL33UIRenderer(); + CGL33UIRenderer(CGL33Device* device); virtual ~CGL33UIRenderer(); virtual void SetProjection(float left, float right, float bottom, float top) override; @@ -49,6 +51,8 @@ public: virtual void Flush() override; private: + CGL33Device* const m_device; + // location of uni_ProjectionMatrix uniform GLint m_projectionMatrix = -1; diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp index 52f9571f..21bfce43 100644 --- a/src/ui/controls/button.cpp +++ b/src/ui/controls/button.cpp @@ -202,8 +202,6 @@ void CButton::Draw() pos.x = m_pos.x + m_dim.x - 5.0f / 640.0f - 3.0f / 640.0f; DrawIcon(pos, dim, uv1, uv2, 0.0f); } - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } diff --git a/src/ui/controls/check.cpp b/src/ui/controls/check.cpp index 4d6afd31..d7958d81 100644 --- a/src/ui/controls/check.cpp +++ b/src/ui/controls/check.cpp @@ -149,8 +149,6 @@ void CCheck::Draw() } } - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); - m_dim = iDim; // Draw the name. diff --git a/src/ui/controls/color.cpp b/src/ui/controls/color.cpp index 05fd4b07..d5ab3846 100644 --- a/src/ui/controls/color.cpp +++ b/src/ui/controls/color.cpp @@ -160,8 +160,6 @@ void CColor::Draw() device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, vertex, 4); m_engine->AddStatisticTriangle(2); - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } void CColor::SetRepeat(bool bRepeat) diff --git a/src/ui/controls/control.cpp b/src/ui/controls/control.cpp index fadf3a91..ba806397 100644 --- a/src/ui/controls/control.cpp +++ b/src/ui/controls/control.cpp @@ -556,8 +556,6 @@ void CControl::Draw() m_engine->GetText()->DrawText(m_name, m_fontType, m_fontSize, pos, m_dim.x, m_textAlign, 0); } } - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } // Draw the vertex array. @@ -666,8 +664,6 @@ void CControl::DrawIcon(Math::Point pos, Math::Point dim, Math::Point uv1, Math: m_engine->AddStatisticTriangle(6); } } - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } // Draws a rectangular icon made up of 9 pieces. @@ -735,8 +731,6 @@ void CControl::DrawIcon(Math::Point pos, Math::Point dim, Math::Point uv1, Math: device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, vertices, 8); m_engine->AddStatisticTriangle(6); - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } // Draw round the hatch of a button. @@ -782,8 +776,6 @@ void CControl::DrawWarning(Math::Point pos, Math::Point dim) pos.x += dim.x; DrawIcon(pos, dim, uv1, uv2); } - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } // Draw the shade under a button. @@ -818,8 +810,6 @@ void CControl::DrawShadow(Math::Point pos, Math::Point dim, float deep) corner.y = 10.0f / 480.0f; DrawIcon(pos, dim, uv1, uv2, corner, 6.0f / 256.0f); - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } diff --git a/src/ui/controls/edit.cpp b/src/ui/controls/edit.cpp index 6c132079..1fbc3f7e 100644 --- a/src/ui/controls/edit.cpp +++ b/src/ui/controls/edit.cpp @@ -1155,8 +1155,6 @@ void CEdit::Draw() { m_scroll->Draw(); } - - m_engine->GetDevice()->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } // Draw an image part. diff --git a/src/ui/controls/group.cpp b/src/ui/controls/group.cpp index f1b864c4..43d51783 100644 --- a/src/ui/controls/group.cpp +++ b/src/ui/controls/group.cpp @@ -646,8 +646,6 @@ void CGroup::Draw() uv2.y -= dp; DrawIcon(pos, dim, uv1, uv2); } - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } diff --git a/src/ui/controls/scroll.cpp b/src/ui/controls/scroll.cpp index 2bb42f47..0bd26f55 100644 --- a/src/ui/controls/scroll.cpp +++ b/src/ui/controls/scroll.cpp @@ -354,8 +354,6 @@ void CScroll::Draw() { m_buttonDown->Draw(); } - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } // Draws a rectangle. diff --git a/src/ui/controls/shortcut.cpp b/src/ui/controls/shortcut.cpp index eb2c9d90..88840ab5 100644 --- a/src/ui/controls/shortcut.cpp +++ b/src/ui/controls/shortcut.cpp @@ -216,8 +216,6 @@ void CShortcut::Draw() DrawIcon(m_pos, m_dim, uv1, uv2); } - - m_engine->GetDevice()->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } // Draw the vertex array. diff --git a/src/ui/controls/slider.cpp b/src/ui/controls/slider.cpp index 69ea8c6b..24d77531 100644 --- a/src/ui/controls/slider.cpp +++ b/src/ui/controls/slider.cpp @@ -475,8 +475,6 @@ void CSlider::Draw() m_engine->GetText()->DrawText(text, m_fontType, m_fontSize, pos, dim.x, Gfx::TEXT_ALIGN_RIGHT, 0); } } - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } std::string CSlider::GetLabel() @@ -543,8 +541,6 @@ void CSlider::DrawVertex(Math::Point pos, Math::Point dim, int icon) uv2.y -= dp; DrawIcon(pos, dim, uv1, uv2, corner, ex); - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } diff --git a/src/ui/controls/window.cpp b/src/ui/controls/window.cpp index 2d0f6631..6f33ba68 100644 --- a/src/ui/controls/window.cpp +++ b/src/ui/controls/window.cpp @@ -866,8 +866,6 @@ void CWindow::Draw() DrawHach(pos, dim); // right hatch } - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); - pos.x = m_pos.x+width/2.0f; pos.y = m_pos.y+m_dim.y-0.01f-h*1.10f; m_engine->GetText()->DrawText(m_name, m_fontType, m_fontSize, pos, width, Gfx::TEXT_ALIGN_CENTER, 0); @@ -892,8 +890,6 @@ void CWindow::Draw() { control->Draw(); } - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } // Draws a rectangle. @@ -1266,8 +1262,6 @@ void CWindow::DrawVertex(Math::Point pos, Math::Point dim, int icon) corner.y = 6.0f/480.0f; DrawIcon(pos, dim, uv1, uv2, corner, 5.0f/256.0f); } - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } // Draws hatching. @@ -1312,8 +1306,6 @@ void CWindow::DrawHach(Math::Point pos, Math::Point dim) ppos.x += ddim.x; } while ( !bStop ); - - device->SetRenderMode(Gfx::RENDER_MODE_INTERFACE); } void CWindow::SetFocus(CControl* focusControl)