CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
BezierMesh.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 "ParametricMesh.hpp"
11
12#include <algorithm>
13#include <limits>
14
16
21 template<int D0, int D1> class BezierMesh {
22 private:
23 static_assert(D0 > 1, "D0 must be > 1.");
24 static_assert(D1 > 1, "D1 must be > 1.");
25
28
29 struct ArrayWrapper {
31
32 ArrayWrapper(const Vector3 (&p)[D1][D0]) { std::copy(&p[0][0], &p[0][0] + D1 * D0, &data[0][0]); }
33 };
34
35 explicit BezierMesh(const ArrayWrapper& p, const Vector2i& segments)
37 MeshVertex vertex;
38
39 vertex.position = bezier2(p.data, t);
40
41 Matrix2x3 J = bezier2Jacobian<1>(p.data, t);
42 vertex.normal = cross(J[0], J[1]);
43
44 // If the normal was zero try a again near by.
45 const double e = std::numeric_limits<double>::epsilon();
46 if(dot(vertex.normal, vertex.normal) < e) {
47 J = bezier2Jacobian<1>(p.data, t + 10.0 * e);
48 vertex.normal = cross(J[0], J[1]);
49 }
50 vertex.normal = normalize(vertex.normal);
51
52 vertex.texCoord = t;
53
54 return vertex;
55 },
56 segments} {}
57
58 public:
61 explicit BezierMesh(const Vector3 (&p)[D1][D0], const Vector2i& segments = {16, 16})
62 : // Work around a msvc lambda capture bug by wrapping the array.
63 BezierMesh{ArrayWrapper{p}, segments} {}
64
65 using Triangles = typename Impl::Triangles;
66 [[nodiscard]] Triangles triangles() const noexcept { return mParametricMesh.triangles(); }
67
68 using Vertices = typename Impl::Vertices;
69 [[nodiscard]] Vertices vertices() const noexcept { return mParametricMesh.vertices(); }
70 };
71
72} // namespace CeresEngine::MeshGenerator
A bezier patch with D0xD1 control points.
Definition BezierMesh.hpp:21
BezierMesh(const ArrayWrapper &p, const Vector2i &segments)
Definition BezierMesh.hpp:35
Impl mParametricMesh
Definition BezierMesh.hpp:27
Vertices vertices() const noexcept
Definition BezierMesh.hpp:69
Triangles triangles() const noexcept
Definition BezierMesh.hpp:66
typename Impl::Vertices Vertices
Definition BezierMesh.hpp:68
typename Impl::Triangles Triangles
Definition BezierMesh.hpp:65
BezierMesh(const Vector3(&p)[D1][D0], const Vector2i &segments={16, 16})
Definition BezierMesh.hpp:61
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
A mesh with values evaluated using a callback function.
Definition ParametricMesh.hpp:18
Triangles triangles() const noexcept
Definition AnyGenerator.hpp:12
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
ArrayWrapper(const Vector3(&p)[D1][D0])
Definition BezierMesh.hpp:32
Vector3 data[D1][D0]
Definition BezierMesh.hpp:30