Base refactor

This commit is contained in:
2023-11-14 09:16:48 -06:00
parent 214082d00f
commit 1817dcaf3a
410 changed files with 749 additions and 20823 deletions

View File

@ -26,5 +26,5 @@ target_include_directories(${DAWN_TARGET_NAME}
# Subdirs
add_subdirectory(assert)
add_subdirectory(display)
add_subdirectory(scene)
# add_subdirectory(display)
# add_subdirectory(scene)

View File

@ -4,6 +4,7 @@
// https://opensource.org/licenses/MIT
#include "assertgl.hpp"
#include "assert/assert.hpp"
#include "dawnopengl.hpp"
void assertNotGLErrorCheck(const char *file, int32_t line) {

View File

@ -4,7 +4,7 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "assert/assert.hpp"
#include "dawnlibs.hpp"
/**
* Asserts that there are no OpenGL errors.

View File

@ -1,53 +0,0 @@
// 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() {
}
float_t BackBufferRenderTarget::getScale() {
return this->scale;
}
float_t BackBufferRenderTarget::getWidth() {
return this->width;
}
float_t BackBufferRenderTarget::getHeight() {
return this->height;
}
void BackBufferRenderTarget::setSize(
const float_t width,
const float_t height
) {
if(this->width == width && this->height == height) return;
// Fixes a new bug that it seems GLFW has introduced.
this->width = width == 0 ? 1 : width;
this->height = height == 0 ? 1 : height;
this->eventRenderTargetResized.invoke(*this, width, height);
}
void BackBufferRenderTarget::setClearColor(const struct Color color) {
this->clearColor = color;
}
void BackBufferRenderTarget::clear(const flag8_t clearFlags) {
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
assertNoGLError();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
assertNoGLError();
}
void BackBufferRenderTarget::bind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
assertNoGLError();
glViewport(0, 0, (GLsizei)this->width, (GLsizei)this->height);
assertNoGLError();
}

View File

@ -1,47 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "assert/assertgl.hpp"
#include "display/RenderTarget.hpp"
namespace Dawn {
class RenderManager;
class BackBufferRenderTarget : public RenderTarget {
private:
float_t width = 1;
float_t height = 1;
struct Color clearColor = COLOR_CORNFLOWER_BLUE;
public:
float_t scale = 1.0f;
/**
* Construct the back buffer render target.
*/
BackBufferRenderTarget();
/**
* Requests to modify the viewport directly. This is mostly to be called
* by whatever is setting the window/display resolution, so that the
* backbuffer can keep track of what the viewport size is. This should
* also be DPI aware, e.g. "4k @ 2xDPI, resulting in a 1080p equiv" should
* still call this method with 3840, 2160.
*
* @param width New width of the back buffer.
* @param height New height of the back buffer.
*/
void setSize(const float_t width, const float_t height);
float_t getScale() override;
float_t getWidth() override;
float_t getHeight() override;
void setClearColor(const struct Color color) override;
void clear(const flag8_t clearFlags) override;
void bind() override;
};
}

View File

@ -1,17 +0,0 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
RenderManager.cpp
BackBufferRenderTarget.cpp
Texture.cpp
TextureRenderTarget.cpp
)
# Subdirs
add_subdirectory(mesh)
add_subdirectory(shader)

View File

@ -1,94 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "dawnopengl.hpp"
#include "assert/assertgl.hpp"
#include "game/DawnGame.hpp"
#include "display/RenderManager.hpp"
using namespace Dawn;
RenderManager::RenderManager() : IRenderManager() {
renderPipeline = std::make_shared<RenderPipeline>();
shaderManager = std::make_shared<ShaderManager>();
backBuffer = std::make_shared<BackBufferRenderTarget>();
}
void RenderManager::init(const std::weak_ptr<DawnGame> game) {
this->game = game;
// Init the render pipeline
renderPipeline->init(
std::static_pointer_cast<RenderManager>(shared_from_this())
);
// Lock the common shaders
lockSimpleTextured = shaderManager->lockShader<SimpleTexturedShader>();
simpleTexturedShader = shaderManager->getShader<SimpleTexturedShader>(
lockSimpleTextured
);
lockUIShaderProgram = shaderManager->lockShader<UIShader>();
uiShader = shaderManager->getShader<UIShader>(lockUIShaderProgram);
lockFontShader = shaderManager->lockShader<FontShader>();
fontShader = shaderManager->getShader<FontShader>(lockFontShader);
assertNoGLError();
// Prepare the initial values
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
assertNoGLError();
glBlendFuncSeparate(
GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA,
GL_ONE,
GL_ONE_MINUS_SRC_ALPHA
);
assertNoGLError();
glDepthMask(GL_TRUE);
assertNoGLError();
glDepthFunc(GL_LESS);
assertNoGLError();
}
std::shared_ptr<RenderTarget> RenderManager::getBackBuffer() {
return backBuffer;
}
std::shared_ptr<RenderPipeline> RenderManager::getRenderPipeline() {
return renderPipeline;
}
std::shared_ptr<ShaderManager> RenderManager::getShaderManager() {
return shaderManager;
}
void RenderManager::setRenderFlags(const flag_t flags) {
renderFlags = flags;
if((flags & RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST) == 0) {
glDisable(GL_DEPTH_TEST);
} else {
glEnable(GL_DEPTH_TEST);
}
assertNoGLError();
if((flags & RENDER_MANAGER_RENDER_FLAG_BLEND) == 0) {
glDisable(GL_BLEND);
} else {
glEnable(GL_BLEND);
}
assertNoGLError();
}
void RenderManager::update() {
this->getRenderPipeline()->render();
}
RenderManager::~RenderManager() {
shaderManager->releaseShader<SimpleTexturedShader>(lockSimpleTextured);
shaderManager->releaseShader<UIShader>(lockUIShaderProgram);
shaderManager->releaseShader<FontShader>(lockFontShader);
}

View File

