From 6f4a8461a93dd5d95f938414e5b0f06839f20784 Mon Sep 17 00:00:00 2001 From: Draklaw Date: Fri, 18 Feb 2022 23:28:45 +0100 Subject: [PATCH] Simplified code. --- src/Mesh.h | 84 --------------------------------------- src/Planet.cpp | 74 ++++++++++++++--------------------- src/Planet.h | 89 +++--------------------------------------- src/VulkanTutorial.cpp | 43 +++++++++++++------- src/core.h | 14 ++++--- 5 files changed, 74 insertions(+), 230 deletions(-) delete mode 100644 src/Mesh.h diff --git a/src/Mesh.h b/src/Mesh.h deleted file mode 100644 index fc21258..0000000 --- a/src/Mesh.h +++ /dev/null @@ -1,84 +0,0 @@ -#pragma once - -#include - -#include -#include - - -template -class GenericMesh { -public: - using Scalar = TScalar; - using Index = TIndex; - using Vector = Eigen::Matrix; - -public: - GenericMesh() = default; - GenericMesh(const GenericMesh&) = delete; - GenericMesh(GenericMesh&&) = default; - ~GenericMesh() = default; - - GenericMesh& operator=(const GenericMesh&) = delete; - GenericMesh& operator=(GenericMesh&&) = default; - - inline size_t vertexCount() const { - return m_positions.size(); - } - inline size_t indexCount() const { - return m_indices.size(); - } - - inline void resizeVertex(Index vertexCount) { - m_positions.resize(vertexCount); - } - inline void resizeIndices(Index indexCount) { - m_indices.resize(indexCount); - } - - inline const Vector& position(Index vertexIndex) const { - assert(vertexIndex < m_positions.size()); - return m_positions[vertexIndex]; - } - inline Vector& position(Index vertexIndex) { - assert(vertexIndex < m_positions.size()); - return m_positions[vertexIndex]; - } - - inline Index index(Index indexIndex) const { - assert(indexIndex < m_indices.size()); - return m_indices[indexIndex]; - } - inline Index& index(Index indexIndex) { - assert(indexIndex < m_indices.size()); - return m_indices[indexIndex]; - } - - inline const Vector* positionData() const { - return m_positions.data(); - } - inline Vector* positionData() { - return m_positions.data(); - } - - inline const Index* indexData() const { - return m_indices.data(); - } - inline Index* indexData() { - return m_indices.data(); - } - - inline const std::vector& positions() const { - return m_positions; - } - - inline const std::vector& indices() const { - return m_indices; - } - -private: - std::vector m_positions; - std::vector m_indices; -}; - -using Mesh = GenericMesh; diff --git a/src/Planet.cpp b/src/Planet.cpp index 6c29942..12bf2c3 100644 --- a/src/Planet.cpp +++ b/src/Planet.cpp @@ -6,27 +6,6 @@ #include -MeshView::MeshView( - Index vertex_count, - Byte* positions, Index position_stride, - Byte* normals, Index normal_stride, - Byte* colors, Index color_stride, - Index index_count, - Byte* indices, Index index_stride -) - : m_vertex_count(vertex_count) - , m_positions(positions) - , m_position_stride(position_stride) - , m_normals(normals) - , m_normal_stride(normal_stride) - , m_colors(colors) - , m_color_stride(color_stride) - , m_index_count(index_count) - , m_indices(indices) - , m_index_stride(index_stride) -{} - - Cell::Cell( const Vector3& corner00, const Vector3& corner01, @@ -58,7 +37,10 @@ size_t Cell::triangle_count(uint32_t subdiv_count) { return 2 * s2 * s2; } -void Cell::build_mesh(MeshView& mesh, uint32_t subdiv_count, Index index_offset) const { +void Cell::build_mesh( + Vector3AV positions, TriangleAV triangles, + uint32_t subdiv_count, Index index_offset +) const { auto const side_edge_count = (1u << subdiv_count); auto const side_vert_count = side_edge_count + 1; @@ -66,19 +48,19 @@ void Cell::build_mesh(MeshView& mesh, uint32_t subdiv_count, Index index_offset) auto const tri_count = 2 * quad_count; auto const vert_count = side_vert_count * side_vert_count; - assert(mesh.vertex_count() == vert_count); - assert(mesh.index_count() == 3 * tri_count); + assert(positions.size() == vert_count); + assert(triangles.size() == tri_count); auto const index = [side_vert_count](uint32_t x, uint32_t y) -> uint32_t { return x + y * side_vert_count; }; - auto const vertex = [&mesh, &index](uint32_t x, uint32_t y) -> Vector3& { - return mesh.position(index(x, y)); + auto const vertex = [&positions, &index](uint32_t x, uint32_t y) -> Vector3& { + return positions[index(x, y)]; }; - vertex( 0, 0) = m_corners[0]; - vertex(side_edge_count, 0) = m_corners[1]; - vertex( 0, side_edge_count) = m_corners[2]; + vertex( 0, 0) = m_corners[0]; + vertex(side_edge_count, 0) = m_corners[1]; + vertex( 0, side_edge_count) = m_corners[2]; vertex(side_edge_count, side_edge_count) = m_corners[3]; struct QuadCell { @@ -124,7 +106,7 @@ void Cell::build_mesh(MeshView& mesh, uint32_t subdiv_count, Index index_offset) } } - auto indexIndex = 0; + auto triangleIndex = 0; for(uint32_t y = 0; y < side_edge_count; y += 1) { for(uint32_t x = 0; x < side_edge_count; x += 1) { const auto i00 = index_offset + index(x, y); @@ -132,13 +114,8 @@ void Cell::build_mesh(MeshView& mesh, uint32_t subdiv_count, Index index_offset) const auto i10 = i00 + side_vert_count; const auto i11 = i10 + 1; - mesh.index(indexIndex++) = i00; - mesh.index(indexIndex++) = i01; - mesh.index(indexIndex++) = i10; - - mesh.index(indexIndex++) = i10; - mesh.index(indexIndex++) = i01; - mesh.index(indexIndex++) = i11; + triangles[triangleIndex++] = Triangle { i00, i01, i10 }; + triangles[triangleIndex++] = Triangle { i10, i01, i11 }; } } } @@ -191,16 +168,25 @@ Planet::Planet() } {} -void Planet::build_mesh(MeshView& mesh, uint32_t subdiv_count, Index index_offset) const { +void Planet::build_mesh( + Vector3AV positions, TriangleAV triangles, + uint32_t subdiv_count, Index index_offset +) const { auto const vertex_count = Cell::vertex_count(subdiv_count); - auto const index_count = 3 * Cell::triangle_count(subdiv_count); + auto const index_count = Cell::triangle_count(subdiv_count); for(Index cell_index = 0; cell_index < CellCount; cell_index += 1) { - auto sub_mesh = mesh.view( - vertex_count, cell_index * vertex_count, - index_count, cell_index * index_count + auto sub_positions = positions.slice( + cell_index * vertex_count, vertex_count + ); + auto sub_triangles = triangles.slice( + cell_index * index_count, index_count + ); + cell(cell_index).build_mesh( + sub_positions, + sub_triangles, + subdiv_count, + index_offset + cell_index * vertex_count ); - cell(cell_index).build_mesh(sub_mesh, subdiv_count, - index_offset + cell_index * vertex_count); } } diff --git a/src/Planet.h b/src/Planet.h index 2fe9de3..0e7ef4f 100644 --- a/src/Planet.h +++ b/src/Planet.h @@ -16,87 +16,6 @@ struct Vertex { }; -class MeshView { -public: - MeshView( - Index vertex_count, - Byte* positions, Index position_stride, - Byte* normals, Index normal_stride, - Byte* colors, Index color_stride, - Index index_count, - Byte* indices, Index index_stride - ); - ~MeshView() = default; - - inline Index vertex_count() const { - return m_vertex_count; - } - inline Index index_count() const { - return m_index_count; - } - - MeshView view( - Index vertex_count, Index vertex_index, - Index index_count, Index index_index - ) { - assert(vertex_index + vertex_count <= m_vertex_count); - assert(index_index + index_count <= m_index_count); - return MeshView( - vertex_count, - m_positions + (vertex_index * m_position_stride), - m_position_stride, - m_normals + (vertex_index * m_normal_stride), - m_normal_stride, - m_colors + (vertex_index * m_color_stride), - m_color_stride, - index_count, - m_indices + (index_index * m_index_stride), - m_index_stride - ); - } - - inline Vector3& position(Index vertex_index) { - assert(vertex_index < m_vertex_count); - return *reinterpret_cast( - m_positions + (vertex_index * m_position_stride) - ); - } - - inline Vector3& normal(Index vertex_index) { - assert(vertex_index < m_vertex_count); - return *reinterpret_cast( - m_normals + (vertex_index * m_normal_stride) - ); - } - - inline Vector3& color(Index vertex_index) { - assert(vertex_index < m_vertex_count); - return *reinterpret_cast( - m_colors + (vertex_index * m_color_stride) - ); - } - - inline Index& index(Index index_index) { - assert(index_index < m_index_count); - return *reinterpret_cast( - m_indices + (index_index * m_index_stride) - ); - } - -private: - Index m_vertex_count; - Byte* m_positions; - Index m_position_stride; - Byte* m_normals; - Index m_normal_stride; - Byte* m_colors; - Index m_color_stride; - Index m_index_count; - Byte* m_indices; - Index m_index_stride; -}; - - class Cell { public: static constexpr uint32_t CornerCount = 4; @@ -121,7 +40,10 @@ public: static size_t vertex_count(uint32_t subdiv_count); static size_t triangle_count(uint32_t subdiv_count); - void build_mesh(MeshView& mesh, uint32_t subdiv_count=4, Index index_offset=0) const; + void build_mesh( + Vector3AV positions, TriangleAV triangles, + uint32_t subdiv_count=4, Index index_offset=0 + ) const; private: static Real height_factor(Vector3 corners[CornerCount]); @@ -156,7 +78,8 @@ public: return m_cells[cell_index]; } - void build_mesh(MeshView& mesh, uint32_t subdiv_count=4, Index index_offset=0) const; + void build_mesh(Vector3AV positions, TriangleAV triangles, + uint32_t subdiv_count=4, Index index_offset=0) const; private: Cell m_cells[CellCount]; diff --git a/src/VulkanTutorial.cpp b/src/VulkanTutorial.cpp index efba0e7..bb2af04 100644 --- a/src/VulkanTutorial.cpp +++ b/src/VulkanTutorial.cpp @@ -91,25 +91,40 @@ VulkanTutorial::VulkanTutorial() { vertices.resize(6 * Cell::vertex_count(subdiv_count)); indices.resize(6 * 3 * Cell::triangle_count(subdiv_count)); - MeshView mesh( - vertices.size(), - reinterpret_cast(vertices.data()) + offsetof(Vertex, position), - sizeof(Vertex), - nullptr, - sizeof(Vertex), - reinterpret_cast(vertices.data()) + offsetof(Vertex, color), - sizeof(Vertex), - indices.size(), - reinterpret_cast(indices.data()), - sizeof(Index) + // MeshView mesh( + // vertices.size(), + // reinterpret_cast(vertices.data()) + offsetof(Vertex, position), + // sizeof(Vertex), + // nullptr, + // sizeof(Vertex), + // reinterpret_cast(vertices.data()) + offsetof(Vertex, color), + // sizeof(Vertex), + // indices.size(), + // reinterpret_cast(indices.data()), + // sizeof(Index) + // ); + + Vector3AV positions( + vertices.size(), vertices.data(), + sizeof(Vertex), offsetof(Vertex, position) + ); + + Vector3AV colors( + vertices.size(), vertices.data(), + sizeof(Vertex), offsetof(Vertex, color) + ); + + TriangleAV triangles( + indices.size() / 3, + indices.data() ); Planet planet; - planet.build_mesh(mesh, subdiv_count); + planet.build_mesh(positions, triangles, subdiv_count); for (size_t vertex_index = 0; vertex_index < vertices.size(); vertex_index += 1) { - mesh.color(vertex_index) = - mesh.position(vertex_index) / 2.0f + Vector3::Constant(0.5f); + colors[vertex_index] = + positions[vertex_index] / 2.0f + Vector3::Constant(0.5f); } // for(size_t vi = 0; vi < vertices.size(); vi += 1) diff --git a/src/core.h b/src/core.h index fb194f5..4b2c02c 100644 --- a/src/core.h +++ b/src/core.h @@ -10,6 +10,7 @@ using Byte = unsigned char; using Index = uint32_t; using Real = float; using Vector3 = Eigen::Matrix; +using Triangle = Eigen::Array; template @@ -43,8 +44,8 @@ public: ~ArrayView() = default; template - ArrayView(Index size, U* data, Index stride=sizeof(T)) - : m_data(reinterpret_cast(data)) + ArrayView(Index size, U* data, Index stride=sizeof(T), Index byte_offset=0) + : m_data(reinterpret_cast(data) + byte_offset) , m_size(size) , m_stride(stride) { @@ -85,12 +86,12 @@ public: } T& operator[](Index index) { - return const_cast(const_cast(*this)[index]); + return const_cast(const_cast(*this)[index]); } ArrayView slice(Index start, Index count, Index step=1) { assert(step > 0); - assert(start + count * step < m_size); + assert(start + count * step <= m_size); return ArrayView( count, m_data + start * m_stride, @@ -102,4 +103,7 @@ private: Byte* m_data = nullptr; Index m_size = 0; Index m_stride = sizeof(T); -}; \ No newline at end of file +}; + +using Vector3AV = ArrayView; +using TriangleAV = ArrayView;