Part one - removed references and smart pointers

This commit is contained in:
2022-11-11 19:08:46 -08:00
parent 4c2fc4cfcf
commit 42645883cd
76 changed files with 3899 additions and 3707 deletions

View File

@ -1,48 +1,48 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "BackBufferRenderTarget.hpp"
using namespace Dawn;
BackBufferRenderTarget::BackBufferRenderTarget(RenderManager &renderManager) :
renderManager(renderManager)
{
}
float_t BackBufferRenderTarget::getWidth() {
return this->width;
}
float_t BackBufferRenderTarget::getHeight() {
return this->height;
}
void BackBufferRenderTarget::setSize(float_t width, float_t height) {
if(this->width == width && this->height == height) return;
this->width = width;
this->height = height;
this->eventRenderTargetResized.invoke(*this, width, height);
}
void BackBufferRenderTarget::setClearColor(struct Color color) {
this->clearColor = color;
}
void BackBufferRenderTarget::clear(flag8_t clearFlags) {
glClearColor(
this->clearColor.r,
this->clearColor.g,
this->clearColor.b,
this->clearColor.a
);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void BackBufferRenderTarget::bind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, (GLsizei)this->width, (GLsizei)this->height);
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "BackBufferRenderTarget.hpp"
using namespace Dawn;
BackBufferRenderTarget::BackBufferRenderTarget(RenderManager &renderManager) :
renderManager(renderManager)
{
}
float_t BackBufferRenderTarget::getWidth() {
return this->width;
}
float_t BackBufferRenderTarget::getHeight() {
return this->height;
}
void BackBufferRenderTarget::setSize(float_t width, float_t height) {
if(this->width == width && this->height == height) return;
this->width = width;
this->height = height;
this->eventRenderTargetResized.invoke(this, width, height);
}
void BackBufferRenderTarget::setClearColor(struct Color color) {
this->clearColor = color;
}
void BackBufferRenderTarget::clear(flag8_t clearFlags) {
glClearColor(
this->clearColor.r,
this->clearColor.g,
this->clearColor.b,
this->clearColor.a
);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void BackBufferRenderTarget::bind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, (GLsizei)this->width, (GLsizei)this->height);
}

View File

@ -1,73 +1,74 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "dawnopengl.hpp"
#include "game/DawnGame.hpp"
#include "display/RenderManager.hpp"
#include "display/StandardRenderPipeline.hpp"
using namespace Dawn;
RenderManager::RenderManager(DawnGame &game) :
IRenderManager(game),
backBuffer(*this)
{
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();
// Prepare the initial values
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
}
RenderTarget & RenderManager::getBackBuffer() {
return this->backBuffer;
}
RenderPipeline & RenderManager::getRenderPipeline() {
return *this->standardRenderPipeline;
}
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();
}
RenderManager::~RenderManager() {
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "dawnopengl.hpp"
#include "game/DawnGame.hpp"
#include "display/RenderManager.hpp"
#include "display/StandardRenderPipeline.hpp"
using namespace Dawn;
RenderManager::RenderManager(DawnGame *game) :
IRenderManager(game),
backBuffer(*this)
{
this->standardRenderPipeline = new StandardRenderPipeline(this);
this->simpleShader = new SimpleTexturedShader();
this->uiShader = new UIShader();
}
void RenderManager::init() {
this->standardRenderPipeline->init();
this->simpleShader->compile();
this->uiShader->compile();
// Prepare the initial values
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
}
RenderTarget * RenderManager::getBackBuffer() {
return &this->backBuffer;
}
RenderPipeline * RenderManager::getRenderPipeline() {
return this->standardRenderPipeline;
}
Shader * RenderManager::getDefaultShader() {
return this->simpleShader;
}
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();
}
RenderManager::~RenderManager() {
delete this->standardRenderPipeline;
delete this->simpleShader;
delete this->uiShader;
}

View File