@ -1,46 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/BackBufferRenderTarget.hpp"
#include "display/shader/ShaderManager.hpp"
#include "display/shader/shaders/SimpleTexturedShader.hpp"
#include "display/shader/shaders/FontShader.hpp"
#include "display/shader/shaders/UIShader.hpp"
#include "display/RenderPipeline.hpp"
namespace Dawn {
class RenderManager : public IRenderManager {
private:
std::shared_ptr<RenderPipeline> renderPipeline;
std::shared_ptr<ShaderManager> shaderManager;
shaderlock_t lockSimpleTextured = -1;
shaderlock_t lockUIShaderProgram = -1;
shaderlock_t lockFontShader = -1;
public:
std::shared_ptr<BackBufferRenderTarget> backBuffer;
std::shared_ptr<SimpleTexturedShader> simpleTexturedShader;
std::shared_ptr<UIShader> uiShader;
std::shared_ptr<FontShader> fontShader;
/**
* Construct a new RenderManager for a game instance.
*/
RenderManager();
std::shared_ptr<RenderTarget> getBackBuffer() override;
std::shared_ptr<RenderPipeline> getRenderPipeline() override;
std::shared_ptr<ShaderManager> getShaderManager() override;
void setRenderFlags(const flag_t renderFlags) override;
void init(const std::weak_ptr<DawnGame> game) override;
void update() override;
/**
* Destroy a previously initialized RenderManager.
*/
~RenderManager();
};
}

View File

@ -1,239 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Texture.hpp"
#include "assert/assertgl.hpp"
#include "util/memory.hpp"
using namespace Dawn;
Texture::Texture() : ITexture() {
useEffect([&]{
this->texturePropertiesNeedUpdating = true;
}, {
&this->wrapModeX, &this->wrapModeY,
&this->filterModeMin, &this->filterModeMag,
&this->mipmapFilterModeMin, &this->mipmapFilterModeMag
});
}
void Texture::bind(const textureslot_t slot) {
assertTrue(this->id != -1, "Texture is not ready!");
glActiveTexture(GL_TEXTURE0 + slot);
assertNoGLError();
glBindTexture(GL_TEXTURE_2D, this->id);
assertNoGLError();
if(this->texturePropertiesNeedUpdating) {
this->updateTextureProperties();
this->texturePropertiesNeedUpdating = false;
}
}
int32_t Texture::getWidth() {
return this->width;
}
int32_t Texture::getHeight() {
return this->height;
}
void Texture::setSize(
const int32_t width,
const int32_t height,
const enum TextureFormat format,
const enum TextureDataFormat dataFormat
) {
if(this->id != -1) {
glDeleteTextures(1, &this->id);
assertNoGLError();
this->id = -1;
}
int32_t maxSize;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
assertTrue(width > 0 && width <= maxSize, "Width is out of bounds!");
assertTrue(height > 0 && height <= maxSize, "Height is out of bounds!");
this->width = width;
this->height = height;
this->format = format;
this->dataFormat = dataFormat;
glGenTextures(1, &this->id);
assertNoGLError();
if(this->id <= 0) assertUnreachable("Texture generation failed!");
// Initialize the texture to blank
glActiveTexture(GL_TEXTURE0);
assertNoGLError();
this->bufferRaw(NULL);
}
void Texture::fill(struct Color color) {
auto pixels = memoryAllocate<struct Color>(
sizeof(struct Color) * this->width * this->height
);
this->buffer(pixels);
memoryFree(pixels);
}
void Texture::fill(const uint8_t color) {
auto pixels = memoryAllocate<uint8_t>(
sizeof(uint8_t) * this->width * this->height
);
this->buffer(pixels);
memoryFree(pixels);
}
bool_t Texture::isReady() {
return this->id != -1;
}
void Texture::updateTextureProperties() {
auto setWrapMode = [&](GLenum axis, enum TextureWrapMode wm) {
switch(wm) {
case TEXTURE_WRAP_MODE_REPEAT:
glTexParameteri(GL_TEXTURE_2D, axis, GL_REPEAT);
break;
case TEXTURE_WRAP_MODE_MIRRORED_REPEAT:
glTexParameteri(GL_TEXTURE_2D, axis, GL_MIRRORED_REPEAT);
break;
case TEXTURE_WRAP_MODE_CLAMP_TO_EDGE:
glTexParameteri(GL_TEXTURE_2D, axis, GL_CLAMP_TO_EDGE);
break;
case TEXTURE_WRAP_MODE_CLAMP_TO_BORDER:
glTexParameteri(GL_TEXTURE_2D, axis, GL_CLAMP_TO_BORDER);
break;
default:
assertUnreachable("Unknown wrap mode!");
}
assertNoGLError();
};
setWrapMode(GL_TEXTURE_WRAP_S, this->wrapModeX);
setWrapMode(GL_TEXTURE_WRAP_T, this->wrapModeY);
auto setFilterMode = [&](
GLenum minMag,
enum TextureFilterMode filter,
enum TextureFilterMode mapFilterMode
) {
switch(filter) {
case TEXTURE_FILTER_MODE_NEAREST: {
glTexParameteri(GL_TEXTURE_2D, minMag, GL_NEAREST);
break;
}
case TEXTURE_FILTER_MODE_LINEAR: {
glTexParameteri(GL_TEXTURE_2D, minMag, GL_LINEAR);
break;
}
default: {
assertUnreachable("Unknown filter mode!");
}
}
assertNoGLError();
};
setFilterMode(
GL_TEXTURE_MIN_FILTER, this->filterModeMin, this->mipmapFilterModeMin
);
setFilterMode(
GL_TEXTURE_MAG_FILTER, this->filterModeMag, this->mipmapFilterModeMag
);
}
void Texture::bufferRaw(const void *data) {
assertTrue(this->isReady(), "Texture is not ready!");
GLenum format;
switch(this->format) {
case TEXTURE_FORMAT_R:
format = GL_RED;
break;
case TEXTURE_FORMAT_RG:
format = GL_RG;
break;
case TEXTURE_FORMAT_RGB:
format = GL_RGB;
break;
case TEXTURE_FORMAT_RGBA:
format = GL_RGBA;
break;
default:
assertUnreachable("Unknown texture format!");
}
GLenum dataFormat;
switch(this->dataFormat) {
case TEXTURE_DATA_FORMAT_UNSIGNED_BYTE:
dataFormat = GL_UNSIGNED_BYTE;
break;
case TEXTURE_DATA_FORMAT_FLOAT:
dataFormat = GL_FLOAT;
break;
default:
assertUnreachable("Unknown texture data format!");
}
glBindTexture(GL_TEXTURE_2D, this->id);
assertNoGLError();
glTexImage2D(
GL_TEXTURE_2D, 0, format,
this->width, this->height,
0, format, dataFormat, data
);
assertNoGLError();
glGenerateMipmap(GL_TEXTURE_2D);
assertNoGLError();
this->texturePropertiesNeedUpdating = true;
}
void Texture::buffer(const struct ColorU8 pixels[]) {
assertTrue(
this->dataFormat == TEXTURE_DATA_FORMAT_UNSIGNED_BYTE,
"Texture data format must be unsigned byte!"
);
this->bufferRaw((void*)pixels);
}
void Texture::buffer(const struct Color pixels[]) {
assertTrue(
this->dataFormat == TEXTURE_DATA_FORMAT_FLOAT,
"Texture data format must be float!"
);
assertTrue(
this->format == TEXTURE_FORMAT_RGBA,
"Texture format must be RGBA!"
);
this->bufferRaw((void*)pixels);
}
void Texture::buffer(const uint8_t pixels[]) {
assertTrue(
this->dataFormat == TEXTURE_DATA_FORMAT_UNSIGNED_BYTE,
"Texture data format must be unsigned byte!"
);
this->bufferRaw((void*)pixels);
}
Texture::~Texture() {
if(this->id != -1) {
glDeleteTextures(1, &this->id);
assertNoGLError();
}
}

