UI Hello World

This commit is contained in:
2022-10-23 01:01:16 -07:00
parent be529b70c1
commit 182eb70361
25 changed files with 663 additions and 41 deletions

View File

@ -16,17 +16,18 @@ RenderManager::RenderManager(DawnGame &game) :
{
this->standardRenderPipeline=std::make_shared<StandardRenderPipeline>(*this);
this->simpleShader = std::make_shared<SimpleTexturedShader>();
this->uiShader = std::make_shared<UIShader>();
}
void RenderManager::init() {
this->standardRenderPipeline->init();
this->simpleShader->compile();
this->uiShader->compile();
// Setup the alpha blend function.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
// Prepare the initial values
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
}
@ -43,6 +44,26 @@ std::shared_ptr<Shader> RenderManager::getDefaultShader() {
return this->simpleShader;
}
std::shared_ptr<UIShader> RenderManager::getUIShader() {
return this->uiShader;
}
void RenderManager::setRenderFlags(renderflag_t flags) {
this->renderFlags = flags;
if((flags & RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST) == 0) {
glDisable(GL_DEPTH_TEST);
} else {
glEnable(GL_DEPTH_TEST);
}
if((flags & RENDER_MANAGER_RENDER_FLAG_BLEND) == 0) {
glDisable(GL_BLEND);
} else {
glEnable(GL_BLEND);
}
}
void RenderManager::update() {
this->getRenderPipeline().render();
}

View File

@ -18,6 +18,7 @@ namespace Dawn {
public:
BackBufferRenderTarget backBuffer;
std::shared_ptr<SimpleTexturedShader> simpleShader;
std::shared_ptr<UIShader> uiShader;
/**
* Construct a new RenderManager for a game instance.
@ -27,6 +28,8 @@ namespace Dawn {
RenderTarget & getBackBuffer() override;
RenderPipeline & getRenderPipeline() override;
std::shared_ptr<Shader> getDefaultShader() override;
std::shared_ptr<UIShader> getUIShader() override;
void setRenderFlags(renderflag_t renderFlags) override;
void init() override;
void update() override;

View File

@ -9,7 +9,7 @@
namespace Dawn {
class SimpleTexturedShader : public Shader {
private:
public:
shaderparameter_t paramProjection;
shaderparameter_t paramView;
shaderparameter_t paramModel;
@ -17,7 +17,6 @@ namespace Dawn {
shaderparameter_t paramTexture;
shaderparameter_t paramHasTexture;
public:
std::map<shaderparameter_t, enum ShaderParameterType>
getParameters() override {
std::map<shaderparameter_t, enum ShaderParameterType> ps;

View File

@ -0,0 +1,119 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/Shader.hpp"
#include "scene/components/Components.hpp"
namespace Dawn {
class UIShader : public Shader {
public:
shaderparameter_t paramProjection;
shaderparameter_t paramView;
shaderparameter_t paramModel;
shaderparameter_t paramColor;
shaderparameter_t paramTexture;
shaderparameter_t paramHasTexture;
std::map<shaderparameter_t, enum ShaderParameterType>
getParameters() override {
std::map<shaderparameter_t, enum ShaderParameterType> ps;
ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR;
ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN;
ps[this->paramTexture] = SHADER_PARAMETER_TYPE_TEXTURE;
return ps;
}
void setDefaultParameters(Material &material) override {
material.colorValues[this->paramColor] = COLOR_WHITE;
}
void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override {
this->setMatrix(this->paramProjection, proj);
this->setMatrix(this->paramView, view);
}
void setMeshParameters(glm::mat4 transform) override {
this->setMatrix(this->paramModel, transform);
}
void bindTexture(
shaderparameter_t param,
std::shared_ptr<Texture> texture
) override {
if(texture == nullptr) {
this->setBoolean(this->paramHasTexture, false);
} else {
this->setBoolean(this->paramHasTexture, true);
this->setTextureSlot(param, 0x00);
texture->bind(0x00);
}
}
void compile() override {
this->compileShader(
// Vertex Shader
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"uniform mat4 u_Proj;\n"
"uniform mat4 u_View;\n"
"uniform mat4 u_Model;\n"
"out vec2 o_TextCoord;\n"
"void main() {\n"
"gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"}",
// Fragment Shader
"#version 330 core\n"
"out vec4 o_Color;\n"
"in vec2 o_TextCoord;\n"
"uniform vec4 u_Color;\n"
"uniform sampler2D u_Text;\n"
"uniform bool u_HasTexture;\n"
"void main() {\n"
"if(u_HasTexture) {\n"
"o_Color = texture(u_Text, o_TextCoord) * u_Color;\n"
"} else {\n"
"o_Color = u_Color;"
"}\n"
"}\n"
);
this->paramProjection = this->getParameterByName("u_Proj");
this->paramView = this->getParameterByName("u_View");
this->paramModel = this->getParameterByName("u_Model");
this->paramColor = this->getParameterByName("u_Color");
this->paramTexture = this->getParameterByName("u_Text");
this->paramHasTexture = this->getParameterByName("u_HasTexture");
this->setBoolean(this->paramHasTexture, false);
}
void setUICamera(glm::mat4 view, glm::mat4 projection) {
this->setMatrix(this->paramView, view);
this->setMatrix(this->paramProjection, projection);
}
void setUIModel(glm::mat4 model) {
this->setMatrix(this->paramModel, model);
}
void setUITexture(std::shared_ptr<Texture> texture) {
this->bindTexture(this->paramTexture, texture);
}
void setUIColor(struct Color color) {
this->setColor(this->paramColor, color);
}
};
}