summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-11-22 03:56:58 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-11-22 03:56:58 +0100
commit9e749583791c9523efab90665ffd8f5c30e4f449 (patch)
treebcb7fd1774e0b1ca4f69ff19c9c2f9e3c63eac1a
parent34c451c6b64ba1c11efa9a390c16e2097a25279a (diff)
a wip
-rw-r--r--editor/editor.cpp1
-rw-r--r--shaders/lightmap.cpp8
-rw-r--r--shaders/lightmap.hpp1
-rw-r--r--src/chunk-render.cpp9
-rw-r--r--src/chunk-scenery.cpp6
-rw-r--r--src/chunk.cpp3
-rw-r--r--src/quads.cpp60
-rw-r--r--src/quads.hpp17
-rw-r--r--src/tile-atlas.cpp17
-rw-r--r--src/tile-atlas.hpp46
-rw-r--r--wall-tileset-tool/main.cpp26
11 files changed, 119 insertions, 75 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp
index a1561081..fd1921e5 100644
--- a/editor/editor.cpp
+++ b/editor/editor.cpp
@@ -1,6 +1,5 @@
#include "editor.hpp"
#include "loader/loader.hpp"
-#include "src/tile-atlas.hpp"
#include "src/world.hpp"
#include "keys.hpp"
diff --git a/shaders/lightmap.cpp b/shaders/lightmap.cpp
index 20d5616e..d782a376 100644
--- a/shaders/lightmap.cpp
+++ b/shaders/lightmap.cpp
@@ -5,6 +5,7 @@
#include "src/chunk.hpp"
#include "src/tile-bbox.hpp"
#include "src/tile-atlas.hpp"
+#include "src/quads.hpp"
#include "src/object.hpp"
#include "loader/loader.hpp"
#include <utility>
@@ -20,6 +21,8 @@
namespace floormat {
+using namespace floormat::Quads;
+
namespace {
constexpr auto neighbor_count = 4;
@@ -212,11 +215,6 @@ lightmap_shader::lightmap_shader(texture_unit_cache& tuc) : tuc{tuc}
block_uniform_buf.bind(GL::Buffer::Target::Uniform, BlockUniform);
}
-std::array<UnsignedShort, 6> lightmap_shader::quad_indexes(size_t N)
-{
- return tile_atlas::indices(N);
-}
-
void lightmap_shader::add_light(Vector2 neighbor_offset, const light_s& light)
{
// NOTE, make a benchmark where the vertex buffer isn't updated every frame
diff --git a/shaders/lightmap.hpp b/shaders/lightmap.hpp
index ff2dd9b4..d4e09815 100644
--- a/shaders/lightmap.hpp
+++ b/shaders/lightmap.hpp
@@ -72,7 +72,6 @@ private:
static Framebuffer make_framebuffer(Vector2i size);
GL::Mesh make_occlusion_mesh();
- static std::array<UnsignedShort, 6> quad_indexes(size_t N);
void add_objects(Vector2 neighbor_offset, chunk& c);
void add_geometry(Vector2 neighbor_offset, chunk& c);
diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp
index b4129489..173a8f8b 100644
--- a/src/chunk-render.cpp
+++ b/src/chunk-render.cpp
@@ -1,5 +1,6 @@
#include "chunk.hpp"
#include "tile-atlas.hpp"
+#include "quads.hpp"
#include "shaders/shader.hpp"
#include "compat/defs.hpp"
#include <algorithm>
@@ -8,12 +9,14 @@
namespace floormat {
+using namespace floormat::Quads;
+
template<size_t N = 1>
static auto make_index_array(size_t max)
{
std::array<std::array<UnsignedShort, 6>, N*TILE_COUNT> array; // NOLINT(cppcoreguidelines-pro-type-member-init)
for (auto i = 0uz; i < max; i++)
- array[i] = tile_atlas::indices(i);
+ array[i] = quad_indexes(i);
return array;
}
@@ -56,7 +59,7 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple
const uint8_t i = _ground->ground_indexes[k];
const auto& atlas = _ground->_ground_atlases[i];
const local_coords pos{i};
- const auto quad = atlas->floor_quad(Vector3(pos) * TILE_SIZE, TILE_SIZE2);
+ const auto quad = floor_quad(Vector3(pos) * TILE_SIZE, TILE_SIZE2);
const auto texcoords = atlas->texcoords_for_id(_ground->_ground_variants[i]);
const float depth = tile_shader::depth_value(pos, tile_shader::ground_depth_offset + hack_offset);
auto& v = vertexes[k];
@@ -89,7 +92,7 @@ GL::Mesh chunk::make_wall_mesh(size_t count)
const auto& variant = _walls->_wall_variants[i];
const local_coords pos{i / 2u};
const auto center = Vector3(pos) * TILE_SIZE;
- const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE);
+ const auto quad = i & 1 ? wall_quad_W(center, TILE_SIZE) : wall_quad_N(center, TILE_SIZE);
const float depth = tile_shader::depth_value(pos, tile_shader::wall_depth_offset);
const auto texcoords = atlas->texcoords_for_id(variant);
auto& v = vertexes[k];
diff --git a/src/chunk-scenery.cpp b/src/chunk-scenery.cpp
index 946f835a..d167168e 100644
--- a/src/chunk-scenery.cpp
+++ b/src/chunk-scenery.cpp
@@ -2,13 +2,15 @@
#include "shaders/shader.hpp"
#include "object.hpp"
#include "anim-atlas.hpp"
-#include "tile-atlas.hpp"
+#include "quads.hpp"
#include <bit>
#include <Corrade/Containers/ArrayViewStl.h>
#include <Magnum/GL/Buffer.h>
namespace floormat {
+using namespace floormat::Quads;
+
auto chunk::ensure_scenery_mesh() noexcept -> scenery_mesh_tuple
{
Array<object_draw_order> array;
@@ -164,7 +166,7 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce
for (auto j = 0uz; j < 4; j++)
scenery_vertexes[i][j] = { quad[j], texcoords[j], depth };
- scenery_indexes[i] = tile_atlas::indices(i);
+ scenery_indexes[i] = quad_indexes(i);
i++;
}
diff --git a/src/chunk.cpp b/src/chunk.cpp
index 52321aa1..0b7c05b0 100644
--- a/src/chunk.cpp
+++ b/src/chunk.cpp
@@ -1,6 +1,5 @@
#include "chunk.hpp"
-#include "src/tile-atlas.hpp"
-#include "anim-atlas.hpp"
+#include "object.hpp"
#include "tile-iterator.hpp"
#include <algorithm>
#include <Magnum/GL/Context.h>
diff --git a/src/quads.cpp b/src/quads.cpp
new file mode 100644
index 00000000..a02654b0
--- /dev/null
+++ b/src/quads.cpp
@@ -0,0 +1,60 @@
+#include "quads.hpp"
+
+namespace floormat::Quads {
+
+std::array<UnsignedShort, 6> quad_indexes(size_t N)
+{
+ using u16 = UnsignedShort;
+ return { /* 3--1 1 */
+ (u16)(0+N*4), (u16)(1+N*4), (u16)(2+N*4), /* | / /| */
+ (u16)(2+N*4), (u16)(1+N*4), (u16)(3+N*4), /* |/ / | */
+ }; /* 2 2--0 */
+}
+
+quad floor_quad(const Vector3 center, const Vector2 size)
+{
+ float x = size[0]*.5f, y = size[1]*.5f;
+ return {{
+ { x + center[0], -y + center[1], center[2]},
+ { x + center[0], y + center[1], center[2]},
+ {-x + center[0], -y + center[1], center[2]},
+ {-x + center[0], y + center[1], center[2]},
+ }};
+}
+
+quad wall_quad_W(const Vector3 center, const Vector3 size)
+{
+ float x = size[0]*.5f, y = size[1]*.5f, z = size[2];
+ return {{
+ {-x + center[0], y + center[1], z + center[2] },
+ {-x + center[0], y + center[1], center[2] },
+ {-x + center[0], -y + center[1], z + center[2] },
+ {-x + center[0], -y + center[1], center[2] },
+ }};
+}
+
+quad wall_quad_N(const Vector3 center, const Vector3 size)
+{
+ float x = size[0]*.5f, y = size[1]*.5f, z = size[2];
+ return {{
+ { x + center[0], -y + center[1], z + center[2] },
+ { x + center[0], -y + center[1], center[2] },
+ {-x + center[0], -y + center[1], z + center[2] },
+ {-x + center[0], -y + center[1], center[2] },
+ }};
+}
+
+texcoords texcoords_at(Vector2ui pos_, Vector2ui size_, Vector2ui image_size_)
+{
+ auto pos = Vector2(pos_), size = Vector2(size_), image_size = Vector2(image_size_);
+ auto offset = pos + Vector2(.5f), end = offset + size - Vector2(1);
+ auto x0 = offset / image_size, x1 = end / image_size;
+ return {{
+ { x1.x(), 1.f - x1.y() }, // bottom right
+ { x1.x(), 1.f - x0.y() }, // top right
+ { x0.x(), 1.f - x1.y() }, // bottom left
+ { x0.x(), 1.f - x0.y() }, // top left
+ }};
+}
+
+} // namespace floormat::Quads
diff --git a/src/quads.hpp b/src/quads.hpp
new file mode 100644
index 00000000..57492f35
--- /dev/null
+++ b/src/quads.hpp
@@ -0,0 +1,17 @@
+#pragma once
+#include <array>
+#include <Magnum/Math/Vector2.h>
+#include <Magnum/Math/Vector3.h>
+
+namespace floormat::Quads {
+
+using quad = std::array<Vector3, 4>;
+using texcoords = std::array<Vector2, 4>;
+
+quad floor_quad(Vector3 center, Vector2 size);
+quad wall_quad_N(Vector3 center, Vector3 size);
+quad wall_quad_W(Vector3 center, Vector3 size);
+std::array<UnsignedShort, 6> quad_indexes(size_t N);
+texcoords texcoords_at(Vector2ui pos, Vector2ui size, Vector2ui image_size);
+
+} // namespace floormat::Quads
diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp
index b16de6e8..1596247f 100644
--- a/src/tile-atlas.cpp
+++ b/src/tile-atlas.cpp
@@ -1,5 +1,5 @@
-
#include "tile-atlas.hpp"
+#include "quads.hpp"
#include "compat/assert.hpp"
#include "tile-image.hpp"
#include "compat/exception.hpp"
@@ -10,6 +10,8 @@
namespace floormat {
+using namespace floormat::Quads;
+
tile_atlas::tile_atlas(StringView path, StringView name, const ImageView2D& image, Vector2ub tile_count, Optional<enum pass_mode> p) :
texcoords_{make_texcoords_array(Vector2ui(image.size()), tile_count)},
path_{path}, name_{name}, size_{image.size()}, dims_{tile_count}, passability{std::move(p)}
@@ -34,19 +36,6 @@ std::array<Vector2, 4> tile_atlas::texcoords_for_id(size_t i) const
return texcoords_[i];
}
-auto tile_atlas::texcoords_at(Vector2ui pos_, Vector2ui size_, Vector2ui image_size_) -> texcoords
-{
- auto pos = Vector2(pos_), size = Vector2(size_), image_size = Vector2(image_size_);
- auto offset = pos + Vector2(.5f), end = offset + size - Vector2(1);
- auto x0 = offset / image_size, x1 = end / image_size;
- return {{
- { x1.x(), 1.f - x1.y() }, // bottom right
- { x1.x(), 1.f - x0.y() }, // top right
- { x0.x(), 1.f - x1.y() }, // bottom left
- { x0.x(), 1.f - x0.y() }, // top left
- }};
-}
-
auto tile_atlas::make_texcoords(Vector2ui pixel_size, Vector2ub tile_count, size_t i) -> texcoords
{
const auto sz = pixel_size/Vector2ui{tile_count};
diff --git a/src/tile-atlas.hpp b/src/tile-atlas.hpp
index 26d3fc17..205da68f 100644
--- a/src/tile-atlas.hpp
+++ b/src/tile-atlas.hpp
@@ -17,12 +17,8 @@ struct tile_atlas final
tile_atlas(StringView path, StringView name, const ImageView2D& img, Vector2ub tile_count, Optional<enum pass_mode> pass_mode);
- static texcoords texcoords_at(Vector2ui pos, Vector2ui size, Vector2ui image_size);
texcoords texcoords_for_id(size_t id) const;
- static constexpr quad floor_quad(Vector3 center, Vector2 size);
- static constexpr quad wall_quad_N(Vector3 center, Vector3 size);
- static constexpr quad wall_quad_W(Vector3 center, Vector3 size);
- static constexpr std::array<UnsignedShort, 6> indices(size_t N);
+
[[maybe_unused]] Vector2ui pixel_size() const { return size_; }
size_t num_tiles() const;
Vector2ub num_tiles2() const { return dims_; }
@@ -44,46 +40,6 @@ private:
Optional<enum pass_mode> passability;
};
-constexpr std::array<UnsignedShort, 6> tile_atlas::indices(size_t N)
-{
- using u16 = UnsignedShort;
- return { /* 3--1 1 */
- (u16)(0+N*4), (u16)(1+N*4), (u16)(2+N*4), /* | / /| */
- (u16)(2+N*4), (u16)(1+N*4), (u16)(3+N*4), /* |/ / | */
- }; /* 2 2--0 */
-}
-
-constexpr tile_atlas::quad tile_atlas::floor_quad(const Vector3 center, const Vector2 size)
-{
- float x = size[0]*.5f, y = size[1]*.5f;
- return {{
- { x + center[0], -y + center[1], center[2]},
- { x + center[0], y + center[1], center[2]},
- {-x + center[0], -y + center[1], center[2]},
- {-x + center[0], y + center[1], center[2]},
- }};
-}
-constexpr tile_atlas::quad tile_atlas::wall_quad_W(const Vector3 center, const Vector3 size)
-{
- float x = size[0]*.5f, y = size[1]*.5f, z = size[2];
- return {{
- {-x + center[0], y + center[1], z + center[2] },
- {-x + center[0], y + center[1], center[2] },
- {-x + center[0], -y + center[1], z + center[2] },
- {-x + center[0], -y + center[1], center[2] },
- }};
-}
-
-constexpr tile_atlas::quad tile_atlas::wall_quad_N(const Vector3 center, const Vector3 size)
-{
- float x = size[0]*.5f, y = size[1]*.5f, z = size[2];
- return {{
- { x + center[0], -y + center[1], z + center[2] },
- { x + center[0], -y + center[1], center[2] },
- {-x + center[0], -y + center[1], z + center[2] },
- {-x + center[0], -y + center[1], center[2] },
- }};
-}
} // namespace floormat
diff --git a/wall-tileset-tool/main.cpp b/wall-tileset-tool/main.cpp
index 2e78742d..29a7b3d4 100644
--- a/wall-tileset-tool/main.cpp
+++ b/wall-tileset-tool/main.cpp
@@ -2,6 +2,8 @@
#include "compat/sysexits.hpp"
#include "compat/fix-argv0.hpp"
#include "src/wall-atlas.hpp"
+#include "serialize/wall-atlas.hpp"
+#include "serialize/json-helper.hpp"
#include "loader/loader.hpp"
#include <utility>
#include <tuple>
@@ -28,6 +30,18 @@ struct options
String input_dir, input_file, output_dir;
};
+std::shared_ptr<wall_atlas> read_from_file(StringView filename)
+{
+ using namespace floormat::Wall::detail;
+
+ const auto jroot = json_helper::from_json_(filename);
+ auto header = read_info_header(jroot);
+ if (!loader.check_atlas_name(header.name))
+ fm_abort("bad atlas name '%s'!", header.name.data());
+
+ return {};
+}
+
Triple<options, Arguments, bool> parse_cmdline(int argc, const char* const* argv) noexcept
{
Corrade::Utility::Arguments args{};
@@ -35,7 +49,6 @@ Triple<options, Arguments, bool> parse_cmdline(int argc, const char* const* argv
args.addArgument("input.json"s);
args.parse(argc, argv);
options opts;
- //Path::exists(args.value<StringView>());
opts.output_dir = Path::join(loader.startup_directory(), args.value<StringView>("output"));
opts.input_file = Path::join(loader.startup_directory(), args.value<StringView>("input.json"));
@@ -44,7 +57,16 @@ Triple<options, Arguments, bool> parse_cmdline(int argc, const char* const* argv
if (opts.output_dir.isEmpty())
opts.output_dir = opts.input_dir;
- return { std::move(opts), std::move(args), false };
+ if (!Path::exists(opts.input_file))
+ Error{Error::Flag::NoSpace} << "fatal: input file '" << opts.input_file << "' doesn't exist";
+ else if (!Path::isDirectory(opts.output_dir))
+ Error{Error::Flag::NoSpace} << "fatal: output directory '" << opts.output_dir << "' doesn't exist";
+ else if (Path::isDirectory(opts.input_file))
+ Error{Error::Flag::NoSpace} << "fatal: input file '" << opts.input_file << "' is a directory";
+ else
+ return { std::move(opts), std::move(args), true };
+
+ return {};
}
[[nodiscard]] static int usage(const Arguments& args) noexcept