summaryrefslogtreecommitdiffhomepage
path: root/draw
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-09 16:17:23 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-09 16:17:23 +0100
commit1febb02d958fa4cf8c15e3ca18f9d644f9fc80fb (patch)
treed9b389f6c2ff8e031bd70d503c289bcfddf2f048 /draw
parent09605450ecc99ad1f4d50e9c143fda6c7762e702 (diff)
depth buffer works!
Diffstat (limited to 'draw')
-rw-r--r--draw/anim.cpp10
-rw-r--r--draw/anim.hpp8
-rw-r--r--draw/wall.cpp19
-rw-r--r--draw/wall.hpp9
-rw-r--r--draw/wireframe.cpp9
-rw-r--r--draw/wireframe.hpp2
6 files changed, 40 insertions, 17 deletions
diff --git a/draw/anim.cpp b/draw/anim.cpp
index 1a02ad93..dac23566 100644
--- a/draw/anim.cpp
+++ b/draw/anim.cpp
@@ -9,8 +9,7 @@ namespace floormat {
anim_mesh::anim_mesh()
{
_mesh.setCount(6)
- .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{})
- .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{})
+ .addVertexBuffer(_vertex_buffer, 0, tile_shader::Position{}, tile_shader::TextureCoordinates{}, tile_shader::Depth{})
.setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort);
CORRADE_INTERNAL_ASSERT(_mesh.isIndexed());
}
@@ -27,9 +26,12 @@ void anim_mesh::draw(tile_shader& shader, const anim_atlas& atlas, rotation r, s
{
const auto center = Vector3(xy.x, xy.y, 0.f) * TILE_SIZE;
const auto pos = atlas.frame_quad(center, r, frame);
- _positions_buffer.setSubData(0, pos);
const auto texcoords = atlas.texcoords_for_frame(r, frame);
- _vertex_buffer.setSubData(0, texcoords);
+ const float depth = tile_shader::depth_value(xy);
+ quad_data array;
+ for (std::size_t i = 0; i < 4; i++)
+ array[i] = { pos[i], texcoords[i], depth };
+ _vertex_buffer.setSubData(0, array);
shader.draw(_mesh);
}
diff --git a/draw/anim.hpp b/draw/anim.hpp
index 04fd92c8..163c5b1a 100644
--- a/draw/anim.hpp
+++ b/draw/anim.hpp
@@ -24,14 +24,18 @@ struct anim_mesh final
void draw(tile_shader& shader, const anim_atlas& atlas, rotation r, std::size_t frame, local_coords xy);
private:
- struct vertex_data final { Vector2 texcoords; };
+ struct vertex_data final {
+ Vector3 position;
+ Vector2 texcoords;
+ float depth = -1;
+ };
using quad_data = std::array<vertex_data, 4>;
static std::array<UnsignedShort, 6> make_index_array();
GL::Mesh _mesh;
GL::Buffer _vertex_buffer{quad_data{}, Magnum::GL::BufferUsage::DynamicDraw},
- _index_buffer{make_index_array()}, _positions_buffer{std::array<Vector3, 4>{}};
+ _index_buffer{make_index_array()};
};
} // namespace floormat
diff --git a/draw/wall.cpp b/draw/wall.cpp
index 4c25b267..0096946e 100644
--- a/draw/wall.cpp
+++ b/draw/wall.cpp
@@ -15,7 +15,7 @@ wall_mesh::wall_mesh()
{
_mesh.setCount((int)(quad_index_count * COUNT))
.addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{})
- .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{})
+ .addVertexBuffer(_constant_buffer, 0, tile_shader::Position{}, tile_shader::Depth{})
.setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort);
CORRADE_INTERNAL_ASSERT(_mesh.isIndexed());
}
@@ -94,16 +94,23 @@ std::array<std::array<UnsignedShort, 6>, wall_mesh::COUNT> wall_mesh::make_index
return array;
}
-std::array<std::array<Vector3, 4>, wall_mesh::COUNT> wall_mesh::make_position_array()
+auto wall_mesh::make_constant_array() -> std::array<std::array<constant, 4>, wall_mesh::COUNT>
{
- std::array<std::array<Vector3, 4>, COUNT> array;
+ std::array<std::array<constant, 4>, COUNT> array;
for (std::uint8_t j = 0; j < TILE_MAX_DIM; j++)
for (std::uint8_t i = 0; i < TILE_MAX_DIM; i++)
{
- const std::size_t idx = (j*TILE_MAX_DIM + i) * 2u;
+ const local_coords coord{i, j};
+ const std::size_t idx = coord.to_index() * 2u;
const auto center = Vector3(i, j, 0) * TILE_SIZE;
- array[idx + 0] = tile_atlas::wall_quad_N(center, TILE_SIZE);
- array[idx + 1] = tile_atlas::wall_quad_W(center, TILE_SIZE);
+ auto wall_n_pos = tile_atlas::wall_quad_N(center, TILE_SIZE);
+ auto wall_w_pos = tile_atlas::wall_quad_W(center, TILE_SIZE);
+ auto depth = tile_shader::depth_value(coord);
+ for (std::size_t k = 0; k < 4; k++)
+ {
+ array[idx + 0][k] = { wall_n_pos[k], depth, };
+ array[idx + 1][k] = { wall_w_pos[k], depth, };
+ }
}
return array;
}
diff --git a/draw/wall.hpp b/draw/wall.hpp
index 81c6b60e..d65744fd 100644
--- a/draw/wall.hpp
+++ b/draw/wall.hpp
@@ -28,6 +28,11 @@ private:
Vector2 texcoords;
};
+ struct constant final {
+ Vector3 position;
+ float depth = -1;
+ };
+
using quad = std::array<vertex, 4>;
using vertex_array = std::array<quad, COUNT>;
using texture_array = std::array<GL::Texture2D*, COUNT>;
@@ -40,9 +45,9 @@ private:
GL::Mesh _mesh;
GL::Buffer _vertex_buffer{vertex_array{}, Magnum::GL::BufferUsage::DynamicDraw},
_index_buffer{make_index_array()},
- _positions_buffer{make_position_array()};
+ _constant_buffer{make_constant_array()};
static std::array<std::array<UnsignedShort, 6>, COUNT> make_index_array();
- static std::array<std::array<Vector3, 4>, COUNT> make_position_array();
+ static std::array<std::array<constant, 4>, COUNT> make_constant_array();
};
} // namespace floormat
diff --git a/draw/wireframe.cpp b/draw/wireframe.cpp
index 053fce7b..bf024f9e 100644
--- a/draw/wireframe.cpp
+++ b/draw/wireframe.cpp
@@ -27,16 +27,21 @@ GL::Texture2D mesh_base::make_constant_texture()
return tex;
}
+struct constant_buf {
+ Vector2 texcoords;
+ float depth = 1;
+};
+
mesh_base::mesh_base(GL::MeshPrimitive primitive, ArrayView<const void> index_data,
std::size_t num_vertices, std::size_t num_indexes) :
_vertex_buffer{Containers::Array<Vector3>{ValueInit, num_vertices}, GL::BufferUsage::DynamicDraw},
- _texcoords_buffer{Containers::Array<Vector2>{ValueInit, num_vertices}},
+ _constant_buffer{Containers::Array<constant_buf>{ValueInit, num_vertices}},
_index_buffer{num_indexes == 0 ? GL::Buffer{NoCreate} : GL::Buffer{index_data}}
{
_mesh.setCount((int)(num_indexes > 0 ? num_indexes : num_vertices))
.setPrimitive(primitive)
.addVertexBuffer(_vertex_buffer, 0, tile_shader::Position{})
- .addVertexBuffer(_texcoords_buffer, 0, tile_shader::TextureCoordinates{});
+ .addVertexBuffer(_constant_buffer, 0, tile_shader::TextureCoordinates{}, tile_shader::Depth{});
if (num_indexes > 0)
_mesh.setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort);
}
diff --git a/draw/wireframe.hpp b/draw/wireframe.hpp
index 1d58ff43..400fbee7 100644
--- a/draw/wireframe.hpp
+++ b/draw/wireframe.hpp
@@ -25,7 +25,7 @@ concept traits = requires (const T& x) {
struct mesh_base
{
static GL::Texture2D make_constant_texture();
- GL::Buffer _vertex_buffer{{}, GL::BufferUsage::DynamicDraw}, _texcoords_buffer, _index_buffer;
+ GL::Buffer _vertex_buffer{{}, GL::BufferUsage::DynamicDraw}, _constant_buffer, _index_buffer;
GL::Texture2D _texture = make_constant_texture();
GL::Mesh _mesh;