diff --git a/include/application.hpp b/include/application.hpp index 61d92f7..acb40e3 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -1,8 +1,12 @@ #pragma once +#include "resource_manager.hpp" + #include #include #include +#include +#include namespace engine { @@ -19,6 +23,30 @@ namespace engine { Application(const Application&) = delete; Application& operator=(const Application&) = delete; + /* resource stuff */ + + template + 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>()); + } + + template + std::shared_ptr addResource(const std::string& name, std::unique_ptr&& resource) + { + auto resourceManager = getResourceManager(); + return resourceManager->add(name, std::move(resource)); + } + + template + std::shared_ptr getResource(const std::string& name) + { + auto resourceManager = getResourceManager(); + return resourceManager->get(name); + } + /* methods */ void gameLoop(); @@ -42,6 +70,24 @@ namespace engine { bool m_enableFrameLimiter = true; + /* resource stuff */ + + std::unordered_map> m_resourceManagers{}; + + template + ResourceManager* 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*>(ptr); + assert(castedPtr != nullptr); + return castedPtr; + } + }; } diff --git a/include/gfx.hpp b/include/gfx.hpp index 0f4f9d7..b34cbbf 100644 --- a/include/gfx.hpp +++ b/include/gfx.hpp @@ -25,8 +25,9 @@ namespace engine::gfx { }; enum class VertexAttribFormat { - VEC2, - VEC3, + FLOAT2, + FLOAT3, + FLOAT4 }; enum class TextureFilter { diff --git a/include/resources/shader.hpp b/include/resources/shader.hpp index 97e3338..7a736ee 100644 --- a/include/resources/shader.hpp +++ b/include/resources/shader.hpp @@ -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); diff --git a/include/scene.hpp b/include/scene.hpp index b197747..1a2d33e 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -2,7 +2,6 @@ #include "log.hpp" -#include "resource_manager.hpp" #include "ecs_system.hpp" #include @@ -26,30 +25,6 @@ namespace engine { Application* app() { return m_app; } - /* resource stuff */ - - template - 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>()); - } - - template - std::shared_ptr addResource(const std::string& name, std::unique_ptr&& resource) - { - auto resourceManager = getResourceManager(); - return resourceManager->add(name, std::move(resource)); - } - - template - std::shared_ptr getResource(const std::string& name) - { - auto resourceManager = getResourceManager(); - 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> m_resourceManagers{}; - - template - ResourceManager* 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*>(ptr); - assert(castedPtr != nullptr); - return castedPtr; - } - /* ecs stuff */ size_t m_nextSignaturePosition = 0; diff --git a/include/scene_manager.hpp b/include/scene_manager.hpp index 1a2b8cb..662f145 100644 --- a/include/scene_manager.hpp +++ b/include/scene_manager.hpp @@ -9,12 +9,6 @@ namespace engine { class Application; class Scene; // "scene.hpp" - namespace resources { - class Texture; - class Shader; - class Mesh; - class Material; - } class SceneManager { diff --git a/res/engine/shaders/basic.frag b/res/engine/shaders/basic.frag deleted file mode 100644 index 991a945..0000000 --- a/res/engine/shaders/basic.frag +++ /dev/null @@ -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)); -} - diff --git a/res/engine/shaders/basic.vert b/res/engine/shaders/basic.vert deleted file mode 100644 index 6c63696..0000000 --- a/res/engine/shaders/basic.vert +++ /dev/null @@ -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)); -} diff --git a/res/engine/shaders/showUVs.frag b/res/engine/shaders/showUVs.frag new file mode 100644 index 0000000..3d32e8f --- /dev/null +++ b/res/engine/shaders/showUVs.frag @@ -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); +} + diff --git a/res/engine/shaders/showUVs.vert b/res/engine/shaders/showUVs.vert new file mode 100644 index 0000000..c4bc3ec --- /dev/null +++ b/res/engine/shaders/showUVs.vert @@ -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; +} diff --git a/res/engine/shaders/texture.vert b/res/engine/shaders/texture.vert index 1a08bcb..86749ee 100644 --- a/res/engine/shaders/texture.vert +++ b/res/engine/shaders/texture.vert @@ -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)); } diff --git a/src/application.cpp b/src/application.cpp index 9848b94..df3c3f6 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -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 @@ -60,6 +65,13 @@ namespace engine { // get base path for resources m_resourcesPath = getResourcesPath(); + + // initialise some basic resources + registerResourceManager(); + registerResourceManager(); + registerResourceManager(); + registerResourceManager(); + } Application::~Application() {} diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 024197e..9d73873 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -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"); } diff --git a/src/resources/shader.cpp b/src/resources/shader.cpp index 26d4ec6..a5f9c5e 100644 --- a/src/resources/shader.cpp +++ b/src/resources/shader.cpp @@ -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; diff --git a/src/util/model_loader.cpp b/src/util/model_loader.cpp index 745f029..bdd4727 100644 --- a/src/util/model_loader.cpp +++ b/src/util/model_loader.cpp @@ -77,11 +77,11 @@ namespace engine::util { auto child = scene->createEntity("_mesh" + std::to_string(i), parentObj); auto childRenderer = scene->addComponent(child); childRenderer->mesh = meshes[parentNode->mMeshes[i]]; - childRenderer->material = std::make_shared(scene->getResource("theShader")); + childRenderer->material = std::make_shared(scene->app()->getResource("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("whiteTexture"); + childRenderer->material->m_texture = scene->app()->getResource("engine.white"); } } @@ -183,7 +183,7 @@ namespace engine::util { try { textures[i] = std::make_shared(parent->app()->gfx(), absPath.string()); } catch (const std::runtime_error&) { - textures[i] = parent->getResource("textures/white.png"); + textures[i] = parent->app()->getResource("engine.white"); } } } diff --git a/test/src/game.cpp b/test/src/game.cpp index 27475cb..9d5575d 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -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(); myScene->registerSystem(); - myScene->registerResourceManager(); - myScene->registerResourceManager(); - @@ -115,10 +109,11 @@ void playGame() app.gfx(), app.getResourcePath("engine/textures/white.png") ); - auto keepTexture = myScene->addResource("whiteTexture", std::move(whiteTexture)); - auto keepShader = myScene->addResource("theShader", std::move(theShader)); + auto keepTexture = app.addResource("engine.white", std::move(whiteTexture)); + auto keepShader = app.addResource("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( app.gfx(),