View File

@ -1,54 +0,0 @@
// 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/ITexture.hpp"
namespace Dawn {
class TextureRenderTarget;
typedef GLuint textureslot_t;
class Texture : public ITexture {
private:
int32_t width = -1;
int32_t height = -1;
GLuint id = -1;
enum TextureFormat format;
enum TextureDataFormat dataFormat;
void updateTextureProperties();
void bufferRaw(const void *data);
public:
Texture();
int32_t getWidth() override;
int32_t getHeight() override;
void setSize(
const int32_t width,
const int32_t height,
const enum TextureFormat format,
const enum TextureDataFormat dataFormat
) override;
void fill(const struct Color) override;
void fill(const uint8_t) override;
bool_t isReady() override;
void buffer(const struct ColorU8 pixels[]) override;
void buffer(const struct Color pixels[]);
void buffer(const uint8_t pixels[]) override;
/**
* Binds the texture to the given slot (for use by the shaders).
*
* @param slot Slot to bind to.
*/
void bind(const textureslot_t slot);
~Texture();
friend class TextureRenderTarget;
};
}

View File

@ -1,116 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TextureRenderTarget.hpp"
#include "assert/assertgl.hpp"
using namespace Dawn;
TextureRenderTarget::TextureRenderTarget(
const float_t width,
const float_t height
) {
this->texture = std::make_shared<Texture>();
this->setSize(width, height);
}
std::shared_ptr<Texture> TextureRenderTarget::getTexture() {
return this->texture;
}
void TextureRenderTarget::setSize(float_t width, float_t height) {
assertTrue(width > 0, "TextureRenderTarget::setSize: Width must be greater than 0!");
assertTrue(height > 0, "TextureRenderTarget::setSize: Height must be greater than 0!");
if(width == this->getWidth() && height == this->getHeight()) return;
// Delete old buffers.
if(this->rboId != -1) glDeleteRenderbuffers(1, &this->rboId);
assertNoGLError();
if(this->fboId != -1) glDeleteFramebuffers(1, &this->fboId);
assertNoGLError();
// Resize texture
this->texture->setSize(
(int32_t)width,
(int32_t)height,
TEXTURE_FORMAT_RGBA,
TEXTURE_DATA_FORMAT_FLOAT
);
this->eventRenderTargetResized.invoke(*this, width, height);
// Create Frame Buffer
glGenFramebuffers(1, &this->fboId);
assertNoGLError();
glBindFramebuffer(GL_FRAMEBUFFER, this->fboId);
assertNoGLError();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, this->texture->id, 0
);
assertNoGLError();
// Create Render Buffer
glGenRenderbuffers(1, &this->rboId);
assertNoGLError();
glBindRenderbuffer(GL_RENDERBUFFER, this->rboId);
assertNoGLError();
glRenderbufferStorage(
GL_RENDERBUFFER,
GL_DEPTH24_STENCIL8,
this->texture->width,
this->texture->height
);
assertNoGLError();
glBindRenderbuffer(GL_RENDERBUFFER, 0);
assertNoGLError();
glFramebufferRenderbuffer(
GL_FRAMEBUFFER,
GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER,
this->rboId
);
assertNoGLError();
// Validate things went correct.
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
assertUnreachable("TextureRenderTarget::setSize: Framebuffer is not complete!");
}
}
float_t TextureRenderTarget::getScale() {
return 1.0f;
}
float_t TextureRenderTarget::getWidth() {
return (float_t)this->texture->getWidth();
}
float_t TextureRenderTarget::getHeight() {
return (float_t)this->texture->getHeight();
}
void TextureRenderTarget::setClearColor(const struct Color color) {
this->clearColor = color;
}
void TextureRenderTarget::clear(const flag8_t clearFlags) {
glClearColor(
clearColor.r,
clearColor.g,
clearColor.b,
clearColor.a
);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void TextureRenderTarget::bind() {
glBindFramebuffer(GL_FRAMEBUFFER, this->fboId);
glViewport(0, 0, this->texture->getWidth(), this->texture->getHeight());
}
TextureRenderTarget::~TextureRenderTarget() {
if(this->rboId != -1) glDeleteRenderbuffers(1, &this->rboId);
if(this->fboId != -1) glDeleteFramebuffers(1, &this->fboId);
}

View File

@ -1,38 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "display/RenderTarget.hpp"
#include "display/Texture.hpp"
namespace Dawn {
class RenderManager;
class TextureRenderTarget : public RenderTarget {
private:
GLuint fboId = -1;
GLuint rboId = -1;
std::shared_ptr<Texture> texture;
struct Color clearColor = COLOR_CORNFLOWER_BLUE;
public:
TextureRenderTarget(const float_t width, const float_t height);
std::shared_ptr<Texture> getTexture();
void setSize(const float_t width, const float_t height);
float_t getScale() override;
float_t getWidth() override;
float_t getHeight() override;
void setClearColor(const struct Color color) override;
void clear(const flag8_t clearFlags) override;
void bind() override;
~TextureRenderTarget();
};
}

View File

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

View File

@ -1,204 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Mesh.hpp"
using namespace Dawn;
void Mesh::createBuffers(
int32_t verticeCount,
int32_t indiceCount
) {
if(verticeCount <= 0) throw "Vertice count must be greater than zero.";
if(indiceCount <= 0) throw "Indice count must be greater than zero.";
this->disposeBuffers();
this->verticeCount = verticeCount;
this->indiceCount = indiceCount;
auto sizePos = sizeof(glm::vec3) * verticeCount;
auto sizeInds = sizeof(meshindice_t) * indiceCount;
auto sizeCoords = sizeof(glm::vec2) * verticeCount;
// Generate vertex array, I don't think I need to do this tbh.
glGenVertexArrays(1, &this->vertexArray);
assertNoGLError();
glBindVertexArray(this->vertexArray);
assertNoGLError();
// Create some buffers, one for the vertex data, one for the indices
GLuint buffer[2];
glGenBuffers(2, buffer);
assertNoGLError();
this->vertexBuffer = buffer[0];
if(this->vertexBuffer < 0) throw "Failed to create vertex buffer";
this->indexBuffer = buffer[1];
if(this->indexBuffer < 0) throw "Failed to create index buffer";
// Buffer an empty set of data then buffer each component
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
assertNoGLError();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
assertNoGLError();
glBufferData(GL_ARRAY_BUFFER, sizePos+sizeCoords, 0, GL_DYNAMIC_DRAW);
assertNoGLError();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeInds, 0, GL_DYNAMIC_DRAW);
assertNoGLError();
// Setup the attrib pointers
size_t offset = 0;
glVertexAttribPointer(
0, sizeof(glm::vec3) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(0);
assertNoGLError();
offset += sizePos;
glVertexAttribPointer(
1, sizeof(glm::vec2) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(1);
assertNoGLError();
}
void Mesh::disposeBuffers() {
glBindBuffer(GL_ARRAY_BUFFER, 0);
assertNoGLError();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
assertNoGLError();
if(this->vertexBuffer != -1) {
glDeleteBuffers(1, &this->vertexBuffer);
assertNoGLError();
this->vertexBuffer = -1;
this->verticeCount = -1;
}
if(this->indexBuffer != -1) {
glDeleteBuffers(1, &this->indexBuffer);
assertNoGLError();
this->indexBuffer = -1;
this->indiceCount = -1;
}
if(this->vertexArray) {
glDeleteVertexArrays(1, &this->vertexArray);
assertNoGLError();
this->vertexArray = -1;
}
}
void Mesh::bufferPositions(int32_t pos, glm::vec3 *positions, int32_t len) {
assertNotNull(positions, "Positions cannot be null");
assertTrue(pos >= 0 && pos < verticeCount, "Position must be within range");
assertTrue(pos+len <= verticeCount, "Position + Length must be within range");
assertTrue(len > 0, "Length must be greater than zero");
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
assertNoGLError();
glBufferSubData(
GL_ARRAY_BUFFER,
sizeof(glm::vec3) * pos,
sizeof(glm::vec3) * len,
(void*)positions
);
assertNoGLError();
}
void Mesh::bufferCoordinates(int32_t pos, glm::vec2 *coordinates, int32_t len) {
assertNotNull(coordinates, "Coordinates cannot be null");
assertTrue(pos >= 0 && pos < verticeCount, "Position must be within range");
assertTrue(pos+len <= verticeCount, "Position + Length must be within range");
assertTrue(len > 0, "Length must be greater than zero");
auto offsetCoordinates = (
(sizeof(glm::vec3) * this->verticeCount) +
(sizeof(glm::vec2) * pos)
);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
assertNoGLError();
glBufferSubData(
GL_ARRAY_BUFFER,
offsetCoordinates,
sizeof(glm::vec2) * len,
(void*)coordinates
);
assertNoGLError();
}
void Mesh::bufferIndices(int32_t pos, meshindice_t *indices, int32_t len) {
assertNotNull(indices, "Indices cannot be null");
assertTrue(pos >= 0 && pos < indiceCount, "Position must be within range");
assertTrue(pos+len <= indiceCount, "Position + Length must be within range");
assertTrue(len > 0, "Length must be greater than zero");
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
assertNoGLError();
glBufferSubData(
GL_ELEMENT_ARRAY_BUFFER,
sizeof(meshindice_t) * pos,
sizeof(meshindice_t) * len,
(void*)indices
);
assertNoGLError();
}
void Mesh::draw(enum MeshDrawMode drawMode, int32_t start, int32_t count) {
if(
count == 0 ||
this->vertexBuffer == -1 ||
this->indexBuffer == -1
) return;
if(count == -1) count = this->indiceCount;
// Re-Bind the buffers
glBindVertexArray(this->vertexArray);
assertNoGLError();
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
assertNoGLError();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
assertNoGLError();
// Re-Calculate the attrib pointers.
size_t offset = 0;
glVertexAttribPointer(
0, sizeof(glm::vec3) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(0);
assertNoGLError();
offset += sizeof(glm::vec3) * this->verticeCount;
glVertexAttribPointer(
1, sizeof(glm::vec2) / sizeof(float_t),
GL_FLOAT, GL_FALSE,
0, (void *)offset
);
assertNoGLError();
glEnableVertexAttribArray(1);
assertNoGLError();
// Render the elements.
glDrawElements(
drawMode, count, GL_UNSIGNED_INT, (void *)(sizeof(meshindice_t) * start)
);
assertNoGLError();
}
Mesh::~Mesh() {
this->disposeBuffers();
}

View File

@ -1,95 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "assert/assertgl.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,
MESH_DRAW_MODE_LINES = GL_LINES
};
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;
/** Pointer to the vertex buffer on the GPU */
GLuint vertexArray = -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.
*
* @param pos Position, within the buffer, to write to.
* @param vertices Array of positions to write.
* @param len How many positions are in the array.
*/
void bufferPositions(int32_t pos, glm::vec3 *positions, int32_t len);
/**
* Write vertice coordinates to the mesh.
*
* @param pos Position, within the buffer, to write to.
* @param coordinates Array of coordinates to write.
* @param len How many coordinates are in the array.
*/
void bufferCoordinates(int32_t pos, glm::vec2 *coordinates, int32_t len);
/**
* Write indices to the mesh.
*
* @param pos Position, within the buffer, to write to.
* @param indices Array of indices to write.
* @param len How many indices are in the array.
*/
void bufferIndices(int32_t pos, meshindice_t *indices, int32_t len);
/**
* 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,13 +0,0 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Shader.cpp
)
add_subdirectory(buffers)
add_subdirectory(shaders)

View File

@ -1,165 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "assert/assertgl.hpp"
#include "Shader.hpp"
using namespace Dawn;
void Shader::compileShader(
std::map<std::string, int32_t> attributeLocations,
const std::string vertexShader,
const std::string fragmentShader
) {
GLint isSuccess;
int32_t maxLength;
char error[1024];
// Load the vertex shader first
this->shaderVertex = glCreateShader(GL_VERTEX_SHADER);
auto vertShaderC = vertexShader.c_str();
glShaderSource(this->shaderVertex, 1, &vertShaderC, 0);
assertNoGLError();
glCompileShader(this->shaderVertex);
assertNoGLError();
// Validate
glGetShaderiv(this->shaderVertex, GL_COMPILE_STATUS, &isSuccess);
if(!isSuccess) {
glGetShaderiv(this->shaderVertex, GL_INFO_LOG_LENGTH, &maxLength);
glGetShaderInfoLog(this->shaderVertex, maxLength, &maxLength, error);
assertUnreachable("Error compiling vert shader %s", error);
throw error;
}
assertNoGLError();
// Now load the Frag shader
this->shaderFrag = glCreateShader(GL_FRAGMENT_SHADER);
auto fragShaderC = fragmentShader.c_str();
glShaderSource(this->shaderFrag, 1, &fragShaderC, 0);
glCompileShader(this->shaderFrag);
glGetShaderiv(this->shaderFrag, GL_COMPILE_STATUS, &isSuccess);
if(!isSuccess) {
glGetShaderiv(this->shaderFrag, GL_INFO_LOG_LENGTH, &maxLength);
glGetShaderInfoLog(this->shaderFrag, maxLength, &maxLength, error);
glDeleteShader(this->shaderVertex);
assertUnreachable("Error compiling frag shader %s", error);
throw error;
}
assertNoGLError();
// Now create the shader program.
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, shaderVertex);
glAttachShader(shaderProgram, shaderFrag);
assertNoGLError();
// Now parse out the variables.
#if DAWN_OPENGL_HLSL
auto itAttr = attributeLocations.begin();
while(itAttr != attributeLocations.end()) {
this->bindAttributeLocation(itAttr->first, itAttr->second);
++itAttr;
}
#endif
//Bind, Verify & Use the shader program
glLinkProgram(this->shaderProgram);
glGetProgramiv(this->shaderProgram, GL_LINK_STATUS, &isSuccess);
if(!isSuccess) {
glGetProgramiv(this->shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
glGetProgramInfoLog(this->shaderProgram, maxLength, &maxLength, error);
glDeleteShader(this->shaderVertex);
glDeleteShader(this->shaderFrag);
assertUnreachable("Error compiling shader program %s", error);
throw error;
}
assertNoGLError();
}
void Shader::bindAttributeLocation(
const std::string name,
const int32_t location
) {
if(this->shaderProgram == -1) throw "Shader has not yet been compiled";
glBindAttribLocation(this->shaderProgram, location, name.c_str());
assertNoGLError();
}
void Shader::setTexture(
const shaderparameter_t param,
const textureslot_t slot
) {
glUniform1i(param, slot);
assertNoGLError();
}
shaderparameter_t Shader::getParameterByName(const std::string name) {
return glGetUniformLocation(this->shaderProgram, name.c_str());
}
shaderbufferlocation_t Shader::getBufferLocationByName(const std::string name) {
return glGetUniformBlockIndex(this->shaderProgram, name.c_str());
}
void Shader::setParameterBuffer(
const shaderbufferlocation_t location,
const shaderbufferslot_t slot
) {
glUniformBlockBinding(this->shaderProgram, location, slot);
assertNoGLError();
}
void Shader::setMatrix(
const shaderparameter_t uniform,
const glm::mat4 matrix
) {
glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix));
assertNoGLError();
}
void Shader::setBoolean(
const shaderparameter_t uni,
const bool value
) {
glUniform1i(uni, value);
assertNoGLError();
}
void Shader::setColor(
const shaderparameter_t uniform,
const struct Color color
) {
glUniform4f(uniform, color.r, color.g, color.b, color.a);
assertNoGLError();
}
void Shader::setVector3(
const shaderparameter_t uniform,
const glm::vec3 vector
) {
glUniform3f(uniform, vector.x, vector.y, vector.z);
assertNoGLError();
}
void Shader::setFloat(
const shaderparameter_t param,
const float_t value
) {
glUniform1f(param, value);
assertNoGLError();
}
void Shader::bind() {
assertTrue(shaderProgram != -1, "Cannot bind a program that is not ready");
glUseProgram(shaderProgram);
assertNoGLError();
}
Shader::~Shader() {
if(shaderProgram != -1) glDeleteProgram(shaderProgram);
if(shaderVertex != -1) glDeleteShader(shaderVertex);
if(shaderFrag != -1) glDeleteShader(shaderFrag);
}

View File

@ -1,111 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/shader/IShader.hpp"
#include "dawnopengl.hpp"
typedef GLuint shaderparameter_t;
namespace Dawn {
class Shader : public IShader<shaderparameter_t> {
private:
/** Pointer to an uploaded vertex shader program */
GLuint shaderVertex = -1;
/** Pointer to an uploaded fragment shader program */
GLuint shaderFrag = -1;
/** Pointer to an uploaded shader program linked */
GLuint shaderProgram = -1;
protected:
/**
* Compiles a GLSL/HLSL shader and stores it on the GPU, updates the
* underlying pointers for you.
*
* @param vertexShader The string source of the vertex shader.
* @param fragmentShader The string source of the fragment shader.
*/
void compileShader(
std::map<std::string, int32_t> attributeLocations,
const std::string vertexShader,
const std::string fragmentShader
);
/**
* Typically HLSL only, this method allows you to specify where vbo
* attributes are bound. Typically 0 for positions, 1 for coordinates,
* etc.
*
* @param name Attribute name in the HLSL shader.
* @param location Index pointing to which location it is to be bound to.
*/
void bindAttributeLocation(
const std::string name,
const int32_t location
);
public:
/**
* Locate a shader parameter by its name.
*
* @param name Name of the parameter to get.
* @return The shader parameter.
*/
shaderparameter_t getParameterByName(const std::string name);
/**
* Locate a shader buffer parameter set by its name.
*
* @param name Name of the buffer to get.
* @return The shader buffer.
*/
shaderbufferlocation_t getBufferLocationByName(const std::string name);
virtual void compile() override = 0;
void bind() override;
void setParameterBuffer(
const shaderbufferlocation_t location,
const shaderbufferslot_t slot
);
void setMatrix(
const shaderparameter_t parameter,
const glm::mat4 matrix
) override;
void setBoolean(
const shaderparameter_t parameter,
const bool_t value
) override;
void setColor(
const shaderparameter_t parameter,
const struct Color color
) override;
void setVector3(
const shaderparameter_t parameter,
const glm::vec3 vector
) override;
void setTexture(
const shaderparameter_t parameter,
const textureslot_t texture
) override;
void setFloat(
const shaderparameter_t parameter,
const float_t value
) override;
/**
* Destroys and deletes the shader from the GPU.
*/
~Shader();
};
}

