Skip to content

Commit

Permalink
path: Show cost as tile colours on grid.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Feb 15, 2024
1 parent 5e8aa7f commit a332d80
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
6 changes: 5 additions & 1 deletion assets/test/shaders/pathfinding/demo_0_cost_field.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ out vec4 out_col;

void main()
{
float cost = v_cost * 2.0;
if (v_cost == 255.0) {
out_col = vec4(0.0, 0.0, 0.0, 1.0);
return;
}
float cost = (v_cost / 256) * 2.0;
float red = clamp(cost, 0.0, 1.0);
float green = clamp(2.0 - cost, 0.0, 1.0);
out_col = vec4(red, green, 0.0, 1.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ out float v_cost;

void main() {
gl_Position = proj * view * model * vec4(position, 1.0);
v_cost = cost / 256.0;
v_cost = cost;
}
97 changes: 55 additions & 42 deletions libopenage/pathfinding/demo/demo_0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,61 +21,70 @@ namespace openage::path::tests {
renderer::resources::MeshData get_cost_field_mesh(const std::shared_ptr<CostField> &field) {
// increase by 1 in every dimension because to get the vertex length
// of each dimension
util::Vector2s size{field->get_size() + 1, field->get_size() + 1};
util::Vector2s size{field->get_size(), field->get_size()};

// add vertices for the cells of the grid
std::vector<float> verts{};
auto vert_count = size[0] * size[1];
verts.reserve(vert_count * 4);
std::vector<float> vert_coords{};
auto tile_count = size[0] * size[1];
vert_coords.reserve(tile_count * 6 * 4);
for (int i = 0; i < (int)size[0]; ++i) {
for (int j = 0; j < (int)size[1]; ++j) {
coord::scene3 v{
static_cast<float>(i),
static_cast<float>(j),
0,
};
auto world_v = v.to_world_space();
verts.push_back(world_v[0]);
verts.push_back(world_v[1]);
verts.push_back(world_v[2]);
verts.push_back(1.0); // TODO: push back actual cost
}
}

// split the grid into triangles using an index array
std::vector<uint16_t> idxs;
idxs.reserve((size[0] - 1) * (size[1] - 1) * 6);
// iterate over all tiles in the grid by columns, i.e. starting
// from the left corner to the bottom corner if you imagine it from
// the camera's point of view
for (size_t i = 0; i < size[0] - 1; ++i) {
for (size_t j = 0; j < size[1] - 1; ++j) {
// since we are working on tiles, we split each tile into two triangles
// with counter-clockwise vertex order
idxs.push_back(j + i * size[1]); // bottom left
idxs.push_back(j + 1 + i * size[1]); // bottom right
idxs.push_back(j + size[1] + i * size[1]); // top left
idxs.push_back(j + 1 + i * size[1]); // bottom right
idxs.push_back(j + size[1] + 1 + i * size[1]); // top right
idxs.push_back(j + size[1] + i * size[1]); // top left
auto cost = field->get_cost(i, j);

coord::scene3 vert_pos{static_cast<float>(i), static_cast<float>(j), 0};
auto world_v = vert_pos.to_world_space();
vert_coords.push_back(world_v[0]);
vert_coords.push_back(world_v[1]);
vert_coords.push_back(world_v[2]);
vert_coords.push_back(cost);

vert_pos = coord::scene3{static_cast<float>(i), static_cast<float>(j + 1), 0};
world_v = vert_pos.to_world_space();
vert_coords.push_back(world_v[0]);
vert_coords.push_back(world_v[1]);
vert_coords.push_back(world_v[2]);
vert_coords.push_back(cost);

vert_pos = coord::scene3{static_cast<float>(i + 1), static_cast<float>(j), 0};
world_v = vert_pos.to_world_space();
vert_coords.push_back(world_v[0]);
vert_coords.push_back(world_v[1]);
vert_coords.push_back(world_v[2]);
vert_coords.push_back(cost);

vert_pos = coord::scene3{static_cast<float>(i + 1), static_cast<float>(j), 0};
world_v = vert_pos.to_world_space();
vert_coords.push_back(world_v[0]);
vert_coords.push_back(world_v[1]);
vert_coords.push_back(world_v[2]);
vert_coords.push_back(cost);

vert_pos = coord::scene3{static_cast<float>(i), static_cast<float>(j + 1), 0};
world_v = vert_pos.to_world_space();
vert_coords.push_back(world_v[0]);
vert_coords.push_back(world_v[1]);
vert_coords.push_back(world_v[2]);
vert_coords.push_back(cost);

vert_pos = coord::scene3{static_cast<float>(i + 1), static_cast<float>(j + 1), 0};
world_v = vert_pos.to_world_space();
vert_coords.push_back(world_v[0]);
vert_coords.push_back(world_v[1]);
vert_coords.push_back(world_v[2]);
vert_coords.push_back(cost);
}
}

renderer::resources::VertexInputInfo info{
{renderer::resources::vertex_input_t::V3F32, renderer::resources::vertex_input_t::F32},
renderer::resources::vertex_layout_t::AOS,
renderer::resources::vertex_primitive_t::TRIANGLES,
renderer::resources::index_t::U16};
renderer::resources::vertex_primitive_t::TRIANGLES};

auto const vert_data_size = verts.size() * sizeof(float);
auto const vert_data_size = vert_coords.size() * sizeof(float);
std::vector<uint8_t> vert_data(vert_data_size);
std::memcpy(vert_data.data(), verts.data(), vert_data_size);

auto const idx_data_size = idxs.size() * sizeof(uint16_t);
std::vector<uint8_t> idx_data(idx_data_size);
std::memcpy(idx_data.data(), idxs.data(), idx_data_size);
std::memcpy(vert_data.data(), vert_coords.data(), vert_data_size);

return {std::move(vert_data), std::move(idx_data), info};
return {std::move(vert_data), info};
}

renderer::resources::MeshData get_grid_mesh(size_t side_length) {
Expand Down Expand Up @@ -320,6 +329,10 @@ void path_demo_0(const util::Path &path) {

// Create the pathfinding fields
auto cost_field = std::make_shared<CostField>(10);
cost_field->set_cost(0, 0, 255);
cost_field->set_cost(1, 0, 254);
cost_field->set_cost(4, 3, 128);

auto integration_field = std::make_shared<IntegrationField>(10);
auto flow_field = std::make_shared<FlowField>(10);

Expand Down

0 comments on commit a332d80

Please sign in to comment.