Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shadow System Enhancements and Renderer Optimizations #51

Merged
merged 14 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SlimeProject

SlimeProject is a game development project consisting of two main components: SlimeOdyssey (a game engine library) and SlimeGame (a game using the SlimeOdyssey engine).
![image](https://github.com/user-attachments/assets/4f4498c2-1deb-4c52-8ac2-a8515645774d)
![image](https://github.com/user-attachments/assets/229d0dc7-eb05-4651-8d49-2df8f8564e9a)

## Project Structure
Expand Down
86 changes: 84 additions & 2 deletions SlimeGame/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,87 @@
#include <spdlog/sinks/stdout_color_sinks.h>
#include <DescriptorManager.h>

#include "spdlog/sinks/base_sink.h"
#include "spdlog/spdlog.h"

#include <iostream>

#ifdef _WIN32
# include <intrin.h>
#else
# include <csignal>
#endif

template<typename Mutex>
class breakpoint_on_error_sink : public spdlog::sinks::base_sink<Mutex>
{
protected:
const char* get_color_code(spdlog::level::level_enum level) const
{
switch (level)
{
case spdlog::level::trace:
return "\033[90m"; // Dark gray
case spdlog::level::debug:
return "\033[36m"; // Cyan
case spdlog::level::info:
return "\033[32m"; // Green
case spdlog::level::warn:
return "\033[33m"; // Yellow
case spdlog::level::err:
return "\033[31m"; // Red
case spdlog::level::critical:
return "\033[1m\033[31m"; // Bold red
default:
return "\033[0m"; // Reset color
}
}

void sink_it_(const spdlog::details::log_msg& msg) override
{
// Get current time
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;

// Format time
std::tm tm_time{};
#ifdef _WIN32
localtime_s(&tm_time, &time);
#else
localtime_r(&time, &tm_time);
#endif

// Print timestamp
std::cout << '[' << std::put_time(&tm_time, "%Y-%m-%d %H:%M:%S") << '.' << std::setfill('0') << std::setw(3) << ms.count() << "] ";

// Print colored log level
std::cout << '[' << get_color_code(msg.level) << spdlog::level::to_string_view(msg.level).data() << "\033[0m] "; // Reset color after log level

// Print message
std::cout.write(msg.payload.data(), static_cast<std::streamsize>(msg.payload.size()));

// End line
std::cout << std::endl;

// Check if the log level is error
if (msg.level == spdlog::level::err || msg.level == spdlog::level::critical)
{
// Trigger a breakpoint
#ifdef _WIN32
__debugbreak();
#else
std::raise(SIGTRAP);
#endif
}
}

void flush_() override
{
}
};


Application::Application()
: m_window({ .title = "Slime Odyssey", .width = 1920, .height = 1080, .resizable = true, .decorated = true, .fullscreen = false }), m_scene(&m_window)
{
Expand Down Expand Up @@ -31,8 +112,9 @@ void Application::Cleanup()

void Application::InitializeLogging()
{
spdlog::set_level(spdlog::level::trace);
spdlog::stdout_color_mt("console");
auto breakpoint_sink = std::make_shared<breakpoint_on_error_sink<std::mutex>>();
spdlog::set_default_logger(std::make_shared<spdlog::logger>("console", breakpoint_sink));
spdlog::set_level(spdlog::level::trace);
}

void Application::InitializeWindow()
Expand Down
2 changes: 1 addition & 1 deletion SlimeGame/src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ShaderManager.h>
#include <VulkanContext.h>

#define DEBUG_SCENE
// #define DEBUG_SCENE


#ifdef DEBUG_SCENE
Expand Down
31 changes: 23 additions & 8 deletions SlimeGame/src/DebugScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ DebugScene::DebugScene(SlimeWindow* window)
: Scene(), m_window(window)
{
Entity mainCamera = Entity("MainCamera");
mainCamera.AddComponent<Camera>(90.0f, 1920.0f / 1080.0f, 0.001f, 1000.0f);
mainCamera.AddComponent<Camera>(90.0f, 1920.0f / 1080.0f, 0.01f, 1000.0f);
m_entityManager.AddEntity(mainCamera);
}

Expand All @@ -28,6 +28,9 @@ int DebugScene::Enter(VulkanContext& vulkanContext, ModelManager& modelManager,
pbrMaterialResource = descriptorManager.CreatePBRMaterial(vulkanContext, modelManager, "PBR Planet Material", "planet_surface/albedo.png", "planet_surface/normal.png", "planet_surface/metallic.png", "planet_surface/roughness.png", "planet_surface/ao.png");
m_pbrMaterials.push_back(pbrMaterialResource);

pbrMaterialResource = descriptorManager.CreatePBRMaterial(vulkanContext, modelManager, "Grass Material", "grass/albedo.png", "grass/normal.png", "planet_surface/metallic.png", "grass/roughness.png", "grass/ao.png");
m_pbrMaterials.push_back(pbrMaterialResource);

InitializeDebugObjects(vulkanContext, modelManager);

return 0;
Expand Down Expand Up @@ -62,7 +65,8 @@ void DebugScene::InitializeDebugObjects(VulkanContext& vulkanContext, ModelManag
{
// Light
auto lightEntity = std::make_shared<Entity>("Light");
DirectionalLight& light = lightEntity->AddComponent<DirectionalLightObject>().light;
DirectionalLight& light = lightEntity->AddComponent<DirectionalLight>();
light.SetColor(glm::vec3(0.98f, 0.506f, 0.365f));
m_entityManager.AddEntity(lightEntity);

VmaAllocator allocator = vulkanContext.GetAllocator();
Expand All @@ -73,12 +77,23 @@ void DebugScene::InitializeDebugObjects(VulkanContext& vulkanContext, ModelManag
auto bunnyMesh = modelManager.LoadModel("stanford-bunny.obj", "pbr");
modelManager.CreateBuffersForMesh(allocator, *bunnyMesh);

auto groundPlane = modelManager.CreatePlane(allocator, 50.0f, 25);
modelManager.CreateBuffersForMesh(allocator, *groundPlane);

// Create the groundPlane
Entity ground = Entity("Ground");
ground.AddComponent<Model>(groundPlane);
ground.AddComponent<PBRMaterial>(m_pbrMaterials[2]);
auto& groundTransform = ground.AddComponent<Transform>();
groundTransform.position = glm::vec3(0.0f, 0.2f, 0.0f); // Slightly above the grid
m_entityManager.AddEntity(ground);

// Create a bunny
Entity bunny = Entity("Bunny");
bunny.AddComponent<Model>(bunnyMesh);
bunny.AddComponent<PBRMaterial>(m_pbrMaterials[0]);
auto& bunnyTransform = bunny.AddComponent<Transform>();
bunnyTransform.position = glm::vec3(10.0f, 4.0f, -10.0f);
bunnyTransform.position = glm::vec3(10.0f, 3.0f, -10.0f);
bunnyTransform.scale = glm::vec3(20.0f);
m_entityManager.AddEntity(bunny);

Expand Down Expand Up @@ -136,7 +151,7 @@ void DebugScene::Update(float dt, VulkanContext& vulkanContext, const InputManag
}
}

void DebugScene::Render(VulkanContext& vulkanContext, ModelManager& modelManager)
void DebugScene::Render()
{
// scene Camera info
ImGui::Begin("Camera Info");
Expand Down Expand Up @@ -164,17 +179,17 @@ void DebugScene::Exit(VulkanContext& vulkanContext, ModelManager& modelManager)
}

// Clean up lights
std::vector<std::shared_ptr<Entity>> lightEntities = m_entityManager.GetEntitiesWithComponents<PointLightObject>();
std::vector<std::shared_ptr<Entity>> lightEntities = m_entityManager.GetEntitiesWithComponents<PointLight>();
for (const auto& entity: lightEntities)
{
PointLightObject& light = entity->GetComponent<PointLightObject>();
PointLight& light = entity->GetComponent<PointLight>();
vmaDestroyBuffer(vulkanContext.GetAllocator(), light.buffer, light.allocation);
}

lightEntities = m_entityManager.GetEntitiesWithComponents<DirectionalLightObject>();
lightEntities = m_entityManager.GetEntitiesWithComponents<DirectionalLight>();
for (const auto& entity: lightEntities)
{
DirectionalLightObject& light = entity->GetComponent<DirectionalLightObject>();
DirectionalLight& light = entity->GetComponent<DirectionalLight>();
vmaDestroyBuffer(vulkanContext.GetAllocator(), light.buffer, light.allocation);
}

Expand Down
2 changes: 1 addition & 1 deletion SlimeGame/src/DebugScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DebugScene : public Scene
DebugScene(SlimeWindow* window);
int Enter(VulkanContext& vulkanContext, ModelManager& modelManager, ShaderManager& shaderManager, DescriptorManager& descriptorManager) override;
void Update(float dt, VulkanContext& vulkanContext, const InputManager* inputManager) override;
void Render(VulkanContext& vulkanContext, ModelManager& modelManager) override;
void Render() override;
void Exit(VulkanContext& vulkanContext, ModelManager& modelManager) override;

private:
Expand Down
Loading