View File

@ -1,127 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "display/shader/IShaderParameterBuffer.hpp"
#include "ShaderParameterBufferTypes.hpp"
namespace Dawn {
typedef GLuint shaderbufferslot_t;
typedef GLuint shaderbufferlocation_t;
template<typename T>
class ShaderParameterBuffer :
public IShaderParameterBuffer<shaderbufferslot_t>
{
protected:
shaderbufferlocation_t id = -1;
size_t size;
public:
void init() {
assertTrue(
this->id == -1,
"ShaderParameterBuffer is already initialized!"
);
this->size = sizeof(T);
glGenBuffers(1, &this->id);
assertNoGLError();
glBindBuffer(GL_UNIFORM_BUFFER, this->id);
assertNoGLError();
glBufferData(GL_UNIFORM_BUFFER, this->size, NULL, GL_DYNAMIC_DRAW);
assertNoGLError();
}
bool_t isReady() {
return this->id != -1;
}
/**
* 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->bufferRaw((void*)data);
}
void bind(shaderbufferslot_t location) override {
assertTrue(this->isReady(), "ShaderParameterBuffer is not ready!");
glBindBuffer(GL_UNIFORM_BUFFER, this->id);
assertNoGLError();
glBindBufferBase(GL_UNIFORM_BUFFER, location, this->id);
assertNoGLError();
}
/**
* Buffers the entire contents of the data struct to this shader param
* buffer object.
*
* @param data Raw data to buffer.
*/
void bufferRaw(void *data) {
this->bufferRaw(data, 0, this->size);
}
/**
* Buffers a specific range of data to this shader param buffer object.
*
* @param data Raw data to buffer.
* @param start Start position of the data to buffer.
* @param length Length of the data to buffer.
*/
void bufferRaw(void *data, size_t start, size_t length) {
assertTrue(this->isReady(), "ShaderParameterBuffer is not ready!");
glBindBuffer(GL_UNIFORM_BUFFER, this->id);
assertNoGLError();
glBufferSubData(
GL_UNIFORM_BUFFER, start, length, (void*)((size_t)data + start)
);
assertNoGLError();
}
/**
* Buffers a sub-range of data to this shader param buffer object.
*
* @param data Raw data to buffer.
* @param sub Pointer to the start of the sub-range.
*/
template<typename D>
void buffer(T* data, D* sub) {
size_t start = (size_t)sub - (size_t)data;
this->bufferRaw((void*)data, start, sizeof(D));
}
/**
* Buffers a sub-range of data to this shader param buffer object.
*
* @param data Raw data to buffer.
* @param subStart Pointer to the start of the sub-range.
* @param subEnd Pointer to the end of the sub-range, inclusive.
*/
template<typename D0, typename D1>
void buffer(T *data, D0 *subStart, D1 *subEnd) {
this->bufferRaw(
(void*)data,
(size_t)subStart - (size_t)data,
((size_t)subEnd - (size_t)subStart) + sizeof(D1)
);
}
/**
* Destroys this shader parameter buffer.
*/
~ShaderParameterBuffer() {
if(this->id != -1) {
glDeleteBuffers(1, &this->id);
assertNoGLError();
this->id = -1;
}
}
};
}

