320 lines
10 KiB
C++
320 lines
10 KiB
C++
#include "engine.hpp"
|
|
|
|
#include <exception>
|
|
#include <game/components.hpp>
|
|
#include <glm/trigonometric.hpp>
|
|
#include <limits>
|
|
#include <ranges>
|
|
|
|
#include "GLFW/glfw3.h"
|
|
#include "glm/ext/matrix_clip_space.hpp"
|
|
#include "glm/ext/matrix_transform.hpp"
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
namespace {
|
|
[[nodiscard]] auto load_scene_to_renderer(trb::render::renderer &renderer,
|
|
trb::game::scene &scene) {
|
|
const auto upload_mesh_node = [&](const auto &mesh) {
|
|
return renderer.register_mesh(
|
|
mesh,
|
|
scene.textures[scene.materials[mesh.material_index].albedo_texture]);
|
|
};
|
|
|
|
return scene.meshes | std::views::transform(upload_mesh_node) |
|
|
std::ranges::to<std::vector<std::uint32_t>>();
|
|
}
|
|
|
|
[[nodiscard]] std::pair<glm::vec3, glm::vec3>
|
|
bounds_of(const trb::game::scene &scene) {
|
|
auto min = glm::vec3(std::numeric_limits<float>::infinity());
|
|
auto max = glm::vec3(-std::numeric_limits<float>::infinity());
|
|
|
|
for (const auto &mesh : scene.meshes) {
|
|
const auto transform = [mesh](glm::vec3 p) {
|
|
return glm::vec3(mesh.transform * glm::vec4(p, 1.0f));
|
|
};
|
|
for (const auto [position, normal, uv] : mesh.vertices) {
|
|
min = glm::min(min, transform(position));
|
|
max = glm::max(max, transform(position));
|
|
}
|
|
}
|
|
|
|
return std::make_pair(min, max);
|
|
}
|
|
|
|
struct level_view {
|
|
glm::mat4 view;
|
|
glm::mat4 projection;
|
|
};
|
|
[[nodiscard]] level_view calculate_camera_matrices(glm::vec3 min, glm::vec3 max,
|
|
float aspectRatio,
|
|
float zoom = 1.0f,
|
|
bool debug = false,
|
|
float timeSeconds = 0.0f) {
|
|
glm::vec3 center = glm::vec3((min.x + max.x) * 0.5f, (min.y + max.y) * 0.5f,
|
|
(min.z + max.z) * 0.5f);
|
|
|
|
glm::mat4 view;
|
|
glm::mat4 projection;
|
|
|
|
if (debug) {
|
|
float bboxSize = glm::length(max - min);
|
|
float baseDistance = bboxSize * 1.5f * 0.3f;
|
|
|
|
float orbitSpeed = 0.2f;
|
|
float bobSpeed = 1.0f;
|
|
float bobAmount = bboxSize * 0.08f;
|
|
|
|
float angle = timeSeconds * orbitSpeed;
|
|
|
|
glm::vec3 offset =
|
|
glm::vec3(glm::cos(angle) * baseDistance * 1.05f,
|
|
glm::sin(timeSeconds * bobSpeed) * bobAmount +
|
|
baseDistance * 0.9f, // up/down bob + base height
|
|
glm::sin(angle) * baseDistance * 1.15f);
|
|
|
|
glm::vec3 cameraPos = center + offset;
|
|
|
|
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
|
|
|
|
view = glm::lookAt(cameraPos, center, up);
|
|
|
|
float fov = 45.0f;
|
|
float nearPlane = baseDistance * 0.1f;
|
|
float farPlane = baseDistance * 4.0f;
|
|
|
|
projection =
|
|
glm::perspective(glm::radians(fov), aspectRatio, nearPlane, farPlane);
|
|
} else {
|
|
glm::vec3 cameraPos = center + glm::vec3(0.0f, 100.0f, 100.0f);
|
|
view = glm::lookAt(cameraPos, center, glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
glm::vec3 corners[8] = {{min.x, min.y, min.z}, {max.x, min.y, min.z},
|
|
{min.x, max.y, min.z}, {max.x, max.y, min.z},
|
|
{min.x, min.y, max.z}, {max.x, min.y, max.z},
|
|
{min.x, max.y, max.z}, {max.x, max.y, max.z}};
|
|
|
|
float vMinX = FLT_MAX, vMaxX = -FLT_MAX;
|
|
float vMinY = FLT_MAX, vMaxY = -FLT_MAX;
|
|
|
|
for (const auto &corner : corners) {
|
|
glm::vec4 vSpace = view * glm::vec4(corner, 1.0f);
|
|
vMinX = glm::min(vMinX, vSpace.x);
|
|
vMaxX = glm::max(vMaxX, vSpace.x);
|
|
vMinY = glm::min(vMinY, vSpace.y);
|
|
vMaxY = glm::max(vMaxY, vSpace.y);
|
|
}
|
|
|
|
float width = (vMaxX - vMinX) / zoom;
|
|
float height = (vMaxY - vMinY) / zoom;
|
|
float midX = (vMinX + vMaxX) * 0.5f;
|
|
float midY = (vMinY + vMaxY) * 0.5f;
|
|
|
|
if (aspectRatio > (width / height)) {
|
|
width = height * aspectRatio;
|
|
} else {
|
|
height = width / aspectRatio;
|
|
}
|
|
|
|
projection = glm::ortho(midX - width * 0.5f, midX + width * 0.5f,
|
|
midY - height * 0.5f, midY + height * 0.5f,
|
|
-1000.0f, 1000.0f);
|
|
}
|
|
|
|
return {view, projection};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
namespace trb::game {
|
|
|
|
engine::game_models engine::load_models(std::filesystem::path model_path) {
|
|
return {
|
|
.boundary = load_glb(model_path / "level_scene.glb"),
|
|
.boxes = load_glb(model_path / "boxes.glb"),
|
|
};
|
|
}
|
|
|
|
engine::map_boxes engine::load_boxes() {
|
|
auto boxes = map_boxes();
|
|
|
|
const auto upload_mesh_to_ecs =
|
|
[this](const game::mesh_node &mesh) -> std::uint32_t {
|
|
const auto entity = entities.create();
|
|
|
|
const auto render_id = renderer.register_mesh(
|
|
mesh, models.boxes.textures[models.boxes.materials[mesh.material_index]
|
|
.albedo_texture]);
|
|
entities.emplace<components::render_id>(entity,
|
|
components::render_id(render_id));
|
|
|
|
return entity;
|
|
};
|
|
|
|
for (const auto &mesh : models.boxes.meshes) {
|
|
if (mesh.name == "wall.full") {
|
|
boxes.solid_full = upload_mesh_to_ecs(mesh);
|
|
} else if (mesh.name == "wall.half") {
|
|
boxes.solid_half = upload_mesh_to_ecs(mesh);
|
|
} else if (mesh.name == "explodable.full") {
|
|
boxes.cork_full = upload_mesh_to_ecs(mesh);
|
|
} else if (mesh.name == "explodable.half") {
|
|
boxes.cork_half = upload_mesh_to_ecs(mesh);
|
|
} else {
|
|
throw std::runtime_error("unexpected mesh found in box model");
|
|
}
|
|
}
|
|
|
|
return boxes;
|
|
}
|
|
|
|
std::vector<std::uint32_t> engine::load_scene_to_ecs(game::scene &scene) {
|
|
const auto load_mesh =
|
|
[this, &scene](const game::mesh_node &mesh) -> std::uint32_t {
|
|
const auto entity = entities.create();
|
|
|
|
const auto render_id = renderer.register_mesh(
|
|
mesh,
|
|
scene.textures[scene.materials[mesh.material_index].albedo_texture]);
|
|
entities.emplace<components::render_id>(entity,
|
|
components::render_id(render_id));
|
|
|
|
return entity;
|
|
};
|
|
|
|
return scene.meshes | std::views::transform(load_mesh) |
|
|
std::ranges::to<std::vector<std::uint32_t>>();
|
|
}
|
|
|
|
render::request
|
|
engine::add_level_bounds_to_render_request(render::request request) const {
|
|
for (const auto eid : level_border_entity_ids) {
|
|
const auto render_id = entities.get<components::render_id>(eid);
|
|
request = request.add_mesh(static_cast<std::uint32_t>(render_id)).commit();
|
|
}
|
|
|
|
return request;
|
|
}
|
|
|
|
render::request
|
|
engine::add_playing_bounds_to_render_request(render::request request) const {
|
|
auto lines = std::vector<glm::vec3>();
|
|
const float xmin = area_origin.x - area_size.x;
|
|
const float xmax = area_origin.x + area_size.x;
|
|
const float zmin = area_origin.z - area_size.z;
|
|
const float zmax = area_origin.z + area_size.z;
|
|
|
|
const auto p1 = glm::vec3(xmin, 0.0f, zmin);
|
|
const auto p2 = glm::vec3(xmax, 0.0f, zmin);
|
|
const auto p3 = glm::vec3(xmax, 0.0f, zmax);
|
|
const auto p4 = glm::vec3(xmin, 0.0f, zmax);
|
|
|
|
lines.push_back(p1);
|
|
lines.push_back(p2);
|
|
lines.push_back(p2);
|
|
lines.push_back(p3);
|
|
lines.push_back(p3);
|
|
lines.push_back(p4);
|
|
lines.push_back(p4);
|
|
lines.push_back(p1);
|
|
|
|
return request.add_lines(lines);
|
|
}
|
|
|
|
void engine::calculate_game_plane_size() {
|
|
const auto &game_plane_mesh = [&]() {
|
|
for (const auto &mesh : models.boundary.meshes) {
|
|
if (mesh.name == "Plane.001") {
|
|
return mesh;
|
|
}
|
|
}
|
|
|
|
throw std::runtime_error("couldn't find game mesh plane");
|
|
}();
|
|
|
|
auto min = glm::vec3(std::numeric_limits<float>::infinity());
|
|
auto max = glm::vec3(-std::numeric_limits<float>::infinity());
|
|
|
|
const auto transform = [&](glm::vec3 p) {
|
|
return glm::vec3(game_plane_mesh.transform * glm::vec4(p, 1.0f));
|
|
};
|
|
|
|
for (const auto &v : game_plane_mesh.vertices) {
|
|
min = glm::min(transform(v.position), min);
|
|
max = glm::max(transform(v.position), max);
|
|
}
|
|
|
|
area_origin = min + (max - min) * 0.5f;
|
|
area_size = (max - min) * 0.5f;
|
|
area_size *= 0.85f;
|
|
}
|
|
|
|
render::request engine::add_level_to_render_request(render::request request,
|
|
level *level) const {
|
|
const auto wall_tile_render_id =
|
|
entities.get<components::render_id>(box_entities.solid_full);
|
|
|
|
for (const auto [eid, position] :
|
|
level->get_entities()->view<components::tile_position>()->each()) {
|
|
request = request.add_mesh(static_cast<std::uint32_t>(wall_tile_render_id))
|
|
.with_transform(transform_given_coordinate(position))
|
|
.commit();
|
|
}
|
|
|
|
return request;
|
|
}
|
|
|
|
glm::mat4 engine::transform_given_coordinate(glm::uvec2 coordinate) const {
|
|
const glm::vec3 full_dimensions = area_size * 2.0f;
|
|
|
|
const float cell_width = full_dimensions.x / 22.0f;
|
|
const float cell_depth = full_dimensions.z / 16.0f;
|
|
|
|
const auto start_point = area_origin - area_size;
|
|
|
|
float world_x = start_point.x +
|
|
(static_cast<float>(coordinate.x) * cell_width) +
|
|
(cell_width * 0.5f);
|
|
float world_z = start_point.z +
|
|
(static_cast<float>(coordinate.y) * cell_depth) +
|
|
(cell_depth * 0.5f);
|
|
|
|
return glm::translate(glm::mat4(1.0f), glm::vec3(world_x, 0.1f, world_z));
|
|
}
|
|
|
|
engine::engine()
|
|
: display(&input, {1920, 1080}, "Tanks Reborn"), renderer(&display),
|
|
models(load_models("../assets/models")),
|
|
level_border_entity_ids(load_scene_to_ecs(models.boundary)),
|
|
box_entities(load_boxes()) {
|
|
calculate_game_plane_size();
|
|
}
|
|
|
|
void engine::run() {
|
|
while (!display.should_close()) {
|
|
display.poll_events();
|
|
|
|
try {
|
|
const auto [map_min, map_max] = ::bounds_of(models.boundary);
|
|
const auto [view, projection] = ::calculate_camera_matrices(
|
|
map_min, map_max, 1920.0f / 1080.0f, 3.0f);
|
|
|
|
auto request = render::request();
|
|
|
|
request = add_level_bounds_to_render_request(request);
|
|
if (current_level.has_value()) {
|
|
request = add_level_to_render_request(request, &(*current_level));
|
|
}
|
|
request = add_playing_bounds_to_render_request(request);
|
|
|
|
renderer.render(request, view, projection);
|
|
} catch (const vuk::VkException &ex) {
|
|
if (ex.what() != std::string("Out of date.")) {
|
|
std::rethrow_exception(std::current_exception());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace trb::game
|