diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 366f36ef..3a23c120 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -94,6 +94,7 @@ add_library(colobotbase STATIC graphics/core/light.h graphics/core/material.h graphics/core/texture.h + graphics/core/transparency.h graphics/core/type.cpp graphics/core/type.h graphics/core/renderers.h diff --git a/src/graphics/core/device.h b/src/graphics/core/device.h index 750babae..5bcbe5d5 100644 --- a/src/graphics/core/device.h +++ b/src/graphics/core/device.h @@ -26,6 +26,7 @@ #include "graphics/core/color.h" #include "graphics/core/texture.h" +#include "graphics/core/transparency.h" #include "graphics/core/vertex.h" #include @@ -125,41 +126,6 @@ struct DeviceCapabilities int maxSamples = 1; }; -/** - * \enum TextureUnit - * \brief Texture unit values for binding textures - * - * These enums should be used for indexing textures instead of raw integers. - */ -enum TextureUnit -{ - TEXTURE_PRIMARY = 0, - TEXTURE_SECONDARY = 1, -}; - -/** - * \enum TransformType - * \brief Type of transformation in rendering pipeline - */ -enum TransformType -{ - TRANSFORM_WORLD, - TRANSFORM_VIEW, - TRANSFORM_PROJECTION, -}; - -/** - * \enum RenderState - * \brief Render states that can be enabled/disabled - */ -enum RenderState -{ - RENDER_STATE_BLENDING, - RENDER_STATE_DEPTH_TEST, - RENDER_STATE_DEPTH_WRITE, - RENDER_STATE_CULLING, -}; - /** * \enum CompFunc * \brief Type of function used to compare values @@ -177,45 +143,15 @@ enum CompFunc }; /** - * \enum BlendFunc - * \brief Type of blending function + * \enum CullFace + * \brief Specifies which faces to cull while rendering polygons */ -enum BlendFunc +enum class CullFace : unsigned char { - BLEND_ZERO, - BLEND_ONE, - BLEND_SRC_COLOR, - BLEND_INV_SRC_COLOR, - BLEND_DST_COLOR, - BLEND_INV_DST_COLOR, - BLEND_SRC_ALPHA, - BLEND_INV_SRC_ALPHA, - BLEND_DST_ALPHA, - BLEND_INV_DST_ALPHA, - BLEND_SRC_ALPHA_SATURATE -}; - -/** - * \enum FogMode - * \brief Type of fog calculation function - */ -enum FogMode -{ - FOG_LINEAR, - FOG_EXP, - FOG_EXP2 -}; - -/** - * \enum CullMode - * \brief Culling mode for polygons - */ -enum CullMode -{ - //! Cull clockwise faces - CULL_CW, - //! Cull counter-clockwise faces - CULL_CCW + NONE, + BACK, + FRONT, + BOTH, }; /** @@ -455,8 +391,16 @@ public: //! Changes rendering viewport virtual void SetViewport(int x, int y, int width, int height) = 0; - //! Enables/disables the given render state - virtual void SetRenderState(RenderState state, bool enabled) = 0; + //! Sets depth test + virtual void SetDepthTest(bool enabled) = 0; + //! Sets depth mask + virtual void SetDepthMask(bool enabled) = 0; + + //! Sets which faces to cull + virtual void SetCullFace(CullFace mode) = 0; + + //! Sets transparency mode + virtual void SetTransparency(TransparencyMode mode) = 0; //! Sets the color mask virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) = 0; diff --git a/src/graphics/core/renderers.h b/src/graphics/core/renderers.h index f893f064..7f46d5c6 100644 --- a/src/graphics/core/renderers.h +++ b/src/graphics/core/renderers.h @@ -24,6 +24,7 @@ #pragma once +#include "graphics/core/transparency.h" #include "graphics/core/vertex.h" #include @@ -33,17 +34,10 @@ namespace Gfx { class CVertexBuffer; +enum class CullFace : unsigned char; enum class PrimitiveType : unsigned char; struct Texture; -enum class TransparencyMode : unsigned char -{ - NONE, - ALPHA, - BLACK, - WHITE, -}; - struct ShadowParam { glm::mat4 matrix; @@ -171,8 +165,8 @@ public: virtual void SetDepthTest(bool enabled) = 0; //! Sets depth mask virtual void SetDepthMask(bool enabled) = 0; - //! Sets cull mode - virtual void SetCullMode(bool enabled) = 0; + //! Sets cull face mode + virtual void SetCullFace(CullFace mode) = 0; //! Sets transparency mode virtual void SetTransparency(TransparencyMode mode) = 0; diff --git a/src/graphics/core/transparency.h b/src/graphics/core/transparency.h new file mode 100644 index 00000000..3e5688b3 --- /dev/null +++ b/src/graphics/core/transparency.h @@ -0,0 +1,40 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2022, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + + /** + * \file graphics/core/transparency.h + * \brief Structures and functions related to transparency + */ + +#pragma once + +// Graphics module namespace +namespace Gfx +{ + +//! Transparency mode +enum class TransparencyMode : unsigned char +{ + NONE, + ALPHA, + BLACK, + WHITE, +}; + +} diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index 7cdc9343..95c88857 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -1560,12 +1560,12 @@ void CEngine::UpdateStaticBuffer(EngineBaseObjDataTier& p4) if (p4.buffer == nullptr) { - p4.buffer = m_device->CreateVertexBuffer(type, &p4.vertices[0], p4.vertices.size()); + p4.buffer = m_device->CreateVertexBuffer(type, p4.vertices.data(), p4.vertices.size()); } else { p4.buffer->SetType(type); - p4.buffer->SetData(&p4.vertices[0], 0, p4.vertices.size()); + p4.buffer->SetData(p4.vertices.data(), 0, p4.vertices.size()); p4.buffer->Update(); } @@ -2883,7 +2883,7 @@ void CEngine::Render() m_device->SetClearColor(color); // Begin the scene - m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, true); + m_device->SetDepthMask(true); m_device->BeginScene(); @@ -2935,7 +2935,7 @@ void CEngine::Draw3DScene() } } - m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); + m_device->SetDepthTest(false); UpdateGroundSpotTextures(); @@ -3096,7 +3096,7 @@ void CEngine::Draw3DScene() objectRenderer->SetModelMatrix(m_objects[objRank].transform); - m_lightMan->UpdateDeviceLights(m_objects[objRank].type); + //m_lightMan->UpdateDeviceLights(m_objects[objRank].type); for (int l2 = 0; l2 < static_cast( p1.next.size() ); l2++) { @@ -3134,7 +3134,7 @@ void CEngine::Draw3DScene() float dirty = ((p3.state & ENG_RSTATE_DUAL_BLACK) && m_dirty) ? 1.0 : 0.0; objectRenderer->SetDirty(dirty); objectRenderer->SetColor({ 1.0f, 1.0f, 1.0f, 1.0f }); - objectRenderer->SetCullMode((p3.state& ENG_RSTATE_2FACE) == 0); + objectRenderer->SetCullFace((p3.state& ENG_RSTATE_2FACE) > 0 ? CullFace::NONE : CullFace::BACK); objectRenderer->SetUVTransform(p3.uvOffset, p3.uvScale); objectRenderer->DrawObject(p3.buffer); } @@ -3148,7 +3148,7 @@ void CEngine::Draw3DScene() objectRenderer->SetDepthMask(false); objectRenderer->SetTransparency(TransparencyMode::BLACK); objectRenderer->SetAlphaScissor(0.0f); - objectRenderer->SetCullMode(false); + objectRenderer->SetCullFace(CullFace::NONE); // Draw transparent objects @@ -3387,20 +3387,19 @@ void CEngine::Capture3DScene() void CEngine::DrawCaptured3DScene() { - m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); + m_device->SetDepthTest(false); - Vertex2D vertices[4]; + auto renderer = m_device->GetUIRenderer(); + renderer->SetTexture(m_capturedWorldTexture); + renderer->SetTransparency(TransparencyMode::NONE); + auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4); vertices[0] = { { 0.0f, 0.0f }, { 0.0f, 0.0f } }; vertices[1] = { { 1.0f, 0.0f }, { 1.0f, 0.0f } }; vertices[2] = { { 0.0f, 1.0f }, { 0.0f, 1.0f } }; vertices[3] = { { 1.0f, 1.0f }, { 1.0f, 1.0f } }; - auto renderer = m_device->GetUIRenderer(); - - renderer->SetTexture(m_capturedWorldTexture); - renderer->SetTransparency(TransparencyMode::NONE); - renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, vertices); + renderer->EndPrimitive(); } void CEngine::RenderDebugSphere(const Math::Sphere& sphere, const glm::mat4& transform, const Gfx::Color& color) @@ -3690,7 +3689,7 @@ void CEngine::RenderShadowMap() CProfiler::StopPerformanceCounter(PCNT_RENDER_SHADOW_MAP); - m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); + m_device->SetDepthTest(false); } void CEngine::UseMSAA(bool enable) @@ -3728,8 +3727,8 @@ void CEngine::UseMSAA(bool enable) framebuffer->Bind(); } - m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, true); - m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, true); + m_device->SetDepthTest(true); + m_device->SetDepthMask(true); m_device->Clear(); } @@ -3753,7 +3752,8 @@ void CEngine::UseMSAA(bool enable) void CEngine::DrawInterface() { - m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); + m_device->SetDepthTest(false); + m_device->SetTransparency(TransparencyMode::NONE); SetInterfaceCoordinates(); @@ -3804,7 +3804,7 @@ void CEngine::DrawInterface() renderer->SetAlphaScissor(0.0f); renderer->SetShadowParams(0, nullptr); renderer->SetColor({ 1.0f, 1.0f, 1.0f, 1.0f }); - renderer->SetCullMode(true); + renderer->SetCullFace(CullFace::BACK); renderer->SetPrimaryTextureEnabled(true); renderer->SetTriplanarMode(m_triplanarMode); @@ -3873,7 +3873,7 @@ void CEngine::DrawInterface() particleRenderer->End(); - m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); + m_device->SetDepthTest(false); SetInterfaceCoordinates(); } @@ -4202,7 +4202,7 @@ void CEngine::UpdateGroundSpotTextures() void CEngine::DrawShadowSpots() { - m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false); + m_device->SetDepthMask(false); glm::mat4 matrix = glm::mat4(1.0f); //m_device->SetTransform(TRANSFORM_WORLD, matrix); @@ -4391,7 +4391,7 @@ void CEngine::DrawShadowSpots() AddStatisticTriangle(2); } - m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, true); + m_device->SetDepthMask(true); } void CEngine::DrawBackground() @@ -4425,18 +4425,17 @@ void CEngine::DrawBackgroundGradient(const Color& up, const Color& down) { 0, 0, 0, 0 } }; - Vertex2D vertex[4] = - { - { { p1.x, p1.y }, {}, color[1] }, - { { p1.x, p2.y }, {}, color[0] }, - { { p2.x, p1.y }, {}, color[1] }, - { { p2.x, p2.y }, {}, color[0] } - }; - auto renderer = m_device->GetUIRenderer(); renderer->SetTexture(Texture{}); renderer->SetTransparency(TransparencyMode::NONE); - renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, vertex); + auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4); + + vertices[0] = { { p1.x, p1.y }, {}, color[1] }; + vertices[1] = { { p1.x, p2.y }, {}, color[0] }; + vertices[2] = { { p2.x, p1.y }, {}, color[1] }; + vertices[3] = { { p2.x, p2.y }, {}, color[0] }; + + renderer->EndPrimitive(); AddStatisticTriangle(2); } @@ -4506,18 +4505,17 @@ void CEngine::DrawBackgroundImage() v2 -= margin_v; } - Vertex2D vertices[4] = - { - { { p1.x, p1.y }, { u1, v2 } }, - { { p1.x, p2.y }, { u1, v1 } }, - { { p2.x, p1.y }, { u2, v2 } }, - { { p2.x, p2.y }, { u2, v1 } } - }; - auto renderer = m_device->GetUIRenderer(); renderer->SetTexture(m_backgroundTex); renderer->SetTransparency(TransparencyMode::NONE); - renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, vertices); + auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4); + + vertices[0] = { { p1.x, p1.y }, { u1, v2 } }; + vertices[1] = { { p1.x, p2.y }, { u1, v1 } }; + vertices[2] = { { p2.x, p1.y }, { u2, v2 } }; + vertices[3] = { { p2.x, p2.y }, { u2, v1 } }; + + renderer->EndPrimitive(); AddStatisticTriangle(2); } @@ -4557,19 +4555,17 @@ void CEngine::DrawForegroundImage() float v1 = 0.2f; float v2 = 1.0f; - - Vertex2D vertex[4] = - { - { { p1.x, p1.y }, { u1, v2 } }, - { { p1.x, p2.y }, { u1, v1 } }, - { { p2.x, p1.y }, { u2, v2 } }, - { { p2.x, p2.y }, { u2, v1 } } - }; - auto renderer = m_device->GetUIRenderer(); renderer->SetTexture(m_foregroundTex); renderer->SetTransparency(TransparencyMode::BLACK); - renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, vertex); + auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4); + + vertices[0] = { { p1.x, p1.y }, { u1, v2 } }; + vertices[1] = { { p1.x, p2.y }, { u1, v1 } }; + vertices[2] = { { p2.x, p1.y }, { u2, v2 } }; + vertices[3] = { { p2.x, p2.y }, { u2, v1 } }; + + renderer->EndPrimitive(); AddStatisticTriangle(2); } @@ -4594,17 +4590,14 @@ void CEngine::DrawOverColor() auto renderer = m_device->GetUIRenderer(); renderer->SetTexture(Texture{}); renderer->SetTransparency(m_overMode); + auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4); - Vertex2D vertex[4] = - { - { { p1.x, p1.y }, {}, colors[1] }, - { { p1.x, p2.y }, {}, colors[0] }, - { { p2.x, p1.y }, {}, colors[1] }, - { { p2.x, p2.y }, {}, colors[0] } - }; - - renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, vertex); + vertices[0] = { { p1.x, p1.y }, {}, colors[1] }; + vertices[1] = { { p1.x, p2.y }, {}, colors[0] }; + vertices[2] = { { p2.x, p1.y }, {}, colors[1] }; + vertices[3] = { { p2.x, p2.y }, {}, colors[0] }; + renderer->EndPrimitive(); AddStatisticTriangle(2); } @@ -4666,13 +4659,6 @@ void CEngine::DrawHighlight() glm::u8vec4 color(255, 255, 0, 255); // yellow - Vertex2D line[3] = - { - { { 0, 0 }, {}, color }, - { { 0, 0 }, {}, color }, - { { 0, 0 }, {}, color } - }; - auto renderer = m_device->GetUIRenderer(); renderer->SetTransparency(TransparencyMode::NONE); renderer->SetTexture(Texture{}); @@ -4680,25 +4666,29 @@ void CEngine::DrawHighlight() float dx = (p2.x - p1.x) / 5.0f; float dy = (p2.y - p1.y) / 5.0f; - line[0].position = glm::vec3(p1.x, p1.y + dy, 0.0f); - line[1].position = glm::vec3(p1.x, p1.y, 0.0f); - line[2].position = glm::vec3(p1.x + dx, p1.y, 0.0f); - renderer->DrawPrimitive(PrimitiveType::LINE_STRIP, 3, line); + auto line = renderer->BeginPrimitive(PrimitiveType::LINE_STRIP, 3); + line[0] = { { p1.x, p1.y + dy }, {}, color }; + line[1] = { { p1.x, p1.y }, {}, color }; + line[2] = { { p1.x + dx, p1.y }, {}, color }; + renderer->EndPrimitive(); - line[0].position = glm::vec3(p2.x - dx, p1.y, 0.0f); - line[1].position = glm::vec3(p2.x, p1.y, 0.0f); - line[2].position = glm::vec3(p2.x, p1.y + dy, 0.0f); - renderer->DrawPrimitive(PrimitiveType::LINE_STRIP, 3, line); + line = renderer->BeginPrimitive(PrimitiveType::LINE_STRIP, 3); + line[0] = { { p2.x - dx, p1.y }, {}, color }; + line[1] = { { p2.x, p1.y }, {}, color }; + line[2] = { { p2.x, p1.y + dy }, {}, color }; + renderer->EndPrimitive(); - line[0].position = glm::vec3(p2.x, p2.y - dy, 0.0f); - line[1].position = glm::vec3(p2.x, p2.y, 0.0f); - line[2].position = glm::vec3(p2.x - dx, p2.y, 0.0f); - renderer->DrawPrimitive(PrimitiveType::LINE_STRIP, 3, line); + line = renderer->BeginPrimitive(PrimitiveType::LINE_STRIP, 3); + line[0] = { { p2.x, p2.y - dy }, {}, color }; + line[1] = { { p2.x, p2.y }, {}, color }; + line[2] = { { p2.x - dx, p2.y }, {}, color }; + renderer->EndPrimitive(); - line[0].position = glm::vec3(p1.x + dx, p2.y, 0.0f); - line[1].position = glm::vec3(p1.x, p2.y, 0.0f); - line[2].position = glm::vec3(p1.x, p2.y - dy, 0.0f); - renderer->DrawPrimitive(PrimitiveType::LINE_STRIP, 3, line); + line = renderer->BeginPrimitive(PrimitiveType::LINE_STRIP, 3); + line[0] = { { p1.x + dx, p2.y }, {}, color }; + line[1] = { { p1.x, p2.y }, {}, color }; + line[2] = { { p1.x, p2.y - dy }, {}, color }; + renderer->EndPrimitive(); } void CEngine::DrawMouse() @@ -4745,17 +4735,16 @@ void CEngine::DrawMouseSprite(const glm::ivec2& pos, const glm::ivec2& size, int u2 -= dp; v2 -= dp; - Vertex2D vertex[4] = - { - { { p1.x, p2.y }, { u1, v2 } }, - { { p1.x, p1.y }, { u1, v1 } }, - { { p2.x, p2.y }, { u2, v2 } }, - { { p2.x, p1.y }, { u2, v1 } } - }; - auto renderer = m_device->GetUIRenderer(); renderer->SetTransparency(mode); - renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, vertex); + auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4); + + vertices[0] = { { p1.x, p2.y }, { u1, v2 } }; + vertices[1] = { { p1.x, p1.y }, { u1, v1 } }; + vertices[2] = { { p2.x, p2.y }, { u2, v2 } }; + vertices[3] = { { p2.x, p1.y }, { u2, v1 } }; + + renderer->EndPrimitive(); AddStatisticTriangle(2); } @@ -4778,16 +4767,14 @@ void CEngine::DrawStats() glm::vec2 margin = { 5.f / m_size.x, 5.f / m_size.y }; - Vertex2D vertex[4] = - { - { { pos.x - margin.x, pos.y - (TOTAL_LINES + 1) * height - margin.y }, {}, black }, - { { pos.x - margin.x, pos.y + height + margin.y }, {}, black }, - { { pos.x + width + margin.x, pos.y - (TOTAL_LINES + 1) * height - margin.y }, {}, black }, - { { pos.x + width + margin.x, pos.y + height + margin.y }, {}, black } - }; + auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4); - renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, vertex); + vertices[0] = { { pos.x - margin.x, pos.y - (TOTAL_LINES + 1) * height - margin.y }, {}, black }; + vertices[1] = { { pos.x - margin.x, pos.y + height + margin.y }, {}, black }; + vertices[2] = { { pos.x + width + margin.x, pos.y - (TOTAL_LINES + 1) * height - margin.y }, {}, black }; + vertices[3] = { { pos.x + width + margin.x, pos.y + height + margin.y }, {}, black }; + renderer->EndPrimitive(); renderer->SetTransparency(TransparencyMode::ALPHA); auto drawStatsLine = [&](const std::string& name, const std::string& value, const std::string& value2) diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 7a519a1e..3c8fe378 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -1006,16 +1006,15 @@ void CText::DrawHighlight(FontMetaChar hl, const glm::ivec2& pos, const glm::ive auto renderer = m_device->GetUIRenderer(); renderer->SetTexture(Texture{}); + renderer->SetTransparency(TransparencyMode::ALPHA); + auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4); - Vertex2D quad[] = - { - { { p1.x, p2.y }, {}, grad[3] }, - { { p1.x, p1.y }, {}, grad[0] }, - { { p2.x, p2.y }, {}, grad[2] }, - { { p2.x, p1.y }, {}, grad[1] } - }; + vertices[0] = { { p1.x, p2.y }, {}, grad[3] }; + vertices[1] = { { p1.x, p1.y }, {}, grad[0] }; + vertices[2] = { { p2.x, p2.y }, {}, grad[2] }; + vertices[3] = { { p2.x, p1.y }, {}, grad[1] }; - renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, quad); + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(2); } @@ -1049,17 +1048,20 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, glm::iv uv2.x -= dp; uv2.y -= dp; - glm::u8vec4 col = { color.r * 255, color.g * 255, color.b * 255, color.a * 255 }; + Gfx::IntColor col = Gfx::ColorToIntColor(color); - Vertex2D quad[4] = - { - { { p1.x, p2.y }, { uv1.x, uv2.y }, col }, - { { p1.x, p1.y }, { uv1.x, uv1.y }, col }, - { { p2.x, p2.y }, { uv2.x, uv2.y }, col }, - { { p2.x, p1.y }, { uv2.x, uv1.y }, col } - }; + auto renderer = m_device->GetUIRenderer(); + renderer->SetTransparency(TransparencyMode::WHITE); + renderer->SetTexture(Texture{ texID }); + auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4); - m_quadBatch->Add(quad, texID, TransparencyMode::WHITE, color); + vertices[0] = { { p1.x, p2.y }, { uv1.x, uv2.y }, col }; + vertices[1] = { { p1.x, p1.y }, { uv1.x, uv1.y }, col }; + vertices[2] = { { p2.x, p2.y }, { uv2.x, uv2.y }, col }; + vertices[3] = { { p2.x, p1.y }, { uv2.x, uv1.y }, col }; + + //m_quadBatch->Add(quad, texID, TransparencyMode::WHITE, color); + renderer->EndPrimitive(); pos.x += width; } @@ -1088,17 +1090,20 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, glm::iv glm::vec2 texCoord2(static_cast(tex.charPos.x + tex.charSize.x - halfPixelMargin) / FONT_TEXTURE_SIZE.x, static_cast(tex.charPos.y + tex.charSize.y - halfPixelMargin) / FONT_TEXTURE_SIZE.y); - glm::u8vec4 col = { color.r * 255, color.g * 255, color.b * 255, color.a * 255 }; + Gfx::IntColor col = Gfx::ColorToIntColor(color); - Vertex2D quad[4] = - { - { { p1.x, p2.y }, { texCoord1.x, texCoord2.y }, col }, - { { p1.x, p1.y }, { texCoord1.x, texCoord1.y }, col }, - { { p2.x, p2.y }, { texCoord2.x, texCoord2.y }, col }, - { { p2.x, p1.y }, { texCoord2.x, texCoord1.y }, col } - }; + auto renderer = m_device->GetUIRenderer(); + renderer->SetTransparency(TransparencyMode::ALPHA); + renderer->SetTexture(Texture{ tex.id }); + auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4); - m_quadBatch->Add(quad, tex.id, TransparencyMode::ALPHA, color); + vertices[0] = { { p1.x, p2.y }, { texCoord1.x, texCoord2.y }, col }; + vertices[1] = { { p1.x, p1.y }, { texCoord1.x, texCoord1.y }, col }; + vertices[2] = { { p2.x, p2.y }, { texCoord2.x, texCoord2.y }, col }; + vertices[3] = { { p2.x, p1.y }, { texCoord2.x, texCoord1.y }, col }; + + //m_quadBatch->Add(quad, tex.id, TransparencyMode::ALPHA, color); + renderer->EndPrimitive(); pos.x += tex.charSize.x * width; } diff --git a/src/graphics/opengl/gl33device.cpp b/src/graphics/opengl/gl33device.cpp index ed08d386..7f6596d9 100644 --- a/src/graphics/opengl/gl33device.cpp +++ b/src/graphics/opengl/gl33device.cpp @@ -258,6 +258,17 @@ void CGL33Device::ConfigChanged(const DeviceConfig& newConfig) void CGL33Device::BeginScene() { Clear(); + + glDisable(GL_BLEND); + m_transparency = TransparencyMode::NONE; + + glDisable(GL_DEPTH_TEST); + m_depthTest = false; + + glDepthMask(GL_TRUE); + m_depthMask = true; + + glFrontFace(GL_CW); // Colobot issue: faces are reversed } void CGL33Device::EndScene() @@ -513,30 +524,80 @@ void CGL33Device::SetViewport(int x, int y, int width, int height) glViewport(x, y, width, height); } -void CGL33Device::SetRenderState(RenderState state, bool enabled) +void CGL33Device::SetDepthTest(bool enabled) { - return; + if (m_depthTest == enabled) return; - if (state == RENDER_STATE_DEPTH_WRITE) - { - glDepthMask(enabled ? GL_TRUE : GL_FALSE); - return; - } - - GLenum flag = 0; - - switch (state) - { - case RENDER_STATE_BLENDING: flag = GL_BLEND; break; - case RENDER_STATE_DEPTH_TEST: flag = GL_DEPTH_TEST; break; - case RENDER_STATE_CULLING: flag = GL_CULL_FACE; break; - default: assert(false); break; - } + m_depthTest = enabled; if (enabled) - glEnable(flag); + glEnable(GL_DEPTH_TEST); else - glDisable(flag); + glDisable(GL_DEPTH_TEST); +} + +void CGL33Device::SetDepthMask(bool enabled) +{ + if (m_depthMask == enabled) return; + + m_depthMask = enabled; + + glDepthMask(enabled ? GL_TRUE : GL_FALSE); +} + +void CGL33Device::SetCullFace(CullFace mode) +{ + if (m_cullFace == mode) return; + + m_cullFace = mode; + + switch (mode) + { + case CullFace::NONE: + glDisable(GL_CULL_FACE); + break; + case CullFace::BACK: + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + break; + case CullFace::FRONT: + glEnable(GL_CULL_FACE); + glCullFace(GL_FRONT); + break; + case CullFace::BOTH: + glEnable(GL_CULL_FACE); + glCullFace(GL_FRONT_AND_BACK); + break; + } +} + +void CGL33Device::SetTransparency(TransparencyMode mode) +{ + if (m_transparency == mode) return; + + m_transparency = mode; + + switch (mode) + { + case TransparencyMode::NONE: + glDisable(GL_BLEND); + break; + case TransparencyMode::ALPHA: + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glBlendEquation(GL_FUNC_ADD); + break; + case TransparencyMode::BLACK: + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR); + glBlendEquation(GL_FUNC_ADD); + break; + case TransparencyMode::WHITE: + glEnable(GL_BLEND); + glBlendFunc(GL_DST_COLOR, GL_ZERO); + glBlendEquation(GL_FUNC_ADD); + break; + } } void CGL33Device::SetColorMask(bool red, bool green, bool blue, bool alpha) diff --git a/src/graphics/opengl/gl33device.h b/src/graphics/opengl/gl33device.h index 5e8967e3..1db93197 100644 --- a/src/graphics/opengl/gl33device.h +++ b/src/graphics/opengl/gl33device.h @@ -131,7 +131,12 @@ public: void SetViewport(int x, int y, int width, int height) override; - void SetRenderState(RenderState state, bool enabled) override; + void SetDepthTest(bool enabled) override; + void SetDepthMask(bool enabled) override; + + void SetCullFace(CullFace mode) override; + + void SetTransparency(TransparencyMode mode) override; void SetColorMask(bool red, bool green, bool blue, bool alpha) override; @@ -185,6 +190,15 @@ private: std::unique_ptr m_particleRenderer; //! Shadow renderer std::unique_ptr m_shadowRenderer; + + //! Depth test + bool m_depthTest = false; + //! Depth mask + bool m_depthMask = true; + //! Cull face mode + CullFace m_cullFace = CullFace::NONE; + //! Transparency mode + TransparencyMode m_transparency = TransparencyMode::NONE; }; } // namespace Gfx diff --git a/src/graphics/opengl/gl33objectrenderer.cpp b/src/graphics/opengl/gl33objectrenderer.cpp index 8784a4a8..bb8d8c34 100644 --- a/src/graphics/opengl/gl33objectrenderer.cpp +++ b/src/graphics/opengl/gl33objectrenderer.cpp @@ -174,12 +174,10 @@ void CGL33ObjectRenderer::CGL33ObjectRenderer::Begin() m_secondaryTexture = 0; m_shadowMap = 0; - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); - - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - glFrontFace(GL_CW); // Colobot issue: faces are reversed + m_device->SetDepthTest(true); + m_device->SetDepthMask(true); + m_device->SetTransparency(TransparencyMode::NONE); + m_device->SetCullFace(CullFace::BACK); SetUVTransform({ 0.0f, 0.0f }, { 1.0f, 1.0f }); } @@ -198,8 +196,6 @@ void CGL33ObjectRenderer::CGL33ObjectRenderer::End() m_primaryTexture = 0; m_secondaryTexture = 0; m_shadowMap = 0; - - glDepthMask(GL_TRUE); } void CGL33ObjectRenderer::SetProjectionMatrix(const glm::mat4& matrix) @@ -306,52 +302,22 @@ void CGL33ObjectRenderer::SetFog(float min, float max, const glm::vec3& color) void CGL33ObjectRenderer::SetDepthTest(bool enabled) { - if (enabled) - glEnable(GL_DEPTH_TEST); - else - glDisable(GL_DEPTH_TEST); + m_device->SetDepthTest(enabled); } void CGL33ObjectRenderer::SetDepthMask(bool enabled) { - glDepthMask(enabled ? GL_TRUE : GL_FALSE); + m_device->SetDepthMask(enabled); } -void CGL33ObjectRenderer::SetCullMode(bool enabled) +void CGL33ObjectRenderer::SetCullFace(CullFace mode) { - if (enabled) - { - glEnable(GL_CULL_FACE); - } - else - { - glDisable(GL_CULL_FACE); - } + m_device->SetCullFace(mode); } void CGL33ObjectRenderer::SetTransparency(TransparencyMode mode) { - switch (mode) - { - case TransparencyMode::NONE: - glDisable(GL_BLEND); - break; - case TransparencyMode::ALPHA: - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glBlendEquation(GL_FUNC_ADD); - break; - case TransparencyMode::BLACK: - glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR); - glBlendEquation(GL_FUNC_ADD); - break; - case TransparencyMode::WHITE: - glEnable(GL_BLEND); - glBlendFunc(GL_DST_COLOR, GL_ZERO); - glBlendEquation(GL_FUNC_ADD); - break; - } + m_device->SetTransparency(mode); } void CGL33ObjectRenderer::SetUVTransform(const glm::vec2& offset, const glm::vec2& scale) diff --git a/src/graphics/opengl/gl33objectrenderer.h b/src/graphics/opengl/gl33objectrenderer.h index 453f7fdc..580fa245 100644 --- a/src/graphics/opengl/gl33objectrenderer.h +++ b/src/graphics/opengl/gl33objectrenderer.h @@ -80,7 +80,7 @@ public: virtual void SetDepthMask(bool enabled) override; //! Sets cull mode parameters - virtual void SetCullMode(bool enabled) override; + virtual void SetCullFace(CullFace mode) override; //! Sets transparency mode virtual void SetTransparency(TransparencyMode mode) override; diff --git a/src/graphics/opengl/gl33particlerenderer.cpp b/src/graphics/opengl/gl33particlerenderer.cpp index dc0cbec1..d99435b8 100644 --- a/src/graphics/opengl/gl33particlerenderer.cpp +++ b/src/graphics/opengl/gl33particlerenderer.cpp @@ -108,9 +108,9 @@ void CGL33ParticleRenderer::Begin() m_texture = 0; - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_FALSE); - glDisable(GL_CULL_FACE); + m_device->SetDepthTest(true); + m_device->SetDepthMask(false); + m_device->SetCullFace(CullFace::NONE); glUniform4f(m_color, 1.0f, 1.0f, 1.0f, 1.0f); @@ -125,8 +125,6 @@ void CGL33ParticleRenderer::End() m_texture = 0; - glDepthMask(GL_TRUE); - glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); } @@ -170,34 +168,9 @@ void CGL33ParticleRenderer::SetTexture(const Texture& texture) glBindTexture(GL_TEXTURE_2D, texture.id); } - void CGL33ParticleRenderer::SetTransparency(TransparencyMode mode) { - switch (mode) - { - case TransparencyMode::NONE: - glDisable(GL_BLEND); - glDepthMask(GL_TRUE); - break; - case TransparencyMode::ALPHA: - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glBlendEquation(GL_FUNC_ADD); - glDepthMask(GL_TRUE); - break; - case TransparencyMode::BLACK: - glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR); - glBlendEquation(GL_FUNC_ADD); - glDepthMask(GL_FALSE); - break; - case TransparencyMode::WHITE: - glEnable(GL_BLEND); - glBlendFunc(GL_DST_COLOR, GL_ZERO); - glBlendEquation(GL_FUNC_ADD); - glDepthMask(GL_FALSE); - break; - } + m_device->SetTransparency(mode); } void CGL33ParticleRenderer::DrawParticle(PrimitiveType type, int count, const Vertex3D* vertices) diff --git a/src/graphics/opengl/gl33renderers.cpp b/src/graphics/opengl/gl33renderers.cpp index ffb73a0b..c81f1593 100644 --- a/src/graphics/opengl/gl33renderers.cpp +++ b/src/graphics/opengl/gl33renderers.cpp @@ -134,27 +134,7 @@ void CGL33UIRenderer::SetColor(const glm::vec4& color) void CGL33UIRenderer::SetTransparency(TransparencyMode mode) { - switch (mode) - { - case TransparencyMode::NONE: - glDisable(GL_BLEND); - break; - case TransparencyMode::ALPHA: - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glBlendEquation(GL_FUNC_ADD); - break; - case TransparencyMode::BLACK: - glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR); - glBlendEquation(GL_FUNC_ADD); - break; - case TransparencyMode::WHITE: - glEnable(GL_BLEND); - glBlendFunc(GL_DST_COLOR, GL_ZERO); - glBlendEquation(GL_FUNC_ADD); - break; - } + m_device->SetTransparency(mode); } void CGL33UIRenderer::DrawPrimitive(PrimitiveType type, int count, const Vertex2D* vertices) @@ -233,9 +213,10 @@ bool CGL33UIRenderer::EndPrimitive() glUseProgram(m_program); glBindBufferBase(GL_UNIFORM_BUFFER, 0, m_uniformBuffer); - glDisable(GL_DEPTH_TEST); UpdateUniforms(); + + m_device->SetDepthTest(false); glDrawArrays(TranslateGfxPrimitive(m_type), m_offset, m_count); @@ -364,14 +345,11 @@ void CGL33TerrainRenderer::Begin() { glUseProgram(m_program); - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); + m_device->SetDepthTest(true); + m_device->SetDepthMask(true); + m_device->SetCullFace(CullFace::BACK); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - glFrontFace(GL_CW); // Colobot issue: faces are reversed - - glDisable(GL_BLEND); + m_device->SetTransparency(TransparencyMode::NONE); glActiveTexture(GL_TEXTURE10); glBindTexture(GL_TEXTURE_2D, m_whiteTexture); @@ -568,7 +546,7 @@ CGL33ShadowRenderer::~CGL33ShadowRenderer() void CGL33ShadowRenderer::Begin() { glViewport(0, 0, m_width, m_height); - glDepthMask(GL_TRUE); + m_device->SetDepthMask(true); glClear(GL_DEPTH_BUFFER_BIT); glUseProgram(m_program); @@ -578,12 +556,12 @@ void CGL33ShadowRenderer::Begin() glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(2.0f, 8.0f); - glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); + m_device->SetColorMask(false, false, false, false); + m_device->SetDepthTest(true); + m_device->SetDepthMask(true); - glDisable(GL_BLEND); - glDisable(GL_CULL_FACE); + m_device->SetTransparency(TransparencyMode::NONE); + m_device->SetCullFace(CullFace::NONE); } void CGL33ShadowRenderer::End() @@ -594,7 +572,7 @@ void CGL33ShadowRenderer::End() glDisable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(0.0f, 0.0f); - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + m_device->SetColorMask(true, true, true, true); } void CGL33ShadowRenderer::SetProjectionMatrix(const glm::mat4& matrix) diff --git a/src/graphics/opengl/gl33renderers.h b/src/graphics/opengl/gl33renderers.h index bf204482..3a936d89 100644 --- a/src/graphics/opengl/gl33renderers.h +++ b/src/graphics/opengl/gl33renderers.h @@ -77,7 +77,7 @@ private: // Vertex array object GLuint m_bufferVAO = 0; // VBO capacity - GLsizei m_bufferCapacity = 4 * 1024; + GLsizei m_bufferCapacity = 128 * 1024; // Buffer mapping state PrimitiveType m_type = {}; diff --git a/src/graphics/opengl/glutil.cpp b/src/graphics/opengl/glutil.cpp index 47b60b12..f4ba87d6 100644 --- a/src/graphics/opengl/glutil.cpp +++ b/src/graphics/opengl/glutil.cpp @@ -372,47 +372,6 @@ GLenum TranslateGfxCompFunc(CompFunc func) return 0; } -BlendFunc TranslateGLBlendFunc(GLenum flag) -{ - switch (flag) - { - case GL_ZERO: return BLEND_ZERO; - case GL_ONE: return BLEND_ONE; - case GL_SRC_COLOR: return BLEND_SRC_COLOR; - case GL_ONE_MINUS_SRC_COLOR: return BLEND_INV_SRC_COLOR; - case GL_DST_COLOR: return BLEND_DST_COLOR; - case GL_ONE_MINUS_DST_COLOR: return BLEND_INV_DST_COLOR; - case GL_SRC_ALPHA: return BLEND_SRC_ALPHA; - case GL_ONE_MINUS_SRC_ALPHA: return BLEND_INV_SRC_ALPHA; - case GL_DST_ALPHA: return BLEND_DST_ALPHA; - case GL_ONE_MINUS_DST_ALPHA: return BLEND_INV_DST_ALPHA; - case GL_SRC_ALPHA_SATURATE: return BLEND_SRC_ALPHA_SATURATE; - default: assert(false); break; - } - - return BLEND_ZERO; -} - -GLenum TranslateGfxBlendFunc(BlendFunc func) -{ - switch (func) - { - case BLEND_ZERO: return GL_ZERO; - case BLEND_ONE: return GL_ONE; - case BLEND_SRC_COLOR: return GL_SRC_COLOR; - case BLEND_INV_SRC_COLOR: return GL_ONE_MINUS_SRC_COLOR; - case BLEND_DST_COLOR: return GL_DST_COLOR; - case BLEND_INV_DST_COLOR: return GL_ONE_MINUS_DST_COLOR; - case BLEND_SRC_ALPHA: return GL_SRC_ALPHA; - case BLEND_INV_SRC_ALPHA: return GL_ONE_MINUS_SRC_ALPHA; - case BLEND_DST_ALPHA: return GL_DST_ALPHA; - case BLEND_INV_DST_ALPHA: return GL_ONE_MINUS_DST_ALPHA; - case BLEND_SRC_ALPHA_SATURATE: return GL_SRC_ALPHA_SATURATE; - default: assert(false); break; - } - return 0; -} - bool InPlane(glm::vec3 normal, float originPlane, glm::vec3 center, float radius) { float distance = originPlane + glm::dot(normal, center); diff --git a/src/graphics/opengl/glutil.h b/src/graphics/opengl/glutil.h index 42f00df7..0a3aba20 100644 --- a/src/graphics/opengl/glutil.h +++ b/src/graphics/opengl/glutil.h @@ -82,10 +82,6 @@ CompFunc TranslateGLCompFunc(GLenum flag); GLenum TranslateGfxCompFunc(CompFunc func); -BlendFunc TranslateGLBlendFunc(GLenum flag); - -GLenum TranslateGfxBlendFunc(BlendFunc func); - bool InPlane(glm::vec3 normal, float originPlane, glm::vec3 center, float radius); GLenum TranslateTextureCoordinate(int index); diff --git a/src/ui/controls/color.cpp b/src/ui/controls/color.cpp index 2fa493dd..a8352999 100644 --- a/src/ui/controls/color.cpp +++ b/src/ui/controls/color.cpp @@ -125,7 +125,6 @@ bool CColor::EventProcess(const Event &event) // Draw the button. void CColor::Draw() { - Gfx::Vertex2D vertex[4]; // 2 triangles Gfx::Color color; glm::vec2 p1, p2; @@ -154,13 +153,14 @@ void CColor::Draw() renderer->SetTexture(Gfx::Texture{0}); renderer->SetTransparency(Gfx::TransparencyMode::NONE); + auto vertex = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4); vertex[0] = { { p1.x, p1.y }, {}, col }; vertex[1] = { { p1.x, p2.y }, {}, col }; vertex[2] = { { p2.x, p1.y }, {}, col }; vertex[3] = { { p2.x, p2.y }, {}, col }; - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4, vertex); + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(2); } diff --git a/src/ui/controls/control.cpp b/src/ui/controls/control.cpp index e325382b..7689b0dc 100644 --- a/src/ui/controls/control.cpp +++ b/src/ui/controls/control.cpp @@ -609,7 +609,6 @@ void CControl::DrawPart(int icon, float zoom, float ex) void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::vec2& uv1, const glm::vec2& uv2, float ex) { - Gfx::Vertex2D vertex[8]; // 6 triangles glm::vec2 p1, p2, p3, p4; auto renderer = m_engine->GetDevice()->GetUIRenderer(); @@ -621,12 +620,14 @@ void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::v if ( ex == 0.0f ) // one piece? { - vertex[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; - vertex[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; - vertex[2] = { { p2.x, p1.y }, { uv2.x, uv2.y } }; - vertex[3] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; + auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4); - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4, vertex); + vertices[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; + vertices[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; + vertices[2] = { { p2.x, p1.y }, { uv2.x, uv2.y } }; + vertices[3] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; + + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(2); } else // 3 pieces? @@ -636,16 +637,18 @@ void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::v p3.x = p1.x + ex*dim.y / (uv2.y - uv1.y); p4.x = p2.x - ex*dim.y / (uv2.y - uv1.y); - vertex[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; - vertex[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; - vertex[2] = { { p3.x, p1.y }, { uv1.x+ex,uv2.y } }; - vertex[3] = { { p3.x, p2.y }, { uv1.x+ex,uv1.y } }; - vertex[4] = { { p4.x, p1.y }, { uv2.x-ex,uv2.y } }; - vertex[5] = { { p4.x, p2.y }, { uv2.x-ex,uv1.y } }; - vertex[6] = { { p2.x, p1.y }, { uv2.x, uv2.y } }; - vertex[7] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; + auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8); - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8, vertex); + vertices[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; + vertices[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; + vertices[2] = { { p3.x, p1.y }, { uv1.x+ex,uv2.y } }; + vertices[3] = { { p3.x, p2.y }, { uv1.x+ex,uv1.y } }; + vertices[4] = { { p4.x, p1.y }, { uv2.x-ex,uv2.y } }; + vertices[5] = { { p4.x, p2.y }, { uv2.x-ex,uv1.y } }; + vertices[6] = { { p2.x, p1.y }, { uv2.x, uv2.y } }; + vertices[7] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; + + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(6); } else @@ -653,16 +656,18 @@ void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::v p3.y = p1.y + ex*dim.x / (uv2.x - uv1.x); p4.y = p2.y - ex*dim.x / (uv2.x - uv1.x); - vertex[0] = { { p2.x, p1.y }, { uv2.x, uv2.y } }; - vertex[1] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; - vertex[2] = { { p2.x, p3.y }, { uv2.x, uv2.y - ex } }; - vertex[3] = { { p1.x, p3.y }, { uv1.x, uv2.y - ex } }; - vertex[4] = { { p2.x, p4.y }, { uv2.x, uv1.y + ex } }; - vertex[5] = { { p1.x, p4.y }, { uv1.x, uv1.y + ex } }; - vertex[6] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; - vertex[7] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; + auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8); - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8, vertex); + vertices[0] = { { p2.x, p1.y }, { uv2.x, uv2.y } }; + vertices[1] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; + vertices[2] = { { p2.x, p3.y }, { uv2.x, uv2.y - ex } }; + vertices[3] = { { p1.x, p3.y }, { uv1.x, uv2.y - ex } }; + vertices[4] = { { p2.x, p4.y }, { uv2.x, uv1.y + ex } }; + vertices[5] = { { p1.x, p4.y }, { uv1.x, uv1.y + ex } }; + vertices[6] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; + vertices[7] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; + + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(6); } } @@ -673,7 +678,6 @@ void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::v void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::vec2& uv1, const glm::vec2& uv2, const glm::vec2& cor, float ex) { - Gfx::Vertex2D vertices[8]; // 6 triangles glm::vec2 p1, p2, p3, p4; glm::vec2 corner = cor; @@ -698,6 +702,8 @@ void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::v p4.y = p2.y - corner.y; // Bottom horizontal band. + auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8); + vertices[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; vertices[1] = { { p1.x, p3.y }, { uv1.x, uv2.y - ex } }; vertices[2] = { { p3.x, p1.y }, { uv1.x + ex, uv2.y } }; @@ -707,10 +713,12 @@ void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::v vertices[6] = { { p2.x, p1.y }, { uv2.x, uv2.y } }; vertices[7] = { { p2.x, p3.y }, { uv2.x, uv2.y - ex } }; - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8, vertices); + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(6); // Central horizontal band. + vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8); + vertices[0] = { { p1.x, p3.y }, { uv1.x, uv2.y - ex } }; vertices[1] = { { p1.x, p4.y }, { uv1.x, uv1.y + ex } }; vertices[2] = { { p3.x, p3.y }, { uv1.x + ex, uv2.y - ex } }; @@ -720,10 +728,12 @@ void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::v vertices[6] = { { p2.x, p3.y }, { uv2.x, uv2.y - ex } }; vertices[7] = { { p2.x, p4.y }, { uv2.x, uv1.y + ex } }; - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8, vertices); + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(6); // Top horizontal band. + vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8); + vertices[0] = { { p1.x, p4.y }, { uv1.x, uv1.y + ex } }; vertices[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; vertices[2] = { { p3.x, p4.y }, { uv1.x + ex, uv1.y + ex } }; @@ -733,7 +743,7 @@ void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::v vertices[6] = { { p2.x, p4.y }, { uv2.x, uv1.y + ex } }; vertices[7] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8, vertices); + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(6); } diff --git a/src/ui/controls/edit.cpp b/src/ui/controls/edit.cpp index 2ca55c7a..e1249295 100644 --- a/src/ui/controls/edit.cpp +++ b/src/ui/controls/edit.cpp @@ -1275,18 +1275,17 @@ void CEdit::DrawHorizontalGradient(const glm::vec2& pos, const glm::vec2& dim, G p2.x = pos.x + dim.x; p2.y = pos.y + dim.y; - glm::u8vec4 col1 = { color1.r * 255, color1.g * 255, color1.b * 255, color1.a * 255 }; - glm::u8vec4 col2 = { color2.r * 255, color2.g * 255, color2.b * 255, color2.a * 255 }; + Gfx::IntColor col1 = Gfx::ColorToIntColor(color1); + Gfx::IntColor col2 = Gfx::ColorToIntColor(color2); - Gfx::Vertex2D quad[] = - { - { { p1.x, p1.y }, {}, col1 }, - { { p1.x, p2.y }, {}, col1 }, - { { p2.x, p1.y }, {}, col2 }, - { { p2.x, p2.y }, {}, col2 } - }; + auto quad = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4); - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4, quad); + quad[0] = { { p1.x, p1.y }, {}, col1 }; + quad[1] = { { p1.x, p2.y }, {}, col1 }; + quad[2] = { { p2.x, p1.y }, {}, col2 }; + quad[3] = { { p2.x, p2.y }, {}, col2 }; + + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(2); } diff --git a/src/ui/controls/map.cpp b/src/ui/controls/map.cpp index 9f37e315..29b21801 100644 --- a/src/ui/controls/map.cpp +++ b/src/ui/controls/map.cpp @@ -1001,15 +1001,15 @@ void CMap::DrawHighlight(const glm::vec2& position) void CMap::DrawTriangle(const glm::vec2& p1, const glm::vec2& p2, const glm::vec2& p3, const glm::vec2& uv1, const glm::vec2& uv2) { - Gfx::Vertex2D vertex[3]; // 1 triangle - auto renderer = m_engine->GetDevice()->GetUIRenderer(); - vertex[0] = { { p1.x, p1.y }, { uv1.x, uv1.y } }; - vertex[1] = { { p2.x, p2.y }, { uv1.x, uv2.y } }; - vertex[2] = { { p3.x, p3.y }, { uv2.x, uv2.y } }; + auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 3); - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLES, 3, vertex); + vertices[0] = { { p1.x, p1.y }, { uv1.x, uv1.y } }; + vertices[1] = { { p2.x, p2.y }, { uv1.x, uv2.y } }; + vertices[2] = { { p3.x, p3.y }, { uv2.x, uv2.y } }; + + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(1); } @@ -1017,17 +1017,16 @@ void CMap::DrawTriangle(const glm::vec2& p1, const glm::vec2& p2, const glm::vec void CMap::DrawPenta(const glm::vec2& p1, const glm::vec2& p2, const glm::vec2& p3, const glm::vec2& p4, const glm::vec2& p5, const glm::vec2& uv1, const glm::vec2& uv2) { - Gfx::Vertex2D vertex[5]; // 1 pentagon - auto renderer = m_engine->GetDevice()->GetUIRenderer(); + auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 5); - vertex[0] = { { p1.x, p1.y }, { uv1.x, uv1.y } }; - vertex[1] = { { p2.x, p2.y }, { uv1.x, uv2.y } }; - vertex[2] = { { p5.x, p5.y }, { uv2.x, uv2.y } }; - vertex[3] = { { p3.x, p3.y }, { uv2.x, uv2.y } }; - vertex[4] = { { p4.x, p4.y }, { uv2.x, uv2.y } }; + vertices[0] = { { p1.x, p1.y }, { uv1.x, uv1.y } }; + vertices[1] = { { p2.x, p2.y }, { uv1.x, uv2.y } }; + vertices[2] = { { p5.x, p5.y }, { uv2.x, uv2.y } }; + vertices[3] = { { p3.x, p3.y }, { uv2.x, uv2.y } }; + vertices[4] = { { p4.x, p4.y }, { uv2.x, uv2.y } }; - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 5, vertex); + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(3); } @@ -1035,11 +1034,8 @@ void CMap::DrawPenta(const glm::vec2& p1, const glm::vec2& p2, const glm::vec2& void CMap::DrawVertex(const glm::vec2& uv1, const glm::vec2& uv2, float zoom) { - Gfx::Vertex2D vertex[4]; // 2 triangles glm::vec2 p1, p2, c; - auto renderer = m_engine->GetDevice()->GetUIRenderer(); - p1.x = m_pos.x; p1.y = m_pos.y; p2.x = m_pos.x + m_dim.x; @@ -1058,12 +1054,15 @@ void CMap::DrawVertex(const glm::vec2& uv1, const glm::vec2& uv2, float zoom) m_mapDim.x = p2.x-p1.x; m_mapDim.y = p2.y-p1.y; - vertex[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; - vertex[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; - vertex[2] = { { p2.x, p1.y }, { uv2.x, uv2.y } }; - vertex[3] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; + auto renderer = m_engine->GetDevice()->GetUIRenderer(); + auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4); - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4, vertex); + vertices[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; + vertices[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; + vertices[2] = { { p2.x, p1.y }, { uv2.x, uv2.y } }; + vertices[3] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; + + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(2); } diff --git a/src/ui/controls/shortcut.cpp b/src/ui/controls/shortcut.cpp index 83ab9ffc..c11491c8 100644 --- a/src/ui/controls/shortcut.cpp +++ b/src/ui/controls/shortcut.cpp @@ -231,7 +231,6 @@ void CShortcut::Draw() void CShortcut::DrawVertex(int icon, float zoom) { - Gfx::Vertex2D vertex[4]; // 2 triangles glm::vec2 p1, p2, c; float u1, u2, v1, v2, dp; @@ -260,14 +259,15 @@ void CShortcut::DrawVertex(int icon, float zoom) u2 -= dp; v2 -= dp; - vertex[0] = { { p1.x, p1.y }, { u1, v2 } }; - vertex[1] = { { p1.x, p2.y }, { u1, v1 } }; - vertex[2] = { { p2.x, p1.y }, { u2, v2 } }; - vertex[3] = { { p2.x, p2.y }, { u2, v1 } }; - auto renderer = m_engine->GetDevice()->GetUIRenderer(); + auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4); - renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4, vertex); + vertices[0] = { { p1.x, p1.y }, { u1, v2 } }; + vertices[1] = { { p1.x, p2.y }, { u1, v1 } }; + vertices[2] = { { p2.x, p1.y }, { u2, v2 } }; + vertices[3] = { { p2.x, p2.y }, { u2, v1 } }; + + renderer->EndPrimitive(); m_engine->AddStatisticTriangle(2); }