CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
ExtrudeMesh.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 "Edge.hpp"
11#include "Iterator.hpp"
12#include "MeshVertex.hpp"
13#include "PathVertex.hpp"
14#include "ShapeVertex.hpp"
15#include "Triangle.hpp"
16
18
22 template<typename Shape, typename Path> class ExtrudeMesh {
23 public:
24 class Triangles {
25 public:
28
29 const auto& shapeEdge = mShapeEdges.generate();
30 const auto& pathEdge = mPathEdges.generate();
31
32 if(odd_ == 0) {
33 triangle.vertices[0] = shapeEdge.vertices[0] + pathEdge.vertices[0] * mMesh->mShapeVertexCount;
34 triangle.vertices[1] = shapeEdge.vertices[1] + pathEdge.vertices[1] * mMesh->mShapeVertexCount;
35 triangle.vertices[2] = shapeEdge.vertices[0] + pathEdge.vertices[1] * mMesh->mShapeVertexCount;
36 } else {
37 triangle.vertices[0] = shapeEdge.vertices[0] + pathEdge.vertices[0] * mMesh->mShapeVertexCount;
38 triangle.vertices[1] = shapeEdge.vertices[1] + pathEdge.vertices[0] * mMesh->mShapeVertexCount;
39 triangle.vertices[2] = shapeEdge.vertices[1] + pathEdge.vertices[1] * mMesh->mShapeVertexCount;
40 }
41
42 return triangle;
43 }
44
45 [[nodiscard]] bool done() const noexcept { return mPathEdges.done(); }
46
47 void next() {
48 odd_ = !odd_;
49
50 if(odd_) {
51 mShapeEdges.next();
52 if(mShapeEdges.done()) {
53 mPathEdges.next();
54 mShapeEdges = mMesh->mShape.edges();
55 }
56 }
57 }
58
59 private:
61
62 decltype(mMesh->mShape.edges()) mShapeEdges;
63 decltype(mMesh->mPath.edges()) mPathEdges;
64 bool odd_;
65
66 Triangles(const ExtrudeMesh& mesh) : mMesh{&mesh}, mShapeEdges{mesh.mShape.edges()}, mPathEdges{mesh.mPath.edges()}, odd_{true} {}
67
68 friend class ExtrudeMesh;
69 };
70
71 class Vertices {
72 public:
74 MeshVertex vertex;
75
76 const auto& shapeVertex = mShapeVertices.generate();
77 const auto& pathVertex = mPathVertices.generate();
78
79 Vector3 pathBinormal = pathVertex.binormal();
80 vertex.position = pathVertex.position + shapeVertex.position[0] * pathVertex.normal + shapeVertex.position[1] * pathBinormal;
81
83 vertex.normal = shapeNormal[0] * pathVertex.normal + shapeNormal[1] * pathBinormal;
84
85 vertex.texCoord[0] = shapeVertex.texCoord;
86 vertex.texCoord[1] = pathVertex.texCoord;
87
88 return vertex;
89 }
90
91 [[nodiscard]] bool done() const noexcept { return mPathVertices.done(); }
92
93 void next() {
94 mShapeVertices.next();
95 if(mShapeVertices.done()) {
96 mPathVertices.next();
97 mShapeVertices = mMesh->mShape.vertices();
98 }
99 }
100
101 private:
103 decltype(mMesh->mShape.vertices()) mShapeVertices;
104 decltype(mMesh->mPath.vertices()) mPathVertices;
105
107
108 friend class ExtrudeMesh;
109 };
110
112 [[nodiscard]] Vertices vertices() const noexcept { return *this; }
113
117
118 private:
122 };
123
124 template<typename Shape, typename Path> ExtrudeMesh<Shape, Path> extrudeMesh(Shape shape, Path path) {
125 return ExtrudeMesh<Shape, Path>{std::move(shape), std::move(path)};
126 }
127
128} // namespace CeresEngine::MeshGenerator
Triangles(const ExtrudeMesh &mesh)
Definition ExtrudeMesh.hpp:66
bool done() const noexcept
Definition ExtrudeMesh.hpp:45
const ExtrudeMesh * mMesh
Definition ExtrudeMesh.hpp:60
bool odd_
Definition ExtrudeMesh.hpp:64
decltype(mMesh->mPath.edges()) mPathEdges
Definition ExtrudeMesh.hpp:63
decltype(mMesh->mShape.edges()) mShapeEdges
Definition ExtrudeMesh.hpp:62
void next()
Definition ExtrudeMesh.hpp:47
Triangle generate() const
Definition ExtrudeMesh.hpp:26
MeshVertex generate() const
Definition ExtrudeMesh.hpp:73
const ExtrudeMesh * mMesh
Definition ExtrudeMesh.hpp:102
decltype(mMesh->mPath.vertices()) mPathVertices
Definition ExtrudeMesh.hpp:104
Vertices(const ExtrudeMesh &mesh)
Definition ExtrudeMesh.hpp:106
bool done() const noexcept
Definition ExtrudeMesh.hpp:91
decltype(mMesh->mShape.vertices()) mShapeVertices
Definition ExtrudeMesh.hpp:103
void next()
Definition ExtrudeMesh.hpp:93
Extrude a shape along a path.
Definition ExtrudeMesh.hpp:22
ExtrudeMesh(Shape shape, Path path)
Definition ExtrudeMesh.hpp:116
Path mPath
Definition ExtrudeMesh.hpp:120
int mShapeVertexCount
Definition ExtrudeMesh.hpp:121
Shape mShape
Definition ExtrudeMesh.hpp:119
Vertices vertices() const noexcept
Definition ExtrudeMesh.hpp:112
Triangles triangles() const noexcept
Definition ExtrudeMesh.hpp:111
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
Vector3i vertices
Zero based indices of the triangle vertices in counterclockwise order.
Definition Triangle.hpp:17
A type that describes a conjunction of shapes that can be filled and stroked.
Definition Shape.hpp:198
Definition AnyGenerator.hpp:12
ExtrudeMesh< Shape, Path > extrudeMesh(Shape shape, Path path)
Definition ExtrudeMesh.hpp:124
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
constexpr CountAlgorithmFunctor count
Returns the number of elements matching an element.
Definition Count.hpp:82
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668