Testing textures and events

This commit is contained in:
2022-10-20 15:49:25 -07:00
parent 375b25ff59
commit 80d6cba854
27 changed files with 513 additions and 26 deletions

View File

@ -7,4 +7,7 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
RenderPipeline.cpp
)
)
# Subdirs
add_subdirectory(mesh)

View File

@ -6,6 +6,7 @@
#pragma once
#include "util/flag.hpp"
#include "display/Color.hpp"
#include "event/Event.hpp"
#define RENDER_TARGET_CLEAR_FLAG_COLOR FLAG_DEFINE(0)
#define RENDER_TARGET_CLEAR_FLAG_DEPTH FLAG_DEFINE(1)
@ -13,6 +14,8 @@
namespace Dawn {
class RenderTarget {
public:
Event<RenderTarget &, float_t, float_t> eventRenderTargetResized;
/**
* Return the width of the render target.
*

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 "display/Color.hpp"
namespace Dawn {
class ITexture {
public:
virtual int32_t getWidth() = 0;
virtual int32_t getHeight() = 0;
virtual void setSize(int32_t width, int32_t height) = 0;
virtual void fill(struct Color) = 0;
virtual bool_t isReady() = 0;
virtual void buffer(struct Color pixels[]) = 0;
};
}

View File

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

View File

@ -0,0 +1,38 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "QuadMesh.hpp"
using namespace Dawn;
void QuadMesh::bufferQuadMeshWithZ(
Mesh &mesh,
glm::vec2 xy0, glm::vec2 uv0,
glm::vec2 xy1, glm::vec2 uv1,
float_t z, int32_t verticeStart, int32_t indiceStart
) {
mesh.bufferPositions(
verticeStart, std::array<glm::vec3, QUAD_VERTICE_COUNT>{{
glm::vec3(xy0, z),
glm::vec3(xy1.x, xy0.y, z),
glm::vec3(xy0.x, xy1.y, z),
glm::vec3(xy1, z)
}}
);
mesh.bufferCoordinates(
verticeStart, std::array<glm::vec2, QUAD_VERTICE_COUNT>{{
uv0, glm::vec2(uv1.x, uv0.y),
glm::vec2(uv0.x, uv1.y), uv1
}}
);
mesh.bufferIndices(
indiceStart, std::array<meshindice_t, QUAD_INDICE_COUNT>{{
verticeStart, verticeStart + 1, verticeStart + 2,
verticeStart + 1, verticeStart + 2, verticeStart + 3
}}
);
}

View File

@ -0,0 +1,53 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/mesh/Mesh.hpp"
#define QUAD_VERTICE_COUNT 4
#define QUAD_INDICE_COUNT 6
#define QUAD_INDICE_PER_QUAD 2
namespace Dawn {
class QuadMesh {
public:
/**
* Buffers the vertices of a quad onto a primitive.
*
* @param primitive 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.
* @param verticeStart Start vertice to buffer to.
* @param indiceStart Start indice to buffer to.
*/
static void bufferQuadMeshWithZ(
Mesh &mesh,
glm::vec2 xy0, glm::vec2 uv0,
glm::vec2 xy1, glm::vec2 uv1,
float_t z, int32_t verticeStart, int32_t indiceStart
);
/**
* Buffers the vertices of a quad onto a primitive.
*
* @param primitive 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 verticeStart Start vertice to buffer to.
* @param indiceStart Start indice to buffer to.
*/
static void bufferQuadMesh(
Mesh &mesh,
glm::vec2 xy0, glm::vec2 uv0,
glm::vec2 xy1, glm::vec2 uv1,
int32_t verticeStart, int32_t indiceStart
);
};
}

View File

@ -0,0 +1,25 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "display/mesh/TriangleMesh.hpp"
using namespace Dawn;
void TriangleMesh::createTriangleMesh(Mesh &mesh) {
mesh.createBuffers(3, 3);
mesh.bufferPositions(0, std::array<glm::vec3, 3>{{
glm::vec3(-0.5f, -0.5f, 0),
glm::vec3(0.5f, -0.5f, 0),
glm::vec3(0, 0.5f, 0)
}});
mesh.bufferCoordinates(0, std::array<glm::vec2, 3>{{
glm::vec2(0, 0),
glm::vec2(0, 1),
glm::vec2(1, 0)
}});
mesh.bufferIndices(0, std::array<meshindice_t,3>{{
0, 1, 2
}});
}

View File

@ -4,7 +4,7 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "display/Texture.hpp"
namespace Dawn {
class Material;
@ -13,7 +13,8 @@ namespace Dawn {
SHADER_PARAMETER_TYPE_MATRIX,
SHADER_PARAMETER_TYPE_BOOLEAN,
SHADER_PARAMETER_TYPE_COLOR,
SHADER_PARAMETER_TYPE_VECTOR3
SHADER_PARAMETER_TYPE_VECTOR3,
SHADER_PARAMETER_TYPE_TEXTURE
};
template<typename T>
@ -93,5 +94,13 @@ namespace Dawn {
* @param vector Vector to set.
*/
virtual void setVector3(T parameter, glm::vec3 vector) = 0;
/**
* Attaches a texture to the currently bound shader.
*
* @param parameter parameter to set the texture on to.
* @param texture Texture to bind to the parameter.
*/
virtual void setTexture(T parameter, std::shared_ptr<Texture> texture)=0;
};
}

View File

