Compare commits

...

4 Commits

19 changed files with 454 additions and 48 deletions

View File

@@ -73,5 +73,4 @@ file(GLOB_RECURSE ShaderSources CONFIGURE_DEPENDS
add_shaders(VulkanEngineShaders ${ShaderSources})
add_dependencies(VulkanEngine VulkanEngineShaders)
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/assets/paving-stones.jpg" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/assets")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/assets/player.fbx" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/assets")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/assets/" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/assets")

BIN
assets/background.fbx Normal file

Binary file not shown.

BIN
assets/bullet.fbx Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

BIN
assets/player_flame.fbx Normal file

Binary file not shown.

22
include/asset/loader.h Normal file
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_;
};
} // namespace veng

View File

@@ -0,0 +1,9 @@
#pragma once
#include "vulkan/texture_handle.h"
namespace veng {
struct Material {
TextureHandle texture;
};
} // namespace veng

View File

@@ -0,0 +1,42 @@
#pragma once
#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(class Graphics* graphics) : graphics_(graphics) {}
~Model();
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;
glm::mat4 UpdateTransform(float dt);
glm::vec3 original_offset = glm::vec3(0.f);
private:
class Graphics* graphics_;
};
} // namespace veng

View File

@@ -1,7 +1,6 @@
#pragma once
struct GLFWmonitor;
struct GLFWwindow;
#include <GLFW/glfw3.h>
namespace veng {
class Window {
@@ -14,6 +13,8 @@ class Window {
bool ShouldClose() const;
GLFWwindow* GetHandle() const;
GLFWkeyfun SetKeyCallback(GLFWkeyfun key_callback);
bool TryMoveToMonitor(std::uint16_t monitor_number);
private:

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

@@ -33,12 +33,15 @@ class Graphics final {
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);
private:

62
src/asset/loader.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include "asset/loader.h"
#include "stb/stb_image.h"
namespace veng {
void Loader::setPath(std::string path) {
scene_ = importer_.ReadFile(
path.c_str(), aiProcess_CalcTangentSpace | aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType | aiProcess_FlipUVs);
if (scene_ == nullptr || !scene_->HasMeshes())
throw std::runtime_error(importer_.GetErrorString());
}
void Loader::loadModel(inModel model) {
aiMesh* mesh = scene_->mMeshes[0];
for (std::uint32_t i = 0; i < mesh->mNumVertices; i++) {
glm::vec2 uv = {mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y};
model.vertices.emplace_back(
glm::vec3{mesh->mVertices[i].x, mesh->mVertices[i].y,
mesh->mVertices[i].z},
uv);
}
for (auto& const it : model.vertices) {
model.original_offset.x += it.position.x / model.vertices.size();
model.original_offset.y += it.position.y / model.vertices.size();
model.original_offset.z += it.position.z / model.vertices.size();
}
for (auto& vertex : model.vertices) {
vertex.position -= model.original_offset;
}
for (std::uint32_t i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; ++j) {
model.indices.push_back(face.mIndices[j]);
}
}
}
std::vector<std::uint8_t> Loader::readTexture() {
aiString texture_path;
if (scene_->mMaterials[scene_->mMeshes[0]->mMaterialIndex]->GetTexture(
aiTextureType_DIFFUSE, 0, &texture_path) != AI_SUCCESS) {
spdlog::warn("No texture");
}
if (scene_->mNumTextures > 0 || texture_path.C_Str()[0] == '*') {
const aiTexture* texture = scene_->GetEmbeddedTexture(texture_path.C_Str());
return std::vector<std::uint8_t>(
(std::uint8_t*)texture->pcData,
(std::uint8_t*)texture->pcData + texture->mWidth);
}
return ReadFile(texture_path.C_Str());
}
} // namespace veng

View File

@@ -0,0 +1,32 @@
#include "asset/object/model.h"
#include "vulkan/graphics.h"
namespace veng {
Model::~Model() {
if (graphics_ == nullptr) return;
graphics_->DestroyTexture(material.texture);
graphics_->DestroyBuffer(vertex_buffer);
graphics_->DestroyBuffer(index_buffer);
}
glm::mat4 Model::UpdateTransform(float dt) {
linear_velocity += linear_acceleration * dt;
position += linear_velocity * dt;
angular_velocity += angular_acceleration * dt;
if (glm::length(angular_velocity) > 1e-6f) {
rotation =
glm::normalize(glm::rotate(rotation, glm::length(angular_velocity * dt),
glm::normalize(angular_velocity)));
}
transform = glm::translate(glm::mat4(1.0f), position) *
glm::mat4_cast(rotation) * glm::scale(glm::mat4(1.0f), scale);
return transform;
}
} // namespace veng

View File

@@ -1,7 +1,8 @@
#include "glfw/glfw_window.h"
#include <GLFW/glfw3.h>
#include "glfw/glfw_monitor.h"
#include "glfw/glfw_window.h"
#include "precomp.h"
namespace veng {
@@ -32,6 +33,10 @@ bool Window::ShouldClose() const { return glfwWindowShouldClose(window_); }
GLFWwindow* Window::GetHandle() const { return window_; }
GLFWkeyfun Window::SetKeyCallback(GLFWkeyfun key_callback) {
return glfwSetKeyCallback(window_, key_callback);
}
bool Window::TryMoveToMonitor(std::uint16_t monitor_number) {
gsl::span<GLFWmonitor*> monitors = veng::GetMonitors();

View File

@@ -1,14 +1,13 @@
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include "assimp/Importer.hpp"
#include "assimp/postprocess.h"
#include "asset/loader.h"
#include "glfw/glfw_initialization.h"
#include "glfw/glfw_monitor.h"
#include "glfw/glfw_window.h"
#include "precomp.h"
#include "vulkan/coordinate.h"
#include "vulkan/graphics.h"
std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
@@ -19,50 +18,82 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
veng::Graphics graphics(&window);
Assimp::Importer assetImporter;
veng::Coord cord;
const struct aiScene* scene = assetImporter.ReadFile(
"assets/player.fbx", aiProcess_CalcTangentSpace | aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices | aiProcess_SortByPType);
veng::Loader loader;
if (nullptr == scene) {
spdlog::error(assetImporter.GetErrorString());
return false;
}
veng::Model player(&graphics);
loader.setPath("assets/player.fbx");
loader.loadModel(player);
player.vertex_buffer = graphics.CreateVertexBuffer(player.vertices);
player.index_buffer = graphics.CreateIndexBuffer(player.indices);
player.material.texture = graphics.CreateTexture(loader.readTexture());
player.scale = glm::vec3(.02f);
player.position.x = 100000.f;
player.position.y = 100000.f;
std::array<veng::Vertex, 4> vertices = {
veng::Vertex{{-.5f, -.5f, 0.f}, {0.f, 1.f}},
veng::Vertex{{.5f, -.5f, 0.f}, {1.f, 1.f}},
veng::Vertex{{-.5f, .5f, 0.f}, {0.f, 0.f}},
veng::Vertex{{.5f, .5f, 0.f}, {1.f, 0.f}}};
veng::BufferHandle buffer = graphics.CreateVertexBuffer(vertices);
veng::Model player_flame(&graphics);
loader.setPath("assets/player_flame.fbx");
loader.loadModel(player_flame);
player_flame.vertex_buffer =
graphics.CreateVertexBuffer(player_flame.vertices);
player_flame.index_buffer = graphics.CreateIndexBuffer(player_flame.indices);
player_flame.material.texture = graphics.CreateTexture(loader.readTexture());
std::array<std::uint32_t, 6> indices = {0, 3, 2, 0, 1, 3};
veng::BufferHandle index_buffer = graphics.CreateIndexBuffer(indices);
veng::Model bullet(&graphics);
loader.setPath("assets/bullet.fbx");
loader.loadModel(bullet);
bullet.vertex_buffer = graphics.CreateVertexBuffer(bullet.vertices);
bullet.index_buffer = graphics.CreateIndexBuffer(bullet.indices);
bullet.material.texture = graphics.CreateTexture(loader.readTexture());
bullet.scale = glm::vec3(.1f);
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), glm::radians(45.f),
glm::vec3(0.f, 1.f, 0.f));
veng::Model background(&graphics);
loader.setPath("assets/background.fbx");
loader.loadModel(background);
background.vertex_buffer = graphics.CreateVertexBuffer(background.vertices);
background.index_buffer = graphics.CreateIndexBuffer(background.indices);
background.material.texture = graphics.CreateTexture(loader.readTexture());
background.position = {background.position.x, background.position.y, 30.f};
background.scale *= 100;
glm::mat4 rotation2 =
glm::rotate(glm::mat4(1.0f), glm::radians(0.f), glm::vec3(0.f, 0.f, 1.f));
veng::Model camera_lag(nullptr);
camera_lag.position = player.position;
camera_lag.position.z = -5;
glm::mat4 view = glm::translate(glm::mat4(1.f), glm::vec3(0.f, 0.f, -2.f));
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(150.f), 800.f / 600.f, 0.1f, 100.f);
// glm::mat4 projection = glm::ortho(-10.f, 10.f, -10.f, 10.f, -10.f, 10.f);
graphics.SetViewProjection(view, projection);
veng::TextureHandle texture =
graphics.CreateTexture("assets/paving-stones.jpg");
/*auto raw_texture = loader.readTexture();
player.material.texture =
graphics.CreateTexture({raw_texture.data(), raw_texture.size()});
wall.material.texture = graphics.CreateTexture("assets/copying-stones.jpg");*/
double last_time = glfwGetTime();
veng::Model bullet2(&graphics);
glm::ivec2 window_size;
while (!window.ShouldClose()) {
glfwPollEvents();
glm::ivec2 current_window_size = window.GetFramebufferSize();
if (current_window_size != window_size && current_window_size.x != 0 && current_window_size.y != 0) {
if (current_window_size != window_size && current_window_size.x != 0 &&
current_window_size.y != 0) {
window_size = current_window_size;
auto grater = (current_window_size.x > current_window_size.y)
? current_window_size.x
: current_window_size.y;
/*projection = glm::ortho(0.01f * (float)current_window_size.x / 2,
-0.01f * (float)current_window_size.x / 2,
-0.01f * (float)current_window_size.y / 2,
0.01f * (float)current_window_size.y / 2,
-1 * (float)grater, (float)grater);*/
projection = glm::perspective(
glm::radians(90.f),
(float)current_window_size.x / (float)current_window_size.y, 0.1f,
@@ -70,28 +101,111 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
graphics.SetViewProjection(view, projection);
}
glm::vec3 forward = player.rotation * glm::vec3(0, 1, 0);
glm::vec3 right = player.rotation * glm::vec3(1, 0, 0);
if (glfwGetKey(window.GetHandle(), GLFW_KEY_A) == GLFW_PRESS) {
right = player.rotation * glm::vec3(0, 0, 1);
player.angular_velocity = right * 6.f;
} else if (glfwGetKey(window.GetHandle(), GLFW_KEY_D) == GLFW_PRESS) {
right = player.rotation * glm::vec3(0, 0, 1);
player.angular_velocity = right * -6.f;
} else {
right = player.rotation * glm::vec3(0, 0, 1);
player.angular_velocity = right * 0.f;
}
background.linear_velocity = player.linear_velocity * -1.f;
if (graphics.BeginFrame()) {
double current_time = glfwGetTime();
float delta_time = static_cast<float>(current_time - last_time);
last_time = current_time;
graphics.SetTexture(texture);
rotation2 = glm::rotate(rotation2, delta_time * glm::radians(-360.f),
glm::vec3(0.f, 0.f, 1.f));
graphics.SetModelMatrix(rotation2);
graphics.RenderIndexedBuffer(buffer, index_buffer, indices.size());
rotation = glm::rotate(rotation, delta_time * glm::radians(360.f),
glm::vec3(0.f, 0.f, 1.f));
graphics.SetModelMatrix(rotation);
graphics.RenderIndexedBuffer(buffer, index_buffer, indices.size());
float stiffness = 500.0f * ((glm::length(player.linear_velocity) > 1.f)
? glm::length(player.linear_velocity)
: 1.f); // 더 크면 빠르게 따라감
float damping = 10.f * glm::sqrt(stiffness); // 임계 감쇠
// 감쇠 스프링 업데이트
glm::vec3 displacement = camera_lag.position - player.position;
camera_lag.linear_velocity +=
(-stiffness * displacement - damping * camera_lag.linear_velocity) *
delta_time;
glm::mat4 view = glm::lookAt(
glm::vec3(camera_lag.position.x, camera_lag.position.y, -5.f),
camera_lag.position, glm::vec3(0.f, -1.f, 0.f));
graphics.SetViewProjection(view, projection);
player.UpdateTransform(delta_time);
player_flame.transform =
glm::translate(player.transform, player_flame.original_offset * 0.5f);
bullet.transform = player.transform;
background.UpdateTransform(delta_time);
camera_lag.UpdateTransform(delta_time);
graphics.RenderModel(player);
graphics.RenderModel(bullet);
bullet2 = bullet;
bullet2.transform =
glm::translate(player.transform, player_flame.original_offset * 0.5f);
graphics.RenderModel(bullet2);
if (glfwGetKey(window.GetHandle(), GLFW_KEY_W) == GLFW_PRESS) {
player.linear_acceleration = glm::normalize(forward) * 10.f;
graphics.RenderModel(player_flame);
} else
player.linear_acceleration = forward * .0f;
spdlog::info("player cord: x:{}, y:{}", player.position.x,
player.position.y);
if (player.position.x - background.position.x >= background.scale.x) {
background.position += glm::vec3(2.f, 0.f, 0.f) * background.scale;
}
if (player.position.x - background.position.x < -background.scale.x) {
background.position -= glm::vec3(2.f, 0.f, 0.f) * background.scale;
}
if (player.position.y - background.position.y >= background.scale.y) {
background.position += glm::vec3(0.f, 2.f, 0.f) * background.scale;
}
if (player.position.y - background.position.y < -background.scale.y) {
background.position -= glm::vec3(0.f, 2.f, 0.f) * background.scale;
}
glm::vec3 sparse;
sparse = glm::vec3(1.f, 1.f, 0.f);
background.position += sparse * background.scale;
background.UpdateTransform(delta_time);
graphics.RenderModel(background);
background.position -= sparse * background.scale;
sparse = glm::vec3(-1.f, 1.f, 0.f);
background.position += sparse * background.scale;
background.UpdateTransform(delta_time);
graphics.RenderModel(background);
background.position -= sparse * background.scale;
sparse = glm::vec3(1.f, -1.f, 0.f);
background.position += sparse * background.scale;
background.UpdateTransform(delta_time);
graphics.RenderModel(background);
background.position -= sparse * background.scale;
sparse = glm::vec3(-1.f, -1.f, 0.f);
background.position += sparse * background.scale;
background.UpdateTransform(delta_time);
graphics.RenderModel(background);
background.position -= sparse * background.scale;
graphics.EndFrame();
}
}
graphics.DestroyTexture(texture);
graphics.DestroyBuffer(buffer);
graphics.DestroyBuffer(index_buffer);
return EXIT_SUCCESS;
}

View File

@@ -1,5 +1,6 @@
#include <vulkan/vulkan.h>
#include "asset/object/model.h"
#include "precomp.h"
#include "vulkan/graphics.h"
#include "vulkan/uniform_transformations.h"
@@ -142,8 +143,8 @@ void Graphics::SetModelMatrix(glm::mat4 model) {
void Graphics::SetViewProjection(glm::mat4 view, glm::mat4 projection) {
UniformTransformations transformations{view, projection};
for (Frame& frame : frames_)
std::memcpy(frame.uniform_buffer_location,
&transformations, sizeof(UniformTransformations));
std::memcpy(frame.uniform_buffer_location, &transformations,
sizeof(UniformTransformations));
}
void Graphics::RenderBuffer(BufferHandle handle, std::uint32_t vertex_count) {
@@ -170,6 +171,12 @@ void Graphics::RenderIndexedBuffer(BufferHandle vertex_buffer,
vkCmdDrawIndexed(frames_[current_frame_].command_buffer, count, 1, 0, 0, 0);
SetModelMatrix(glm::mat4(1.f));
}
void Graphics::RenderModel(Model& model) {
SetTexture(model.material.texture);
SetModelMatrix(model.transform);
RenderIndexedBuffer(model.vertex_buffer, model.index_buffer,
model.indices.size());
}
VkCommandBuffer Graphics::BeginTransientCommandBuffer() {
VkCommandBufferAllocateInfo allocation_info = {};

62
src/vulkan/coordinate.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include "vulkan/coordinate.h"
namespace veng {
Coord Coord::operator+(const Coord& other) const {
Coord result;
result.seg.x = this->seg.x + other.seg.x;
result.seg.y = this->seg.y + other.seg.y;
result.seg.z = this->seg.z + other.seg.z;
if (this->pos.x + other.pos.x > border) {
result.seg.x += 1;
result.pos.x += glm::mod(this->pos.x, border);
result.pos.x += glm::mod(other.pos.x, border);
} else {
result.pos.x = this->pos.x + other.pos.x;
}
if (this->pos.y + other.pos.y > border) {
result.seg.y += 1;
result.pos.y += glm::mod(this->pos.y, border);
result.pos.y += glm::mod(other.pos.y, border);
} else {
result.pos.y = this->pos.y + other.pos.y;
}
if (this->pos.z + other.pos.z > border) {
result.seg.z += 1;
result.pos.z += glm::mod(this->pos.z, border);
result.pos.z += glm::mod(other.pos.z, border);
} else {
result.pos.z = this->pos.z + other.pos.z;
}
return result;
}
Coord Coord::operator-(const Coord& other) const {
Coord result;
result.seg.x = this->seg.x - other.seg.x;
result.seg.y = this->seg.y - other.seg.y;
result.seg.z = this->seg.z - other.seg.z;
if (this->pos.x - other.pos.x < -border) {
result.seg.x -= 1;
result.pos.x -= glm::mod(this->pos.x, border);
result.pos.x -= glm::mod(other.pos.x, border);
} else {
result.pos.x = this->pos.x - other.pos.x;
}
if (this->pos.y - other.pos.y < -border) {
result.seg.y -= 1;
result.pos.y -= glm::mod(this->pos.y, border);
result.pos.y -= glm::mod(other.pos.y, border);
} else {
result.pos.y = this->pos.y - other.pos.y;
}
if (this->pos.z - other.pos.z < -border) {
result.seg.z -= 1;
result.pos.z -= glm::mod(this->pos.z, border);
result.pos.z -= glm::mod(other.pos.z, border);
} else {
result.pos.z = this->pos.z - other.pos.z;
}
return result;
}
} // namespace veng

View File

@@ -31,9 +31,18 @@ void Graphics::CreateTextureSampler() {
}
TextureHandle Graphics::CreateTexture(gsl::czstring path) {
std::vector<std::uint8_t> data = ReadFile(path);
return CreateTexture({data.data(), data.size()});
}
TextureHandle Graphics::CreateTexture(
std::vector<std::uint8_t> image_file_data) {
return CreateTexture({image_file_data.data(), image_file_data.size()});
}
TextureHandle Graphics::CreateTexture(gsl::span<std::uint8_t> image_file_data) {
glm::ivec2 image_extents;
std::int32_t channels;
std::vector<std::uint8_t> image_file_data = ReadFile(path);
stbi_uc* pixel_data = stbi_load_from_memory(
image_file_data.data(), image_file_data.size(), &image_extents.x,
&image_extents.y, &channels, STBI_rgb_alpha);