View File

@ -1,54 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "assert/assertgl.hpp"
#include "util/macro.hpp"
// Definition Helpers
#define SHADER_PARAMETER_BUFFER_ARRAY_DEFINE(name, size, contents) \
struct MACRO_JOIN(name, _struct) { \
contents \
}; \
struct MACRO_JOIN(name, _struct) name[size];
#define SHADER_PARAMETER_BUFFER_DEFINE(name, structure) \
struct MACRO_JOIN(name, Data) {\
structure; \
}; \
class name : public ShaderParameterBuffer<struct MACRO_JOIN(name, Data)> { };
// Integer and Integer Arrays
#ifdef DAWN_OPENGL_SHADER_BUFFER_INTEGER_PADDING
#define SHADER_PARAMETER_BUFFER_INTEGER(name, i) \
int32_t name; \
int32_t MACRO_JOIN(padding, i)[DAWN_OPENGL_SHADER_BUFFER_INTEGER_PADDING];
#else
#define SHADER_PARAMETER_BUFFER_INTEGER(name, i) int32_t name;
#endif
#define SHADER_PARAMETER_BUFFER_INTEGER_ARRAY(name, size) SHADER_PARAMETER_BUFFER_ARRAY_DEFINE(name, size, SHADER_PARAMETER_BUFFER_INTEGER(value, 0))
// Color and Color Arrays
#define SHADER_PARAMETER_BUFFER_COLOR(name) \
struct Color name;
#define SHADER_PARAMETER_BUFFER_COLOR_ARRAY(name, size) \
struct Color name[size];
// MAT4
#define SHADER_PARAMETER_BUFFER_MAT4(name) \
glm::mat4 name;
// VEC2
#define SHADER_PARAMETER_BUFFER_VEC2(name) \
glm::vec2 name;
#define SHADER_PARAMETER_BUFFER_VEC2_ARRAY(name, size) \
glm::vec2 name[size];
// EOF Fix
#define NOTHING "Fixes an error with EOF"

