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);
};
}