Clean stuff up

This commit is contained in:
Bailey Harrison 2023-01-20 16:30:35 +00:00
parent 0a39baea20
commit c489c66a44
15 changed files with 120 additions and 158 deletions

View File

@ -1,8 +1,12 @@
#pragma once
#include "resource_manager.hpp"
#include <memory>
#include <string>
#include <filesystem>
#include <unordered_map>
#include <assert.h>
namespace engine {
@ -19,6 +23,30 @@ namespace engine {
Application(const Application&) = delete;
Application& operator=(const Application&) = delete;
/* resource stuff */
template <typename T>
void registerResourceManager()
{
size_t hash = typeid(T).hash_code();
assert(m_resourceManagers.contains(hash) == false && "Registering resource manager type more than once.");
m_resourceManagers.emplace(hash, std::make_unique<ResourceManager<T>>());
}
template <typename T>
std::shared_ptr<T> addResource(const std::string& name, std::unique_ptr<T>&& resource)
{
auto resourceManager = getResourceManager<T>();
return resourceManager->add(name, std::move(resource));
}
template <typename T>
std::shared_ptr<T> getResource(const std::string& name)
{
auto resourceManager = getResourceManager<T>();
return resourceManager->get(name);
}
/* methods */
void gameLoop();
@ -42,6 +70,24 @@ namespace engine {
bool m_enableFrameLimiter = true;
/* resource stuff */
std::unordered_map<size_t, std::unique_ptr<IResourceManager>> m_resourceManagers{};
template <typename T>
ResourceManager<T>* getResourceManager()
{
size_t hash = typeid(T).hash_code();
auto it = m_resourceManagers.find(hash);
if (it == m_resourceManagers.end()) {
throw std::runtime_error("Cannot find resource manager.");
}
auto ptr = it->second.get();
auto castedPtr = dynamic_cast<ResourceManager<T>*>(ptr);
assert(castedPtr != nullptr);
return castedPtr;
}
};
}

View File

@ -25,8 +25,9 @@ namespace engine::gfx {
};
enum class VertexAttribFormat {
VEC2,
VEC3,
FLOAT2,
FLOAT3,
FLOAT4
};
enum class TextureFilter {

View File

@ -14,13 +14,10 @@ public:
// defines what vertex inputs are defined, position is always vec3
struct VertexParams {
bool hasNormal;
bool hasUV0;
bool hasUV1;
bool hasUV2;
bool hasUV3;
bool hasTangent;
bool hasColor;
bool hasNormal; // vec3
bool hasTangent; // vec3
bool hasColor; // vec3
bool hasUV0; // vec2
};
Shader(GFXDevice* gfx, const char* vertPath, const char* fragPath, const VertexParams& vertexParams, bool alphaBlending, bool cullBackFace);

View File

@ -2,7 +2,6 @@
#include "log.hpp"
#include "resource_manager.hpp"
#include "ecs_system.hpp"
#include <map>
@ -26,30 +25,6 @@ namespace engine {
Application* app() { return m_app; }
/* resource stuff */
template <typename T>
void registerResourceManager()
{
size_t hash = typeid(T).hash_code();
assert(m_resourceManagers.contains(hash) == false && "Registering resource manager type more than once.");
m_resourceManagers.emplace(hash, std::make_unique<ResourceManager<T>>());
}
template <typename T>
std::shared_ptr<T> addResource(const std::string& name, std::unique_ptr<T>&& resource)
{
auto resourceManager = getResourceManager<T>();
return resourceManager->add(name, std::move(resource));
}
template <typename T>
std::shared_ptr<T> getResource(const std::string& name)
{
auto resourceManager = getResourceManager<T>();
return resourceManager->get(name);
}
/* ecs stuff */
uint32_t createEntity(const std::string& tag, uint32_t parent = 0);
@ -127,24 +102,6 @@ namespace engine {
Application* const m_app;
uint32_t m_nextEntityID = 1000;
/* resource stuff */
std::map<size_t, std::unique_ptr<IResourceManager>> m_resourceManagers{};
template <typename T>
ResourceManager<T>* getResourceManager()
{
size_t hash = typeid(T).hash_code();
auto it = m_resourceManagers.find(hash);
if (it == m_resourceManagers.end()) {
throw std::runtime_error("Cannot find resource manager.");
}
auto ptr = it->second.get();
auto castedPtr = dynamic_cast<ResourceManager<T>*>(ptr);
assert(castedPtr != nullptr);
return castedPtr;
}
/* ecs stuff */
size_t m_nextSignaturePosition = 0;

View File

@ -9,12 +9,6 @@ namespace engine {
class Application;
class Scene; // "scene.hpp"
namespace resources {
class Texture;
class Shader;
class Mesh;
class Material;
}
class SceneManager {

View File

@ -1,36 +0,0 @@
#version 450
layout(location = 0) in vec3 fragPos;
layout(location = 1) in vec3 fragNorm;
layout(location = 2) in vec2 fragUV;
layout(location = 3) in vec3 fragLightPos;
layout(location = 0) out vec4 outColor;
void main() {
// constants
vec3 lightColor = vec3(1.0, 1.0, 1.0);
vec3 ambientColor = vec3(1.0, 1.0, 1.0);
float ambientStrength = 0.05;
vec3 baseColor = vec3(fragUV.x, 0.0, fragUV.y);
vec3 emission = vec3(0.0, 0.0, 0.0);
// code
vec3 norm = normalize(fragNorm);
vec3 lightDir = normalize(fragLightPos - fragPos);
vec3 diffuse = max(dot(norm, lightDir), 0.0) * lightColor;
vec3 ambient = ambientColor * ambientStrength;
vec3 viewDir = normalize(-fragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = 0.5 * spec * lightColor;
vec3 lighting = min(diffuse + ambient + specular, 1.0);
outColor = min( ( vec4(baseColor, 1.0) ) * vec4(lighting + emission, 1.0), vec4(1.0));
}

View File

@ -1,28 +0,0 @@
#version 450
layout(binding = 0) uniform UBO {
mat4 proj;
} ubo;
layout( push_constant ) uniform Constants {
mat4 model;
mat4 view;
} constants;
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNorm;
layout(location = 2) in vec2 inUV;
layout(location = 0) out vec3 fragPos;
layout(location = 1) out vec3 fragNorm;
layout(location = 2) out vec2 fragUV;
layout(location = 3) out vec3 fragLightPos;
void main() {
gl_Position = ubo.proj * constants.view * constants.model * vec4(inPosition, 1.0);
fragPos = vec3(constants.view * constants.model * vec4(inPosition, 1.0));
fragNorm = mat3(transpose(inverse(constants.view * constants.model))) * inNorm;
fragUV = inUV;
vec3 lightPos = vec3(-500.0, 500.0, 40.0);
fragLightPos = vec3(constants.view * vec4(lightPos, 1.0));
}

View File

@ -0,0 +1,11 @@
#version 450
layout(location = 0) in vec2 fragUV;
layout(location = 0) out vec4 outColor;
void main() {
vec3 baseColor = vec3(fragUV, 0.0);
outColor = vec4(baseColor, 1.0);
}

View File

@ -0,0 +1,21 @@
#version 450
layout(binding = 0) uniform UBO {
mat4 proj;
} ubo;
layout( push_constant ) uniform Constants {
mat4 model;
mat4 view;
} constants;
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNorm;
layout(location = 2) in vec2 inUV;
layout(location = 0) out vec2 fragUV;
void main() {
gl_Position = ubo.proj * constants.view * constants.model * vec4(inPosition, 1.0);
fragUV = inUV;
}

View File

@ -20,9 +20,11 @@ layout(location = 3) out vec3 fragLightPos;
void main() {
gl_Position = ubo.proj * constants.view * constants.model * vec4(inPosition, 1.0);
fragPos = vec3(constants.view * constants.model * vec4(inPosition, 1.0));
fragNorm = mat3(transpose(inverse(constants.view * constants.model))) * inNorm;
fragUV = inUV;
vec3 lightPos = vec3(-10.0, 10.0, 10.0);
fragLightPos = vec3(constants.view * vec4(lightPos, 1.0));
}

View File

@ -9,6 +9,11 @@
#include "scene.hpp"
#include "resources/mesh.hpp"
#include "resources/material.hpp"
#include "resources/shader.hpp"
#include "resources/texture.hpp"
// To allow the FPS-limiter to put the thread to sleep
#include <thread>
@ -60,6 +65,13 @@ namespace engine {
// get base path for resources
m_resourcesPath = getResourcesPath();
// initialise some basic resources
registerResourceManager<engine::resources::Mesh>();
registerResourceManager<engine::resources::Material>();
registerResourceManager<engine::resources::Shader>();
registerResourceManager<engine::resources::Texture>();
}
Application::~Application() {}

View File

@ -140,10 +140,12 @@ namespace engine {
static VkFormat getVertexAttribFormat(gfx::VertexAttribFormat fmt)
{
switch (fmt) {
case gfx::VertexAttribFormat::VEC2:
case gfx::VertexAttribFormat::FLOAT2:
return VK_FORMAT_R32G32_SFLOAT;
case gfx::VertexAttribFormat::VEC3:
case gfx::VertexAttribFormat::FLOAT3:
return VK_FORMAT_R32G32B32_SFLOAT;
case gfx::VertexAttribFormat::FLOAT4:
return VK_FORMAT_R32G32B32A32_SFLOAT;
}
throw std::runtime_error("Unknown vertex attribute format");
}

View File

@ -15,36 +15,24 @@ namespace engine::resources {
uint32_t stride = 0;
gfx::VertexFormat vertFormat{};
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::FLOAT3, stride);
stride += 3 * sizeof(float);
if (vertexParams.hasNormal) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::FLOAT3, stride);
stride += 3 * sizeof(float);
}
if (vertexParams.hasUV0) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasUV1) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasUV2) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasUV3) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride);
stride += 2 * sizeof(float);
}
if (vertexParams.hasTangent) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::FLOAT4, stride);
stride += 4 * sizeof(float);
}
if (vertexParams.hasColor) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride);
stride += 3 * sizeof(float);
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::FLOAT4, stride);
stride += 4 * sizeof(float);
}
if (vertexParams.hasUV0) {
vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::FLOAT2, stride);
stride += 2 * sizeof(float);
}
vertFormat.stride = stride;

