implement positioned rendering

This commit is contained in:
2026-04-06 01:59:18 -03:00
parent 41582dcfa6
commit b62582bc1d
4 changed files with 62 additions and 36 deletions

View File

@@ -196,6 +196,31 @@ engine::add_level_bounds_to_render_request(render::request request) const {
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) {
@@ -214,20 +239,14 @@ void engine::calculate_game_plane_size() {
return glm::vec3(game_plane_mesh.transform * glm::vec4(p, 1.0f));
};
area_height = 0.0f;
auto total_found = 0.0f;
for (const auto &v : game_plane_mesh.vertices) {
total_found += 1.0f;
area_height += v.position.y;
min = glm::min(transform(v.position), min);
max = glm::max(transform(v.position), max);
}
area_height /= total_found;
area_min = {min.x, min.z};
area_max = {max.x, max.z};
area_min *= 0.85f;
area_max *= 0.85f;
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,
@@ -238,14 +257,31 @@ render::request engine::add_level_to_render_request(render::request request,
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(glm::translate(glm::mat4(1.0f),
glm::vec3(0.0f, 0.0f, 0.0f)))
.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")),
@@ -261,7 +297,7 @@ void engine::run() {
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, 2.6f, true, glfwGetTime());
map_min, map_max, 1920.0f / 1080.0f, 3.0f);
auto request = render::request();
@@ -269,24 +305,7 @@ void engine::run() {
if (current_level.has_value()) {
request = add_level_to_render_request(request, &(*current_level));
}
auto lines = std::vector<glm::vec3>();
const auto offset = 0.0f;
auto p1 = glm::vec3(area_min.x, offset, area_min.y);
auto p2 = glm::vec3(area_max.x, offset, area_min.y);
auto p3 = glm::vec3(area_max.x, offset, area_max.y);
auto p4 = glm::vec3(area_min.x, offset, area_max.y);
lines.emplace_back(p1);
lines.emplace_back(p2);
lines.emplace_back(p2);
lines.emplace_back(p3);
lines.emplace_back(p3);
lines.emplace_back(p4);
lines.emplace_back(p4);
lines.emplace_back(p1);
request.add_lines(lines);
request = add_playing_bounds_to_render_request(request);
renderer.render(request, view, projection);
} catch (const vuk::VkException &ex) {