init tick 분리

This commit is contained in:
2025-05-26 02:07:44 +09:00
commit c0ed33ca17
52 changed files with 11767 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
#pragma once
#include "asset/object/model.h"
#include "assimp/Importer.hpp"
#include "assimp/postprocess.h"
#include "assimp/scene.h"
namespace veng {
typedef Model& inModel;
class Loader {
public:
void setPath(std::string path);
void loadModel(inModel model);
std::vector<std::uint8_t> readTexture();
private:
Assimp::Importer importer_;
const struct aiScene* scene_ = nullptr;
};
} // namespace veng

View File

@@ -0,0 +1,10 @@
#pragma once
#include "vulkan/texture_handle.h"
namespace veng {
struct Material {
TextureHandle texture_handle;
std::vector<std::uint8_t> texture_image;
};
} // namespace veng

View File

@@ -0,0 +1,156 @@
#pragma once
#include <functional>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <vector>
#include "material.h"
#include "vulkan/buffer_handle.h"
#include "vulkan/vertex.h"
namespace veng {
struct Model {
Model() : graphics_(nullptr) {}
Model(class Graphics* graphics) : graphics_(graphics) {}
~Model();
Model(const Model& other)
: vertices(other.vertices),
vertex_buffer(other.vertex_buffer),
indices(other.indices),
index_buffer(other.index_buffer),
transform(other.transform),
position(other.position),
linear_velocity(other.linear_velocity),
linear_acceleration(other.linear_acceleration),
rotation(other.rotation),
angular_velocity(other.angular_velocity),
angular_acceleration(other.angular_acceleration),
scale(other.scale),
material(other.material),
original_offset(other.original_offset),
owner(other.owner),
radius(other.radius),
OnColision(other.OnColision),
visible(other.visible),
colision(other.colision) {
graphics_ = nullptr;
}
Model(Model&& other)
: vertices(other.vertices),
vertex_buffer(other.vertex_buffer),
indices(other.indices),
index_buffer(other.index_buffer),
transform(other.transform),
position(other.position),
linear_velocity(other.linear_velocity),
linear_acceleration(other.linear_acceleration),
rotation(other.rotation),
angular_velocity(other.angular_velocity),
angular_acceleration(other.angular_acceleration),
scale(other.scale),
material(other.material),
original_offset(other.original_offset),
owner(other.owner),
radius(other.radius),
OnColision(other.OnColision),
visible(other.visible),
colision(other.colision) {
graphics_ = other.graphics_;
other.graphics_ = nullptr;
}
Model& operator=(const Model& other) {
if (this != &other) {
vertices = other.vertices;
vertex_buffer = other.vertex_buffer;
indices = other.indices;
index_buffer = other.index_buffer;
transform = other.transform;
position = other.position;
linear_velocity = other.linear_velocity;
linear_acceleration = other.linear_acceleration;
rotation = other.rotation;
angular_velocity = other.angular_velocity;
angular_acceleration = other.angular_acceleration;
scale = other.scale;
material = other.material;
original_offset = other.original_offset;
owner = other.owner;
radius = other.radius;
OnColision = other.OnColision;
visible = other.visible;
colision = other.colision;
graphics_ = nullptr;
}
return *this;
}
Model& operator=(Model&& other) noexcept {
if (this != &other) {
vertices = std::move(other.vertices);
vertex_buffer = other.vertex_buffer;
indices = std::move(other.indices);
index_buffer = other.index_buffer;
transform = other.transform;
position = other.position;
linear_velocity = other.linear_velocity;
linear_acceleration = other.linear_acceleration;
rotation = other.rotation;
angular_velocity = other.angular_velocity;
angular_acceleration = other.angular_acceleration;
scale = other.scale;
material = std::move(other.material);
original_offset = other.original_offset;
owner = other.owner;
radius = other.radius;
OnColision = other.OnColision;
visible = other.visible;
colision = other.colision;
// graphics_만 옮기고, 원본은 nullptr
graphics_ = other.graphics_;
other.graphics_ = nullptr;
}
return *this;
}
std::vector<veng::Vertex> vertices;
veng::BufferHandle vertex_buffer;
std::vector<std::uint32_t> indices;
veng::BufferHandle index_buffer;
glm::mat4 transform = glm::rotate(glm::mat4(1.0f), glm::radians(180.f),
glm::vec3(0.f, 0.f, 1.f));
glm::vec3 position = glm::vec3(0.f);
glm::vec3 linear_velocity = glm::vec3(0.f);
glm::vec3 linear_acceleration = glm::vec3(0.f);
glm::quat rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
glm::vec3 angular_velocity = glm::vec3(0.f); // 축 * 각속도(rad/s)
glm::vec3 angular_acceleration = glm::vec3(0.f);
glm::vec3 scale = glm::vec3(1.f);
Material material;
void Update(float dt);
glm::vec3 original_offset = glm::vec3(0.f);
Model* owner = this;
std::float_t radius = 0.f;
std::function<void(Model* self, Model* other)> OnColision = nullptr;
bool visible = true;
bool colision = false;
private:
class Graphics* graphics_;
};
} // namespace veng