@ -34,6 +34,7 @@ namespace Dawn {
{
public:
std::unique_ptr<DawnHostData> data;
DawnGame *game;
/**
* Construct a new DawnHost. Hosts are set by the various dawn platform

View File

@ -12,7 +12,9 @@ using namespace Dawn;
Camera::Camera(SceneItem &item) :
SceneItemComponent(item)
{
this->updateProjection();
this->getRenderTarget().eventRenderTargetResized.addListener(
this, &Camera::onRenderTargetResize
);
}
void Camera::updateProjection() {
@ -54,6 +56,18 @@ RenderTarget & Camera::getRenderTarget() {
return *this->target;
}
void Camera::setRenderTarget(std::shared_ptr<RenderTarget> renderTarget) {
if(renderTarget == this->target) return;
this->getRenderTarget().eventRenderTargetResized.removeListener(
this, &Camera::onRenderTargetResize
);
this->target = renderTarget;
this->getRenderTarget().eventRenderTargetResized.addListener(
this, &Camera::onRenderTargetResize
);
this->updateProjection();
}
float_t Camera::getAspect() {
RenderTarget &target = this->getRenderTarget();
return target.getWidth() / target.getHeight();
@ -61,4 +75,14 @@ float_t Camera::getAspect() {
void Camera::start() {
this->updateProjection();
}
void Camera::onRenderTargetResize(RenderTarget &target, float_t w, float_t h) {
this->updateProjection();
}
Camera::~Camera() {
this->getRenderTarget().eventRenderTargetResized.removeListener(
this, &Camera::onRenderTargetResize
);
}

View File

@ -14,9 +14,13 @@ namespace Dawn {
};
class Camera : public SceneItemComponent {
protected:
std::shared_ptr<RenderTarget> target = nullptr;
void onRenderTargetResize(RenderTarget &target, float_t w, float_t h);
public:
glm::mat4 projection;
std::shared_ptr<RenderTarget> target = nullptr;
// Perspective
enum CameraType type = CAMERA_TYPE_PERSPECTIVE;
@ -55,6 +59,13 @@ namespace Dawn {
*/
RenderTarget & getRenderTarget();
/**
* Updates the render target for the camera to use.
*
* @param renderTarget Render target for this camera to draw to.
*/
void setRenderTarget(std::shared_ptr<RenderTarget> renderTarget);
/**
* Returs the aspect ratio of the camera.
*
@ -66,5 +77,10 @@ namespace Dawn {
* Event triggered by the scene item when the item is added to the scene.
*/
void start() override;
/**
* Disposes a previously initialized camera.
*/
~Camera();
};
}

View File

@ -46,6 +46,10 @@ void Material::setShaderParameters() {
case SHADER_PARAMETER_TYPE_VECTOR3:
this->shader->setVector3(it->first, this->vec3Values[it->first]);
break;
case SHADER_PARAMETER_TYPE_TEXTURE:
this->shader->setTexture(it->first, this->textureValues[it->first]);
break;
default:
throw "An unsupported or invalid shader parameter type was supplied.";

View File

@ -26,6 +26,7 @@ namespace Dawn {
std::map<shaderparameter_t, bool_t> boolValues;
std::map<shaderparameter_t, glm::mat4> matrixValues;
std::map<shaderparameter_t, glm::vec3> vec3Values;
std::map<shaderparameter_t, std::shared_ptr<Texture>> textureValues;
/**
* Material component constructor.

97
src/dawn/util/memory.hpp Normal file
View File

@ -0,0 +1,97 @@
/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawnlibs.hpp"
/**
* Allocate some space in memory to use for your needs. Memory allocation may
* change how it functions later on to keep things nice and efficient. For now
* this is just an API forward for malloc.
*
* @param size Size of the array you wish to buffer.
* @return Pointer to the space in memory to use.
*/
static inline void * memoryAllocate(const size_t size) {
return (void *)malloc(size);
}
/**
* Free some previously allocated memory space.
* @param pointer Pointer in memory to free.
*/
static inline void memoryFree(void *pointer) {
free(pointer);
}
/**
* Copies data from one buffer to another. Typically used for array operations.
*
* @param source Source pointer.
* @param destination Destination buffer.
* @param size Size in bytes of data to copy.
*/
static inline void memoryCopy(
void *source,
void *destination,
size_t size
) {
memcpy(destination, source, size);
}
/**
* Compares the data within two memory banks. Shorthand for memcpy.
*
* @param left Left item to compare.
* @param right Right item to compare.
* @param size Count of bytes to compare.
* @return 0 for equal, <0 for left being greater, >0 for right being greater.
*/
static inline int32_t memoryCompare(
const void *left,
const void *right,
const size_t size
) {
return memcmp(left, right, size);
}
/**
* Fill destination with a repeating set of bytes.
*
* @param dest Destination pointer in memory.
* @param data Data byte to write.
* @param length How many times to write that byte.
*/
static inline void memorySet(
void *dest,
uint8_t data,
size_t length
) {
memset(dest, data, length);
}
/**
* Reallocate a part of memory. Reallocation simply creates a new buffer that
* will take all of the existing contents and then free's the original buffer.
*
* @param pointer Pointer to pointer in memory that you wish to re-allocate.
* @param currentSize Current size of the buffer that the pointer points to.
* @param newSize The new size of the buffer.
* @return The new size param you provided.
*/
static inline size_t memoryReallocate(
void **pointer,
size_t currentSize,
size_t newSize
) {
// Create the new buffer
void *newBuffer = memoryAllocate(newSize);
memoryCopy(*pointer, newBuffer, currentSize);
memoryFree(*pointer);
*pointer = newBuffer;
return newSize;
}