View File

@ -1,9 +0,0 @@
# 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

@ -1,30 +0,0 @@
// 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 {
SHADER_PARAMETER_BUFFER_MAT4(view);
SHADER_PARAMETER_BUFFER_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

@ -1,13 +0,0 @@
# 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
UIShader.cpp
)

View File

@ -1,88 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "FontShader.hpp"
#include "display/mesh/QuadMesh.hpp"
using namespace Dawn;
void FontShader::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"
"layout (std140) uniform ub_UICanvas {\n"
"mat4 u_View;\n"
"mat4 u_Projection;\n"
"};"
"layout (shared) uniform ub_Font {\n"
"vec4 u_FontColors[" MACRO_STRINGIFY(FONT_SHADER_PARTS_MAX) "];\n"
"int u_FontTextures[" MACRO_STRINGIFY(FONT_SHADER_PARTS_MAX) "];\n"
"vec2 u_FontLinePositions[" MACRO_STRINGIFY(FONT_SHADER_PARTS_MAX) "];\n"
"int u_FontQuadMappings[" MACRO_STRINGIFY(FONT_SHADER_QUADS_MAX) "];\n"
"};\n"
"uniform mat4 u_Model;\n"
"out vec2 o_TextCoord;\n"
"out vec4 o_VertColor;\n"
"flat out int o_TextIndex;\n"
"void main() {\n"
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"int quadIndex = gl_VertexID / " MACRO_STRINGIFY(QUAD_VERTICE_COUNT) ";\n"
"int partIndex = u_FontQuadMappings[quadIndex];\n"
"gl_Position = u_Projection * u_View * u_Model * (\n"
"vec4(aPos.xy, 0, 1.0) + vec4(u_FontLinePositions[partIndex], 0, 0)\n"
");\n"
"o_VertColor = u_FontColors[partIndex];\n"
"o_TextIndex = u_FontTextures[partIndex];\n"
"}",
// Fragment Shader
"#version 330 core\n"
"in vec2 o_TextCoord;\n"
"in vec4 o_VertColor;\n"
"flat in int o_TextIndex;\n"
"out vec4 o_Color;\n"
"uniform sampler2D u_Text0;\n"
"uniform sampler2D u_Text1;\n"
"uniform sampler2D u_Text2;\n"
"uniform sampler2D u_Text3;\n"
"void main() {\n"
"o_Color = o_VertColor;\n"
"vec4 tColor;"
"if(o_TextIndex == 0) \n{"
"tColor = texture(u_Text0, o_TextCoord);\n"
"} else if(o_TextIndex == 1) \n{"
"tColor = texture(u_Text1, o_TextCoord);\n"
"} else if(o_TextIndex == 2) \n{"
"tColor = texture(u_Text2, o_TextCoord);\n"
"} else {\n"
"tColor = texture(u_Text3, o_TextCoord);\n"
"}\n"
"o_Color.a *= tColor.r;\n"
"}\n"
);
#else
#error Shader Type unknown
#endif
this->paramModel = this->getParameterByName("u_Model");
this->bufferUiCanvas = this->getBufferLocationByName("ub_UICanvas");
this->bufferFont = this->getBufferLocationByName("ub_Font");
this->paramTexture0 = this->getParameterByName("u_Text0");
this->paramTexture1 = this->getParameterByName("u_Text1");
this->paramTexture2 = this->getParameterByName("u_Text2");
this->paramTexture3 = this->getParameterByName("u_Text3");
}

