CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
LatheMesh.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Axis.hpp"
11#include "MeshVertex.hpp"
12#include "ShapeVertex.hpp"
13#include "Triangle.hpp"
14
15#include <memory>
16#include <vector>
17
19
23 template<typename Shape> class LatheMesh {
24 public:
25 class Triangles {
26 public:
29
30 const auto& shapeEdge = mShapeEdges.generate();
31
32 int slice = i_ / 2;
33
34 const int delta = mMesh->mSlices + 1;
35
36 if(i_ % 2 == 0) {
37 triangle.vertices[0] = shapeEdge.vertices[0] * delta + slice;
38 triangle.vertices[1] = shapeEdge.vertices[1] * delta + slice;
39 triangle.vertices[2] = shapeEdge.vertices[1] * delta + slice + 1;
40 } else {
41 triangle.vertices[0] = shapeEdge.vertices[0] * delta + slice;
42 triangle.vertices[1] = shapeEdge.vertices[1] * delta + slice + 1;
43 triangle.vertices[2] = shapeEdge.vertices[0] * delta + slice + 1;
44 }
45
46 return triangle;
47 }
48
49 [[nodiscard]] bool done() const noexcept { return mShapeEdges.done(); }
50
51 void next() {
52 ++i_;
53 if(i_ == 2 * mMesh->mSlices) {
54 i_ = 0;
55 mShapeEdges.next();
56 }
57 }
58
59 private:
61
62 decltype(mMesh->mShape.edges()) mShapeEdges;
63
64 int i_;
65
66 Triangles(const LatheMesh& mesh) : mMesh{&mesh}, mShapeEdges{mesh.mShape.edges()}, i_{0} {}
67
68 friend class LatheMesh;
69 };
70
71 class Vertices {
72 public:
74 MeshVertex vertex;
75
76 const auto shapeVertex = mShapeVertices.generate();
77 const Vector2 normal = shapeVertex.normal();
78
79 const double deltaAngle = mMesh->sweep_ / mMesh->mSlices;
80 double angle = i_ * deltaAngle + mMesh->start_;
81
82 const Quaternion q = qrotate(angle, mMesh->axis_);
83
84 vertex.position = Math::transform(q, Vector3{shapeVertex.position[0], shapeVertex.position[1], 0.0});
85
86 vertex.normal = Math::transform(q, Vector3{normal[0], normal[1], 0.0});
87
88 vertex.texCoord[0] = shapeVertex.texCoord;
89 vertex.texCoord[1] = angle / mMesh->sweep_;
90
91 return vertex;
92 }
93
94 [[nodiscard]] bool done() const noexcept { return mShapeVertices.done(); }
95
96 void next() {
97 ++i_;
98
99 if(i_ == mMesh->mSlices + 1) {
100 i_ = 0;
101 mShapeVertices.next();
102 }
103 }
104
105 private:
107
108 decltype(mMesh->mShape.vertices()) mShapeVertices;
109
110 int i_;
111
112 Vertices(const LatheMesh& mesh) : mMesh{&mesh}, mShapeVertices{mesh.mShape.vertices()}, i_{0} {}
113
114 friend class LatheMesh;
115 };
116
123 LatheMesh(Shape shape, const Vector2& axis, const int slices = 32, const double start = 0.0, const double sweep = radians(360.0))
124 : axis_{axis, 0.0}, mShape{std::move(shape)}, mSlices{slices}, start_{start}, sweep_{sweep} {}
125
127 [[nodiscard]] Vertices vertices() const noexcept { return *this; }
128
129 private:
133 double start_;
134 double sweep_;
135 };
136
137 template<typename Shape> LatheMesh<Shape> lathe(Shape shape, Axis axis = Axis::X, int slices = 32, double start = 0.0, double sweep = radians(360.0)) {
138 return LatheMesh<Shape>{std::move(shape), axis, slices, start, sweep};
139 }
140
141} // namespace CeresEngine::MeshGenerator
Triangle generate() const
Definition LatheMesh.hpp:27
decltype(mMesh->mShape.edges()) mShapeEdges
Definition LatheMesh.hpp:62
bool done() const noexcept
Definition LatheMesh.hpp:49
const LatheMesh * mMesh
Definition LatheMesh.hpp:60
void next()
Definition LatheMesh.hpp:51
Triangles(const LatheMesh &mesh)
Definition LatheMesh.hpp:66
int i_
Definition LatheMesh.hpp:110
void next()
Definition LatheMesh.hpp:96
decltype(mMesh->mShape.vertices()) mShapeVertices
Definition LatheMesh.hpp:108
MeshVertex generate() const
Definition LatheMesh.hpp:73
Vertices(const LatheMesh &mesh)
Definition LatheMesh.hpp:112
const LatheMesh * mMesh
Definition LatheMesh.hpp:106
bool done() const noexcept
Definition LatheMesh.hpp:94
Spins a shape around an axis to create a mesh.
Definition LatheMesh.hpp:23
double start_
Definition LatheMesh.hpp:133
Vector3 axis_
Definition LatheMesh.hpp:130
Triangles triangles() const noexcept
Definition LatheMesh.hpp:126
int mSlices
Definition LatheMesh.hpp:132
double sweep_
Definition LatheMesh.hpp:134
LatheMesh(Shape shape, const Vector2 &axis, const int slices=32, const double start=0.0, const double sweep=radians(360.0))
Definition LatheMesh.hpp:123
Vertices vertices() const noexcept
Definition LatheMesh.hpp:127
Shape mShape
Definition LatheMesh.hpp:131
Definition MeshVertex.hpp:14
Vector3 normal
Unit vector perpendicular to the surface.
Definition MeshVertex.hpp:19
Vector2 texCoord
UV texture coordinates.
Definition MeshVertex.hpp:22
Vector3 position
Definition MeshVertex.hpp:16
Definition Triangle.hpp:14
A type that describes a conjunction of shapes that can be filled and stroked.
Definition Shape.hpp:198
Definition AnyGenerator.hpp:12
Axis
Definition Axis.hpp:12
LatheMesh< Shape > lathe(Shape shape, Axis axis=Axis::X, int slices=32, double start=0.0, double sweep=radians(360.0))
Definition LatheMesh.hpp:137
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
Axis axis(const StringView &str) noexcept
Returns the Axis constant that is represented by str.
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
TVector3< T > transform(const TQuaternion< T > &q, const TVector3< T > &v)
Definition Math.hpp:694
Definition Span.hpp:668