Example of Rendering UI done.

This commit is contained in:
2023-12-12 13:40:50 -06:00
parent debe4146eb
commit d9278414c5
15 changed files with 218 additions and 118 deletions

View File

@ -12,52 +12,66 @@
using namespace Dawn;
void UICanvas::onInit() {
mesh = std::make_shared<Mesh>();
mesh->createBuffers(
QUAD_VERTICE_COUNT * UI_SHADER_QUAD_COUNT,
QUAD_INDICE_COUNT * UI_SHADER_QUAD_COUNT
);
for(int32_t i = 0; i < UI_SHADER_QUAD_COUNT; i++) {
QuadMesh::bufferWithIndex(
mesh,
glm::vec4(0, 0, 1, 1),
glm::vec4(0, 0, 1, 1),
i * QUAD_VERTICE_COUNT,
i * QUAD_INDICE_COUNT,
i * QUAD_VERTICE_COUNT
);
}
}
void UICanvas::onDispose() {
mesh = nullptr;
}
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);
std::unordered_map<shadertexturebinding_t, std::shared_ptr<Texture>> textures;
UIShaderData data = {
.projection = ctx.camera->getProjection(),
.view = ctx.camera->getItem()->getWorldTransform(),
.model = selfTransform
.model = this->getItem()->getWorldTransform()
};
data.test.first = COLOR_RED;
data.test.random = glm::vec3(0, 1, 0);
data.test.second = 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);
size_t quadCount = 0;
auto itComponents = components.begin();
while(itComponents != components.end()) {
auto component = *itComponents;
// Get this components' quads.
auto quads = component->getQuads(selfTransform);
auto quads = component->getQuads(glm::mat4(1.0));
for(auto quad : quads) {
data.quads[quadCount++] = quad;
assertTrue(quadCount <= UI_SHADER_QUAD_COUNT, "Too many UI quads!");
}
++itComponents;
}
// No render passes if no quads.
if(quadCount == 0) return passes;
auto pass = createRenderPass<UIShader, UIShaderData>(
std::ref(*this),
data,
textures,
mesh,
MeshDrawMode::TRIANGLES,
0, quadCount * QUAD_INDICE_COUNT
);
passes.push_back(pass);
return passes;
}

View File

