diff --git a/include/resources/mesh.hpp b/include/resources/mesh.hpp index e82c68f..86922f9 100644 --- a/include/resources/mesh.hpp +++ b/include/resources/mesh.hpp @@ -1,5 +1,7 @@ #pragma once +#include "log.hpp" + #include "gfx.hpp" #include "gfx_device.hpp" @@ -24,22 +26,16 @@ public: Mesh(GFXDevice* gfx, const std::vector& vertices) : m_gfx(gfx) { - m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data()); - - std::vector indices(m_count); - for (uint32_t i = 0; i < m_count; i++) { + std::vector indices(vertices.size()); + for (uint32_t i = 0; i < indices.size(); i++) { indices[i] = i; } - m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data()); - - m_count = indices.size(); + initMesh(vertices, indices); } Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices) : m_gfx(gfx) { - m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data()); - m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data()); - m_count = indices.size(); + initMesh(vertices, indices); } ~Mesh() { @@ -60,6 +56,14 @@ private: const gfx::Buffer* m_ib; uint32_t m_count; + void initMesh(const std::vector& vertices, const std::vector& indices) + { + m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data()); + m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data()); + m_count = indices.size(); + INFO("Loaded mesh, vertices: {}, indices: {}", vertices.size(), indices.size()); + } + }; } diff --git a/include/resources/shader.hpp b/include/resources/shader.hpp index 82fcfb0..abacbb4 100644 --- a/include/resources/shader.hpp +++ b/include/resources/shader.hpp @@ -1,5 +1,7 @@ #pragma once +#include "log.hpp" + #include "gfx.hpp" #include "gfx_device.hpp" @@ -65,6 +67,8 @@ public: vertFormat.stride = stride; m_pipeline = m_gfx->createPipeline(vertPath, fragPath, vertFormat, sizeof(glm::mat4), alphaBlending, cullBackFace); + + INFO("Loaded shader: {}, vertex attribs: {}", vertPath, vertFormat.attributeDescriptions.size()); } ~Shader() { diff --git a/res/engine/shaders/texture.vert b/res/engine/shaders/texture.vert index 6c63696..1a08bcb 100644 --- a/res/engine/shaders/texture.vert +++ b/res/engine/shaders/texture.vert @@ -23,6 +23,6 @@ void main() { 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); + 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 07f646c..9848b94 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -81,11 +81,17 @@ namespace engine { /* logic */ m_sceneManager->updateActiveScene(m_window->dt()); - if(m_window->getKeyPress(inputs::Key::K_L)) { -// m_enableFrameLimiter = !m_enableFrameLimiter; + if(m_window->getKeyPress(inputs::Key::K_F)) [[unlikely]] { m_window->infoBox("fps", std::to_string(m_window->getFPS()) + " fps " + std::to_string(m_window->dt() * 1000.0f) + " ms"); } + uint64_t now = m_window->getNanos(); + if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] { + lastTick = now; + INFO("fps: {}", m_window->getAvgFPS()); + m_window->resetAvgFPS(); + } + /* draw */ m_gfx->renderFrame(); @@ -93,12 +99,6 @@ namespace engine { m_window->getInputAndEvents(); /* fps limiter */ - uint64_t now = m_window->getNanos(); - if (now - lastTick >= 1000000000LL * 10LL) { - lastTick = now; - m_window->setTitle(std::to_string(m_window->getAvgFPS())); - m_window->resetAvgFPS(); - } if (m_enableFrameLimiter) { std::this_thread::sleep_until(endFrame); } diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 3f1c4ef..024197e 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -283,7 +283,7 @@ namespace engine { switch (messageSeverity) { case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: - TRACE("VULKAN MESSAGE{}: ID: {} MSG: {}", msgType, pCallbackData->pMessageIdName, pCallbackData->pMessage); + DEBUG("VULKAN MESSAGE{}: ID: {} MSG: {}", msgType, pCallbackData->pMessageIdName, pCallbackData->pMessage); break; case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: INFO("VULKAN MESSAGE{}: ID: {} MSG: {}", msgType, pCallbackData->pMessageIdName, pCallbackData->pMessage); @@ -1233,7 +1233,7 @@ namespace engine { VkPhysicalDeviceProperties devProps; vkGetPhysicalDeviceProperties(pimpl->physicalDevice, &devProps); - INFO("Selected physical device: {}", devProps.deviceName); + DEBUG("Selected physical device: {}", devProps.deviceName); @@ -1702,7 +1702,6 @@ namespace engine { auto vertShaderCode = util::readTextFile(vertShaderPath); auto fragShaderCode = util::readTextFile(fragShaderPath); - INFO("Opened shader: {}", std::filesystem::path(vertShaderPath).filename().string()); VkShaderModule vertShaderModule = compileShader(pimpl->device, shaderc_vertex_shader, vertShaderCode->data(), vertShaderPath); VkShaderModule fragShaderModule = compileShader(pimpl->device, shaderc_fragment_shader, fragShaderCode->data(), fragShaderPath); diff --git a/src/resources/texture.cpp b/src/resources/texture.cpp index 5c51c59..3f236a6 100644 --- a/src/resources/texture.cpp +++ b/src/resources/texture.cpp @@ -21,7 +21,7 @@ Texture::Texture(GFXDevice* gfxDevice, const std::string& path) m_gpuTexture = m_gfxDevice->createTexture(texbuf->data(), (uint32_t)width, (uint32_t)height, gfx::TextureFilter::LINEAR, filter); - DEBUG("loaded texture {}, width: {} height: {} size: {}", path, width, height, texbuf->size()); + INFO("Loaded texture: {}, width: {} height: {}", path, width, height); } diff --git a/src/scene_manager.cpp b/src/scene_manager.cpp index 788039d..62477ba 100644 --- a/src/scene_manager.cpp +++ b/src/scene_manager.cpp @@ -24,7 +24,7 @@ namespace engine { void SceneManager::updateActiveScene(float ts) { - if (m_activeSceneIndex >= 0) { + if (m_activeSceneIndex >= 0) [[likely]] { assert((size_t)m_activeSceneIndex < m_scenes.size()); m_scenes[m_activeSceneIndex]->update(ts); } diff --git a/src/systems/render.cpp b/src/systems/render.cpp index dffacc4..e10f1a2 100644 --- a/src/systems/render.cpp +++ b/src/systems/render.cpp @@ -48,11 +48,12 @@ namespace engine { for (uint32_t entity : m_entities) { - auto t = m_scene->getComponent(entity); auto r = m_scene->getComponent(entity); - DEBUG("rendering mesh: {}, parent: {}", t->tag, t->parent); + assert(r->material != nullptr); + assert(r->mesh != nullptr); + assert(r->material->m_texture != nullptr); gfx->updateUniformBuffer(r->material->getShader()->getPipeline(), &projMatrix, sizeof(projMatrix), 0); diff --git a/src/util/model_loader.cpp b/src/util/model_loader.cpp index 804f3f7..7305d8b 100644 --- a/src/util/model_loader.cpp +++ b/src/util/model_loader.cpp @@ -105,7 +105,7 @@ namespace engine::util { public: void write(const char* message) override { (void)message; - DEBUG("ASSIMP: {}", message); + TRACE("ASSIMP: {}", message); } }; @@ -160,23 +160,23 @@ namespace engine::util { assert(scene->HasLights() == false); assert(scene->hasSkeletons() == false); - INFO("material count: {}, mesh count: {}", scene->mNumMaterials, scene->mNumMeshes); + TRACE("material count: {}, mesh count: {}", scene->mNumMaterials, scene->mNumMeshes); std::map> textures{}; for (uint32_t i = 0; i < scene->mNumMaterials; i++) { const aiMaterial* m = scene->mMaterials[i]; - INFO("Material {}:", i); - INFO(" Name: {}", m->GetName().C_Str()); + TRACE("Material {}:", i); + TRACE(" Name: {}", m->GetName().C_Str()); for (uint32_t j = 0; j < m->mNumProperties; j++) { - const aiMaterialProperty* p = m->mProperties[j]; - INFO(" prop {}, key: {}", j, p->mKey.C_Str()); + [[maybe_unused]] const aiMaterialProperty* p = m->mProperties[j]; + TRACE(" prop {}, key: {}", j, p->mKey.C_Str()); } if (aiGetMaterialTextureCount(m, aiTextureType_DIFFUSE) >= 1) { aiString texPath{}; aiGetMaterialTexture(m, aiTextureType_DIFFUSE, 0, &texPath); - INFO(" Diffuse tex: {}", texPath.C_Str()); + TRACE(" Diffuse tex: {}", texPath.C_Str()); std::filesystem::path absPath = path; absPath = absPath.parent_path(); absPath /= texPath.C_Str(); @@ -195,8 +195,8 @@ namespace engine::util { meshMaterialIndices.push_back(m->mMaterialIndex); std::vector vertices(m->mNumVertices); std::vector indices(m->mNumFaces * 3); - INFO("Mesh {}: vertex count {}", i, vertices.size()); - INFO("Mesh {}: index count {}", i, indices.size()); + TRACE("Mesh {}: vertex count {}", i, vertices.size()); + TRACE("Mesh {}: index count {}", i, indices.size()); for (uint32_t j = 0; j < vertices.size(); j++) { Vertex v{}; @@ -228,6 +228,8 @@ namespace engine::util { buildGraph(textures, meshes, meshMaterialIndices, scene->mRootNode, parent, obj); + INFO("Loaded model: {}, meshes: {}, textures: {}", scene->GetShortFilename(path.c_str()), meshes.size(), textures.size()); + Assimp::DefaultLogger::kill(); return obj; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 602a0e5..79a82c9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -13,6 +13,8 @@ set(GAME_SOURCES src/game.hpp src/camera_controller.cpp src/camera_controller.hpp + src/meshgen.cpp + src/meshgen.hpp ) diff --git a/test/src/game.cpp b/test/src/game.cpp index 39f6076..a792e99 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -1,6 +1,7 @@ #include "config.h" #include "camera_controller.hpp" +#include "meshgen.hpp" #include "application.hpp" #include "window.hpp" @@ -108,7 +109,27 @@ void playGame() auto keepTexture = myScene->addResource("whiteTexture", std::move(whiteTexture)); auto keepShader = myScene->addResource("theShader", std::move(theShader)); - engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae")); +// uint32_t astronaut = engine::util::loadMeshFromFile(myScene, app.getResourcePath("models/astronaut/astronaut.dae")); +// myScene->addComponent(astronaut); + + uint32_t sphere = myScene->createEntity("sphere"); + auto sphereRenderable = myScene->addComponent(sphere); + sphereRenderable->material = std::make_unique(keepShader); + sphereRenderable->material->m_texture = keepTexture; + sphereRenderable->mesh = genSphereMesh(app.gfx(), 100.0f, 100, true); + + uint32_t light = myScene->createEntity("light"); + myScene->getComponent(light)->position = glm::vec3{-10.0f, 10.0f, 10.0f}; + auto lightRenderable = myScene->addComponent(light); + lightRenderable->material = sphereRenderable->material; + lightRenderable->mesh = genSphereMesh(app.gfx(), 0.5f, 10, false, true); + + uint32_t floor = myScene->createEntity("floor"); +// myScene->getComponent(floor)->position = glm::vec3{-50.0f, -0.5f, -50.0f}; + auto floorRenderable = myScene->addComponent(floor); + floorRenderable->material = sphereRenderable->material; + floorRenderable->mesh = genCuboidMesh(app.gfx(), 1.0f, 1.0f, 1.0f); + myScene->addComponent(floor); app.gameLoop(); diff --git a/test/src/meshgen.cpp b/test/src/meshgen.cpp index a655f1c..57aae85 100644 --- a/test/src/meshgen.cpp +++ b/test/src/meshgen.cpp @@ -4,15 +4,13 @@ #include #include -#include +#include -#include - -std::unique_ptr genSphereMesh(float r, int detail, bool windInside) +std::unique_ptr genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside, bool flipNormals) { using namespace glm; - std::vector vertices{}; + std::vector vertices{}; float angleStep = two_pi() / (float)detail; @@ -66,15 +64,18 @@ std::unique_ptr genSphereMesh(float r, int detail, bool } - glm::vec3 vector1 = (vertices.end() - 1)->pos - (vertices.end() - 2)->pos; - glm::vec3 vector2 = (vertices.end() - 2)->pos - (vertices.end() - 3)->pos; - glm::vec3 norm = glm::normalize(glm::cross(vector1, vector2)); + vec3 vector1 = (vertices.end() - 1)->pos - (vertices.end() - 2)->pos; + vec3 vector2 = (vertices.end() - 2)->pos - (vertices.end() - 3)->pos; + vec3 norm = normalize(cross(vector1, vector2)); // TODO: FIX NORMALS if (!windInside) norm = -norm; + if (flipNormals) + norm = -norm; + for (auto it = vertices.end() - 6; it != vertices.end(); it++) { it->norm = norm; } @@ -82,54 +83,68 @@ std::unique_ptr genSphereMesh(float r, int detail, bool } } - return std::make_unique(vertices); + return std::make_unique(gfx, vertices); } -std::unique_ptr genCuboidMesh(float x, float y, float z) +std::unique_ptr genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z) { // x goes -> // y goes ^ // z goes into the screen - using glm::vec3; + using namespace glm; - std::vector v{}; + std::vector v{}; - // 0 top_left_front - v.push_back({{ 0.0f, y, 0.0f }, {}, {}}); - // 1 bottom_left_front - v.push_back({{ 0.0f, 0.0f, 0.0f }, {}, {}}); - // 2 top_right_front - v.push_back({{ x, y, 0.0f }, {}, {}}); - // 3 bottom_right_front - v.push_back({{ x, 0.0f, 0.0f }, {}, {}}); + // front + v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}}); + v.push_back({{x, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}}); - // 4 top_left_back - v.push_back({{ 0.0f, y, z }, {}, {}}); - // 5 bottom_left_back - v.push_back({{ 0.0f, 0.0f, z }, {}, {}}); - // 6 top_right_back - v.push_back({{ x, y, z }, {}, {}}); - // 7 bottom_right_back - v.push_back({{ x, 0.0f, z }, {}, {}}); + // back + v.push_back({{0.0f, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}); + v.push_back({{x, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}}); - // front quad - std::vector indices{ - // front - 0, 1, 3, 2, 0, 3, - // back - 4, 5, 7, 6, 4, 7, - // bottom - 5, 1, 3, 7, 5, 3, - // top - 4, 0, 2, 6, 4, 2, - // left - 4, 5, 1, 0, 4, 1, - // right - 2, 3, 7, 6, 2, 7 - }; + // left + v.push_back({{0.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); - return std::make_unique(v, indices); + // right + v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, y, x}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}); + + // bottom + v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}); + + // top + v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}}); + v.push_back({{x, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}}); + + return std::make_unique(gfx, v); } diff --git a/test/src/meshgen.hpp b/test/src/meshgen.hpp index d20f15d..724587e 100644 --- a/test/src/meshgen.hpp +++ b/test/src/meshgen.hpp @@ -3,5 +3,5 @@ #include "resources/mesh.hpp" // generates a UV sphere -std::unique_ptr genSphereMesh(float r, int detail, bool windInside = false); -std::unique_ptr genCuboidMesh(float x, float y, float z); +std::unique_ptr genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside = false, bool flipNormals = false); +std::unique_ptr genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z);