Added shaders for UI and fixed UI renderer

This commit is contained in:
Antoine Pilote
2024-09-10 19:18:29 -04:00
parent 1949946cdd
commit 2c48590a4a
9 changed files with 799 additions and 105 deletions

View File

@@ -7,9 +7,13 @@
#include "FileSystem.h"
#include <glad/glad.h>
#include <src/Rendering/Shaders/ShaderManager.h>
namespace NuakeUI
{
struct Vertex {
struct Vertex
{
Vector3 Position;
Vector2 UV;
};
@@ -45,15 +49,8 @@ namespace NuakeUI
void Renderer::ReloadShaders()
{
// Rectangle Shader
std::string vertexSource = FileSystem::ReadFile("../resources/panel.vert.glsl");
std::string fragSource = FileSystem::ReadFile("../resources/panel.frag.glsl");
mShader = std::make_shared<Shader>(vertexSource, fragSource);
// SDF Shader
vertexSource = FileSystem::ReadFile("../resources/text.vert.glsl");
fragSource = FileSystem::ReadFile("../resources/text.frag.glsl");
mSDFShader = std::make_shared<Shader>(vertexSource, fragSource);
mShader = ShaderManager::GetShader("Resources/Shaders/ui_panel.shader");
mSDFShader = ShaderManager::GetShader("Resources/Shaders/ui_text.shader");
}
void Renderer::SetViewportSize(const Vector2& size)
@@ -66,12 +63,12 @@ namespace NuakeUI
{
int viewportW = (int)mSize.x;
int viewportH = (int)mSize.y;
//glViewport(0, 0, viewportW, viewportH);
glViewport(0, 0, viewportW, viewportH);
}
void Renderer::DrawNode(std::shared_ptr<Node> node, int z)
{
//glEnable(GL_DEPTH_TEST);
glDisable(GL_DEPTH_TEST);
float parentScroll = 0.f;
float parentPaddingRight = 0.f;
@@ -115,92 +112,90 @@ namespace NuakeUI
}
mShader->Bind();
//mShader->SetUniforms({
// { "u_Model", transform },
// { "u_Size", Vector2(width, height) },
// { "u_View", mView },
// { "u_Color", node->ComputedStyle.BackgroundColor },
// { "u_Border", node->ComputedStyle.BorderSize },
// { "u_BorderRadius", node->ComputedStyle.BorderRadius },
// { "u_BorderColor", node->ComputedStyle.BorderColor },
// { "u_BackgroundImage", 0 },
// { "u_HasBackgroundImage", hasBackgroundImage ? 1.f : 0.f}
//});
mShader->SetUniforms({
{ "u_Model", transform },
{ "u_Size", Vector2(width, height) },
{ "u_View", mView },
{ "u_Color", node->ComputedStyle.BackgroundColor },
{ "u_Border", node->ComputedStyle.BorderSize },
{ "u_BorderRadius", node->ComputedStyle.BorderRadius },
{ "u_BorderColor", node->ComputedStyle.BorderColor },
{ "u_BackgroundImage", 0 },
{ "u_HasBackgroundImage", hasBackgroundImage ? 1.f : 0.f}
});
//bool hideOverflow = node->Parent != nullptr && node->Parent->ComputedStyle.Overflow == OverflowType::Hidden;
//if (hideOverflow)
//{
// glEnable(GL_SCISSOR_TEST);
// Vector2 parentPosition = node->Parent->ComputedPosition;
// Vector2 parentSize = node->Parent->ComputedSize;
// int scissorX = (int)parentPosition.x;
// int scissorY = 1080 - (int)(parentPosition.y - parentSize.y);
// int scissorW = (int)parentSize.x;
// int scissorH = (int)parentSize.y;
// glScissor(scissorX, scissorY, scissorW, scissorH);
//}
bool hideOverflow = node->Parent != nullptr && node->Parent->ComputedStyle.Overflow == OverflowType::Hidden;
if (hideOverflow)
{
glEnable(GL_SCISSOR_TEST);
Vector2 parentPosition = node->Parent->ComputedPosition;
Vector2 parentSize = node->Parent->ComputedSize;
int scissorX = (int)parentPosition.x;
int scissorY = 1080 - (int)(parentPosition.y - parentSize.y);
int scissorW = (int)parentSize.x;
int scissorH = (int)parentSize.y;
glScissor(scissorX, scissorY, scissorW, scissorH);
}
// TODO: Keep overflow until we get out of here
//mVertexArray->Bind();
//glDrawArrays(GL_TRIANGLES, 0, 6);
//mVertexArray->Unbind();
//mShader->Unbind();
//
//if (hideOverflow)
// glDisable(GL_SCISSOR_TEST);
mVertexArray->Bind();
glDrawArrays(GL_TRIANGLES, 0, 6);
mVertexArray->Unbind();
mShader->Unbind();
if (hideOverflow)
glDisable(GL_SCISSOR_TEST);
}
void Renderer::DrawString(const std::string& string, NodeStyle& nodeStyle, std::shared_ptr<Font> font, Vector3 position)
{
//const float fontSize = nodeStyle.FontSize / 64.f;
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//mSDFShader->Bind();
//font->mAtlas->Bind(5);
//mSDFShader->SetUniforms({
// { "u_View", mView },
// { "u_Atlas", 5 },
// { "u_Scale", 1.0f},
// { "u_SDF_BorderSize", 0.0f},
// { "u_FontColor", nodeStyle.FontColor },
// { "u_PxRange", fontSize },
// { "u_SDF_BorderSize", {1.0, 1.0}}
//});
//
//mVertexArray->Bind();
//
//float advance = 0.0f;
//for (char const& c : string)
//{
// Char letter = font->GetChar((int)c);
//
// // Move the cursor
// Matrix4 model = Matrix4(1.f);
// model = glm::translate(model, position);
// model = glm::translate(model, Vector3(advance * fontSize, (-(letter.PlaneBounds.top) + (font->LineHeight)) * fontSize, 0));
//
// advance += (letter.Advance);
//
// // Scaling of the quad
// float scaleX = (float)(letter.PlaneBounds.right - letter.PlaneBounds.left);
// float scaleY = (float)(letter.PlaneBounds.top - letter.PlaneBounds.bottom);
// model = glm::scale(model, Vector3(scaleX * fontSize, scaleY * fontSize, 1.f));
//
// // Set Uniforms
// mSDFShader->SetUniforms({
// { "u_Model", model },
// { "u_TexturePos", Vector2(letter.AtlasBounds.Pos.x, letter.AtlasBounds.Pos.y) },
// { "u_TextureScale", Vector2(letter.AtlasBounds.Size.x, letter.AtlasBounds.Size.y) },
// });
//
//
// glDrawArrays(GL_TRIANGLES, 0, 6);
//}
//
//
//mVertexArray->Unbind();
const float fontSize = nodeStyle.FontSize / 64.f;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
mSDFShader->Bind();
font->mAtlas->Bind(5);
mSDFShader->SetUniforms({
{ "u_View", mView },
{ "u_Atlas", 5 },
{ "u_Scale", 1.0f},
{ "u_SDF_BorderSize", 0.0f},
{ "u_FontColor", nodeStyle.FontColor },
{ "u_PxRange", fontSize },
{ "u_SDF_BorderSize", {1.0, 1.0}}
});
mVertexArray->Bind();
float advance = 0.0f;
for (char const& c : string)
{
Char letter = font->GetChar((int)c);
// Move the cursor
Matrix4 model = Matrix4(1.f);
model = glm::translate(model, position);
model = glm::translate(model, Vector3(advance * fontSize, (-(letter.PlaneBounds.top) + (font->LineHeight)) * fontSize, 0));
advance += (letter.Advance);
// Scaling of the quad
float scaleX = (float)(letter.PlaneBounds.right - letter.PlaneBounds.left);
float scaleY = (float)(letter.PlaneBounds.top - letter.PlaneBounds.bottom);
model = glm::scale(model, Vector3(scaleX * fontSize, scaleY * fontSize, 1.f));
// Set Uniforms
mSDFShader->SetUniforms({
{ "u_Model", model },
{ "u_TexturePos", Vector2(letter.AtlasBounds.Pos.x, letter.AtlasBounds.Pos.y) },
{ "u_TextureScale", Vector2(letter.AtlasBounds.Size.x, letter.AtlasBounds.Size.y) },
});
glDrawArrays(GL_TRIANGLES, 0, 6);
}
mVertexArray->Unbind();
}
}

View File

@@ -39,8 +39,8 @@ namespace NuakeUI
Renderer();
~Renderer() = default;
std::shared_ptr<Shader> mShader;
std::shared_ptr<Shader> mSDFShader;
Shader* mShader;
Shader* mSDFShader;
std::shared_ptr<VertexArray> mVertexArray;
std::shared_ptr<VertexBuffer> mVertexBuffer;
};