Uniform arrays working.

This commit is contained in:
2023-12-11 23:54:52 -06:00
parent 81adf91816
commit 0fa08a1c92
18 changed files with 447 additions and 23 deletions

View File

@ -35,6 +35,7 @@ add_subdirectory(scene)
# add_subdirectory(state)
add_subdirectory(time)
add_subdirectory(util)
add_subdirectory(ui)
# Definitions
# target_compile_definitions(${DAWN_TARGET_NAME}

View File

@ -9,4 +9,5 @@ target_sources(${DAWN_TARGET_NAME}
)
# Subdirs
add_subdirectory(display)
add_subdirectory(display)
add_subdirectory(ui)

View File

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

View File

@ -0,0 +1,64 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UICanvas.hpp"
#include "display/pass/RenderPass.hpp"
#include "display/shader/UIShader.hpp"
#include "display/mesh/QuadMesh.hpp"
using namespace Dawn;
void UICanvas::onInit() {
}
void UICanvas::onDispose() {
}
std::vector<std::shared_ptr<IRenderPass>> UICanvas::getPasses(
struct RenderPassContext &ctx
) {
std::vector<std::shared_ptr<IRenderPass>> passes;
auto selfTransform = this->getItem()->getWorldTransform();
auto mesh = std::make_shared<Mesh>();
mesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
QuadMesh::buffer(mesh, glm::vec4(0, 0, 32, 32), glm::vec4(0, 0, 1, 1), 0, 0);
UIShaderData data = {
.projection = ctx.camera->getProjection(),
.view = ctx.camera->getItem()->getWorldTransform(),
.model = selfTransform
};
data.colors[0] = COLOR_WHITE;
data.colors[1] = COLOR_RED;
data.colors[2] = COLOR_GREEN;
data.colors[3] = COLOR_BLUE;
auto pass = createRenderPass<UIShader, UIShaderData>(
std::ref(*this),
data,
std::unordered_map<shadertexturebinding_t, std::shared_ptr<Texture>>(),
mesh,
MeshDrawMode::TRIANGLES,
0, -1
);
passes.push_back(pass);
auto itComponents = components.begin();
while(itComponents != components.end()) {
auto component = *itComponents;
// Get this components' quads.
auto quads = component->getQuads(selfTransform);
++itComponents;
}
return passes;
}

View File

@ -0,0 +1,28 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItem.hpp"
#include "component/display/IRenderableComponent.hpp"
#include "ui/UIComponent.hpp"
namespace Dawn {
class UICanvas :
public SceneComponent,
public IRenderableComponent
{
protected:
virtual void onInit() override;
virtual void onDispose() override;
public:
std::vector<std::shared_ptr<UIComponent>> components;
std::vector<std::shared_ptr<IRenderPass>> getPasses(
struct RenderPassContext &ctx
) override;
};
}

View File

@ -4,6 +4,7 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "game/Game.hpp"
#include "display/pass/IRenderPass.hpp"
#include "display/shader/Shader.hpp"
#include "display/Texture.hpp"

View File

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

13
src/dawn/ui/UIAlign.hpp Normal file
View File

@ -0,0 +1,13 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
namespace Dawn {
struct UIAlign {
glm::vec2 position;
};
}

View File

@ -0,0 +1,37 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIComponent.hpp"
using namespace Dawn;
std::vector<std::shared_ptr<UIComponent>> UIComponent::getChildren() {
return {};
}
std::vector<struct UIQuad> UIComponent::getQuads(
const glm::mat4 parent
) {
// Get self transform
glm::mat4 transform = glm::translate(
glm::mat4(1.0f), glm::vec3(position, 0.0f)
);
// Add parent transform
transform = parent * transform;
// Get self quads and insert new transform.
std::vector<struct UIQuad> quads;
auto selfQuads = this->getSelfQuads(transform);
// Get children
auto children = getChildren();
for(auto &c : children) {
auto childQuads = c->getQuads(transform);
quads.insert(quads.end(), childQuads.begin(), childQuads.end());
}
return quads;
}

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 "ui/UIAlign.hpp"
#include "ui/UIQuad.hpp"
namespace Dawn {
class UICanvas;
class UIComponent {
protected:
virtual std::vector<struct UIQuad> getSelfQuads(
const glm::mat4 transform
) = 0;
virtual std::vector<std::shared_ptr<UIComponent>> getChildren();
std::vector<struct UIQuad> getQuads(
const glm::mat4 parent
);
public:
glm::vec2 position;
friend class UICanvas;
};
}