View File

@ -77,11 +77,11 @@ namespace engine::util {
auto child = scene->createEntity("_mesh" + std::to_string(i), parentObj);
auto childRenderer = scene->addComponent<RenderableComponent>(child);
childRenderer->mesh = meshes[parentNode->mMeshes[i]];
childRenderer->material = std::make_shared<resources::Material>(scene->getResource<resources::Shader>("theShader"));
childRenderer->material = std::make_shared<resources::Material>(scene->app()->getResource<resources::Shader>("engine.textured"));
if (textures.contains(meshTextureIndices[parentNode->mMeshes[i]])) {
childRenderer->material->m_texture = textures.at(meshTextureIndices[parentNode->mMeshes[i]]);
} else {
childRenderer->material->m_texture = scene->getResource<resources::Texture>("whiteTexture");
childRenderer->material->m_texture = scene->app()->getResource<resources::Texture>("engine.white");
}
}
@ -183,7 +183,7 @@ namespace engine::util {
try {
textures[i] = std::make_shared<resources::Texture>(parent->app()->gfx(), absPath.string());
} catch (const std::runtime_error&) {
textures[i] = parent->getResource<resources::Texture>("textures/white.png");
textures[i] = parent->app()->getResource<resources::Texture>("engine.white");
}
}
}