@ -14,6 +14,8 @@ namespace Dawn {
public IRenderableComponent
{
protected:
std::shared_ptr<Mesh> mesh;
virtual void onInit() override;
virtual void onDispose() override;

View File

@ -12,13 +12,46 @@ void QuadMesh::buffer(
const glm::vec4 positions,
const glm::vec4 coordinates,
const int32_t verticeStart,
const int32_t indiceStart
const int32_t indiceStart,
const float_t depth
) {
glm::vec3 vertices[QUAD_VERTICE_COUNT] = {
glm::vec3(positions.x, positions.y, 0),
glm::vec3(positions.z, positions.y, 0),
glm::vec3(positions.x, positions.w, 0),
glm::vec3(positions.z, positions.w, 0)
glm::vec3(positions.x, positions.y, depth),
glm::vec3(positions.z, positions.y, depth),
glm::vec3(positions.x, positions.w, depth),
glm::vec3(positions.z, positions.w, depth)
};
glm::vec2 coords[QUAD_VERTICE_COUNT] = {
glm::vec2(coordinates.x, coordinates.y),
glm::vec2(coordinates.z, coordinates.y),
glm::vec2(coordinates.x, coordinates.w),
glm::vec2(coordinates.z, coordinates.w)
};
int32_t indices[QUAD_INDICE_COUNT] = {
verticeStart, verticeStart + 1, verticeStart + 3,
verticeStart, verticeStart + 2, verticeStart + 3
};
mesh->bufferPositions(verticeStart, vertices, QUAD_VERTICE_COUNT);
mesh->bufferCoordinates(verticeStart, coords, QUAD_VERTICE_COUNT);
mesh->bufferIndices(indiceStart, indices, QUAD_INDICE_COUNT);
}
void QuadMesh::bufferWithIndex(
const std::shared_ptr<Mesh> mesh,
const glm::vec4 positions,
const glm::vec4 coordinates,
const int32_t verticeStart,
const int32_t indiceStart,
const int32_t indexOffset
) {
glm::vec3 vertices[QUAD_VERTICE_COUNT] = {
glm::vec3(positions.x, positions.y, indexOffset),
glm::vec3(positions.z, positions.y, indexOffset + 1),
glm::vec3(positions.x, positions.w, indexOffset + 2),
glm::vec3(positions.z, positions.w, indexOffset + 3)
};
glm::vec2 coords[QUAD_VERTICE_COUNT] = {

View File

@ -20,13 +20,36 @@ namespace Dawn {
* @param coordinates The coordinates of the vertices.
* @param verticeStart The starting index of the vertices.
* @param indiceStart The starting index of the indices.
* @param depth The depth of the vertices (Z coordinate).
*/
static void buffer(
const std::shared_ptr<Mesh> mesh,
const glm::vec4 positions,
const glm::vec4 coordinates,
const int32_t verticeStart,
const int32_t indiceStart
const int32_t indiceStart,
const float_t depth = 0.0f
);
/**
* Buffers quad mesh vertices and indices into the given mesh. This will
* store the index of the vertice in the Z component, allowing you to find
* which vertex ID you are rendering in your shader.
*
* @param mesh The mesh to buffer into.
* @param positions The positions of the vertices.
* @param coordinates The coordinates of the vertices.
* @param verticeStart The starting index of the vertices.
* @param indiceStart The starting index of the indices.
* @param indexOffset The offset to add to the index of each vertex.
*/
static void bufferWithIndex(
const std::shared_ptr<Mesh> mesh,
const glm::vec4 positions,
const glm::vec4 coordinates,
const int32_t verticeStart,
const int32_t indiceStart,
const int32_t indexOffset = 0
);
};
}

View File

@ -11,20 +11,20 @@ std::vector<std::shared_ptr<UIComponent>> UIComponent::getChildren() {
return {};
}
std::vector<struct UIQuad> UIComponent::getQuads(
std::vector<struct UIShaderQuad> UIComponent::getQuads(
const glm::mat4 parent
) {
// Get self transform
glm::mat4 transform = glm::translate(
glm::mat4(1.0f), glm::vec3(position, 0.0f)
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);
std::vector<struct UIShaderQuad> quads = this->getSelfQuads(transform);
// Get children
auto children = getChildren();

View File

@ -5,20 +5,20 @@
#pragma once
#include "ui/UIAlign.hpp"
#include "ui/UIQuad.hpp"
#include "display/shader/UIShader.hpp"
namespace Dawn {
class UICanvas;
class UIComponent {
protected:
virtual std::vector<struct UIQuad> getSelfQuads(
virtual std::vector<struct UIShaderQuad> getSelfQuads(
const glm::mat4 transform
) = 0;
virtual std::vector<std::shared_ptr<UIComponent>> getChildren();
std::vector<struct UIQuad> getQuads(
std::vector<struct UIShaderQuad> getQuads(
const glm::mat4 parent
);

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 "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

@ -7,18 +7,15 @@
using namespace Dawn;
std::vector<struct UIQuad> UIRectangle::getSelfQuads(
std::vector<struct UIShaderQuad> UIRectangle::getSelfQuads(
const glm::mat4 transform
) {
std::vector<struct UIQuad> quads;
quads.push_back(UIQuad(
std::vector<struct UIShaderQuad> quads;
quads.push_back({
transform,
glm::vec4(0,0,size.x,size.y),
glm::vec4(0, 0, size.x, size.y),
uv,
color,
texture
));
color
});
return quads;
}

View File

@ -9,7 +9,7 @@
namespace Dawn {
class UIRectangle final : public UIComponent {
protected:
std::vector<struct UIQuad> getSelfQuads(
std::vector<struct UIShaderQuad> getSelfQuads(
const glm::mat4 transform
) override;

11
src/dawn/util/Macro.hpp Normal file
View File

@ -0,0 +1,11 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#define MACRO_STRINGIFY_RAW(x) #x
#define MACRO_STRINGIFY(x) MACRO_STRINGIFY_RAW(x)
#define MACRO_JOIN(x, y) x ## y