View File

@ -1,34 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIShader.hpp"
#include "util/macro.hpp"
#define FONT_SHADER_PARTS_MAX 8
#define FONT_SHADER_QUADS_MAX 1024
#define FONT_SHADER_TEXTURE_MAX 4
namespace Dawn {
SHADER_PARAMETER_BUFFER_DEFINE(FontShaderBuffer, \
SHADER_PARAMETER_BUFFER_COLOR_ARRAY(colors, FONT_SHADER_PARTS_MAX);
SHADER_PARAMETER_BUFFER_INTEGER_ARRAY(textures, FONT_SHADER_PARTS_MAX);
SHADER_PARAMETER_BUFFER_VEC2_ARRAY(linePositions, FONT_SHADER_PARTS_MAX);
SHADER_PARAMETER_BUFFER_INTEGER_ARRAY(quadMappings, FONT_SHADER_QUADS_MAX);
);
class FontShader : public Shader {
public:
shaderparameter_t paramModel;
shaderparameter_t paramTexture0;
shaderparameter_t paramTexture1;
shaderparameter_t paramTexture2;
shaderparameter_t paramTexture3;
shaderbufferlocation_t bufferUiCanvas;
shaderbufferlocation_t bufferFont;
void compile() override;
};
}

View File

@ -1,62 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SimpleBillboardedShader.hpp"
using namespace Dawn;
void SimpleBillboardedShader::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_Model;\n"
"out vec2 o_TextCoord;\n"
"void main() {\n"
"vec3 billboardPos = u_Model[3].xyz;\n"
"vec3 viewDirection = normalize(billboardPos - u_View[3].xyz);\n"
"vec3 up = normalize(vec3(0, 1, 0));\n"
"vec3 right = normalize(cross(up, viewDirection));\n"
"up = normalize(cross(viewDirection, right));\n"
"vec3 billboardPosCam = vec3(u_View * vec4(billboardPos, 1.0));\n"
"gl_Position = u_Projection * vec4(billboardPosCam + (right * aPos.x + up * aPos.y), 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"
);
#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 +0,0 @@
// 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"
#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp"
namespace Dawn {
class SimpleBillboardedShader : public Shader {
public:
shaderbufferlocation_t bufferRenderPipeline;
shaderparameter_t paramModel;
shaderparameter_t paramColor;
shaderparameter_t paramTexture;
shaderparameter_t paramHasTexture;
void compile() override;
};
}

View File

@ -1,99 +0,0 @@
// 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_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(\n"
"mul(mul(float4(aPos, 1.0), u_Model), u_View), u_Proj\n"
");\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 +0,0 @@
// 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"
#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp"
namespace Dawn {
class SimpleTexturedShader : public Shader {
public:
shaderparameter_t paramModel;
shaderparameter_t paramColor;
shaderparameter_t paramTexture;
shaderparameter_t paramHasTexture;
shaderbufferlocation_t bufferRenderPipeline;
void compile() override;
};
}

View File

@ -1,63 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIShader.hpp"
using namespace Dawn;
void UIShader::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"
"layout (std140) uniform ub_UICanvas {\n"
"mat4 u_View;\n"
"mat4 u_Projection;\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"
// "o_Color = u_Color;\n"
// "o_Color.a = texture(u_Text, o_TextCoord).r;\n"
"} else {\n"
"o_Color = u_Color;"
"}\n"
"}\n"
);
#else
#error Shader Type must be GLSL
#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->bufferUiCanvas = this->getBufferLocationByName("ub_UICanvas");
}

