Simplified code.
This commit is contained in:
84
src/Mesh.h
84
src/Mesh.h
@@ -1,84 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <core.h>
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
|
|
||||||
template<typename TScalar, typename TIndex>
|
|
||||||
class GenericMesh {
|
|
||||||
public:
|
|
||||||
using Scalar = TScalar;
|
|
||||||
using Index = TIndex;
|
|
||||||
using Vector = Eigen::Matrix<Scalar, 3, 1>;
|
|
||||||
|
|
||||||
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<Vector>& positions() const {
|
|
||||||
return m_positions;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const std::vector<Index>& indices() const {
|
|
||||||
return m_indices;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<Vector> m_positions;
|
|
||||||
std::vector<Index> m_indices;
|
|
||||||
};
|
|
||||||
|
|
||||||
using Mesh = GenericMesh<Real, uint32_t>;
|
|
||||||
@@ -6,27 +6,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
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(
|
Cell::Cell(
|
||||||
const Vector3& corner00,
|
const Vector3& corner00,
|
||||||
const Vector3& corner01,
|
const Vector3& corner01,
|
||||||
@@ -58,7 +37,10 @@ size_t Cell::triangle_count(uint32_t subdiv_count) {
|
|||||||
return 2 * s2 * s2;
|
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_edge_count = (1u << subdiv_count);
|
||||||
auto const side_vert_count = side_edge_count + 1;
|
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 tri_count = 2 * quad_count;
|
||||||
auto const vert_count = side_vert_count * side_vert_count;
|
auto const vert_count = side_vert_count * side_vert_count;
|
||||||
|
|
||||||
assert(mesh.vertex_count() == vert_count);
|
assert(positions.size() == vert_count);
|
||||||
assert(mesh.index_count() == 3 * tri_count);
|
assert(triangles.size() == tri_count);
|
||||||
|
|
||||||
auto const index = [side_vert_count](uint32_t x, uint32_t y) -> uint32_t {
|
auto const index = [side_vert_count](uint32_t x, uint32_t y) -> uint32_t {
|
||||||
return x + y * side_vert_count;
|
return x + y * side_vert_count;
|
||||||
};
|
};
|
||||||
auto const vertex = [&mesh, &index](uint32_t x, uint32_t y) -> Vector3& {
|
auto const vertex = [&positions, &index](uint32_t x, uint32_t y) -> Vector3& {
|
||||||
return mesh.position(index(x, y));
|
return positions[index(x, y)];
|
||||||
};
|
};
|
||||||
|
|
||||||
vertex( 0, 0) = m_corners[0];
|
vertex( 0, 0) = m_corners[0];
|
||||||
vertex(side_edge_count, 0) = m_corners[1];
|
vertex(side_edge_count, 0) = m_corners[1];
|
||||||
vertex( 0, side_edge_count) = m_corners[2];
|
vertex( 0, side_edge_count) = m_corners[2];
|
||||||
vertex(side_edge_count, side_edge_count) = m_corners[3];
|
vertex(side_edge_count, side_edge_count) = m_corners[3];
|
||||||
|
|
||||||
struct QuadCell {
|
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 y = 0; y < side_edge_count; y += 1) {
|
||||||
for(uint32_t x = 0; x < side_edge_count; x += 1) {
|
for(uint32_t x = 0; x < side_edge_count; x += 1) {
|
||||||
const auto i00 = index_offset + index(x, y);
|
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 i10 = i00 + side_vert_count;
|
||||||
const auto i11 = i10 + 1;
|
const auto i11 = i10 + 1;
|
||||||
|
|
||||||
mesh.index(indexIndex++) = i00;
|
triangles[triangleIndex++] = Triangle { i00, i01, i10 };
|
||||||
mesh.index(indexIndex++) = i01;
|
triangles[triangleIndex++] = Triangle { i10, i01, i11 };
|
||||||
mesh.index(indexIndex++) = i10;
|
|
||||||
|
|
||||||
mesh.index(indexIndex++) = i10;
|
|
||||||
mesh.index(indexIndex++) = i01;
|
|
||||||
mesh.index(indexIndex++) = 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 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) {
|
for(Index cell_index = 0; cell_index < CellCount; cell_index += 1) {
|
||||||
auto sub_mesh = mesh.view(
|
auto sub_positions = positions.slice(
|
||||||
vertex_count, cell_index * vertex_count,
|
cell_index * vertex_count, vertex_count
|
||||||
index_count, cell_index * index_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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
89
src/Planet.h
89
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<Vector3*>(
|
|
||||||
m_positions + (vertex_index * m_position_stride)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Vector3& normal(Index vertex_index) {
|
|
||||||
assert(vertex_index < m_vertex_count);
|
|
||||||
return *reinterpret_cast<Vector3*>(
|
|
||||||
m_normals + (vertex_index * m_normal_stride)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Vector3& color(Index vertex_index) {
|
|
||||||
assert(vertex_index < m_vertex_count);
|
|
||||||
return *reinterpret_cast<Vector3*>(
|
|
||||||
m_colors + (vertex_index * m_color_stride)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Index& index(Index index_index) {
|
|
||||||
assert(index_index < m_index_count);
|
|
||||||
return *reinterpret_cast<Index*>(
|
|
||||||
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 {
|
class Cell {
|
||||||
public:
|
public:
|
||||||
static constexpr uint32_t CornerCount = 4;
|
static constexpr uint32_t CornerCount = 4;
|
||||||
@@ -121,7 +40,10 @@ public:
|
|||||||
static size_t vertex_count(uint32_t subdiv_count);
|
static size_t vertex_count(uint32_t subdiv_count);
|
||||||
static size_t triangle_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:
|
private:
|
||||||
static Real height_factor(Vector3 corners[CornerCount]);
|
static Real height_factor(Vector3 corners[CornerCount]);
|
||||||
@@ -156,7 +78,8 @@ public:
|
|||||||
return m_cells[cell_index];
|
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:
|
private:
|
||||||
Cell m_cells[CellCount];
|
Cell m_cells[CellCount];
|
||||||
|
|||||||
@@ -91,25 +91,40 @@ VulkanTutorial::VulkanTutorial() {
|
|||||||
vertices.resize(6 * Cell::vertex_count(subdiv_count));
|
vertices.resize(6 * Cell::vertex_count(subdiv_count));
|
||||||
indices.resize(6 * 3 * Cell::triangle_count(subdiv_count));
|
indices.resize(6 * 3 * Cell::triangle_count(subdiv_count));
|
||||||
|
|
||||||
MeshView mesh(
|
// MeshView mesh(
|
||||||
vertices.size(),
|
// vertices.size(),
|
||||||
reinterpret_cast<Byte*>(vertices.data()) + offsetof(Vertex, position),
|
// reinterpret_cast<Byte*>(vertices.data()) + offsetof(Vertex, position),
|
||||||
sizeof(Vertex),
|
// sizeof(Vertex),
|
||||||
nullptr,
|
// nullptr,
|
||||||
sizeof(Vertex),
|
// sizeof(Vertex),
|
||||||
reinterpret_cast<Byte*>(vertices.data()) + offsetof(Vertex, color),
|
// reinterpret_cast<Byte*>(vertices.data()) + offsetof(Vertex, color),
|
||||||
sizeof(Vertex),
|
// sizeof(Vertex),
|
||||||
indices.size(),
|
// indices.size(),
|
||||||
reinterpret_cast<Byte*>(indices.data()),
|
// reinterpret_cast<Byte*>(indices.data()),
|
||||||
sizeof(Index)
|
// 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 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) {
|
for (size_t vertex_index = 0; vertex_index < vertices.size(); vertex_index += 1) {
|
||||||
mesh.color(vertex_index) =
|
colors[vertex_index] =
|
||||||
mesh.position(vertex_index) / 2.0f + Vector3::Constant(0.5f);
|
positions[vertex_index] / 2.0f + Vector3::Constant(0.5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// for(size_t vi = 0; vi < vertices.size(); vi += 1)
|
// for(size_t vi = 0; vi < vertices.size(); vi += 1)
|
||||||
|
|||||||
12
src/core.h
12
src/core.h
@@ -10,6 +10,7 @@ using Byte = unsigned char;
|
|||||||
using Index = uint32_t;
|
using Index = uint32_t;
|
||||||
using Real = float;
|
using Real = float;
|
||||||
using Vector3 = Eigen::Matrix<Real, 3, 1>;
|
using Vector3 = Eigen::Matrix<Real, 3, 1>;
|
||||||
|
using Triangle = Eigen::Array<Index, 3, 1>;
|
||||||
|
|
||||||
|
|
||||||
template<typename Scalar, typename Derived0, typename Derived1>
|
template<typename Scalar, typename Derived0, typename Derived1>
|
||||||
@@ -43,8 +44,8 @@ public:
|
|||||||
~ArrayView() = default;
|
~ArrayView() = default;
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
ArrayView(Index size, U* data, Index stride=sizeof(T))
|
ArrayView(Index size, U* data, Index stride=sizeof(T), Index byte_offset=0)
|
||||||
: m_data(reinterpret_cast<Byte*>(data))
|
: m_data(reinterpret_cast<Byte*>(data) + byte_offset)
|
||||||
, m_size(size)
|
, m_size(size)
|
||||||
, m_stride(stride)
|
, m_stride(stride)
|
||||||
{
|
{
|
||||||
@@ -85,12 +86,12 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
T& operator[](Index index) {
|
T& operator[](Index index) {
|
||||||
return const_cast<T&>(const_cast<ArrayView&>(*this)[index]);
|
return const_cast<T&>(const_cast<const ArrayView&>(*this)[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayView slice(Index start, Index count, Index step=1) {
|
ArrayView slice(Index start, Index count, Index step=1) {
|
||||||
assert(step > 0);
|
assert(step > 0);
|
||||||
assert(start + count * step < m_size);
|
assert(start + count * step <= m_size);
|
||||||
return ArrayView(
|
return ArrayView(
|
||||||
count,
|
count,
|
||||||
m_data + start * m_stride,
|
m_data + start * m_stride,
|
||||||
@@ -103,3 +104,6 @@ private:
|
|||||||
Index m_size = 0;
|
Index m_size = 0;
|
||||||
Index m_stride = sizeof(T);
|
Index m_stride = sizeof(T);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using Vector3AV = ArrayView<Vector3>;
|
||||||
|
using TriangleAV = ArrayView<Triangle>;
|
||||||
|
|||||||
Reference in New Issue
Block a user