일단 float * float 위에서는 움직이는데 큰 좌표계 구성 안하면 멀티플레이 절대 불가능

This commit is contained in:
2025-05-21 01:52:41 +09:00
parent 0ea46b288d
commit 96c9f70828
16 changed files with 379 additions and 106 deletions

View File

@@ -1,16 +1,13 @@
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include "asset/object/model.h"
#include "assimp/Importer.hpp"
#include "assimp/postprocess.h"
#include "assimp/scene.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,83 +16,63 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
veng::Window window("Vulkan Engine", {800, 600});
window.TryMoveToMonitor(0);
std::shared_ptr<veng::Graphics> graphics =
std::make_shared<veng::Graphics>(&window);
veng::Graphics graphics(&window);
Assimp::Importer assetImporter;
veng::Coord cord;
const struct aiScene* scene = assetImporter.ReadFile(
"assets/wall.fbx", aiProcess_CalcTangentSpace | aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType | aiProcess_FlipUVs);
veng::Loader loader;
if (scene == nullptr || !scene->HasMeshes()) {
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;
veng::Model player(graphics);
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());
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};
player.vertices.emplace_back(
glm::vec3{mesh->mVertices[i].x, mesh->mVertices[i].y,
mesh->mVertices[i].z},
uv);
}
player.vertex_buffer = graphics->CreateVertexBuffer(player.vertices);
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);
for (std::uint32_t i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; ++j) {
player.indices.push_back(face.mIndices[j]);
}
}
player.index_buffer = graphics->CreateIndexBuffer(player.indices);
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;
aiString string;
if (scene->mMaterials[scene->mMeshes[0]->mMaterialIndex]->GetTexture(
aiTextureType_DIFFUSE, 0, &string) != AI_SUCCESS) {
std::cout << "No texture" << std::endl;
}
veng::Model wall(graphics);
wall.vertices = {veng::Vertex{{-1.f, -1.f, 0.f}, {0.f, 1.f}},
veng::Vertex{{1.f, -1.f, 0.f}, {1.f, 1.f}},
veng::Vertex{{-1.f, 1.f, 0.f}, {0.f, 0.f}},
veng::Vertex{{1.f, 1.f, 0.f}, {1.f, 0.f}}};
wall.vertex_buffer = graphics->CreateVertexBuffer(wall.vertices);
wall.indices = {0, 3, 2, 0, 1, 3};
wall.index_buffer = graphics->CreateIndexBuffer(wall.indices);
player.transform =
glm::rotate(glm::mat4(1.0f), glm::radians(0.f), glm::vec3(0.f, 1.f, 0.f));
// player.transform = glm::scale(player.transform,
// glm::vec3(50.f, 50.f, 50.f));
wall.transform = glm::rotate(glm::mat4(1.0f), glm::radians(180.f),
glm::vec3(0.f, 0.f, 1.f));
// plane.transform = glm::scale(plane.transform, glm::vec3(100.f, 100.f,
// 100.f));
veng::Model camera_lag(nullptr);
camera_lag.position = player.position;
camera_lag.position.z = -5;
glm::mat4 view =
glm::lookAt(glm::vec3(0.f, 0.f, 10.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);
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);
// player.material.texture =
// graphics->CreateTexture("assets/paving-stones.jpg");
auto texture = scene->GetEmbeddedTexture(string.C_Str());
player.material.texture = graphics->CreateTexture(
gsl::span{(std::uint8_t*)texture->pcData, texture->mWidth});
wall.material.texture = graphics->CreateTexture("assets/copying-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();
@@ -110,36 +87,114 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) {
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,
/*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(
-1 * (float)grater, (float)grater);*/
projection = glm::perspective(
glm::radians(90.f),
(float)current_window_size.x / (float)current_window_size.y, 0.1f,
100.f);*/
graphics->SetViewProjection(view, projection);
100.f);
graphics.SetViewProjection(view, projection);
}
// std::cout << glfwGetKey(window.GetHandle(), GLFW_KEY_W) << std::endl;
glm::vec3 forward = player.rotation * glm::vec3(0, 1, 0);
glm::vec3 right = player.rotation * glm::vec3(1, 0, 0);
if (graphics->BeginFrame()) {
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;
player.transform =
glm::rotate(player.transform, delta_time * glm::radians(60.f),
glm::vec3(0.f, 0.f, 1.f));
graphics->RenderModel(player);
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); // 임계 감쇠
wall.transform =
glm::rotate(wall.transform, delta_time * glm::radians(-60.f),
glm::vec3(0.f, 1.f, 0.f));
graphics->RenderModel(wall);
// 감쇠 스프링 업데이트
glm::vec3 displacement = camera_lag.position - player.position;
graphics->EndFrame();
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);
background.UpdateTransform(delta_time);
camera_lag.UpdateTransform(delta_time);
graphics.RenderModel(player);
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();
}
}