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 c0037a9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 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;
}
55 changes: 47 additions & 8 deletions libopenage/pathfinding/demo/demo_0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,62 @@

namespace openage::path::tests {

renderer::resources::MeshData get_cost_field_mesh(const std::shared_ptr<CostField> &field) {
/**
* Create a mesh for the cost field.
*
* @param field Cost field to visualize.
* @param resolution Determines the number of subdivisions per grid cell. The number of
* quads per cell is resolution^2. (default = 2)
*
* @return Mesh data for the cost field.
*/
renderer::resources::MeshData get_cost_field_mesh(const std::shared_ptr<CostField> &field,
size_t resolution = 2) {
// 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() * resolution + 1,
field->get_size() * resolution + 1,
};
auto vert_distance = 1.0f / resolution;

// add vertices for the cells of the grid
std::vector<float> verts{};
auto vert_count = size[0] * size[1];
verts.reserve(vert_count * 4);
for (int i = 0; i < (int)size[0]; ++i) {
for (int j = 0; j < (int)size[1]; ++j) {
for (int i = 0; i < static_cast<int>(size[0]); ++i) {
for (int j = 0; j < static_cast<int>(size[1]); ++j) {
// for each vertex, compare the surrounding tiles
std::vector<float> surround{};
if (j - 1 >= 0 and i - 1 >= 0) {
auto cost = field->get_cost((i - 1) / resolution, (j - 1) / resolution);
surround.push_back(cost);
}
if (j < static_cast<int>(field->get_size()) and i - 1 >= 0) {
auto cost = field->get_cost((i - 1) / resolution, j / resolution);
surround.push_back(cost);
}
if (j < static_cast<int>(field->get_size()) and i < static_cast<int>(field->get_size())) {
auto cost = field->get_cost(i / resolution, j / resolution);
surround.push_back(cost);
}
if (j - 1 >= 0 and i < static_cast<int>(field->get_size())) {
auto cost = field->get_cost(i / resolution, (j - 1) / resolution);
surround.push_back(cost);
}
// use the cost of the most expensive surrounding tile
auto max_cost = *std::max_element(surround.begin(), surround.end());

coord::scene3 v{
static_cast<float>(i),
static_cast<float>(j),
static_cast<float>(i * vert_distance),
static_cast<float>(j * vert_distance),
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
verts.push_back(max_cost); // TODO: push back actual cost
}
}

Expand Down Expand Up @@ -320,6 +355,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 Expand Up @@ -351,7 +390,7 @@ void path_demo_0(const util::Path &path) {
camera->get_view_matrix(),
"proj",
camera->get_projection_matrix());
auto grid_mesh = get_grid_mesh(cost_field->get_size());
auto grid_mesh = get_grid_mesh(10);
auto grid_geometry = renderer->add_mesh_geometry(grid_mesh);
renderer::Renderable grid_renderable{
grid_unifs,
Expand Down

0 comments on commit c0037a9

Please sign in to comment.