diff --git a/assets/test/shaders/pathfinding/demo_0_cost_field.frag.glsl b/assets/test/shaders/pathfinding/demo_0_cost_field.frag.glsl index 26fe7ef44e..7ccb86dcf4 100644 --- a/assets/test/shaders/pathfinding/demo_0_cost_field.frag.glsl +++ b/assets/test/shaders/pathfinding/demo_0_cost_field.frag.glsl @@ -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); diff --git a/assets/test/shaders/pathfinding/demo_0_cost_field.vert.glsl b/assets/test/shaders/pathfinding/demo_0_cost_field.vert.glsl index cf76408fe5..d12f0db1d9 100644 --- a/assets/test/shaders/pathfinding/demo_0_cost_field.vert.glsl +++ b/assets/test/shaders/pathfinding/demo_0_cost_field.vert.glsl @@ -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; } diff --git a/libopenage/pathfinding/demo/demo_0.cpp b/libopenage/pathfinding/demo/demo_0.cpp index f70ad0439e..3f68b48c2d 100644 --- a/libopenage/pathfinding/demo/demo_0.cpp +++ b/libopenage/pathfinding/demo/demo_0.cpp @@ -21,61 +21,70 @@ namespace openage::path::tests { renderer::resources::MeshData get_cost_field_mesh(const std::shared_ptr &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 verts{}; - auto vert_count = size[0] * size[1]; - verts.reserve(vert_count * 4); + std::vector 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(i), - static_cast(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 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(i), static_cast(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(i), static_cast(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(i + 1), static_cast(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(i + 1), static_cast(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(i), static_cast(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(i + 1), static_cast(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 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 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) { @@ -320,6 +329,10 @@ void path_demo_0(const util::Path &path) { // Create the pathfinding fields auto cost_field = std::make_shared(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(10); auto flow_field = std::make_shared(10);