View File

@ -75,9 +75,6 @@ void playGame()
app.inputManager()->addInputAxis("lookx", engine::inputs::MouseAxis::X);
app.inputManager()->addInputAxis("looky", engine::inputs::MouseAxis::Y);
// scene setup
auto myScene = app.sceneManager()->createEmptyScene();
@ -88,9 +85,6 @@ void playGame()
myScene->registerSystem<RotateSystem>();
myScene->registerSystem<CameraControllerSystem>();
myScene->registerResourceManager<engine::resources::Shader>();
myScene->registerResourceManager<engine::resources::Texture>();
@ -115,10 +109,11 @@ void playGame()
app.gfx(),
app.getResourcePath("engine/textures/white.png")
);
auto keepTexture = myScene->addResource<engine::resources::Texture>("whiteTexture", std::move(whiteTexture));
auto keepShader = myScene->addResource<engine::resources::Shader>("theShader", std::move(theShader));
auto keepTexture = app.addResource<engine::resources::Texture>("engine.white", std::move(whiteTexture));
auto keepShader = app.addResource<engine::resources::Shader>("engine.textured", std::move(theShader));
// uint32_t astronaut = engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae"));
uint32_t astronaut = engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae"));
(void)astronaut;
auto grassTexture = std::make_shared<engine::resources::Texture>(
app.gfx(),