Moved non-CRenderer-specific functionality back to CDevice and refactored some of the state change methods

Rewritten control rendering code to use improved way of sending geometry to GPU memory
dev
Tomasz Kapuściński 2022-02-05 19:07:46 +01:00
parent 074706e7ec
commit aacc2d0596
20 changed files with 381 additions and 455 deletions

View File

@ -94,6 +94,7 @@ add_library(colobotbase STATIC
graphics/core/light.h graphics/core/light.h
graphics/core/material.h graphics/core/material.h
graphics/core/texture.h graphics/core/texture.h
graphics/core/transparency.h
graphics/core/type.cpp graphics/core/type.cpp
graphics/core/type.h graphics/core/type.h
graphics/core/renderers.h graphics/core/renderers.h

View File

@ -26,6 +26,7 @@
#include "graphics/core/color.h" #include "graphics/core/color.h"
#include "graphics/core/texture.h" #include "graphics/core/texture.h"
#include "graphics/core/transparency.h"
#include "graphics/core/vertex.h" #include "graphics/core/vertex.h"
#include <glm/glm.hpp> #include <glm/glm.hpp>
@ -125,41 +126,6 @@ struct DeviceCapabilities
int maxSamples = 1; 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 * \enum CompFunc
* \brief Type of function used to compare values * \brief Type of function used to compare values
@ -177,45 +143,15 @@ enum CompFunc
}; };
/** /**
* \enum BlendFunc * \enum CullFace
* \brief Type of blending function * \brief Specifies which faces to cull while rendering polygons
*/ */
enum BlendFunc enum class CullFace : unsigned char
{ {
BLEND_ZERO, NONE,
BLEND_ONE, BACK,
BLEND_SRC_COLOR, FRONT,
BLEND_INV_SRC_COLOR, BOTH,
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
}; };
/** /**
@ -455,8 +391,16 @@ public:
//! Changes rendering viewport //! Changes rendering viewport
virtual void SetViewport(int x, int y, int width, int height) = 0; virtual void SetViewport(int x, int y, int width, int height) = 0;
//! Enables/disables the given render state //! Sets depth test
virtual void SetRenderState(RenderState state, bool enabled) = 0; 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 //! Sets the color mask
virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) = 0; virtual void SetColorMask(bool red, bool green, bool blue, bool alpha) = 0;

View File

@ -24,6 +24,7 @@
#pragma once #pragma once
#include "graphics/core/transparency.h"
#include "graphics/core/vertex.h" #include "graphics/core/vertex.h"
#include <glm/glm.hpp> #include <glm/glm.hpp>
@ -33,17 +34,10 @@ namespace Gfx
{ {
class CVertexBuffer; class CVertexBuffer;
enum class CullFace : unsigned char;
enum class PrimitiveType : unsigned char; enum class PrimitiveType : unsigned char;
struct Texture; struct Texture;
enum class TransparencyMode : unsigned char
{
NONE,
ALPHA,
BLACK,
WHITE,
};
struct ShadowParam struct ShadowParam
{ {
glm::mat4 matrix; glm::mat4 matrix;
@ -171,8 +165,8 @@ public:
virtual void SetDepthTest(bool enabled) = 0; virtual void SetDepthTest(bool enabled) = 0;
//! Sets depth mask //! Sets depth mask
virtual void SetDepthMask(bool enabled) = 0; virtual void SetDepthMask(bool enabled) = 0;
//! Sets cull mode //! Sets cull face mode
virtual void SetCullMode(bool enabled) = 0; virtual void SetCullFace(CullFace mode) = 0;
//! Sets transparency mode //! Sets transparency mode
virtual void SetTransparency(TransparencyMode mode) = 0; virtual void SetTransparency(TransparencyMode mode) = 0;

View File

@ -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,
};
}

View File

@ -1560,12 +1560,12 @@ void CEngine::UpdateStaticBuffer(EngineBaseObjDataTier& p4)
if (p4.buffer == nullptr) 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 else
{ {
p4.buffer->SetType(type); 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(); p4.buffer->Update();
} }
@ -2883,7 +2883,7 @@ void CEngine::Render()
m_device->SetClearColor(color); m_device->SetClearColor(color);
// Begin the scene // Begin the scene
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, true); m_device->SetDepthMask(true);
m_device->BeginScene(); m_device->BeginScene();
@ -2935,7 +2935,7 @@ void CEngine::Draw3DScene()
} }
} }
m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); m_device->SetDepthTest(false);
UpdateGroundSpotTextures(); UpdateGroundSpotTextures();
@ -3096,7 +3096,7 @@ void CEngine::Draw3DScene()
objectRenderer->SetModelMatrix(m_objects[objRank].transform); 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<int>( p1.next.size() ); l2++) for (int l2 = 0; l2 < static_cast<int>( 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; float dirty = ((p3.state & ENG_RSTATE_DUAL_BLACK) && m_dirty) ? 1.0 : 0.0;
objectRenderer->SetDirty(dirty); objectRenderer->SetDirty(dirty);
objectRenderer->SetColor({ 1.0f, 1.0f, 1.0f, 1.0f }); 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->SetUVTransform(p3.uvOffset, p3.uvScale);
objectRenderer->DrawObject(p3.buffer); objectRenderer->DrawObject(p3.buffer);
} }
@ -3148,7 +3148,7 @@ void CEngine::Draw3DScene()
objectRenderer->SetDepthMask(false); objectRenderer->SetDepthMask(false);
objectRenderer->SetTransparency(TransparencyMode::BLACK); objectRenderer->SetTransparency(TransparencyMode::BLACK);
objectRenderer->SetAlphaScissor(0.0f); objectRenderer->SetAlphaScissor(0.0f);
objectRenderer->SetCullMode(false); objectRenderer->SetCullFace(CullFace::NONE);
// Draw transparent objects // Draw transparent objects
@ -3387,20 +3387,19 @@ void CEngine::Capture3DScene()
void CEngine::DrawCaptured3DScene() 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[0] = { { 0.0f, 0.0f }, { 0.0f, 0.0f } };
vertices[1] = { { 1.0f, 0.0f }, { 1.0f, 0.0f } }; vertices[1] = { { 1.0f, 0.0f }, { 1.0f, 0.0f } };
vertices[2] = { { 0.0f, 1.0f }, { 0.0f, 1.0f } }; vertices[2] = { { 0.0f, 1.0f }, { 0.0f, 1.0f } };
vertices[3] = { { 1.0f, 1.0f }, { 1.0f, 1.0f } }; vertices[3] = { { 1.0f, 1.0f }, { 1.0f, 1.0f } };
auto renderer = m_device->GetUIRenderer(); renderer->EndPrimitive();
renderer->SetTexture(m_capturedWorldTexture);
renderer->SetTransparency(TransparencyMode::NONE);
renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, vertices);
} }
void CEngine::RenderDebugSphere(const Math::Sphere& sphere, const glm::mat4& transform, const Gfx::Color& color) 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); CProfiler::StopPerformanceCounter(PCNT_RENDER_SHADOW_MAP);
m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); m_device->SetDepthTest(false);
} }
void CEngine::UseMSAA(bool enable) void CEngine::UseMSAA(bool enable)
@ -3728,8 +3727,8 @@ void CEngine::UseMSAA(bool enable)
framebuffer->Bind(); framebuffer->Bind();
} }
m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, true); m_device->SetDepthTest(true);
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, true); m_device->SetDepthMask(true);
m_device->Clear(); m_device->Clear();
} }
@ -3753,7 +3752,8 @@ void CEngine::UseMSAA(bool enable)
void CEngine::DrawInterface() void CEngine::DrawInterface()
{ {
m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); m_device->SetDepthTest(false);
m_device->SetTransparency(TransparencyMode::NONE);
SetInterfaceCoordinates(); SetInterfaceCoordinates();
@ -3804,7 +3804,7 @@ void CEngine::DrawInterface()
renderer->SetAlphaScissor(0.0f); renderer->SetAlphaScissor(0.0f);
renderer->SetShadowParams(0, nullptr); renderer->SetShadowParams(0, nullptr);
renderer->SetColor({ 1.0f, 1.0f, 1.0f, 1.0f }); renderer->SetColor({ 1.0f, 1.0f, 1.0f, 1.0f });
renderer->SetCullMode(true); renderer->SetCullFace(CullFace::BACK);
renderer->SetPrimaryTextureEnabled(true); renderer->SetPrimaryTextureEnabled(true);
renderer->SetTriplanarMode(m_triplanarMode); renderer->SetTriplanarMode(m_triplanarMode);
@ -3873,7 +3873,7 @@ void CEngine::DrawInterface()
particleRenderer->End(); particleRenderer->End();
m_device->SetRenderState(RENDER_STATE_DEPTH_TEST, false); m_device->SetDepthTest(false);
SetInterfaceCoordinates(); SetInterfaceCoordinates();
} }
@ -4202,7 +4202,7 @@ void CEngine::UpdateGroundSpotTextures()
void CEngine::DrawShadowSpots() void CEngine::DrawShadowSpots()
{ {
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, false); m_device->SetDepthMask(false);
glm::mat4 matrix = glm::mat4(1.0f); glm::mat4 matrix = glm::mat4(1.0f);
//m_device->SetTransform(TRANSFORM_WORLD, matrix); //m_device->SetTransform(TRANSFORM_WORLD, matrix);
@ -4391,7 +4391,7 @@ void CEngine::DrawShadowSpots()
AddStatisticTriangle(2); AddStatisticTriangle(2);
} }
m_device->SetRenderState(RENDER_STATE_DEPTH_WRITE, true); m_device->SetDepthMask(true);
} }
void CEngine::DrawBackground() void CEngine::DrawBackground()
@ -4425,18 +4425,17 @@ void CEngine::DrawBackgroundGradient(const Color& up, const Color& down)
{ 0, 0, 0, 0 } { 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(); auto renderer = m_device->GetUIRenderer();
renderer->SetTexture(Texture{}); renderer->SetTexture(Texture{});
renderer->SetTransparency(TransparencyMode::NONE); 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); AddStatisticTriangle(2);
} }
@ -4506,18 +4505,17 @@ void CEngine::DrawBackgroundImage()
v2 -= margin_v; 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(); auto renderer = m_device->GetUIRenderer();
renderer->SetTexture(m_backgroundTex); renderer->SetTexture(m_backgroundTex);
renderer->SetTransparency(TransparencyMode::NONE); 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); AddStatisticTriangle(2);
} }
@ -4557,19 +4555,17 @@ void CEngine::DrawForegroundImage()
float v1 = 0.2f; float v1 = 0.2f;
float v2 = 1.0f; 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(); auto renderer = m_device->GetUIRenderer();
renderer->SetTexture(m_foregroundTex); renderer->SetTexture(m_foregroundTex);
renderer->SetTransparency(TransparencyMode::BLACK); 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); AddStatisticTriangle(2);
} }
@ -4594,17 +4590,14 @@ void CEngine::DrawOverColor()
auto renderer = m_device->GetUIRenderer(); auto renderer = m_device->GetUIRenderer();
renderer->SetTexture(Texture{}); renderer->SetTexture(Texture{});
renderer->SetTransparency(m_overMode); renderer->SetTransparency(m_overMode);
auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4);
Vertex2D vertex[4] = vertices[0] = { { p1.x, p1.y }, {}, colors[1] };
{ vertices[1] = { { p1.x, p2.y }, {}, colors[0] };
{ { p1.x, p1.y }, {}, colors[1] }, vertices[2] = { { p2.x, p1.y }, {}, colors[1] };
{ { p1.x, p2.y }, {}, colors[0] }, vertices[3] = { { p2.x, p2.y }, {}, colors[0] };
{ { p2.x, p1.y }, {}, colors[1] },
{ { p2.x, p2.y }, {}, colors[0] }
};
renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, vertex);
renderer->EndPrimitive();
AddStatisticTriangle(2); AddStatisticTriangle(2);
} }
@ -4666,13 +4659,6 @@ void CEngine::DrawHighlight()
glm::u8vec4 color(255, 255, 0, 255); // yellow 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(); auto renderer = m_device->GetUIRenderer();
renderer->SetTransparency(TransparencyMode::NONE); renderer->SetTransparency(TransparencyMode::NONE);
renderer->SetTexture(Texture{}); renderer->SetTexture(Texture{});
@ -4680,25 +4666,29 @@ void CEngine::DrawHighlight()
float dx = (p2.x - p1.x) / 5.0f; float dx = (p2.x - p1.x) / 5.0f;
float dy = (p2.y - p1.y) / 5.0f; float dy = (p2.y - p1.y) / 5.0f;
line[0].position = glm::vec3(p1.x, p1.y + dy, 0.0f); auto line = renderer->BeginPrimitive(PrimitiveType::LINE_STRIP, 3);
line[1].position = glm::vec3(p1.x, p1.y, 0.0f); line[0] = { { p1.x, p1.y + dy }, {}, color };
line[2].position = glm::vec3(p1.x + dx, p1.y, 0.0f); line[1] = { { p1.x, p1.y }, {}, color };
renderer->DrawPrimitive(PrimitiveType::LINE_STRIP, 3, line); line[2] = { { p1.x + dx, p1.y }, {}, color };
renderer->EndPrimitive();
line[0].position = glm::vec3(p2.x - dx, p1.y, 0.0f); line = renderer->BeginPrimitive(PrimitiveType::LINE_STRIP, 3);
line[1].position = glm::vec3(p2.x, p1.y, 0.0f); line[0] = { { p2.x - dx, p1.y }, {}, color };
line[2].position = glm::vec3(p2.x, p1.y + dy, 0.0f); line[1] = { { p2.x, p1.y }, {}, color };
renderer->DrawPrimitive(PrimitiveType::LINE_STRIP, 3, line); line[2] = { { p2.x, p1.y + dy }, {}, color };
renderer->EndPrimitive();
line[0].position = glm::vec3(p2.x, p2.y - dy, 0.0f); line = renderer->BeginPrimitive(PrimitiveType::LINE_STRIP, 3);
line[1].position = glm::vec3(p2.x, p2.y, 0.0f); line[0] = { { p2.x, p2.y - dy }, {}, color };
line[2].position = glm::vec3(p2.x - dx, p2.y, 0.0f); line[1] = { { p2.x, p2.y }, {}, color };
renderer->DrawPrimitive(PrimitiveType::LINE_STRIP, 3, line); line[2] = { { p2.x - dx, p2.y }, {}, color };
renderer->EndPrimitive();
line[0].position = glm::vec3(p1.x + dx, p2.y, 0.0f); line = renderer->BeginPrimitive(PrimitiveType::LINE_STRIP, 3);
line[1].position = glm::vec3(p1.x, p2.y, 0.0f); line[0] = { { p1.x + dx, p2.y }, {}, color };
line[2].position = glm::vec3(p1.x, p2.y - dy, 0.0f); line[1] = { { p1.x, p2.y }, {}, color };
renderer->DrawPrimitive(PrimitiveType::LINE_STRIP, 3, line); line[2] = { { p1.x, p2.y - dy }, {}, color };
renderer->EndPrimitive();
} }
void CEngine::DrawMouse() void CEngine::DrawMouse()
@ -4745,17 +4735,16 @@ void CEngine::DrawMouseSprite(const glm::ivec2& pos, const glm::ivec2& size, int
u2 -= dp; u2 -= dp;
v2 -= 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(); auto renderer = m_device->GetUIRenderer();
renderer->SetTransparency(mode); 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); AddStatisticTriangle(2);
} }
@ -4778,16 +4767,14 @@ void CEngine::DrawStats()
glm::vec2 margin = { 5.f / m_size.x, 5.f / m_size.y }; glm::vec2 margin = { 5.f / m_size.x, 5.f / m_size.y };
Vertex2D vertex[4] = auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 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 }
};
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); renderer->SetTransparency(TransparencyMode::ALPHA);
auto drawStatsLine = [&](const std::string& name, const std::string& value, const std::string& value2) auto drawStatsLine = [&](const std::string& name, const std::string& value, const std::string& value2)

View File

@ -1006,16 +1006,15 @@ void CText::DrawHighlight(FontMetaChar hl, const glm::ivec2& pos, const glm::ive
auto renderer = m_device->GetUIRenderer(); auto renderer = m_device->GetUIRenderer();
renderer->SetTexture(Texture{}); renderer->SetTexture(Texture{});
renderer->SetTransparency(TransparencyMode::ALPHA);
auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4);
Vertex2D quad[] = vertices[0] = { { p1.x, p2.y }, {}, grad[3] };
{ vertices[1] = { { p1.x, p1.y }, {}, grad[0] };
{ { p1.x, p2.y }, {}, grad[3] }, vertices[2] = { { p2.x, p2.y }, {}, grad[2] };
{ { p1.x, p1.y }, {}, grad[0] }, vertices[3] = { { p2.x, p1.y }, {}, grad[1] };
{ { p2.x, p2.y }, {}, grad[2] },
{ { p2.x, p1.y }, {}, grad[1] }
};
renderer->DrawPrimitive(PrimitiveType::TRIANGLE_STRIP, 4, quad); renderer->EndPrimitive();
m_engine->AddStatisticTriangle(2); m_engine->AddStatisticTriangle(2);
} }
@ -1049,17 +1048,20 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, glm::iv
uv2.x -= dp; uv2.x -= dp;
uv2.y -= 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] = auto renderer = m_device->GetUIRenderer();
{ renderer->SetTransparency(TransparencyMode::WHITE);
{ { p1.x, p2.y }, { uv1.x, uv2.y }, col }, renderer->SetTexture(Texture{ texID });
{ { p1.x, p1.y }, { uv1.x, uv1.y }, col }, auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4);
{ { p2.x, p2.y }, { uv2.x, uv2.y }, col },
{ { p2.x, p1.y }, { uv2.x, uv1.y }, col }
};
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; pos.x += width;
} }
@ -1088,17 +1090,20 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, glm::iv
glm::vec2 texCoord2(static_cast<float>(tex.charPos.x + tex.charSize.x - halfPixelMargin) / FONT_TEXTURE_SIZE.x, glm::vec2 texCoord2(static_cast<float>(tex.charPos.x + tex.charSize.x - halfPixelMargin) / FONT_TEXTURE_SIZE.x,
static_cast<float>(tex.charPos.y + tex.charSize.y - halfPixelMargin) / FONT_TEXTURE_SIZE.y); static_cast<float>(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] = auto renderer = m_device->GetUIRenderer();
{ renderer->SetTransparency(TransparencyMode::ALPHA);
{ { p1.x, p2.y }, { texCoord1.x, texCoord2.y }, col }, renderer->SetTexture(Texture{ tex.id });
{ { p1.x, p1.y }, { texCoord1.x, texCoord1.y }, col }, auto vertices = renderer->BeginPrimitive(PrimitiveType::TRIANGLE_STRIP, 4);
{ { p2.x, p2.y }, { texCoord2.x, texCoord2.y }, col },
{ { p2.x, p1.y }, { texCoord2.x, texCoord1.y }, col }
};
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; pos.x += tex.charSize.x * width;
} }

View File

@ -258,6 +258,17 @@ void CGL33Device::ConfigChanged(const DeviceConfig& newConfig)
void CGL33Device::BeginScene() void CGL33Device::BeginScene()
{ {
Clear(); 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() void CGL33Device::EndScene()
@ -513,30 +524,80 @@ void CGL33Device::SetViewport(int x, int y, int width, int height)
glViewport(x, y, width, 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) m_depthTest = enabled;
{
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;
}
if (enabled) if (enabled)
glEnable(flag); glEnable(GL_DEPTH_TEST);
else 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) void CGL33Device::SetColorMask(bool red, bool green, bool blue, bool alpha)

View File

@ -131,7 +131,12 @@ public:
void SetViewport(int x, int y, int width, int height) override; 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; void SetColorMask(bool red, bool green, bool blue, bool alpha) override;
@ -185,6 +190,15 @@ private:
std::unique_ptr<CGL33ParticleRenderer> m_particleRenderer; std::unique_ptr<CGL33ParticleRenderer> m_particleRenderer;
//! Shadow renderer //! Shadow renderer
std::unique_ptr<CGL33ShadowRenderer> m_shadowRenderer; std::unique_ptr<CGL33ShadowRenderer> 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 } // namespace Gfx

View File

@ -174,12 +174,10 @@ void CGL33ObjectRenderer::CGL33ObjectRenderer::Begin()
m_secondaryTexture = 0; m_secondaryTexture = 0;
m_shadowMap = 0; m_shadowMap = 0;
glEnable(GL_DEPTH_TEST); m_device->SetDepthTest(true);
glDepthMask(GL_TRUE); m_device->SetDepthMask(true);
m_device->SetTransparency(TransparencyMode::NONE);
glEnable(GL_CULL_FACE); m_device->SetCullFace(CullFace::BACK);
glCullFace(GL_BACK);
glFrontFace(GL_CW); // Colobot issue: faces are reversed
SetUVTransform({ 0.0f, 0.0f }, { 1.0f, 1.0f }); SetUVTransform({ 0.0f, 0.0f }, { 1.0f, 1.0f });
} }
@ -198,8 +196,6 @@ void CGL33ObjectRenderer::CGL33ObjectRenderer::End()
m_primaryTexture = 0; m_primaryTexture = 0;
m_secondaryTexture = 0; m_secondaryTexture = 0;
m_shadowMap = 0; m_shadowMap = 0;
glDepthMask(GL_TRUE);
} }
void CGL33ObjectRenderer::SetProjectionMatrix(const glm::mat4& matrix) 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) void CGL33ObjectRenderer::SetDepthTest(bool enabled)
{ {
if (enabled) m_device->SetDepthTest(enabled);
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
} }
void CGL33ObjectRenderer::SetDepthMask(bool 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) m_device->SetCullFace(mode);
{
glEnable(GL_CULL_FACE);
}
else
{
glDisable(GL_CULL_FACE);
}
} }
void CGL33ObjectRenderer::SetTransparency(TransparencyMode mode) void CGL33ObjectRenderer::SetTransparency(TransparencyMode mode)
{ {
switch (mode) m_device->SetTransparency(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 CGL33ObjectRenderer::SetUVTransform(const glm::vec2& offset, const glm::vec2& scale) void CGL33ObjectRenderer::SetUVTransform(const glm::vec2& offset, const glm::vec2& scale)

View File

@ -80,7 +80,7 @@ public:
virtual void SetDepthMask(bool enabled) override; virtual void SetDepthMask(bool enabled) override;
//! Sets cull mode parameters //! Sets cull mode parameters
virtual void SetCullMode(bool enabled) override; virtual void SetCullFace(CullFace mode) override;
//! Sets transparency mode //! Sets transparency mode
virtual void SetTransparency(TransparencyMode mode) override; virtual void SetTransparency(TransparencyMode mode) override;

View File

@ -108,9 +108,9 @@ void CGL33ParticleRenderer::Begin()
m_texture = 0; m_texture = 0;
glEnable(GL_DEPTH_TEST); m_device->SetDepthTest(true);
glDepthMask(GL_FALSE); m_device->SetDepthMask(false);
glDisable(GL_CULL_FACE); m_device->SetCullFace(CullFace::NONE);
glUniform4f(m_color, 1.0f, 1.0f, 1.0f, 1.0f); glUniform4f(m_color, 1.0f, 1.0f, 1.0f, 1.0f);
@ -125,8 +125,6 @@ void CGL33ParticleRenderer::End()
m_texture = 0; m_texture = 0;
glDepthMask(GL_TRUE);
glBindVertexArray(0); glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
@ -170,34 +168,9 @@ void CGL33ParticleRenderer::SetTexture(const Texture& texture)
glBindTexture(GL_TEXTURE_2D, texture.id); glBindTexture(GL_TEXTURE_2D, texture.id);
} }
void CGL33ParticleRenderer::SetTransparency(TransparencyMode mode) void CGL33ParticleRenderer::SetTransparency(TransparencyMode mode)
{ {
switch (mode) m_device->SetTransparency(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;
}
} }
void CGL33ParticleRenderer::DrawParticle(PrimitiveType type, int count, const Vertex3D* vertices) void CGL33ParticleRenderer::DrawParticle(PrimitiveType type, int count, const Vertex3D* vertices)

View File

@ -134,27 +134,7 @@ void CGL33UIRenderer::SetColor(const glm::vec4& color)
void CGL33UIRenderer::SetTransparency(TransparencyMode mode) void CGL33UIRenderer::SetTransparency(TransparencyMode mode)
{ {
switch (mode) m_device->SetTransparency(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 CGL33UIRenderer::DrawPrimitive(PrimitiveType type, int count, const Vertex2D* vertices) void CGL33UIRenderer::DrawPrimitive(PrimitiveType type, int count, const Vertex2D* vertices)
@ -233,9 +213,10 @@ bool CGL33UIRenderer::EndPrimitive()
glUseProgram(m_program); glUseProgram(m_program);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, m_uniformBuffer); glBindBufferBase(GL_UNIFORM_BUFFER, 0, m_uniformBuffer);
glDisable(GL_DEPTH_TEST);
UpdateUniforms(); UpdateUniforms();
m_device->SetDepthTest(false);
glDrawArrays(TranslateGfxPrimitive(m_type), m_offset, m_count); glDrawArrays(TranslateGfxPrimitive(m_type), m_offset, m_count);
@ -364,14 +345,11 @@ void CGL33TerrainRenderer::Begin()
{ {
glUseProgram(m_program); glUseProgram(m_program);
glEnable(GL_DEPTH_TEST); m_device->SetDepthTest(true);
glDepthMask(GL_TRUE); m_device->SetDepthMask(true);
m_device->SetCullFace(CullFace::BACK);
glEnable(GL_CULL_FACE); m_device->SetTransparency(TransparencyMode::NONE);
glCullFace(GL_BACK);
glFrontFace(GL_CW); // Colobot issue: faces are reversed
glDisable(GL_BLEND);
glActiveTexture(GL_TEXTURE10); glActiveTexture(GL_TEXTURE10);
glBindTexture(GL_TEXTURE_2D, m_whiteTexture); glBindTexture(GL_TEXTURE_2D, m_whiteTexture);
@ -568,7 +546,7 @@ CGL33ShadowRenderer::~CGL33ShadowRenderer()
void CGL33ShadowRenderer::Begin() void CGL33ShadowRenderer::Begin()
{ {
glViewport(0, 0, m_width, m_height); glViewport(0, 0, m_width, m_height);
glDepthMask(GL_TRUE); m_device->SetDepthMask(true);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
glUseProgram(m_program); glUseProgram(m_program);
@ -578,12 +556,12 @@ void CGL33ShadowRenderer::Begin()
glEnable(GL_POLYGON_OFFSET_FILL); glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(2.0f, 8.0f); glPolygonOffset(2.0f, 8.0f);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); m_device->SetColorMask(false, false, false, false);
glEnable(GL_DEPTH_TEST); m_device->SetDepthTest(true);
glDepthMask(GL_TRUE); m_device->SetDepthMask(true);
glDisable(GL_BLEND); m_device->SetTransparency(TransparencyMode::NONE);
glDisable(GL_CULL_FACE); m_device->SetCullFace(CullFace::NONE);
} }
void CGL33ShadowRenderer::End() void CGL33ShadowRenderer::End()
@ -594,7 +572,7 @@ void CGL33ShadowRenderer::End()
glDisable(GL_POLYGON_OFFSET_FILL); glDisable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(0.0f, 0.0f); 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) void CGL33ShadowRenderer::SetProjectionMatrix(const glm::mat4& matrix)

View File

@ -77,7 +77,7 @@ private:
// Vertex array object // Vertex array object
GLuint m_bufferVAO = 0; GLuint m_bufferVAO = 0;
// VBO capacity // VBO capacity
GLsizei m_bufferCapacity = 4 * 1024; GLsizei m_bufferCapacity = 128 * 1024;
// Buffer mapping state // Buffer mapping state
PrimitiveType m_type = {}; PrimitiveType m_type = {};

View File

@ -372,47 +372,6 @@ GLenum TranslateGfxCompFunc(CompFunc func)
return 0; 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) bool InPlane(glm::vec3 normal, float originPlane, glm::vec3 center, float radius)
{ {
float distance = originPlane + glm::dot(normal, center); float distance = originPlane + glm::dot(normal, center);

View File

@ -82,10 +82,6 @@ CompFunc TranslateGLCompFunc(GLenum flag);
GLenum TranslateGfxCompFunc(CompFunc func); GLenum TranslateGfxCompFunc(CompFunc func);
BlendFunc TranslateGLBlendFunc(GLenum flag);
GLenum TranslateGfxBlendFunc(BlendFunc func);
bool InPlane(glm::vec3 normal, float originPlane, glm::vec3 center, float radius); bool InPlane(glm::vec3 normal, float originPlane, glm::vec3 center, float radius);
GLenum TranslateTextureCoordinate(int index); GLenum TranslateTextureCoordinate(int index);

View File

@ -125,7 +125,6 @@ bool CColor::EventProcess(const Event &event)
// Draw the button. // Draw the button.
void CColor::Draw() void CColor::Draw()
{ {
Gfx::Vertex2D vertex[4]; // 2 triangles
Gfx::Color color; Gfx::Color color;
glm::vec2 p1, p2; glm::vec2 p1, p2;
@ -154,13 +153,14 @@ void CColor::Draw()
renderer->SetTexture(Gfx::Texture{0}); renderer->SetTexture(Gfx::Texture{0});
renderer->SetTransparency(Gfx::TransparencyMode::NONE); renderer->SetTransparency(Gfx::TransparencyMode::NONE);
auto vertex = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4);
vertex[0] = { { p1.x, p1.y }, {}, col }; vertex[0] = { { p1.x, p1.y }, {}, col };
vertex[1] = { { p1.x, p2.y }, {}, col }; vertex[1] = { { p1.x, p2.y }, {}, col };
vertex[2] = { { p2.x, p1.y }, {}, col }; vertex[2] = { { p2.x, p1.y }, {}, col };
vertex[3] = { { p2.x, p2.y }, {}, col }; vertex[3] = { { p2.x, p2.y }, {}, col };
renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4, vertex); renderer->EndPrimitive();
m_engine->AddStatisticTriangle(2); m_engine->AddStatisticTriangle(2);
} }

View File

@ -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, void CControl::DrawIcon(const glm::vec2& pos, const glm::vec2& dim, const glm::vec2& uv1, const glm::vec2& uv2,
float ex) float ex)
{ {
Gfx::Vertex2D vertex[8]; // 6 triangles
glm::vec2 p1, p2, p3, p4; glm::vec2 p1, p2, p3, p4;
auto renderer = m_engine->GetDevice()->GetUIRenderer(); 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? if ( ex == 0.0f ) // one piece?
{ {
vertex[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4);
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 } };
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); m_engine->AddStatisticTriangle(2);
} }
else // 3 pieces? 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); p3.x = p1.x + ex*dim.y / (uv2.y - uv1.y);
p4.x = p2.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 } }; auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8);
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 } };
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); m_engine->AddStatisticTriangle(6);
} }
else 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); p3.y = p1.y + ex*dim.x / (uv2.x - uv1.x);
p4.y = p2.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 } }; auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8);
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 } };
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); 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, 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) const glm::vec2& cor, float ex)
{ {
Gfx::Vertex2D vertices[8]; // 6 triangles
glm::vec2 p1, p2, p3, p4; glm::vec2 p1, p2, p3, p4;
glm::vec2 corner = cor; 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; p4.y = p2.y - corner.y;
// Bottom horizontal band. // Bottom horizontal band.
auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8);
vertices[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; vertices[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } };
vertices[1] = { { p1.x, p3.y }, { uv1.x, uv2.y - ex } }; vertices[1] = { { p1.x, p3.y }, { uv1.x, uv2.y - ex } };
vertices[2] = { { p3.x, p1.y }, { uv1.x + ex, uv2.y } }; 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[6] = { { p2.x, p1.y }, { uv2.x, uv2.y } };
vertices[7] = { { p2.x, p3.y }, { uv2.x, uv2.y - ex } }; 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); m_engine->AddStatisticTriangle(6);
// Central horizontal band. // Central horizontal band.
vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8);
vertices[0] = { { p1.x, p3.y }, { uv1.x, uv2.y - ex } }; vertices[0] = { { p1.x, p3.y }, { uv1.x, uv2.y - ex } };
vertices[1] = { { p1.x, p4.y }, { uv1.x, uv1.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 } }; 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[6] = { { p2.x, p3.y }, { uv2.x, uv2.y - ex } };
vertices[7] = { { p2.x, p4.y }, { uv2.x, uv1.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); m_engine->AddStatisticTriangle(6);
// Top horizontal band. // Top horizontal band.
vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8);
vertices[0] = { { p1.x, p4.y }, { uv1.x, uv1.y + ex } }; vertices[0] = { { p1.x, p4.y }, { uv1.x, uv1.y + ex } };
vertices[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; vertices[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } };
vertices[2] = { { p3.x, p4.y }, { uv1.x + ex, uv1.y + ex } }; 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[6] = { { p2.x, p4.y }, { uv2.x, uv1.y + ex } };
vertices[7] = { { p2.x, p2.y }, { uv2.x, uv1.y } }; vertices[7] = { { p2.x, p2.y }, { uv2.x, uv1.y } };
renderer->DrawPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 8, vertices); renderer->EndPrimitive();
m_engine->AddStatisticTriangle(6); m_engine->AddStatisticTriangle(6);
} }

View File

@ -1275,18 +1275,17 @@ void CEdit::DrawHorizontalGradient(const glm::vec2& pos, const glm::vec2& dim, G
p2.x = pos.x + dim.x; p2.x = pos.x + dim.x;
p2.y = pos.y + dim.y; p2.y = pos.y + dim.y;
glm::u8vec4 col1 = { color1.r * 255, color1.g * 255, color1.b * 255, color1.a * 255 }; Gfx::IntColor col1 = Gfx::ColorToIntColor(color1);
glm::u8vec4 col2 = { color2.r * 255, color2.g * 255, color2.b * 255, color2.a * 255 }; Gfx::IntColor col2 = Gfx::ColorToIntColor(color2);
Gfx::Vertex2D quad[] = auto quad = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4);
{
{ { p1.x, p1.y }, {}, col1 },
{ { p1.x, p2.y }, {}, col1 },
{ { p2.x, p1.y }, {}, col2 },
{ { p2.x, p2.y }, {}, col2 }
};
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); m_engine->AddStatisticTriangle(2);
} }

View File

@ -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) 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(); auto renderer = m_engine->GetDevice()->GetUIRenderer();
vertex[0] = { { p1.x, p1.y }, { uv1.x, uv1.y } }; auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 3);
vertex[1] = { { p2.x, p2.y }, { uv1.x, uv2.y } };
vertex[2] = { { p3.x, p3.y }, { uv2.x, uv2.y } };
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); 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) 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 renderer = m_engine->GetDevice()->GetUIRenderer();
auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 5);
vertex[0] = { { p1.x, p1.y }, { uv1.x, uv1.y } }; vertices[0] = { { p1.x, p1.y }, { uv1.x, uv1.y } };
vertex[1] = { { p2.x, p2.y }, { uv1.x, uv2.y } }; vertices[1] = { { p2.x, p2.y }, { uv1.x, uv2.y } };
vertex[2] = { { p5.x, p5.y }, { uv2.x, uv2.y } }; vertices[2] = { { p5.x, p5.y }, { uv2.x, uv2.y } };
vertex[3] = { { p3.x, p3.y }, { uv2.x, uv2.y } }; vertices[3] = { { p3.x, p3.y }, { uv2.x, uv2.y } };
vertex[4] = { { p4.x, p4.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); 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) void CMap::DrawVertex(const glm::vec2& uv1, const glm::vec2& uv2, float zoom)
{ {
Gfx::Vertex2D vertex[4]; // 2 triangles
glm::vec2 p1, p2, c; glm::vec2 p1, p2, c;
auto renderer = m_engine->GetDevice()->GetUIRenderer();
p1.x = m_pos.x; p1.x = m_pos.x;
p1.y = m_pos.y; p1.y = m_pos.y;
p2.x = m_pos.x + m_dim.x; 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.x = p2.x-p1.x;
m_mapDim.y = p2.y-p1.y; m_mapDim.y = p2.y-p1.y;
vertex[0] = { { p1.x, p1.y }, { uv1.x, uv2.y } }; auto renderer = m_engine->GetDevice()->GetUIRenderer();
vertex[1] = { { p1.x, p2.y }, { uv1.x, uv1.y } }; auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4);
vertex[2] = { { p2.x, p1.y }, { uv2.x, uv2.y } };
vertex[3] = { { p2.x, p2.y }, { uv2.x, uv1.y } };
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); m_engine->AddStatisticTriangle(2);
} }

View File

@ -231,7 +231,6 @@ void CShortcut::Draw()
void CShortcut::DrawVertex(int icon, float zoom) void CShortcut::DrawVertex(int icon, float zoom)
{ {
Gfx::Vertex2D vertex[4]; // 2 triangles
glm::vec2 p1, p2, c; glm::vec2 p1, p2, c;
float u1, u2, v1, v2, dp; float u1, u2, v1, v2, dp;
@ -260,14 +259,15 @@ void CShortcut::DrawVertex(int icon, float zoom)
u2 -= dp; u2 -= dp;
v2 -= 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 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); m_engine->AddStatisticTriangle(2);
} }