@ -1,41 +1,41 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/_RenderManager.hpp"
#include "display/BackBufferRenderTarget.hpp"
#include "display/shader/SimpleTexturedShader.hpp"
namespace Dawn {
class StandardRenderPipeline;
class RenderManager : public IRenderManager {
private:
std::shared_ptr<StandardRenderPipeline> standardRenderPipeline;
public:
BackBufferRenderTarget backBuffer;
std::shared_ptr<SimpleTexturedShader> simpleShader;
std::shared_ptr<UIShader> uiShader;
/**
* Construct a new RenderManager for a game instance.
*/
RenderManager(DawnGame &game);
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;
/**
* Destroy a previously initialized RenderManager.
*/
~RenderManager();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/_RenderManager.hpp"
#include "display/BackBufferRenderTarget.hpp"
#include "display/shader/SimpleTexturedShader.hpp"
namespace Dawn {
class StandardRenderPipeline;
class RenderManager : public IRenderManager {
private:
StandardRenderPipeline *standardRenderPipeline;
public:
BackBufferRenderTarget backBuffer;
SimpleTexturedShader *simpleShader;
UIShader *uiShader;
/**
* Construct a new RenderManager for a game instance.
*/
RenderManager(DawnGame *game);
RenderTarget * getBackBuffer() override;
RenderPipeline * getRenderPipeline() override;
Shader * getDefaultShader() override;
UIShader * getUIShader() override;
void setRenderFlags(renderflag_t renderFlags) override;
void init() override;
void update() override;
/**
* Destroy a previously initialized RenderManager.
*/
~RenderManager();
};
}

View File

@ -1,14 +1,14 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "StandardRenderPipeline.hpp"
#include "scene/components/Components.hpp"
using namespace Dawn;
StandardRenderPipeline::StandardRenderPipeline(RenderManager &renderManager) :
RenderPipeline(renderManager)
{
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "StandardRenderPipeline.hpp"
#include "scene/components/Components.hpp"
using namespace Dawn;
StandardRenderPipeline::StandardRenderPipeline(RenderManager *renderManager) :
RenderPipeline(renderManager)
{
}

View File

@ -1,15 +1,15 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "display/RenderPipeline.hpp"
namespace Dawn {
class StandardRenderPipeline : public RenderPipeline {
public:
StandardRenderPipeline(RenderManager &renderManager);
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "display/RenderPipeline.hpp"
namespace Dawn {
class StandardRenderPipeline : public RenderPipeline {
public:
StandardRenderPipeline(RenderManager *renderManager);
};
}

View File

@ -1,32 +1,37 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "display/_Texture.hpp"
#include "util/memory.hpp"
namespace Dawn {
typedef GLuint textureslot_t;
class Texture : public ITexture {
private:
int32_t width = -1;
int32_t height = -1;
GLuint id = -1;
public:
void bind(textureslot_t slot);
int32_t getWidth() override;
int32_t getHeight() override;
void setSize(int32_t width, int32_t height) override;
void fill(struct Color) override;
bool_t isReady() override;
void buffer(struct Color pixels[]) override;
~Texture();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "display/_Texture.hpp"
#include "util/memory.hpp"
namespace Dawn {
typedef GLuint textureslot_t;
class Texture : public ITexture {
private:
int32_t width = -1;
int32_t height = -1;
GLuint id = -1;
public:
int32_t getWidth() override;
int32_t getHeight() override;
void setSize(int32_t width, int32_t height) override;
void fill(struct Color) override;
bool_t isReady() override;
void buffer(struct Color pixels[]) override;
/**
* Binds the texture to the given slot (for use by the shaders).
*
* @param slot Slot to bind to.
*/
void bind(textureslot_t slot);
~Texture();
};
}

View File

@ -1,136 +1,137 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/_Mesh.hpp"
#include "dawnopengl.hpp"
/** Indice that references a specific vertice */
typedef int32_t meshindice_t;
namespace Dawn {
enum MeshDrawMode {
MESH_DRAW_MODE_TRIANGLES = GL_TRIANGLES
};
class Mesh {
private:
protected:
/** Pointer to the vertex buffer on the GPU */
GLuint vertexBuffer = -1;
/** Pointer to the index buffer on the GPU */
GLuint indexBuffer = -1;
/** How many vertices are in the mesh */
int32_t verticeCount = -1;
/** How many indices are in the mesh */
int32_t indiceCount = -1;
public:
/**
* Create a new set of buffers for the mesh to use.
*
* @param verticeCount How many Vertices will this buffer support.
* @param indiceCount How many Indices will this buffer support.
*/
void createBuffers(
int32_t verticeCount,
int32_t indiceCount
);
/**
* Cleanup the buffers on a given mesh. This is useful if you intend to
* expand the count of vertices your mesh supports.
*/
void disposeBuffers();
/**
* Write vertice positions to the mesh.
*
* @tparam N Size of the array, in terms of number of elements.
* @param position Position, within the buffer, to write to.
* @param vertices Array of positions to write.
*/
template<size_t N>
void bufferPositions(
int32_t position,
std::array<glm::vec3, N> positions
) {
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
sizeof(glm::vec3) * position,
sizeof(positions),
(void *)positions.data()
);
}
/**
* Write vertice coordinates to the mesh.
*
* @tparam N Size of the array, in terms of number of elements.
* @param position Position, within the buffer, to write to.
* @param coordinates Array of coordinates to write.
*/
template<size_t N>
void bufferCoordinates(
int32_t position,
std::array<glm::vec2, N> coordinates
) {
auto offsetCoordinates = (
(sizeof(glm::vec3) * this->verticeCount) +
(sizeof(glm::vec2) * position)
);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
offsetCoordinates,
sizeof(coordinates),
(void *)coordinates.data()
);
}
/**
* Write indices to the mesh.
*
* @tparam N Size of the array, in terms of number of elements.
* @param position Position, within the buffer, to write to.
* @param indices Array of indices to write.
*/
template<size_t N>
void bufferIndices(
int32_t position,
std::array<meshindice_t, N> indices
) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
glBufferSubData(
GL_ELEMENT_ARRAY_BUFFER,
sizeof(meshindice_t) * position,
sizeof(indices),
(void *)indices.data()
);
}
/**
* Draw a primitive. Primitives are drawn by their indices.
*
* @param drawMode Which drawing mode to use to draw the primitive.
* @param start Start indice (index) to draw.
* @param count Count of indices to draw. Use -1 to draw all.
*/
void draw(
enum MeshDrawMode drawMode,
int32_t start,
int32_t count
);
/**
* Cleanup a previously initiated mesh.
*/
~Mesh();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/_Mesh.hpp"
#include "dawnopengl.hpp"
#include "assert/assert.hpp"
/** Indice that references a specific vertice */
typedef int32_t meshindice_t;
namespace Dawn {
enum MeshDrawMode {
MESH_DRAW_MODE_TRIANGLES = GL_TRIANGLES
};
class Mesh {
private:
protected:
/** Pointer to the vertex buffer on the GPU */
GLuint vertexBuffer = -1;
/** Pointer to the index buffer on the GPU */
GLuint indexBuffer = -1;
/** How many vertices are in the mesh */
int32_t verticeCount = -1;
/** How many indices are in the mesh */
int32_t indiceCount = -1;
public:
/**
* Create a new set of buffers for the mesh to use.
*
* @param verticeCount How many Vertices will this buffer support.
* @param indiceCount How many Indices will this buffer support.
*/
void createBuffers(
int32_t verticeCount,
int32_t indiceCount
);
/**
* Cleanup the buffers on a given mesh. This is useful if you intend to
* expand the count of vertices your mesh supports.
*/
void disposeBuffers();
/**
* Write vertice positions to the mesh.
*
* @tparam N Size of the array, in terms of number of elements.
* @param position Position, within the buffer, to write to.
* @param vertices Array of positions to write.
*/
template<size_t N>
void bufferPositions(
int32_t position,
std::array<glm::vec3, N> positions
) {
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
sizeof(glm::vec3) * position,
sizeof(positions),
(void *)positions.data()
);
}
/**
* Write vertice coordinates to the mesh.
*
* @tparam N Size of the array, in terms of number of elements.
* @param position Position, within the buffer, to write to.
* @param coordinates Array of coordinates to write.
*/
template<size_t N>
void bufferCoordinates(
int32_t position,
std::array<glm::vec2, N> coordinates
) {
auto offsetCoordinates = (
(sizeof(glm::vec3) * this->verticeCount) +
(sizeof(glm::vec2) * position)
);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferSubData(
GL_ARRAY_BUFFER,
offsetCoordinates,
sizeof(coordinates),
(void *)coordinates.data()
);
}
/**
* Write indices to the mesh.
*
* @tparam N Size of the array, in terms of number of elements.
* @param position Position, within the buffer, to write to.
* @param indices Array of indices to write.
*/
template<size_t N>
void bufferIndices(
int32_t position,
std::array<meshindice_t, N> indices
) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
glBufferSubData(
GL_ELEMENT_ARRAY_BUFFER,
sizeof(meshindice_t) * position,
sizeof(indices),
(void *)indices.data()
);
}
/**
* Draw a primitive. Primitives are drawn by their indices.
*
* @param drawMode Which drawing mode to use to draw the primitive.
* @param start Start indice (index) to draw.
* @param count Count of indices to draw. Use -1 to draw all.
*/
void draw(
enum MeshDrawMode drawMode,
int32_t start,
int32_t count
);
/**
* Cleanup a previously initiated mesh.
*/
~Mesh();
};
}

View File

@ -1,101 +1,101 @@
// 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 SimpleTexturedShader : 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,
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);
}
};
// 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 SimpleTexturedShader : 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,
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);
}
};
}

View File

@ -1,119 +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,
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(Texture *texture) {
this->bindTexture(this->paramTexture, texture);
}
void setUIColor(struct Color color) {
this->setColor(this->paramColor, color);
}
};
// 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,
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(Texture *texture) {
this->bindTexture(this->paramTexture, texture);
}
void setUIColor(struct Color color) {
this->setColor(this->paramColor, color);
}
};
}