View File

@ -1,28 +0,0 @@
// 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 "display/shader/buffers/RenderPipelineShaderBuffer.hpp"
#define UI_SHADER_PROGRAM_PRIORITY 1000
namespace Dawn {
SHADER_PARAMETER_BUFFER_DEFINE(UICanvasShaderBuffer, \
SHADER_PARAMETER_BUFFER_MAT4(projection);
SHADER_PARAMETER_BUFFER_MAT4(view);
);
class UIShader : public Shader {
public:
shaderparameter_t paramModel;
shaderparameter_t paramColor;
shaderparameter_t paramTexture;
shaderparameter_t paramHasTexture;
shaderbufferlocation_t bufferUiCanvas;
void compile() override;
};
}

View File

@ -1,7 +0,0 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
add_subdirectory(components)

View File

@ -1,7 +0,0 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
add_subdirectory(display)

View File

@ -1,7 +0,0 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirs
add_subdirectory(material)

View File

@ -1,11 +0,0 @@
# 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
SimpleTexturedMaterial.cpp
SimpleBillboardedMaterial.cpp
)

View File

@ -1,57 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/shader/shaders/SimpleBillboardedShader.hpp"
#include "SimpleBillboardedMaterial.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
SimpleBillboardedMaterial::SimpleBillboardedMaterial(std::weak_ptr<SceneItem> i) :
Material(i)
{
}
void SimpleBillboardedMaterial::onStart() {
this->shaderLock = getShaderManager().lockShader<SimpleBillboardedShader>();
}
void SimpleBillboardedMaterial::onDispose() {
getShaderManager().releaseShader<SimpleBillboardedShader>(this->shaderLock);
}
std::vector<struct ShaderPassItem>
SimpleBillboardedMaterial::getRenderPasses(IRenderableContext &context)
{
auto mesh = item.lock()->getComponent<MeshRenderer>();
auto shader = getShaderManager().getShader<SimpleBillboardedShader>(this->shaderLock);
assertNotNull(mesh, "Mesh cannot be null");
assertNotNull(mesh->mesh, "Mesh cannot be null");
assertNotNull(shader, "Shader cannot be null");
struct ShaderPassItem onlyPass;
onlyPass.mesh = mesh->mesh;
onlyPass.shader = shader;
onlyPass.colorValues[shader->paramColor] = this->color;
onlyPass.matrixValues[shader->paramModel] = item.lock()->getWorldTransform();
onlyPass.parameterBuffers[shader->bufferRenderPipeline] =
&context.renderPipeline->shaderBuffer
;
onlyPass.renderFlags = (
RENDER_MANAGER_RENDER_FLAG_BLEND
);
if(this->texture != nullptr) {
onlyPass.boolValues[shader->paramHasTexture] = true;
onlyPass.textureSlots[0] = this->texture;
onlyPass.textureValues[shader->paramTexture] = 0;
} else {
onlyPass.boolValues[shader->paramHasTexture] = false;
}
return { onlyPass };
}

View File

@ -1,34 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/components/display/Material.hpp"
#include "scene/components/display/mesh/MeshRenderer.hpp"
namespace Dawn {
class SimpleBillboardedMaterial : public Material {
private:
shaderlock_t shaderLock = -1;
public:
// @optional
Texture *texture = nullptr;
// @optional
struct Color color = COLOR_WHITE;
/**
* SimpleBillboardMaterial scene item component interface.
*
* @param i Scene Item this interface belongs to.
*/
SimpleBillboardedMaterial(std::weak_ptr<SceneItem> i);
void onStart() override;
void onDispose() override;
std::vector<struct ShaderPassItem>
getRenderPasses(IRenderableContext &context) override
;
};
}

View File

@ -1,68 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SimpleTexturedMaterial.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
SimpleTexturedMaterial::SimpleTexturedMaterial(std::weak_ptr<SceneItem> i) :
Material(i)
{
}
void SimpleTexturedMaterial::onStart() {
this->shaderLock = getShaderManager().lockShader<SimpleTexturedShader>();
}
void SimpleTexturedMaterial::onDispose() {
getShaderManager().releaseShader<SimpleTexturedShader>(this->shaderLock);
}
std::vector<struct ShaderPassItem>
SimpleTexturedMaterial::getRenderPasses(IRenderableContext &context)
{
auto mesh = this->item.lock()->getComponent<MeshRenderer>();
auto shader = getShaderManager()
.getShader<SimpleTexturedShader>(this->shaderLock)
;
assertNotNull(mesh, "Mesh cannot be null");
assertNotNull(mesh->mesh, "Mesh cannot be null");
assertNotNull(shader, "Shader cannot be null");
struct ShaderPassItem onlyPass;
onlyPass.mesh = mesh->mesh;
onlyPass.shader = shader;
onlyPass.colorValues[shader->paramColor] = this->color;
onlyPass.matrixValues[shader->paramModel] =
item.lock()->getWorldTransform()
;
onlyPass.parameterBuffers[
shader->bufferRenderPipeline
] = &context.renderPipeline->shaderBuffer;
if(this->opaque) {
onlyPass.renderFlags = (
RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST
);
} else {
onlyPass.priority = 100;
onlyPass.renderFlags = (
RENDER_MANAGER_RENDER_FLAG_BLEND
);
}
if(this->texture != nullptr) {
onlyPass.boolValues[shader->paramHasTexture] = true;
onlyPass.textureSlots[0] = this->texture;
onlyPass.textureValues[shader->paramTexture] = 0;
} else {
onlyPass.boolValues[shader->paramHasTexture] = false;
}
return { onlyPass };
}

View File

@ -1,36 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/components/display/Material.hpp"
#include "scene/components/display/mesh/MeshRenderer.hpp"
namespace Dawn {
class SimpleTexturedMaterial : public Material {
private:
shaderlock_t shaderLock = -1;
public:
// @optional
Texture *texture = nullptr;
// @optional
struct Color color = COLOR_WHITE;
// @optional
bool_t opaque = true;
/**
* SimpleTexturedShader scene item component interface.
*
* @param i Scene Item this interface belongs to.
*/
SimpleTexturedMaterial(std::weak_ptr<SceneItem> i);
void onStart() override;
void onDispose() override;
std::vector<struct ShaderPassItem>
getRenderPasses(IRenderableContext &context) override
;
};
}