View File

@@ -0,0 +1,12 @@
#pragma once
namespace veng {
struct GlfwInitialization {
public:
GlfwInitialization();
~GlfwInitialization();
GlfwInitialization(const GlfwInitialization&) = delete;
GlfwInitialization& operator=(const GlfwInitialization&) = delete;
};
} // namespace veng

View File

@@ -0,0 +1,12 @@
#pragma once
struct GLFWmonitor;
struct GLFWwindow;
namespace veng {
gsl::span<GLFWmonitor *> GetMonitors();
glm::ivec2 GetMonitorPosition(gsl::not_null<GLFWmonitor *> monitor);
glm::ivec2 GetMonitorWorkAreaSize(gsl::not_null<GLFWmonitor *> monitor);
void MoveWindowToMonitor(gsl::not_null<GLFWwindow *> window,
gsl::not_null<GLFWmonitor *> monitor);
} // namespace veng

View File

@@ -0,0 +1,23 @@
#pragma once
#include <GLFW/glfw3.h>
namespace veng {
class Window {
public:
Window(gsl::czstring name, glm::ivec2 size);
~Window();
glm::ivec2 GetWindowSize() const;
glm::ivec2 GetFramebufferSize() const;
bool ShouldClose() const;
GLFWwindow* GetHandle() const;
GLFWkeyfun SetKeyCallback(GLFWkeyfun key_callback);
bool TryMoveToMonitor(std::uint16_t monitor_number);
private:
GLFWwindow* window_;
};
} // namespace veng