34
src/dawn/ui/UIQuad.hpp Normal file
View File

@ -0,0 +1,34 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/Color.hpp"
#include "display/Texture.hpp"
namespace Dawn {
struct UIQuad {
glm::mat4 transform;
glm::vec4 quad;
glm::vec4 uv;
struct Color color;
std::shared_ptr<Texture> texture;
UIQuad(
const glm::mat4 transform,
const glm::vec4 quad,
const glm::vec4 uv,
const struct Color color,
const std::shared_ptr<Texture> texture
) :
transform(transform),
quad(quad),
uv(uv),
color(color),
texture(texture)
{
}
};
}

View File

@ -0,0 +1,24 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIRectangle.hpp"
using namespace Dawn;
std::vector<struct UIQuad> UIRectangle::getSelfQuads(
const glm::mat4 transform
) {
std::vector<struct UIQuad> quads;
quads.push_back(UIQuad(
transform,
glm::vec4(0,0,size.x,size.y),
uv,
color,
texture
));
return quads;
}

View File

@ -0,0 +1,22 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "ui/UIComponent.hpp"
namespace Dawn {
class UIRectangle final : public UIComponent {
protected:
std::vector<struct UIQuad> getSelfQuads(
const glm::mat4 transform
) override;
public:
struct Color color = COLOR_WHITE;
std::shared_ptr<Texture> texture = nullptr;
glm::vec2 size = glm::vec2(32, 32);
glm::vec4 uv = glm::vec4(0,0,1,1);
};
}

View File

@ -10,6 +10,9 @@
#include "component/display/material/SimpleTexturedMaterial.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "component/ui/UICanvas.hpp"
#include "ui/UIRectangle.hpp"
#include <ft2build.h>
#include FT_FREETYPE_H
@ -28,20 +31,27 @@ void Dawn::helloWorldScene(Scene &s) {
auto camera = cameraItem->addComponent<Camera>();
cameraItem->lookAt({ 120, 120, 120 }, { 0, 0, 0 }, { 0, 1, 0 });
auto quad = s.createSceneItem();
auto quadMesh = std::make_shared<Mesh>();
// auto quad = s.createSceneItem();
// auto quadMesh = std::make_shared<Mesh>();
glm::vec2 position = { 0, 0 };
glm::vec2 size = texture->bufferStringToMesh(
quadMesh,
L"Hello World!",
position,
true
);
// glm::vec2 position = { 0, 0 };
// glm::vec2 size = texture->bufferStringToMesh(
// quadMesh,
// L"Hello World!",
// position,
// true
// );
auto quadRenderer = quad->addComponent<MeshRenderer>();
quadRenderer->mesh = quadMesh;
// auto quadRenderer = quad->addComponent<MeshRenderer>();
// quadRenderer->mesh = quadMesh;
auto quadMaterial = quad->addComponent<SimpleTexturedMaterial>();
quadMaterial->setTexture(texture->texture);
// auto quadMaterial = quad->addComponent<SimpleTexturedMaterial>();
// quadMaterial->setTexture(texture->texture);
auto uiCanvasItem = s.createSceneItem();
auto uiCanvas = uiCanvasItem->addComponent<UICanvas>();
auto rect = std::make_shared<UIRectangle>();
uiCanvas->components.push_back(rect);
}

View File

@ -9,4 +9,5 @@ target_sources(${DAWN_TARGET_NAME}
Shader.cpp
ShaderStage.cpp
SimpleTexturedShader.cpp
UIShader.cpp
)

View File

