Fixed whatever was wrong with my shader

This commit is contained in:
2022-12-05 19:27:32 -08:00
parent 8059158bae
commit 5b6f9124b5
21 changed files with 284 additions and 61 deletions

View File

@ -43,6 +43,20 @@ void TilesetAsset::updateAsync() {
this->state = 0x04;
assertTrue(this->tileset.rows > 0);
strCurrent = strNext+1;
strNext = strchr(strCurrent, '|');
*strNext = '\0';
this->tileset.divX = atoi(strCurrent);
this->state = 0x05;
assertTrue(this->tileset.rows > 0);
strCurrent = strNext+1;
strNext = strchr(strCurrent, '|');
*strNext = '\0';
this->tileset.divY = atoi(strCurrent);
this->state = 0x06;
assertTrue(this->tileset.rows > 0);
// Begin reading tiles.
int32_t done = 0;
int32_t count = this->tileset.columns * this->tileset.rows;
@ -79,7 +93,7 @@ void TilesetAsset::updateAsync() {
done++;
}
this->state = 0x05;
this->state = 0x07;
this->loaded = true;
this->eventLoaded.invoke();

View File

@ -126,10 +126,6 @@ void RenderPipeline::renderUI(
// Clear / Bind / Update the render target.
renderTarget->bind();
renderTarget->clear(
RENDER_TARGET_CLEAR_FLAG_DEPTH |
RENDER_TARGET_CLEAR_FLAG_COLOR
);
this->renderManager->setRenderFlags(
RENDER_MANAGER_RENDER_FLAG_BLEND |
RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST

View File

@ -16,12 +16,20 @@ namespace Dawn {
struct Tileset {
public:
std::vector<struct Tile> tiles;
struct Tile getTile(int32_t tile) {
assertTrue(tile >= 0);
assertTrue(tile < this->tiles.size());
return this->tiles[tile];
}
};
struct TilesetGrid : public Tileset{
public:
int32_t rows;
int32_t columns;
int32_t divX;
int32_t divY;
TilesetGrid() {
@ -30,8 +38,8 @@ namespace Dawn {
TilesetGrid(
int32_t columns,
int32_t rows,
int32_t width,
int32_t height,
int32_t w,
int32_t h,
int32_t gapX,
int32_t gapY,
int32_t borderX,
@ -39,49 +47,43 @@ namespace Dawn {
) {
assertTrue(columns >= 1);
assertTrue(rows >= 1);
assertTrue(width >= 1);
assertTrue(height >= 1);
assertTrue(w >= 1);
assertTrue(h >= 1);
assertTrue(gapX >= 0);
assertTrue(gapY >= 0);
assertTrue(borderX >= 0);
assertTrue(borderY >= 0);
assertTrue(width >= (columns + (gapX * columns) + borderX + borderX));
assertTrue(height >= (rows + (gapY * rows) + borderY + borderY));
assertTrue(w >= (columns + (gapX * columns) + borderX + borderX));
assertTrue(h >= (rows + (gapY * rows) + borderY + borderY));
this->rows = rows;
this->columns = columns;
// Calculate division sizes (pixels)
float_t divX = (
(float_t)width - ((float_t)borderX * 2.0f) -
((float_t)gapX * ((float_t)columns - 1))
) / columns;
float_t divY = (
(float_t)height - ((float_t)borderY * 2.0f) -
((float_t)gapY * ((float_t)rows - 1))
) / rows;
this->divX = (w - (borderX * 2.0f) - (gapX * (columns - 1))) / columns;
this->divY = (h - (borderY * 2.0f) - (gapY * (rows - 1))) / rows;
// Calculate the division sizes (units)
float_t tdivX = divX / (float_t)width;
float_t tdivY = divY / (float_t)height;
float_t tdivX = (float_t)this->divX / (float_t)w;
float_t tdivY = (float_t)this->divY / (float_t)h;
for(int32_t y = 0; y < rows; y++) {
for(int32_t x = 0; x < columns; x++) {
struct Tile tile;
tile.uv0.x = (borderX + (divX * x) + (gapX * x)) / width;
tile.uv0.x = (borderX + ((float_t)this->divX * x) + (gapX * x)) / w;
tile.uv1.x = tile.uv0.x + tdivX;
tile.uv0.y = (borderY + (divY * y) + (gapY * y)) / height;
tile.uv0.y = (borderY + ((float_t)this->divY * y) + (gapY * y)) / h;
tile.uv1.y = tile.uv0.y + tdivY;
this->tiles.push_back(tile);
}
}
}
struct Tile getTile(int32_t row, int32_t column) {
struct Tile getTileFromGrid(int32_t row, int32_t column) {
assertTrue(row > 0 && row < this->rows);
assertTrue(column > 0 && column < this->columns);
return this->tiles[row + (column * this->rows)];
return this->getTile(row + (column * this->rows));
}
};
}

View File

@ -50,6 +50,33 @@ void QuadMesh::bufferQuadMesh(
);
}
void QuadMesh::bufferCoordinates(
Mesh *mesh,
glm::vec2 uv0, glm::vec2 uv1,
int32_t verticeStart
) {
assertNotNull(mesh);
mesh->bufferCoordinates(verticeStart, std::array<glm::vec2, QUAD_VERTICE_COUNT>{{
uv0, glm::vec2(uv1.x, uv0.y),
glm::vec2(uv0.x, uv1.y), uv1
}});
}
void QuadMesh::bufferPositions(
Mesh *mesh,
glm::vec2 xy0, glm::vec2 xy1,
int32_t verticeStart
) {
mesh->bufferPositions(
verticeStart, std::array<glm::vec3, QUAD_VERTICE_COUNT>{{
glm::vec3(xy0, 0),
glm::vec3(xy1.x, xy0.y, 0),
glm::vec3(xy0.x, xy1.y, 0),
glm::vec3(xy1, 0)
}}
);
}
void QuadMesh::initQuadMesh(
Mesh *mesh,
glm::vec2 xy0, glm::vec2 uv0,

View File

@ -16,7 +16,7 @@ namespace Dawn {
/**
* Buffers the vertices of a quad onto a primitive.
*
* @param primitive The primitive to buffer to.
* @param mesh The primitive to buffer to.
* @param xy0 The lower X and Y coordinate.
* @param uv0 The lower Xand Y texture coordinate.
* @param xy1 The higher X and Y coordinate.
@ -35,7 +35,7 @@ namespace Dawn {
/**
* Buffers the vertices of a quad onto a primitive.
*
* @param primitive The primitive to buffer to.
* @param mesh The primitive to buffer to.
* @param xy0 The lower X and Y coordinate.
* @param uv0 The lower Xand Y texture coordinate.
* @param xy1 The higher X and Y coordinate.
@ -50,6 +50,44 @@ namespace Dawn {
int32_t verticeStart, int32_t indiceStart
);
/**
* Buffers texture coordinates on to an already initialized quad mesh.
*
* @param mesh Mesh to buffer the texture coordinates on to.
* @param uv0 Lower X and Y coordinates.
* @param uv1 Upper X and Y coordinates.
* @param verticeStart Start vertice to buffer in to.
*/
static void bufferCoordinates(
Mesh *mesh,
glm::vec2 uv0, glm::vec2 uv1,
int32_t verticeStart
);
/**
* Buffers the positions of a quad onto a primitive.
*
* @param mesh The primitive to buffer to.
* @param xy0 The lower X and Y coordinate.
* @param xy1 The higher X and Y coordinate.
* @param verticeStart Start vertice to buffer to.
*/
static void bufferPositions(
Mesh *mesh,
glm::vec2 xy0, glm::vec2 xy1,
int32_t verticeStart
);
/**
* Initializes a mesh to be a single quad.
*
* @param mesh The primitive to buffer to.
* @param xy0 The lower X and Y coordinate.
* @param uv0 The lower Xand Y texture coordinate.
* @param xy1 The higher X and Y coordinate.
* @param uv1 The higher X and Y texture coordinate.
* @param z The Z position of the coordinates.
*/
static void initQuadMesh(
Mesh *mesh,
glm::vec2 xy0, glm::vec2 uv0,

View File

@ -36,8 +36,8 @@ void SceneItem::init() {
auto it2 = deps.begin();
while(it2 != deps.end()) {
if(*it2 == nullptr) {
continue;
++it2;
continue;
}
// Has the dep not yet inited?

View File

@ -5,4 +5,5 @@
# Subdirs
add_subdirectory(display)
add_subdirectory(example)
add_subdirectory(ui)

View File

@ -10,4 +10,6 @@
#include "scene/components/display/Material.hpp"
#include "scene/components/display/TiledSprite.hpp"
#include "scene/components/example/ExampleSpin.hpp"
#include "scene/components/ui/UICanvas.hpp"

View File

@ -12,7 +12,7 @@ namespace Dawn {
class Material : public SceneItemComponent {
private:
Shader *shader;
Shader *shader = nullptr;
/**
* Internal method that will be invoked to go through and update all of

View File

@ -14,15 +14,14 @@ MeshRenderer::MeshRenderer(SceneItem *item) : SceneItemComponent(item) {
std::vector<SceneItemComponent*> MeshRenderer::getDependencies() {
return std::vector<SceneItemComponent*>{
this->item->getComponent<MeshHost>()
this->meshHost = this->item->getComponent<MeshHost>()
};
}
void MeshRenderer::onStart() {
SceneItemComponent::onStart();
if(mesh == nullptr) {
auto host = this->item->getComponent<MeshHost>();
if(host != nullptr) this->mesh = &host->mesh;
if(this->mesh == nullptr && this->meshHost != nullptr) {
this->mesh = &this->meshHost->mesh;
}
}

View File

@ -8,7 +8,12 @@
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class MeshHost;
class MeshRenderer : public SceneItemComponent {
protected:
MeshHost *meshHost = nullptr;
public:
Mesh * mesh = nullptr;

View File

@ -12,6 +12,52 @@ TiledSprite::TiledSprite(SceneItem *item) : SceneItemComponent(item) {
}
glm::vec2 TiledSprite::getUV0() {
auto tile = this->tileset->getTile(tileIndex);
return glm::vec2(
(this->flipState & TILED_SPRITE_FLIP_X) == 0 ? tile.uv0.x : tile.uv1.x,
(this->flipState & TILED_SPRITE_FLIP_Y) == 0 ? tile.uv0.y : tile.uv1.y
);
}
glm::vec2 TiledSprite::getUV1() {
auto tile = this->tileset->getTile(tileIndex);
return glm::vec2(
(this->flipState & TILED_SPRITE_FLIP_X) == 0 ? tile.uv1.x : tile.uv0.x,
(this->flipState & TILED_SPRITE_FLIP_Y) == 0 ? tile.uv1.y : tile.uv0.y
);
}
void TiledSprite::setTileset(Tileset *tileset) {
assertNotNull(tileset);
this->tileset = tileset;
this->setTile(0);
}
void TiledSprite::setTile(int32_t tileIndex) {
assertNotNull(this->tileset);
this->tileIndex = tileIndex;
if(this->host != nullptr) {
QuadMesh::bufferCoordinates(
&this->host->mesh, this->getUV0(), this->getUV1(), 0
);
}
}
void TiledSprite::setSize(glm::vec2 size, glm::vec2 center) {
this->xy0 = -center;
this->xy1 = size - center;
if(this->host != nullptr) {
QuadMesh::bufferPositions(
&this->host->mesh, this->xy0, this->xy1, 0
);
}
}
void TiledSprite::setSize(glm::vec2 size) {
this->setSize(size, size / 2.0f);
}
std::vector<SceneItemComponent*> TiledSprite::getDependencies() {
this->renderer = this->item->getComponent<MeshRenderer>();
this->host = this->item->getComponent<MeshHost>();
@ -24,12 +70,12 @@ std::vector<SceneItemComponent*> TiledSprite::getDependencies() {
void TiledSprite::onStart() {
SceneItemComponent::onStart();
assertNotNull(this->host);
assertNotNull(this->tileset);
QuadMesh::initQuadMesh(&this->host->mesh,
glm::vec2(0, 0), glm::vec2(0, 0),
glm::vec2(1, 1), glm::vec2(0.125f, 0.125f),
this->xy0, this->getUV0(),
this->xy1, this->getUV1(),
0
);
}

View File

@ -10,15 +10,32 @@
#include "display/mesh/QuadMesh.hpp"
#include "display/Tileset.hpp"
#define TILED_SPRITE_FLIP_Y FLAG_DEFINE(0)
#define TILED_SPRITE_FLIP_X FLAG_DEFINE(1)
namespace Dawn {
class TiledSprite : public SceneItemComponent {
protected:
MeshRenderer *renderer;
MeshHost *host;
MeshRenderer *renderer = nullptr;
MeshHost *host = nullptr;
Tileset *tileset = nullptr;
flag_t flipState = TILED_SPRITE_FLIP_Y;
int32_t tileIndex;
glm::vec2 xy0 = glm::vec2(0, 0);
glm::vec2 xy1 = glm::vec2(1, 1);
glm::vec2 getUV0();
glm::vec2 getUV1();
public:
TiledSprite(SceneItem *item);
void setTileset(Tileset *tileset);
void setTile(int32_t tile);
void setFlippedState(flag_t flippedState);
void setSize(glm::vec2 size, glm::vec2 center);
void setSize(glm::vec2 size);
std::vector<SceneItemComponent*> getDependencies() override;
void onStart() override;
};

View File

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

View File

@ -0,0 +1,44 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "ExampleSpin.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
#include "scene/components/display/MeshRenderer.hpp"
#include "display/mesh/CubeMesh.hpp"
using namespace Dawn;
SceneItem * ExampleSpin::create(Scene *scene) {
auto item = scene->createSceneItem();
auto mr = item->addComponent<MeshRenderer>();
mr->mesh = new Mesh();
mr->mesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(mr->mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1, 1, 1), 0, 0);
auto mat = item->addComponent<Material>();
item->addComponent<ExampleSpin>();
return item;
}
ExampleSpin::ExampleSpin(SceneItem *item) :
SceneItemComponent(item)
{
getScene()->eventSceneUnpausedUpdate.addListener(this, &ExampleSpin::onUnpausedUpdate);
}
void ExampleSpin::onUnpausedUpdate() {
auto quat = this->transform->getLocalRotation();
quat = glm::rotate(quat, getGame()->timeManager.delta, glm::vec3(0, 1, 0));
quat = glm::rotate(quat, getGame()->timeManager.delta / 2.0f, glm::vec3(1, 0, 0));
quat = glm::rotate(quat, getGame()->timeManager.delta / 4.0f, glm::vec3(0, 0, 1));
this->transform->setLocalRotation(quat);
}
ExampleSpin::~ExampleSpin() {
getScene()->eventSceneUnpausedUpdate.removeListener(this, &ExampleSpin::onUnpausedUpdate);
}

View File

@ -0,0 +1,19 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/shader/Shader.hpp"
namespace Dawn {
class ExampleSpin : public SceneItemComponent {
public:
static SceneItem * create(Scene *scene);
ExampleSpin(SceneItem *item);
void onUnpausedUpdate();
~ExampleSpin();
};
}