16
Client/include/precomp.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <glm/glm.hpp>
#include <gsl/gsl>
#include <optional>
#include <string>
#include <string_view>
#include "spdlog/spdlog.h"
#include "utilities.h"
#define MAX_BUFFERED_FRAMES (2)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
#pragma once
#include <filesystem>
namespace veng {
bool streq(gsl::czstring left, gsl::czstring right);
std::vector<std::uint8_t> ReadFile(std::filesystem::path shader_path);
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <vulkan/vulkan.h>
namespace veng {
struct BufferHandle {
VkBuffer buffer = VK_NULL_HANDLE;
VkDeviceMemory memory = VK_NULL_HANDLE;
};
} // namespace veng

View File

@@ -0,0 +1,39 @@
#pragma once
#include <unordered_map>
#include "asset/object/model.h"
namespace std {
template <>
struct hash<glm::i64vec3> {
std::size_t operator()(const glm::i64vec3& v) const noexcept {
std::size_t h1 = std::hash<int64_t>()(v.x);
std::size_t h2 = std::hash<int64_t>()(v.y);
std::size_t h3 = std::hash<int64_t>()(v.z);
return h1 ^ (h2 << 1) ^ (h3 << 2);
}
};
} // namespace std
namespace veng {
class Coord {
public:
Coord() : seg(glm::i64vec3(0)), pos(glm::vec3(0.f)) {}
Coord(glm::vec3 _pos) : seg(glm::i64vec3(0)), pos(_pos) {}
Coord(glm::i64vec3 _seg, glm::vec3 _pos) : seg(_seg), pos(_pos) {}
glm::i64vec3 seg;
glm::vec3 pos;
Coord operator+(const Coord& other) const;
Coord operator-(const Coord& other) const;
std::unordered_map<glm::i64vec3, glm::vec3> coord_system;
private:
const std::float_t border = 1000.f;
};
} // namespace veng

View File

@@ -0,0 +1,52 @@
#pragma once
#include "asset/loader.h"
#include "physics.h"
#include "graphics.h"
namespace veng {
class Engine {
public:
Engine(gsl::not_null<Graphics*> vulkan_graphics)
: vulkan_graphics(vulkan_graphics) {}
void init();
void LoadModelAsset(std::string path, std::string name);
const Model* GetStaticModel(std::string name);
Model* SpawnModel(std::string asset_name, std::string name);
Model* SpawnLifedModel(std::string asset_name, std::string name,
std::float_t lifespan);
Model* GetSpawnedObject(std::string name);
void Update();
std::function<void(Engine&)> BeginPlay = [](Engine& engine) {};
std::function<void(Engine&, std::float_t delta_time)> Tick =
[](Engine& engine, std::float_t delta_time) {};
glm::mat4 view =
glm::lookAt(glm::vec3(0.f, 0.f, -5.f), glm::vec3(0.f, 0.f, 0.f),
glm::vec3(0.f, -1.f, 0.f));
glm::mat4 projection =
glm::perspective(glm::radians(103.f), 800.f / 600.f, 0.1f, 1000.f);
gsl::not_null<Graphics*> vulkan_graphics;
private:
Loader asset_loader_;
Physics physics_controller_;
glm::ivec2 window_size_ = {0, 0};
std::double_t last_frame_time_ = 0.0;
std::unordered_map<std::string, veng::Model> model_assets_;
std::unordered_map<std::string, veng::Model> dynamic_immortal_models_;
std::unordered_map<std::string, std::pair<veng::Model, std::float_t>>
dynamic_models_;
};
} // namespace veng

View File

@@ -0,0 +1,193 @@
#pragma once
#include <vulkan/vulkan.h>
#include "buffer_handle.h"
#include "glfw/glfw_window.h"
#include "precomp.h"
#include "texture_handle.h"
#include "vertex.h"
namespace veng {
struct Frame {
VkSemaphore image_available_signal = VK_NULL_HANDLE;
VkSemaphore render_finished_signal = VK_NULL_HANDLE;
VkFence still_rendering_fence = VK_NULL_HANDLE;
VkCommandBuffer command_buffer = VK_NULL_HANDLE;
VkDescriptorSet uniform_set = VK_NULL_HANDLE;
BufferHandle uniform_buffer;
void* uniform_buffer_location;
};
class Graphics final {
public:
Graphics(gsl::not_null<Window*> window);
~Graphics();
bool BeginFrame();
void SetModelMatrix(glm::mat4 model);
void SetViewProjection(glm::mat4 view, glm::mat4 projection);
void SetTexture(TextureHandle handle);
void RenderBuffer(BufferHandle handle, std::uint32_t vertex_count);
void RenderIndexedBuffer(BufferHandle vertex_buffer,
BufferHandle index_buffer, std::uint32_t count);
void RenderModel(struct Model* model);
void EndFrame();
BufferHandle CreateVertexBuffer(gsl::span<Vertex> vertices);
BufferHandle CreateIndexBuffer(gsl::span<std::uint32_t> indices);
void DestroyBuffer(BufferHandle handle);
TextureHandle CreateTexture(gsl::czstring path);
TextureHandle CreateTexture(std::vector<std::uint8_t> image_file_data);
TextureHandle CreateTexture(gsl::span<std::uint8_t> image_file_data);
void DestroyTexture(TextureHandle handle);
gsl::not_null<Window*> window;
private:
struct QueueFamilyIndices {
std::optional<std::uint32_t> graphics_family = std::nullopt;
std::optional<std::uint32_t> presentation_family = std::nullopt;
bool IsValid() const {
return graphics_family.has_value() && presentation_family.has_value();
}
};
struct SwapChainProperties {
VkSurfaceCapabilitiesKHR capabilities = {};
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> present_modes;
bool IsValid() const { return !formats.empty() && !present_modes.empty(); }
};
// 초기화
void InitializeVulkan();
void CreateInstance();
void SetupDebugMessenger();
void PickPhysicalDevice();
void CreateLogicalDeviceAndQueues();
void CreateSurface();
void CreateSwapChain();
void CreateImageViews();
void CreateRenderPass();
void CreateGraphicsPipeline();
void CreateFramebuffers();
void CreateCommandPool();
void CreateCommandBuffer();
void CreateSignals();
void CreateDescriptorSetLayouts();
void CreateDescriptorPools();
void CreateDescriptorSets();
void CreateTextureSampler();
void CreateDepthResources();
void RecreateSwapChain();
void CleanupSwapChain();
// 랜더링
void BeginCommands();
void EndCommands();
std::vector<gsl::czstring> GetRequiredInstanceExtentions();
static gsl::span<gsl::czstring> GetSuggestedInstanceExtentions();
static std::vector<VkExtensionProperties> GetSupprotedInstanceExtensions();
static bool AreAllExtensionsSupported(gsl::span<gsl::czstring> extensions);
static std::vector<VkLayerProperties> GetSupprotedValidationLayers();
static bool AreAllLayersSupported(gsl::span<gsl::czstring> extensions);
QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device);
SwapChainProperties GetSwapChainProperties(VkPhysicalDevice device);
bool IsDeviceSuitable(VkPhysicalDevice device);
std::vector<VkPhysicalDevice> GetAvailableDevices();
bool AreAllDeviceExtensionsSupported(VkPhysicalDevice device);
std::vector<VkExtensionProperties> GetDeviceAvailableExtensions(
VkPhysicalDevice device);
VkSurfaceFormatKHR ChooseSwapSurfaceFormat(
gsl::span<VkSurfaceFormatKHR> formats);
VkPresentModeKHR ChooseSwapPresentMode(
gsl::span<VkPresentModeKHR> present_modes);
VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
std::uint32_t ChooseSwapImageCount(
const VkSurfaceCapabilitiesKHR& capabilities);
VkShaderModule CreateShaderModule(gsl::span<std::uint8_t> buffer);
std::uint32_t FindMemoryType(std::uint32_t type_bits_filter,
VkMemoryPropertyFlags required_properties);
BufferHandle CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
VkMemoryPropertyFlags properties);
VkCommandBuffer BeginTransientCommandBuffer();
void EndTransientCommandBuffer(VkCommandBuffer command_buffer);
void CreateUniformBuffers();
TextureHandle CreateImage(glm::ivec2 size, VkFormat image_format,
VkBufferUsageFlags usage,
VkMemoryPropertyFlags properties);
void TransitionImageLayout(VkImage image, VkImageLayout old_layout,
VkImageLayout new_layout);
void CopyBufferToImage(VkBuffer buffer, VkImage image, glm::ivec2 image_size);
VkImageView CreateImageView(VkImage image, VkFormat format,
VkImageAspectFlags aspect_flag);
VkViewport GetViewport();
VkRect2D GetScissor();
std::array<gsl::czstring, 1> required_device_extentions_ = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME};
VkInstance instance_ = VK_NULL_HANDLE;
VkDebugUtilsMessengerEXT debug_messenger_;
VkPhysicalDevice physical_device_ = VK_NULL_HANDLE;
VkDevice logical_device_ = VK_NULL_HANDLE;
VkQueue graphics_queue_ = VK_NULL_HANDLE;
VkQueue present_queue_ = VK_NULL_HANDLE;
VkSurfaceKHR surface_ = VK_NULL_HANDLE;
VkSwapchainKHR swap_chain_ = VK_NULL_HANDLE;
VkSurfaceFormatKHR surface_format_;
VkPresentModeKHR present_mode_;
VkExtent2D extent_;
std::vector<VkImage> swap_chain_images_;
std::vector<VkImageView> swap_chain_image_views_;
std::vector<VkFramebuffer> swap_chain_framebuffers_;
VkPipelineLayout pipeline_layout_ = VK_NULL_HANDLE;
VkRenderPass render_pass_ = VK_NULL_HANDLE;
VkPipeline pipeline_ = VK_NULL_HANDLE;
VkDescriptorSetLayout descriptor_set_layout_ = VK_NULL_HANDLE;
VkCommandPool command_pool_ = VK_NULL_HANDLE;
std::uint32_t current_image_index_ = 0;
VkDescriptorSetLayout uniform_set_layout_ = VK_NULL_HANDLE;
VkDescriptorPool uniform_pool_ = VK_NULL_HANDLE;
VkDescriptorSetLayout texture_set_layout_ = VK_NULL_HANDLE;
VkDescriptorPool texture_pool_ = VK_NULL_HANDLE;
VkSampler texture_sampler_ = VK_NULL_HANDLE;
TextureHandle depth_texture_;
std::array<Frame, MAX_BUFFERED_FRAMES> frames_;
std::int32_t current_frame_ = 0;
bool validation_enabled_ = false;
};
VkDebugUtilsMessengerCreateInfoEXT GetCreateMessengerInfo();
bool IsExtensionSupported(gsl::span<VkExtensionProperties> extensions,
gsl::czstring name);
} // namespace veng