@ -22,6 +22,7 @@ namespace Dawn {
std::string name;
size_t offset;
enum ShaderParameterType type;
int32_t count;
GLint location = -1;
@ -33,6 +34,19 @@ namespace Dawn {
this->name = name;
this->offset = (size_t)offset;
this->type = type;
this->count = 1;
}
ShaderOpenGLParameter(
const std::string &name,
const void *offset,
const enum ShaderParameterType type,
const int32_t count
) {
this->name = name;
this->offset = (size_t)offset;
this->type = type;
this->count = count;
}
};
@ -136,6 +150,9 @@ namespace Dawn {
switch(param.type) {
case ShaderParameterType::MAT4: {
glm::mat4 *matrix = (glm::mat4 *)value;
if(param.count != 1) {
assertUnreachable("I haven't implemented multiple mat4s");
}
glUniformMatrix4fv(
param.location, 1, GL_FALSE, glm::value_ptr(*matrix)
);
@ -144,25 +161,21 @@ namespace Dawn {
case ShaderParameterType::COLOR: {
auto color = (Color *)value;
glUniform4f(
glUniform4fv(
param.location,
color->r,
color->g,
color->b,
color->a
param.count,
(GLfloat*)value
);
break;
}
case ShaderParameterType::BOOLEAN: {
auto boolean = (bool_t *)value;
glUniform1i(param.location, *boolean ? 1 : 0);
glUniform1iv(param.location, param.count, (GLint*)value);
break;
}
case ShaderParameterType::TEXTURE: {
textureslot_t texture = *((textureslot_t*)value);
glUniform1i(param.location, texture);
glUniform1iv(param.location, param.count, (GLint*)value);
break;
}

View File

@ -0,0 +1,100 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/shader/UIShader.hpp"
using namespace Dawn;
void UIShader::getStages(
const enum ShaderOpenGLVariant variant,
const struct UIShaderData *rel,
std::vector<std::shared_ptr<ShaderStage>> &stages,
std::vector<struct ShaderOpenGLParameter> &parameters
) {
// Stages
std::shared_ptr<ShaderStage> vertex;
std::shared_ptr<ShaderStage> fragment;
switch(variant) {
case ShaderOpenGLVariant::GLSL_330_CORE:
vertex = std::make_shared<ShaderStage>(
ShaderStageType::VERTEX,
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"uniform mat4 u_Projection;\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 = std::make_shared<ShaderStage>(
ShaderStageType::FRAGMENT,
"#version 330 core\n"
"in vec2 o_TextCoord;\n"
"uniform vec4 u_Color[4];\n"
"out vec4 o_Color;\n"
"void main() {\n"
"o_Color = u_Color[2];\n"
"}\n"
);
break;
default:
assertUnreachable("Unsupported ShaderOpenGLVariant");
}
// Add stages
stages.push_back(vertex);
stages.push_back(fragment);
// Parameters
parameters.push_back(ShaderOpenGLParameter(
"u_Projection",
&rel->projection,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderOpenGLParameter(
"u_View",
&rel->view,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderOpenGLParameter(
"u_Model",
&rel->model,
ShaderParameterType::MAT4
));
parameters.push_back(ShaderOpenGLParameter(
"u_Color",
&rel->colors,
ShaderParameterType::COLOR,
4
));
// parameters.push_back(ShaderOpenGLParameter(
// "u_Color",
// &rel->color,
// ShaderParameterType::COLOR
// ));
// parameters.push_back(ShaderOpenGLParameter(
// "u_HasTexture",
// &rel->hasTexture,
// ShaderParameterType::BOOLEAN
// ));
// parameters.push_back(ShaderOpenGLParameter(
// "u_Texture",
// &rel->texture,
// ShaderParameterType::TEXTURE
// ));
}

View File

@ -0,0 +1,26 @@
// 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 {
struct UIShaderData {
glm::mat4 projection;
glm::mat4 view;
glm::mat4 model;
struct Color colors[4];
};
class UIShader : public Shader<UIShaderData> {
protected:
void getStages(
const enum ShaderOpenGLVariant variant,
const struct UIShaderData *rel,
std::vector<std::shared_ptr<ShaderStage>> &stages,
std::vector<struct ShaderOpenGLParameter> &parameters
) override;
};
}