#include "Lightkeeper.h" #include #include "glm/gtx/transform.hpp" #include "spdlog/spdlog.h" #include #include "destrum/Components/MeshRendererComponent.h" #include "destrum/Components/Rotator.h" #include "destrum/ObjectModel/GameObject.h" LightKeeper::LightKeeper(): App(), renderer(meshCache, materialCache) { } LightKeeper::~LightKeeper() { } void LightKeeper::customInit() { materialCache.init(gfxDevice); renderer.init(gfxDevice, m_params.renderSize); const float aspectRatio = static_cast(m_params.renderSize.x) / static_cast(m_params.renderSize.y); camera.setAspectRatio(aspectRatio); auto file = AssetFS::GetInstance().ReadBytes("engine://assetfstest.txt"); std::string fileStr(file.begin(), file.end()); spdlog::info("Read from assetfstest.txt: {}", fileStr); testMesh.name = "Test Mesh"; testMesh.vertices = vertices; testMesh.indices = indices; testMeshID = meshCache.addMesh(gfxDevice, testMesh); spdlog::info("TestMesh uploaded with id: {}", testMeshID); const auto testimgpath = AssetFS::GetInstance().GetFullPath("engine://textures/kobe.png"); auto testimgID = gfxDevice.loadImageFromFile(testimgpath); spdlog::info("Test image loaded with id: {}", testimgID); testMaterialID = materialCache.addMaterial(gfxDevice, { .baseColor = glm::vec3(1.f), .diffuseTexture = testimgID, }); spdlog::info("Test material created with id: {}", testMaterialID); camera.SetRotation(glm::radians(glm::vec2(90.f, 0.f))); auto& scene = SceneManager::GetInstance().CreateScene("Main"); auto testCube = std::make_shared("TestCube"); auto meshComp = testCube->AddComponent(); meshComp->SetMeshID(testMeshID); meshComp->SetMaterialID(testMaterialID); // testCube->AddComponent(10, 5); scene.Add(testCube); } void LightKeeper::customUpdate(float dt) { camera.Update(dt); SceneManager::GetInstance().Update(); } void LightKeeper::customDraw() { renderer.beginDrawing(gfxDevice); const RenderContext ctx{ .renderer = renderer, .camera = camera, .sceneData = { .camera = camera, .ambientColor = glm::vec3(0.1f), .ambientIntensity = 0.5f, .fogColor = glm::vec3(0.5f), .fogDensity = 0.01f } }; SceneManager::GetInstance().Render(ctx); renderer.endDrawing(); const auto cmd = gfxDevice.beginFrame(); const auto& drawImage = renderer.getDrawImage(gfxDevice); renderer.draw( cmd, gfxDevice, camera, GameRenderer::SceneData{ camera, glm::vec3(0.1f), 0.5f, glm::vec3(0.5f), 0.01f }); gfxDevice.endFrame( cmd, drawImage, { .clearColor = {{0.f, 0.f, 0.5f, 1.f}}, .drawImageBlitRect = glm::ivec4{} }); } void LightKeeper::customCleanup() { SceneManager::GetInstance().Destroy(); renderer.cleanup(gfxDevice.getDevice().device); } void LightKeeper::onWindowResize(int newWidth, int newHeight) { renderer.resize(gfxDevice, glm::ivec2{newWidth, newHeight}); const float aspectRatio = static_cast(newWidth) / static_cast(newHeight); camera.setAspectRatio(aspectRatio); }