View File

@@ -0,0 +1,20 @@
#pragma once
#include "asset/object/model.h"
#include "vulkan/vertex.h"
namespace veng {
class Physics {
public:
void invokeOnColisionEvent(gsl::span<Model*> models);
bool RayTrace(const glm::vec3& rayOrigin, const glm::vec3& rayDir,
const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2,
std::float_t& outDistance);
private:
bool IsPointInsideMesh_(const glm::vec3& point,
const std::vector<veng::Vertex>& vertices,
const std::vector<std::uint32_t>& indices);
};
} // namespace veng

View File

@@ -0,0 +1,14 @@
#pragma once
#include <vulkan/vulkan.h>
namespace veng {
struct TextureHandle {
VkImage image = VK_NULL_HANDLE;
VkImageView image_view = VK_NULL_HANDLE;
VkDeviceMemory memory = VK_NULL_HANDLE;
VkDescriptorSet set = VK_NULL_HANDLE;
};
} // namespace veng

View File

@@ -0,0 +1,8 @@
#pragma once
namespace veng {
struct UniformTransformations {
glm::mat4 view;
glm::mat4 projection;
};
} // namespace veng

View File

@@ -0,0 +1,38 @@
#pragma once
#include <vulkan/vulkan.h>
namespace veng {
struct Vertex {
Vertex() : position(glm::vec3(0.f)), uv(glm::vec2(0.f)) {}
Vertex(glm::vec3 _position, glm::vec2 _uv) : position(_position), uv(_uv) {}
glm::vec3 position;
glm::vec2 uv;
static VkVertexInputBindingDescription GetBindingDescription() {
VkVertexInputBindingDescription description = {};
description.binding = 0;
description.stride = sizeof(Vertex);
description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
return description;
}
static std::array<VkVertexInputAttributeDescription, 2> GetAttributeDescriptions() {
std::array<VkVertexInputAttributeDescription, 2> descriptions = {};
descriptions[0].binding = 0;
descriptions[0].location = 0;
descriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
descriptions[0].offset = offsetof(Vertex, position);
descriptions[1].binding = 0;
descriptions[1].location = 1;
descriptions[1].format = VK_FORMAT_R32G32_SFLOAT;
descriptions[1].offset = offsetof(Vertex, uv);
return descriptions;
}
};
} // namespace veng