#include "engine.hpp" #include #include #include #include #include #include "GLFW/glfw3.h" #include "glm/ext/matrix_clip_space.hpp" #include "glm/ext/matrix_transform.hpp" #include 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>(); } [[nodiscard]] std::pair bounds_of(const trb::game::scene &scene) { auto min = glm::vec3(std::numeric_limits::infinity()); auto max = glm::vec3(-std::numeric_limits::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.1f; 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(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 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(entity, components::render_id(render_id)); return entity; }; return scene.meshes | std::views::transform(load_mesh) | std::ranges::to>(); } 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(eid); request = request.add_mesh(static_cast(render_id)).commit(); } return request; } render::request engine::add_playing_bounds_to_render_request(render::request request) const { auto lines = std::vector(); 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); } engine::mesh_bounds engine::compute_size_of_mesh(const mesh_node &mesh) { auto bounds = mesh_bounds(); const auto project = [&mesh](const glm::vec3 &v) { return glm::vec3(mesh.transform * glm::vec4(v, 1.0f)); }; auto min = glm::vec3(std::numeric_limits::infinity()); auto max = glm::vec3(-std::numeric_limits::infinity()); for (const auto &v : mesh.vertices) { min = glm::min(project(v.position), min); max = glm::max(project(v.position), max); } return { .min = min, .max = max, .size = max - min, }; } 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"); }(); const auto [min, max, size] = compute_size_of_mesh(game_plane_mesh); area_origin = min + (max - min) * 0.5f; area_size = (max - min) * 0.5f; area_size *= 0.85f; } void engine::calculate_box_size() { const auto &full_box = [&]() { for (const auto &mesh : models.boxes.meshes) { if (mesh.name == "wall.full") { return mesh; } } throw std::runtime_error("couldn't find game mesh plane"); }(); const auto &half_box = [&]() { for (const auto &mesh : models.boxes.meshes) { if (mesh.name == "wall.half") { return mesh; } } throw std::runtime_error("couldn't find game mesh plane"); }(); const auto [full_max, full_min, full_size] = compute_size_of_mesh(full_box); const auto [half_max, half_min, half_size] = compute_size_of_mesh(half_box); full_box_size = full_size; half_box_size = half_size; } render::request engine::add_level_to_render_request(render::request request, level *level) const { for (const auto [eid, position, wall] : level->get_entities() ->view() .each()) { const auto coord_transform = transform_given_coordinate(position); auto current_height = (wall.heights[0] == components::wall_height::full ? full_box_size.y : half_box_size.y) * 0.5f; current_height = 0.0f; for (int i = 0; i < wall.wall_count; i++) { const auto wall_entity_id = wall.type == components::wall_type::cork ? (wall.heights[i] == components::wall_height::full ? box_entities.cork_full : box_entities.cork_half) : (wall.heights[i] == components::wall_height::full ? box_entities.solid_full : box_entities.solid_half); const auto wall_tile_render_id = entities.get(wall_entity_id); const float half_extent = (wall.heights[i] == components::wall_height::full ? full_box_size.y : half_box_size.y) * 0.5f; const auto height = glm::translate( glm::mat4(1.0f), glm::vec3(0.0f, current_height + half_extent, 0.0f)); request = request.add_mesh(static_cast(wall_tile_render_id)) .with_transform(coord_transform * height) .commit(); current_height += (wall.heights[i] == components::wall_height::full ? full_box_size.y : half_box_size.y); } } 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(coordinate.x) * cell_width) + (cell_width * 0.5f); float world_z = start_point.z + (static_cast(coordinate.y) * cell_depth) + (cell_depth * 0.5f); return glm::translate(glm::mat4(1.0f), glm::vec3(world_x, 0.0f, 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(); calculate_box_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); //, true, 3.0f); //, // true, glfwGetTime()); 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