First working example

This commit is contained in:
2023-05-29 19:50:04 -07:00
parent 96dd33c6b8
commit 8da23b123a
21 changed files with 263 additions and 161 deletions

View File

@ -7,9 +7,9 @@
#include "display/_RenderManager.hpp"
#include "display/BackBufferRenderTarget.hpp"
#include "display/shader/ShaderManager.hpp"
#include "display/shader/SimpleTexturedShader.hpp"
#include "display/shader/FontShader.hpp"
#include "display/shader/UIShader.hpp"
#include "display/shader/shaders/SimpleTexturedShader.hpp"
#include "display/shader/shaders/FontShader.hpp"
#include "display/shader/shaders/UIShader.hpp"
#include "display/RenderPipeline.hpp"
#include "display/font/FontManager.hpp"
#include "display/font/ExampleFont.hpp"

View File

@ -7,7 +7,7 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Shader.cpp
FontShader.cpp
SimpleTexturedShader.cpp
SimpleBillboardedShader.cpp
)
)
add_subdirectory(buffers)
add_subdirectory(shaders)

View File

@ -13,7 +13,7 @@ namespace Dawn {
typedef GLuint shaderbufferlocation_t;
template<typename T>
class ShaderParameterBuffer : public IShaderParameterBuffer<T, shaderbufferslot_t> {
class ShaderParameterBuffer : public IShaderParameterBuffer<shaderbufferslot_t> {
protected:
shaderbufferlocation_t id = -1;
size_t size;
@ -29,7 +29,14 @@ namespace Dawn {
glBufferData(GL_UNIFORM_BUFFER, this->size, NULL, GL_DYNAMIC_DRAW);
}
void buffer(T *data) override {
/**
* Basic buffer method. Buffers the entire contents of the data struct to
* this shader parameter buffer.
*
* @param data Data to buffer to the parameter.
*/
void buffer(T *data) {
this->bind(0);
this->bufferRaw((void*)data);
}

View File

@ -0,0 +1,9 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
)

View File

@ -0,0 +1,30 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/ShaderParameterBuffer.hpp"
namespace Dawn {
struct RenderPipelineShaderBufferData {
glm::mat4 view;
glm::mat4 projection;
};
class RenderPipelineShaderBuffer : public ShaderParameterBuffer<struct RenderPipelineShaderBufferData> {
public:
static std::string getShaderUniformName() {
return "ub_RenderPipeline";
}
static std::string getShaderUniform() {
return std::string(
"layout (std140) uniform ub_RenderPipeline {\n"
"mat4 u_View;\n"
"mat4 u_Projection;\n"
"};"
);
}
};
}

View File

@ -0,0 +1,12 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
FontShader.cpp
SimpleTexturedShader.cpp
SimpleBillboardedShader.cpp
)

View File

@ -1,96 +1,98 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SimpleTexturedShader.hpp"
using namespace Dawn;
void SimpleTexturedShader::compile() {
#if DAWN_OPENGL_GLSL
this->compileShader(
{
{ "aPos", 0 },
{ "aTexCoord", 1 }
},
// 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"
"in vec2 o_TextCoord;\n"
"out vec4 o_Color;\n"
"uniform vec4 u_Color;\n"
"uniform bool u_HasTexture;\n"
"uniform sampler2D u_Text;\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"
);
#elif DAWN_OPENGL_HLSL
this->compileShader(
{
{ "aPos", 0 },
{ "aTexCoord", 1 }
},
// Vertex Shader
"uniform float4x4 u_Proj;\n"
"uniform float4x4 u_View;\n"
"uniform float4x4 u_Model;\n"
"void main("
"float3 aPos,\n"
"float2 aTexCoord,\n"
"float2 out o_TextCoord : TEXCOORD0,\n"
"float4 out gl_Position : POSITION\n"
") {\n"
"o_TextCoord = aTexCoord;\n"
"gl_Position = mul(mul(mul(float4(aPos, 1.0), u_Model), u_View), u_Proj);\n"
"}",
// Fragment Shader
"uniform float4 u_Color;\n"
"uniform bool u_HasTexture;\n"
"uniform sampler2D u_Text : TEXUNIT0;\n"
"float4 main(\n"
"float2 o_TextCoord : TEXCOORD0\n"
") {\n"
"float4 o_Color;\n"
"if(u_HasTexture) {\n"
"o_Color = mul(tex2D(u_Text, o_TextCoord), u_Color);\n"
"} else {\n"
"o_Color = u_Color;\n"
"}\n"
"return o_Color;\n"
"}\n"
);
#else
#error Shader Type must be either GLSL or HLSL
#endif
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");
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SimpleTexturedShader.hpp"
using namespace Dawn;
void SimpleTexturedShader::compile() {
#if DAWN_OPENGL_GLSL
this->compileShader(
{
{ "aPos", 0 },
{ "aTexCoord", 1 }
},
// Vertex Shader
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n" +
RenderPipelineShaderBuffer::getShaderUniform() + ""
// "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_Projection * u_View * u_Model * vec4(aPos, 1.0);\n"
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"}",
// Fragment Shader
"#version 330 core\n"
"in vec2 o_TextCoord;\n"
"out vec4 o_Color;\n"
"uniform vec4 u_Color;\n"
"uniform bool u_HasTexture;\n"
"uniform sampler2D u_Text;\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"
);
#elif DAWN_OPENGL_HLSL
this->compileShader(
{
{ "aPos", 0 },
{ "aTexCoord", 1 }
},
// Vertex Shader
"uniform float4x4 u_Proj;\n"
"uniform float4x4 u_View;\n"
"uniform float4x4 u_Model;\n"
"void main("
"float3 aPos,\n"
"float2 aTexCoord,\n"
"float2 out o_TextCoord : TEXCOORD0,\n"
"float4 out gl_Position : POSITION\n"
") {\n"
"o_TextCoord = aTexCoord;\n"
"gl_Position = mul(mul(mul(float4(aPos, 1.0), u_Model), u_View), u_Proj);\n"
"}",
// Fragment Shader
"uniform float4 u_Color;\n"
"uniform bool u_HasTexture;\n"
"uniform sampler2D u_Text : TEXUNIT0;\n"
"float4 main(\n"
"float2 o_TextCoord : TEXCOORD0\n"
") {\n"
"float4 o_Color;\n"
"if(u_HasTexture) {\n"
"o_Color = mul(tex2D(u_Text, o_TextCoord), u_Color);\n"
"} else {\n"
"o_Color = u_Color;\n"
"}\n"
"return o_Color;\n"
"}\n"
);
#else
#error Shader Type must be either GLSL or HLSL
#endif
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->bufferRenderPipeline = this->getBufferLocationByName(RenderPipelineShaderBuffer::getShaderUniformName());
}

View File

@ -1,21 +1,24 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/Shader.hpp"
namespace Dawn {
class SimpleTexturedShader : public Shader {
public:
shaderparameter_t paramProjection;
shaderparameter_t paramView;
shaderparameter_t paramModel;
shaderparameter_t paramColor;
shaderparameter_t paramTexture;
shaderparameter_t paramHasTexture;
void compile() override;
};
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp"
#include "display/shader/Shader.hpp"
namespace Dawn {
class SimpleTexturedShader : public Shader {
public:
shaderparameter_t paramProjection;
shaderparameter_t paramView;
shaderparameter_t paramModel;
shaderparameter_t paramColor;
shaderparameter_t paramTexture;
shaderparameter_t paramHasTexture;
shaderbufferlocation_t bufferRenderPipeline;
void compile() override;
};
}

View File

@ -3,7 +3,7 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/shader/SimpleBillboardedShader.hpp"
#include "display/shader/shaders/SimpleBillboardedShader.hpp"
#include "SimpleBillboardedMaterial.hpp"
#include "game/DawnGame.hpp"

View File

@ -36,8 +36,12 @@ std::vector<struct ShaderPassItem> SimpleTexturedMaterial::getRenderPasses() {
onlyPass.shader = shader;
onlyPass.colorValues[shader->paramColor] = this->color;
onlyPass.matrixValues[shader->paramModel] = this->transform->getWorldTransform();
onlyPass.matrixValues[shader->paramView] = camera->transform->getWorldTransform();
onlyPass.matrixValues[shader->paramProjection] = camera->getProjection();
onlyPass.parameterBuffers[shader->bufferRenderPipeline] = &this->getGame()->renderManager.getRenderPipeline()->shaderBuffer;
// onlyPass.matrixValues[shader->paramView] = camera->transform->getWorldTransform();
// onlyPass.matrixValues[shader->paramProjection] = camera->getProjection();
onlyPass.renderFlags = (
RENDER_MANAGER_RENDER_FLAG_BLEND |
RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST