Refactor logging
This commit is contained in:
@@ -4,7 +4,7 @@ add_library(fever_engine
|
||||
Window.cpp
|
||||
scene.cpp
|
||||
framebuffer.cpp
|
||||
util/Log.cpp
|
||||
log.cpp
|
||||
image.cpp
|
||||
mesh.cpp
|
||||
glad.cpp
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "render.h"
|
||||
#include "shader.h"
|
||||
#include "time.h"
|
||||
#include "util/Log.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <array>
|
||||
@@ -17,6 +16,7 @@
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <iostream>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
@@ -51,7 +51,7 @@ void Controller::run()
|
||||
auto standard_material_shader =
|
||||
m_shader_cache.load(shader_hash, Material::SHADER_NAME).first->second;
|
||||
|
||||
Log::logger().info("Startup complete. Enter game loop.");
|
||||
spdlog::info("Startup complete. Enter game loop.");
|
||||
|
||||
// This is the game loop
|
||||
while (glfwWindowShouldClose(&m_gameWindow->glfw_window()) == GLFW_FALSE) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "Window.h"
|
||||
#include "util/Log.h"
|
||||
#include "glad.h"
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glad/gl.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
static constexpr unsigned INIT_WINDOW_WIDTH = 1280;
|
||||
static constexpr unsigned INIT_WINDOW_HEIGHT = 720;
|
||||
@@ -27,7 +27,7 @@ Window::Window()
|
||||
[](GLFWwindow* window) { glfwDestroyWindow(window); });
|
||||
|
||||
if (!m_glfw_window) {
|
||||
Log::logger().critical("Failed to create window");
|
||||
spdlog::critical("Failed to create window");
|
||||
}
|
||||
|
||||
// Create OpenGL context
|
||||
@@ -66,7 +66,7 @@ void Window::set_catched_cursor(bool value)
|
||||
|
||||
void Window::glfw_error_callback(int error, char const* description)
|
||||
{
|
||||
Log::logger().warn("GLFW [{:d}]: {:s}\n", error, description);
|
||||
spdlog::warn("GLFW [{:d}]: {:s}\n", error, description);
|
||||
}
|
||||
|
||||
void Window::framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height)
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include "camera.h"
|
||||
#include "util/Log.h"
|
||||
#include "Window.h"
|
||||
#include "input.h"
|
||||
#include "time.h"
|
||||
#include "Window.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
@@ -10,7 +9,7 @@
|
||||
auto Camera::projection_matrix() const -> glm::mat4
|
||||
{
|
||||
return std::visit(
|
||||
[](auto &&arg) -> glm::mat4 {
|
||||
[](auto&& arg) -> glm::mat4 {
|
||||
using T = std::decay_t<decltype(arg)>;
|
||||
if constexpr (std::is_same_v<T, Perspective>) {
|
||||
return glm::perspective(arg.fov / 2, arg.aspect_ratio, arg.near, arg.far);
|
||||
@@ -21,25 +20,25 @@ auto Camera::projection_matrix() const -> glm::mat4
|
||||
projection);
|
||||
};
|
||||
|
||||
auto Camera::view_matrix(GlobalTransform const &transform) -> glm::mat4
|
||||
auto Camera::view_matrix(GlobalTransform const& transform) -> glm::mat4
|
||||
{
|
||||
return glm::lookAt(
|
||||
transform.position(), transform.position() + front_vector(transform), UP_VECTOR);
|
||||
}
|
||||
|
||||
auto Camera::front_vector(GlobalTransform const &transform) -> glm::vec3
|
||||
auto Camera::front_vector(GlobalTransform const& transform) -> glm::vec3
|
||||
{
|
||||
return glm::normalize(transform.transform * glm::vec4(0.0, 0.0, 1.0, 0.0));
|
||||
}
|
||||
|
||||
void Camera::keyboard_movement(entt::registry ®istry)
|
||||
void Camera::keyboard_movement(entt::registry& registry)
|
||||
{
|
||||
struct KeyboardMovementContext
|
||||
{
|
||||
bool accelerate{};
|
||||
};
|
||||
|
||||
auto &movement_context = registry.ctx().emplace<KeyboardMovementContext>();
|
||||
auto& movement_context = registry.ctx().emplace<KeyboardMovementContext>();
|
||||
auto const& key_input = registry.ctx().get<Input::Key>();
|
||||
auto const& delta_time = registry.ctx().get<Time::Delta>();
|
||||
|
||||
@@ -51,10 +50,11 @@ void Camera::keyboard_movement(entt::registry ®istry)
|
||||
front_vec.y = 0;
|
||||
|
||||
glm::vec3 delta_pos = glm::vec3(0., 0., 0.);
|
||||
float delta_factor = SPEED * delta_time.delta.count() * (movement_context.accelerate ? ACCELERATION : 1.0F);
|
||||
float delta_factor =
|
||||
SPEED * delta_time.delta.count() * (movement_context.accelerate ? ACCELERATION : 1.0F);
|
||||
movement_context.accelerate = false;
|
||||
|
||||
for (auto const &[key, pressed] : key_input.key_map) {
|
||||
for (auto const& [key, pressed] : key_input.key_map) {
|
||||
if (key == GLFW_KEY_W && pressed) {
|
||||
delta_pos += delta_factor * glm::normalize(front_vec);
|
||||
}
|
||||
@@ -80,7 +80,7 @@ void Camera::keyboard_movement(entt::registry ®istry)
|
||||
camera_transform.translation += delta_pos;
|
||||
}
|
||||
|
||||
void Camera::mouse_orientation(entt::registry ®istry)
|
||||
void Camera::mouse_orientation(entt::registry& registry)
|
||||
{
|
||||
auto camera_view = registry.view<Camera, Transform>();
|
||||
auto camera_entity = camera_view.front();
|
||||
@@ -98,7 +98,7 @@ void Camera::mouse_orientation(entt::registry ®istry)
|
||||
auto yaw = static_cast<float>(deltaX);
|
||||
|
||||
// Orthographic projection currently unsupported
|
||||
auto &camera_perspective = std::get<Perspective>(camera.projection);
|
||||
auto& camera_perspective = std::get<Perspective>(camera.projection);
|
||||
|
||||
camera_perspective.pitch += glm::radians(pitch);
|
||||
camera_perspective.yaw += glm::radians(yaw);
|
||||
@@ -111,7 +111,7 @@ void Camera::mouse_orientation(entt::registry ®istry)
|
||||
glm::quat(glm::vec3(-camera_perspective.pitch, -camera_perspective.yaw, 0.0));
|
||||
}
|
||||
|
||||
void Camera::aspect_ratio_update(entt::registry ®istry)
|
||||
void Camera::aspect_ratio_update(entt::registry& registry)
|
||||
{
|
||||
float aspect_ratio = registry.ctx().get<Window::Descriptor>().aspect_ratio;
|
||||
|
||||
@@ -120,6 +120,6 @@ void Camera::aspect_ratio_update(entt::registry ®istry)
|
||||
auto [camera] = camera_view.get(camera_entity);
|
||||
|
||||
// Orthographic projection currently unsupported
|
||||
auto &camera_perspective = std::get<Perspective>(camera.projection);
|
||||
auto& camera_perspective = std::get<Perspective>(camera.projection);
|
||||
camera_perspective.aspect_ratio = aspect_ratio;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "framebuffer.h"
|
||||
#include "util/Log.h"
|
||||
|
||||
#include <array>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
Framebuffer::Framebuffer(glm::u32vec2 physical_dimensions)
|
||||
{
|
||||
@@ -35,7 +35,7 @@ Framebuffer::Framebuffer(glm::u32vec2 physical_dimensions)
|
||||
GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depth_stencil_buffer);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||
Log::logger().error("Framebuffer not complete");
|
||||
spdlog::error("Framebuffer not complete");
|
||||
}
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
28
src/glad.cpp
28
src/glad.cpp
@@ -1,7 +1,7 @@
|
||||
#include "glad.h"
|
||||
#include "util/Log.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
static void gl_debug_callback(GLenum source,
|
||||
GLenum type,
|
||||
@@ -109,17 +109,17 @@ static void gl_debug_callback(GLenum source,
|
||||
}
|
||||
|
||||
if (severity == GL_DEBUG_SEVERITY_HIGH || severity == GL_DEBUG_SEVERITY_MEDIUM) {
|
||||
Log::logger().debug("[OpenGL Debug Message]\n"
|
||||
"Message: {}\n"
|
||||
"Source: {}\n"
|
||||
"Type: {}\n"
|
||||
"ID: {}\n"
|
||||
"Severity: {}\n",
|
||||
_message,
|
||||
_source,
|
||||
_type,
|
||||
id,
|
||||
_severity);
|
||||
spdlog::debug("[OpenGL Debug Message]\n"
|
||||
"Message: {}\n"
|
||||
"Source: {}\n"
|
||||
"Type: {}\n"
|
||||
"ID: {}\n"
|
||||
"Severity: {}\n",
|
||||
_message,
|
||||
_source,
|
||||
_type,
|
||||
id,
|
||||
_severity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ void init_glad()
|
||||
{
|
||||
// Initialize GLAD
|
||||
if (gladLoadGL(glfwGetProcAddress) == 0) {
|
||||
Log::logger().critical("Failed to initialize GLAD");
|
||||
spdlog::critical("Failed to initialize GLAD");
|
||||
std::quick_exit(-1);
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ void init_glad()
|
||||
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
auto const* gl_version = reinterpret_cast<char const*>(glGetString(GL_VERSION));
|
||||
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
Log::logger().debug("OpenGL version: {}", gl_version);
|
||||
spdlog::debug("OpenGL version: {}", gl_version);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
glDebugMessageCallback(&gl_debug_callback, nullptr);
|
||||
#endif
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
#include "name.h"
|
||||
#include "relationship.h"
|
||||
#include "scene.h"
|
||||
#include "util/Log.h"
|
||||
|
||||
#include <iterator>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
struct AttributeLocations
|
||||
{
|
||||
@@ -41,18 +41,18 @@ static auto create_indices(std::span<uint8_t const> gltf_index_data) -> Indices
|
||||
return Indices{.values = std::move(index_data)};
|
||||
}
|
||||
|
||||
static auto load_texture(fx::gltf::Texture const &texture,
|
||||
fx::gltf::Document const &gltf,
|
||||
std::filesystem::path const &document_path,
|
||||
static auto load_texture(fx::gltf::Texture const& texture,
|
||||
fx::gltf::Document const& gltf,
|
||||
std::filesystem::path const& document_path,
|
||||
Image::ColorFormat colorFormat,
|
||||
entt::resource_cache<Image> &image_cache) -> entt::resource<Image>
|
||||
entt::resource_cache<Image>& image_cache) -> entt::resource<Image>
|
||||
{
|
||||
auto const &gltf_image = gltf.images.at(texture.source);
|
||||
auto const& gltf_image = gltf.images.at(texture.source);
|
||||
auto const base_directory = document_path.parent_path();
|
||||
|
||||
if (gltf_image.uri.empty()) {
|
||||
auto const &image_buffer_view = gltf.bufferViews.at(gltf_image.bufferView);
|
||||
auto const &image_buffer = gltf.buffers.at(image_buffer_view.buffer);
|
||||
auto const& image_buffer_view = gltf.bufferViews.at(gltf_image.bufferView);
|
||||
auto const& image_buffer = gltf.buffers.at(image_buffer_view.buffer);
|
||||
|
||||
std::string const image_name =
|
||||
document_path.string() + ".image." + std::to_string(texture.source);
|
||||
@@ -82,12 +82,12 @@ static auto load_texture(fx::gltf::Texture const &texture,
|
||||
return image_cache.load(image_hash, image_data, colorFormat).first->second;
|
||||
}
|
||||
|
||||
static auto load_material(fx::gltf::Material const &material,
|
||||
fx::gltf::Document const &gltf,
|
||||
std::filesystem::path const &document_path,
|
||||
entt::resource_cache<Material> &material_cache,
|
||||
entt::resource_cache<Image> &image_cache,
|
||||
entt::resource_cache<Shader, ShaderLoader> &shader_cache)
|
||||
static auto load_material(fx::gltf::Material const& material,
|
||||
fx::gltf::Document const& gltf,
|
||||
std::filesystem::path const& document_path,
|
||||
entt::resource_cache<Material>& material_cache,
|
||||
entt::resource_cache<Image>& image_cache,
|
||||
entt::resource_cache<Shader, ShaderLoader>& shader_cache)
|
||||
-> entt::resource<Material>
|
||||
{
|
||||
auto base_color_texture_id = material.pbrMetallicRoughness.baseColorTexture.index;
|
||||
@@ -95,14 +95,14 @@ static auto load_material(fx::gltf::Material const &material,
|
||||
|
||||
std::optional<entt::resource<Image>> base_color_image;
|
||||
if (base_color_texture_id != -1) {
|
||||
auto const &base_color_texture = gltf.textures.at(base_color_texture_id);
|
||||
auto const& base_color_texture = gltf.textures.at(base_color_texture_id);
|
||||
base_color_image = load_texture(
|
||||
base_color_texture, gltf, document_path, Image::ColorFormat::SRGB, image_cache);
|
||||
}
|
||||
|
||||
std::optional<entt::resource<Image>> normal_map_image;
|
||||
if (normal_texture_id != -1) {
|
||||
auto const &normal_texture = gltf.textures.at(normal_texture_id);
|
||||
auto const& normal_texture = gltf.textures.at(normal_texture_id);
|
||||
normal_map_image =
|
||||
load_texture(normal_texture, gltf, document_path, Image::ColorFormat::RGB, image_cache);
|
||||
}
|
||||
@@ -112,7 +112,7 @@ static auto load_material(fx::gltf::Material const &material,
|
||||
shader_cache.load(shader_hash, Material::SHADER_NAME).first->second;
|
||||
|
||||
if (material.name.empty()) {
|
||||
Log::logger().warn("glTF material has no name.");
|
||||
spdlog::warn("glTF material has no name.");
|
||||
}
|
||||
|
||||
entt::hashed_string material_hash(material.name.c_str());
|
||||
@@ -126,13 +126,13 @@ static auto load_material(fx::gltf::Material const &material,
|
||||
|
||||
static auto load_attribute(std::string_view attribute_name,
|
||||
uint32_t attribute_id,
|
||||
fx::gltf::Document const &gltf)
|
||||
fx::gltf::Document const& gltf)
|
||||
-> std::optional<std::pair<std::size_t, VertexAttributeData>>
|
||||
{
|
||||
auto const &attribute_accessor = gltf.accessors.at(attribute_id);
|
||||
auto const& attribute_accessor = gltf.accessors.at(attribute_id);
|
||||
|
||||
if (attribute_accessor.componentType != fx::gltf::Accessor::ComponentType::Float) {
|
||||
Log::logger().critical("Only float attributes supported!");
|
||||
spdlog::critical("Only float attributes supported!");
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
@@ -158,8 +158,8 @@ static auto load_attribute(std::string_view attribute_name,
|
||||
return {};
|
||||
}
|
||||
|
||||
auto const &buffer_view = gltf.bufferViews.at(attribute_accessor.bufferView);
|
||||
auto const &buffer = gltf.buffers.at(buffer_view.buffer);
|
||||
auto const& buffer_view = gltf.bufferViews.at(attribute_accessor.bufferView);
|
||||
auto const& buffer = gltf.buffers.at(buffer_view.buffer);
|
||||
|
||||
std::span<uint8_t const> buffer_span(buffer.data);
|
||||
std::span<uint8_t const> vertex_attribute_data_span =
|
||||
@@ -183,32 +183,32 @@ static auto load_attribute(std::string_view attribute_name,
|
||||
vertex_attribute_data_span);
|
||||
}
|
||||
|
||||
Log::logger().critical("Unsupported vertex attribute type!");
|
||||
spdlog::critical("Unsupported vertex attribute type!");
|
||||
std::terminate();
|
||||
}();
|
||||
|
||||
return std::make_pair(vertex_attribute_id.value(), std::move(vertex_attribute_data));
|
||||
}
|
||||
|
||||
auto load_gltf_primitive(fx::gltf::Primitive const &gltf_primitive,
|
||||
fx::gltf::Document const &gltf,
|
||||
auto load_gltf_primitive(fx::gltf::Primitive const& gltf_primitive,
|
||||
fx::gltf::Document const& gltf,
|
||||
std::string_view primitive_identifier,
|
||||
entt::resource_cache<Material> &material_cache,
|
||||
entt::resource_cache<Mesh> &mesh_cache) -> GltfPrimitive
|
||||
entt::resource_cache<Material>& material_cache,
|
||||
entt::resource_cache<Mesh>& mesh_cache) -> GltfPrimitive
|
||||
{
|
||||
// Load attributes
|
||||
auto tangent_it =
|
||||
std::find_if(gltf_primitive.attributes.cbegin(),
|
||||
gltf_primitive.attributes.cend(),
|
||||
[](auto const &attribute) { return attribute.first == "TANGENT"; });
|
||||
[](auto const& attribute) { return attribute.first == "TANGENT"; });
|
||||
|
||||
if (tangent_it == gltf_primitive.attributes.cend()) {
|
||||
Log::logger().critical("glTF scene has to include tangent and normal components!");
|
||||
spdlog::critical("glTF scene has to include tangent and normal components!");
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
std::map<Mesh::VertexAttributeId, VertexAttributeData> attributes;
|
||||
for (auto const &attribute : gltf_primitive.attributes) {
|
||||
for (auto const& attribute : gltf_primitive.attributes) {
|
||||
auto vertex_attribute = load_attribute(attribute.first, attribute.second, gltf);
|
||||
|
||||
if (!vertex_attribute.has_value()) {
|
||||
@@ -220,9 +220,9 @@ auto load_gltf_primitive(fx::gltf::Primitive const &gltf_primitive,
|
||||
}
|
||||
|
||||
// Load indices
|
||||
auto const &indices_accessor = gltf.accessors.at(gltf_primitive.indices);
|
||||
auto const &indices_buffer_view = gltf.bufferViews.at(indices_accessor.bufferView);
|
||||
auto const &indices_buffer = gltf.buffers.at(indices_buffer_view.buffer);
|
||||
auto const& indices_accessor = gltf.accessors.at(gltf_primitive.indices);
|
||||
auto const& indices_buffer_view = gltf.bufferViews.at(indices_accessor.bufferView);
|
||||
auto const& indices_buffer = gltf.buffers.at(indices_buffer_view.buffer);
|
||||
|
||||
std::span<uint8_t const> indices_buffer_span(indices_buffer.data);
|
||||
std::span<uint8_t const> indices_data_span =
|
||||
@@ -239,7 +239,7 @@ auto load_gltf_primitive(fx::gltf::Primitive const &gltf_primitive,
|
||||
return create_indices<Indices::UnsignedInt>(indices_data_span);
|
||||
}
|
||||
|
||||
Log::logger().critical("Unsupported indices type!");
|
||||
spdlog::critical("Unsupported indices type!");
|
||||
std::terminate();
|
||||
}();
|
||||
|
||||
@@ -250,14 +250,14 @@ auto load_gltf_primitive(fx::gltf::Primitive const &gltf_primitive,
|
||||
.first->second;
|
||||
|
||||
// Get material by hash
|
||||
auto const &gltf_material = gltf.materials.at(gltf_primitive.material);
|
||||
auto const& gltf_material = gltf.materials.at(gltf_primitive.material);
|
||||
entt::hashed_string material_hash(gltf_material.name.c_str());
|
||||
entt::resource<Material> material = material_cache[material_hash];
|
||||
|
||||
return GltfPrimitive{.mesh = mesh, .material = material};
|
||||
}
|
||||
|
||||
auto GltfLoader::operator()(std::filesystem::path const &document_path) -> result_type
|
||||
auto GltfLoader::operator()(std::filesystem::path const& document_path) -> result_type
|
||||
{
|
||||
fx::gltf::ReadQuotas const read_quotas{.MaxFileSize = MAX_SIZE,
|
||||
.MaxBufferByteLength = MAX_SIZE};
|
||||
@@ -275,7 +275,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul
|
||||
|
||||
// Load materials
|
||||
std::vector<entt::resource<Material>> materials;
|
||||
for (auto const &gltf_material : gltf.materials) {
|
||||
for (auto const& gltf_material : gltf.materials) {
|
||||
entt::resource<Material> material = load_material(
|
||||
gltf_material, gltf, document_path, material_cache, image_cache, shader_cache);
|
||||
materials.push_back(material);
|
||||
@@ -285,14 +285,14 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul
|
||||
std::vector<entt::resource<GltfMesh>> gltf_meshes;
|
||||
gltf_meshes.reserve(gltf.meshes.size());
|
||||
|
||||
for (auto const &gltf_mesh : gltf.meshes) {
|
||||
for (auto const& gltf_mesh : gltf.meshes) {
|
||||
// Load primitives
|
||||
unsigned primitive_count = 0;
|
||||
|
||||
std::vector<GltfPrimitive> primitives;
|
||||
primitives.reserve(gltf_mesh.primitives.size());
|
||||
|
||||
for (auto const &gltf_primitive : gltf_mesh.primitives) {
|
||||
for (auto const& gltf_primitive : gltf_mesh.primitives) {
|
||||
std::string const primitive_identifier = document_path.string() + "." + gltf_mesh.name +
|
||||
"." + ".primitive." +
|
||||
std::to_string(primitive_count);
|
||||
@@ -303,7 +303,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul
|
||||
}
|
||||
|
||||
if (gltf_mesh.name.empty()) {
|
||||
Log::logger().warn("glTF mesh has no name.");
|
||||
spdlog::warn("glTF mesh has no name.");
|
||||
}
|
||||
|
||||
entt::hashed_string gltf_mesh_hash(gltf_mesh.name.c_str());
|
||||
@@ -318,11 +318,11 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul
|
||||
std::unordered_map<std::size_t, entt::resource<GltfNode>> nodes_map;
|
||||
nodes_map.reserve(gltf.nodes.size());
|
||||
for (std::size_t i = 0; i < gltf.nodes.size(); ++i) {
|
||||
auto const &node = gltf.nodes.at(i);
|
||||
auto const& node = gltf.nodes.at(i);
|
||||
|
||||
auto mesh = [this, &node, &gltf_meshes]() -> std::optional<entt::resource<GltfMesh>> {
|
||||
if (node.mesh != -1) {
|
||||
auto const &gltf_mesh = gltf_meshes.at(node.mesh);
|
||||
auto const& gltf_mesh = gltf_meshes.at(node.mesh);
|
||||
return {gltf_mesh};
|
||||
}
|
||||
|
||||
@@ -331,10 +331,10 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul
|
||||
|
||||
auto camera = [this, &node, &gltf]() -> std::optional<GltfCamera> {
|
||||
if (node.camera != -1) {
|
||||
auto const &camera = gltf.cameras.at(node.camera);
|
||||
auto const& camera = gltf.cameras.at(node.camera);
|
||||
|
||||
if (camera.type != fx::gltf::Camera::Type::Perspective) {
|
||||
Log::logger().warn("Only perspective projections supported.");
|
||||
spdlog::warn("Only perspective projections supported.");
|
||||
}
|
||||
|
||||
// Only perspective supported until now
|
||||
@@ -353,7 +353,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul
|
||||
Transform transform{.translation = translation, .orientation = rotation, .scale = scale};
|
||||
|
||||
if (node.name.empty()) {
|
||||
Log::logger().warn("glTF node has no name.");
|
||||
spdlog::warn("glTF node has no name.");
|
||||
}
|
||||
|
||||
entt::hashed_string node_hash(node.name.c_str());
|
||||
@@ -372,45 +372,45 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul
|
||||
// Resolve child hierarchy
|
||||
// TODO WRONG!!! does only work for 1 child generation
|
||||
for (std::size_t i = 0; i < gltf.nodes.size(); ++i) {
|
||||
auto const &gltf_node = gltf.nodes.at(i);
|
||||
auto const& gltf_node = gltf.nodes.at(i);
|
||||
std::vector<entt::resource<GltfNode>> children;
|
||||
for (int child_node_id : gltf_node.children) {
|
||||
auto child_node = nodes_map.extract(child_node_id);
|
||||
children.push_back(child_node.mapped());
|
||||
}
|
||||
auto const &node = nodes_map.at(i);
|
||||
auto const& node = nodes_map.at(i);
|
||||
node->children = std::move(children);
|
||||
};
|
||||
|
||||
std::vector<entt::resource<GltfNode>> nodes;
|
||||
nodes.reserve(nodes_map.size());
|
||||
for (auto const &node : nodes_map) {
|
||||
for (auto const& node : nodes_map) {
|
||||
nodes.push_back(node.second);
|
||||
}
|
||||
|
||||
// Load scenes
|
||||
std::vector<entt::resource<Scene>> scenes;
|
||||
for (auto const &gltf_scene : gltf.scenes) {
|
||||
for (auto const& gltf_scene : gltf.scenes) {
|
||||
// Get nodes by hash
|
||||
std::vector<entt::resource<GltfNode>> nodes;
|
||||
nodes.reserve(gltf_scene.nodes.size());
|
||||
|
||||
for (auto node_id : gltf_scene.nodes) {
|
||||
auto const &node = gltf.nodes.at(node_id);
|
||||
auto const& node = gltf.nodes.at(node_id);
|
||||
entt::hashed_string node_hash(node.name.c_str());
|
||||
nodes.push_back(gltf_node_cache[node_hash]);
|
||||
}
|
||||
|
||||
if (gltf_scene.name.empty()) {
|
||||
Log::logger().warn("glTF scene has no name.");
|
||||
spdlog::warn("glTF scene has no name.");
|
||||
}
|
||||
|
||||
entt::registry registry;
|
||||
|
||||
// Spawn an entity for every node in scene
|
||||
for (auto const &node : nodes) {
|
||||
std::function<entt::entity(GltfNode const &, std::optional<entt::entity>)> spawn_node =
|
||||
[this, &spawn_node, ®istry](GltfNode const &node,
|
||||
for (auto const& node : nodes) {
|
||||
std::function<entt::entity(GltfNode const&, std::optional<entt::entity>)> spawn_node =
|
||||
[this, &spawn_node, ®istry](GltfNode const& node,
|
||||
std::optional<entt::entity> parent) {
|
||||
auto entity = registry.create();
|
||||
registry.emplace<Name>(entity, node.name);
|
||||
@@ -425,7 +425,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul
|
||||
|
||||
auto mesh = node.mesh;
|
||||
if (mesh.has_value()) {
|
||||
for (auto const &primitive : mesh.value()->primitives) {
|
||||
for (auto const& primitive : mesh.value()->primitives) {
|
||||
auto mesh_entity = registry.create();
|
||||
registry.emplace<Parent>(mesh_entity, Parent{.parent = entity});
|
||||
registry.emplace<Transform>(mesh_entity, Transform{});
|
||||
@@ -451,7 +451,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul
|
||||
}
|
||||
|
||||
// Spawn child nodes
|
||||
for (auto const &child : node.children) {
|
||||
for (auto const& child : node.children) {
|
||||
auto child_entity = spawn_node(child, entity);
|
||||
child_entities.push_back(child_entity);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "image.h"
|
||||
#include "util/Log.h"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
@@ -10,15 +11,17 @@ Image::Image(std::span<uint8_t const> bytes, ColorFormat colorFormat) : colorFor
|
||||
int height{};
|
||||
int components{};
|
||||
|
||||
uint8_t *stbi_image =
|
||||
stbi_load_from_memory(bytes.data(), static_cast<int>(bytes.size()), &width, &height, &components, 0);
|
||||
uint8_t* stbi_image = stbi_load_from_memory(
|
||||
bytes.data(), static_cast<int>(bytes.size()), &width, &height, &components, 0);
|
||||
|
||||
std::size_t const buffer_length = static_cast<unsigned>(width * height * components);
|
||||
|
||||
// Copy the image data into a vector as stbi currently does not support writing into user-defined buffers.
|
||||
std::copy(stbi_image,
|
||||
&stbi_image[buffer_length], // NOLINT (cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
||||
std::back_inserter(data));
|
||||
// Copy the image data into a vector as stbi currently does not support writing into
|
||||
// user-defined buffers.
|
||||
std::copy(
|
||||
stbi_image,
|
||||
&stbi_image[buffer_length], // NOLINT (cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
||||
std::back_inserter(data));
|
||||
|
||||
dataFormat = [components]() {
|
||||
switch (components) {
|
||||
@@ -32,7 +35,7 @@ Image::Image(std::span<uint8_t const> bytes, ColorFormat colorFormat) : colorFor
|
||||
return DataFormat::RGBA8Uint;
|
||||
|
||||
default:
|
||||
Log::logger().warn("Unsupported data format for image.");
|
||||
spdlog::warn("Unsupported data format for image.");
|
||||
return DataFormat::RGBA8Uint;
|
||||
}
|
||||
}();
|
||||
@@ -42,7 +45,7 @@ Image::Image(std::span<uint8_t const> bytes, ColorFormat colorFormat) : colorFor
|
||||
stbi_image_free(stbi_image);
|
||||
}
|
||||
|
||||
GpuImage::GpuImage(Image const &image)
|
||||
GpuImage::GpuImage(Image const& image)
|
||||
{
|
||||
GLenum internalFormat{};
|
||||
GLenum dataFormat{};
|
||||
@@ -57,7 +60,8 @@ GpuImage::GpuImage(Image const &image)
|
||||
dataFormat = GL_RGB;
|
||||
break;
|
||||
case Image::DataFormat::RGBA8Uint:
|
||||
internalFormat = (image.colorFormat == Image::ColorFormat::SRGB) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
|
||||
internalFormat =
|
||||
(image.colorFormat == Image::ColorFormat::SRGB) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
|
||||
dataFormat = GL_RGBA;
|
||||
break;
|
||||
}
|
||||
@@ -65,8 +69,10 @@ GpuImage::GpuImage(Image const &image)
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, static_cast<GLint>(image.sampler.magFilter));
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, static_cast<GLint>(image.sampler.minFilter));
|
||||
glTexParameteri(
|
||||
GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, static_cast<GLint>(image.sampler.magFilter));
|
||||
glTexParameteri(
|
||||
GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, static_cast<GLint>(image.sampler.minFilter));
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, LOD_BIAS);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, static_cast<GLint>(image.sampler.wrapS));
|
||||
|
||||
17
src/log.cpp
Normal file
17
src/log.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "log.h"
|
||||
|
||||
#include <spdlog/cfg/env.h>
|
||||
|
||||
void Log::initialize()
|
||||
{
|
||||
spdlog::set_pattern("[%H:%M:%S.%e] [%^%l%$] %v");
|
||||
|
||||
#ifndef NDEBUG
|
||||
spdlog::set_level(spdlog::level::debug);
|
||||
#else
|
||||
spdlog::set_level(spdlog::level::warn);
|
||||
#endif
|
||||
|
||||
// Override level when running with environment variable.
|
||||
spdlog::cfg::load_env_levels();
|
||||
}
|
||||
9
src/log.h
Normal file
9
src/log.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace Log {
|
||||
|
||||
void initialize();
|
||||
|
||||
} // namespace Log
|
||||
@@ -1,18 +1,17 @@
|
||||
#include "Controller.h"
|
||||
#include "util/Log.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cout << "[Debug Mode]" << std::endl;
|
||||
#endif
|
||||
Log::initialize();
|
||||
|
||||
// Initialize GLFW
|
||||
if (glfwInit() == 0) {
|
||||
Log::logger().critical("Could not initialize GLFW");
|
||||
spdlog::critical("Could not initialize GLFW");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
#include "mesh.h"
|
||||
#include "util/Log.h"
|
||||
|
||||
#include <exception>
|
||||
|
||||
GpuMesh::GpuMesh(Mesh const &mesh)
|
||||
GpuMesh::GpuMesh(Mesh const& mesh)
|
||||
{
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
// Vertex attributes
|
||||
for (auto const &[attribute_id, attribute_data] : mesh.attributes) {
|
||||
for (auto const& [attribute_id, attribute_data] : mesh.attributes) {
|
||||
// BUG: https://github.com/llvm/llvm-project/issues/48582
|
||||
auto attr_id = attribute_id;
|
||||
|
||||
@@ -18,7 +17,7 @@ GpuMesh::GpuMesh(Mesh const &mesh)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
|
||||
std::visit(
|
||||
[attr_id](auto &&arg) {
|
||||
[attr_id](auto&& arg) {
|
||||
glBufferData(GL_ARRAY_BUFFER, arg.size(), arg.data(), GL_STATIC_DRAW);
|
||||
|
||||
int const components = [arg]() -> int {
|
||||
@@ -56,7 +55,7 @@ GpuMesh::GpuMesh(Mesh const &mesh)
|
||||
glGenBuffers(1, &ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
std::visit(
|
||||
[this](auto &&arg) {
|
||||
[this](auto&& arg) {
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, arg.size(), arg.data(), GL_STATIC_DRAW);
|
||||
|
||||
indices_count = arg.size();
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "relationship.h"
|
||||
#include "shader.h"
|
||||
#include "transform.h"
|
||||
#include "util/Log.h"
|
||||
#include "Window.h"
|
||||
|
||||
Scene::Scene(entt::registry registry) : m_registry(std::move(registry))
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "shader.h"
|
||||
#include "util/Log.h"
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <fstream>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
Shader::Shader(std::string_view name, std::filesystem::path const &directory)
|
||||
Shader::Shader(std::string_view name, std::filesystem::path const& directory)
|
||||
: program(glCreateProgram())
|
||||
{
|
||||
std::filesystem::path vertex_shader_path = directory / name;
|
||||
@@ -27,9 +27,9 @@ Shader::Shader(std::string_view name, std::filesystem::path const &directory)
|
||||
|
||||
GLint linked = 0;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &linked);
|
||||
|
||||
|
||||
if (linked == 0) {
|
||||
Log::logger().warn(R"(Failed to link Shader "{}")", name);
|
||||
spdlog::warn(R"(Failed to link Shader "{}")", name);
|
||||
}
|
||||
|
||||
#ifdef NDEBUG
|
||||
@@ -40,7 +40,7 @@ Shader::Shader(std::string_view name, std::filesystem::path const &directory)
|
||||
glDeleteShader(fragment_shader);
|
||||
#endif
|
||||
|
||||
Log::logger().trace(R"(Loaded Shader "{}")", name);
|
||||
spdlog::trace(R"(Loaded Shader "{}")", name);
|
||||
}
|
||||
|
||||
Shader::~Shader()
|
||||
@@ -58,13 +58,13 @@ void Shader::unbind()
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
auto Shader::parse(const std::filesystem::path &path) -> std::string
|
||||
auto Shader::parse(std::filesystem::path const& path) -> std::string
|
||||
{
|
||||
std::fstream file;
|
||||
file.open(path, std::ios::in);
|
||||
|
||||
if (!file.is_open()) {
|
||||
Log::logger().critical(R"(Shader "{}" not found!)", path.string());
|
||||
spdlog::critical(R"(Shader "{}" not found!)", path.string());
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ auto Shader::parse(const std::filesystem::path &path) -> std::string
|
||||
auto Shader::compile(std::string_view source, GLenum type) -> GLuint
|
||||
{
|
||||
GLuint program = glCreateShader(type);
|
||||
auto const *src = source.data();
|
||||
auto const* src = source.data();
|
||||
glShaderSource(program, 1, &src, nullptr);
|
||||
glCompileShader(program);
|
||||
|
||||
@@ -89,7 +89,7 @@ auto Shader::compile(std::string_view source, GLenum type) -> GLuint
|
||||
message.reserve(static_cast<unsigned>(length));
|
||||
|
||||
glGetShaderInfoLog(program, length, &length, message.data());
|
||||
Log::logger().error("Shader compilation failed: {}", message);
|
||||
spdlog::error("Shader compilation failed: {}", message);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -108,14 +108,13 @@ auto Shader::retrieveUniformLocation(std::string_view uniform_name) const -> GLi
|
||||
if (location != -1) {
|
||||
uniform_location_cache[uniform_name.data()] = location;
|
||||
} else {
|
||||
Log::logger().warn(R"(Uniform "{}" not found.)", uniform_name);
|
||||
spdlog::warn(R"(Uniform "{}" not found.)", uniform_name);
|
||||
}
|
||||
|
||||
return location;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Shader::set_uniform(std::string_view name, T value) const
|
||||
template <typename T> void Shader::set_uniform(std::string_view name, T value) const
|
||||
{
|
||||
GLint location = retrieveUniformLocation(name);
|
||||
|
||||
@@ -126,9 +125,10 @@ void Shader::set_uniform(std::string_view name, T value) const
|
||||
} else if constexpr (std::is_same_v<T, float>) {
|
||||
glUniform1f(location, value);
|
||||
} else if constexpr (std::is_same_v<T, glm::vec2>) {
|
||||
glUniform2f(location, value.x, value.y); //NOLINT(cppcoreguidelines-pro-type-union-access)
|
||||
glUniform2f(location, value.x, value.y); // NOLINT(cppcoreguidelines-pro-type-union-access)
|
||||
} else if constexpr (std::is_same_v<T, glm::vec3>) {
|
||||
glUniform3f(location, value.x, value.y, value.z); //NOLINT(cppcoreguidelines-pro-type-union-access)
|
||||
glUniform3f(
|
||||
location, value.x, value.y, value.z); // NOLINT(cppcoreguidelines-pro-type-union-access)
|
||||
} else if constexpr (std::is_same_v<T, glm::mat3>) {
|
||||
glUniformMatrix3fv(location, 1, GL_FALSE, glm::value_ptr(value));
|
||||
} else if constexpr (std::is_same_v<T, glm::mat4>) {
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
#include "Log.h"
|
||||
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/cfg/env.h>
|
||||
|
||||
Log Log::s_instance;
|
||||
|
||||
Log::Log() noexcept
|
||||
{
|
||||
m_logger = spdlog::stdout_color_mt("Core");
|
||||
m_logger->set_pattern("[%H:%M:%S.%e] [%n] [%^%l%$] %v");
|
||||
|
||||
#ifndef NDEBUG
|
||||
m_logger->set_level(spdlog::level::debug);
|
||||
#else
|
||||
m_logger->set_level(spdlog::level::warn);
|
||||
#endif
|
||||
|
||||
// Override level when running with environment variable.
|
||||
spdlog::cfg::load_env_levels();
|
||||
}
|
||||
|
||||
spdlog::logger &Log::logger()
|
||||
{
|
||||
return *s_instance.m_logger;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
class Log
|
||||
{
|
||||
public:
|
||||
static spdlog::logger &logger();
|
||||
|
||||
private:
|
||||
Log() noexcept;
|
||||
|
||||
static Log s_instance;
|
||||
|
||||
std::shared_ptr<spdlog::logger> m_logger;
|
||||
};
|
||||
Reference in New Issue
Block a user