Plasma Engine  2.0
Loading...
Searching...
No Matches
Geometry.h
1#pragma once
2
3#include <Core/CoreDLL.h>
4#include <Foundation/Containers/Deque.h>
5#include <Foundation/Containers/HybridArray.h>
6#include <Foundation/Math/Color.h>
7#include <Foundation/Math/Color8UNorm.h>
8#include <Foundation/Math/Mat4.h>
9#include <Foundation/Math/Vec3.h>
10
16class PL_CORE_DLL plGeometry
17{
18public:
20 struct Vertex
21 {
22 PL_DECLARE_POD_TYPE();
23
24 plVec3 m_vPosition;
25 plVec3 m_vNormal;
26 plVec3 m_vTangent;
27 float m_fBiTangentSign;
28 plVec2 m_vTexCoord;
29 plColor m_Color;
30 plVec4U16 m_BoneIndices;
31 plColorLinearUB m_BoneWeights;
32
33 bool operator<(const Vertex& rhs) const;
34 bool operator==(const Vertex& rhs) const;
35 };
36
38 struct Polygon
39 {
40 // Reverses the order of vertices.
41 void FlipWinding();
42
43 plVec3 m_vNormal;
45 };
46
48 struct Line
49 {
50 PL_DECLARE_POD_TYPE();
51
52 plUInt32 m_uiStartVertex;
53 plUInt32 m_uiEndVertex;
54 };
55
58 {
59 PL_DECLARE_POD_TYPE();
60
61 GeoOptions(){}; // NOLINT: This struct is used before the sourrounding class ends, so it needs a default constructor. = default doesn't work here.
62
63 plColor m_Color = plColor(1, 1, 1, 1);
64 plMat4 m_Transform = plMat4::MakeIdentity();
65 plUInt16 m_uiBoneIndex = 0;
66
67 bool IsFlipWindingNecessary() const;
68 };
69
71 plDeque<Vertex>& GetVertices() { return m_Vertices; }
72
74 plDeque<Polygon>& GetPolygons() { return m_Polygons; }
75
77 plDeque<Line>& GetLines() { return m_Lines; }
78
80 const plDeque<Vertex>& GetVertices() const { return m_Vertices; }
81
83 const plDeque<Polygon>& GetPolygons() const { return m_Polygons; }
84
86 const plDeque<Line>& GetLines() const { return m_Lines; }
87
89 void Clear();
90
92 plUInt32 AddVertex(const plVec3& vPos, const plVec3& vNormal, const plVec2& vTexCoord, const plColor& color, const plVec4U16& vBoneIndices = plVec4U16::MakeZero(), const plColorLinearUB& boneWeights = plColorLinearUB(255, 0, 0, 0));
93
95 plUInt32 AddVertex(const plVec3& vPos, const plVec3& vNormal, const plVec2& vTexCoord, const plColor& color, const plVec4U16& vBoneIndices, const plColorLinearUB& boneWeights, const plMat4& mTransform)
96 {
97 return AddVertex(mTransform.TransformPosition(vPos), mTransform.TransformDirection(vNormal).GetNormalized(), vTexCoord, color, vBoneIndices, boneWeights);
98 }
99
101 plUInt32 AddVertex(const plVec3& vPos, const plVec3& vNormal, const plVec2& vTexCoord, const plColor& color, const plUInt16 uiBoneIndex, const plMat4& mTransform)
102 {
103 return AddVertex(mTransform.TransformPosition(vPos), mTransform.TransformDirection(vNormal).GetNormalized(), vTexCoord, color, plVec4U16(uiBoneIndex, 0, 0, 0), plColorLinearUB(255, 0, 0, 0));
104 }
105
107 plUInt32 AddVertex(const plVec3& vPos, const plVec3& vNormal, const plVec2& vTexCoord, const GeoOptions& options)
108 {
109 return AddVertex(options.m_Transform.TransformPosition(vPos), options.m_Transform.TransformDirection(vNormal).GetNormalized(), vTexCoord, options.m_Color, plVec4U16(options.m_uiBoneIndex, 0, 0, 0), plColorLinearUB(255, 0, 0, 0));
110 }
111
113 void AddPolygon(const plArrayPtr<plUInt32>& vertices, bool bFlipWinding);
114
116 void AddLine(plUInt32 uiStartVertex, plUInt32 uiEndVertex);
117
121 void TriangulatePolygons(plUInt32 uiMaxVerticesInPolygon = 3);
122
125 void ComputeFaceNormals();
126
131 void ComputeSmoothVertexNormals();
132
136 void ComputeTangents();
137
143 void ValidateTangents(float fEpsilon = 0.01f);
144
146 plUInt32 CalculateTriangleCount() const;
147
149 void SetAllVertexBoneIndices(const plVec4U16& vBoneIndices, plUInt32 uiFirstVertex = 0);
150
152 void SetAllVertexColor(const plColor& color, plUInt32 uiFirstVertex = 0);
153
155 void SetAllVertexTexCoord(const plVec2& vTexCoord, plUInt32 uiFirstVertex = 0);
156
161 void Transform(const plMat4& mTransform, bool bTransformPolyNormals);
162
164 void Merge(const plGeometry& other);
165
169 void AddRectXY(const plVec2& vSize, plUInt32 uiTesselationX = 1, plUInt32 uiTesselationY = 1, const GeoOptions& options = GeoOptions());
170
174 void AddBox(const plVec3& vFullExtents, bool bExtraVerticesForTexturing, const GeoOptions& options = GeoOptions());
175
177 void AddLineBox(const plVec3& vSize, const GeoOptions& options = GeoOptions());
178
182 void AddLineBoxCorners(const plVec3& vSize, float fCornerFraction, const GeoOptions& options = GeoOptions());
183
187 void AddPyramid(const plVec3& vSize, bool bCap, const GeoOptions& options = GeoOptions());
188
199 void AddGeodesicSphere(float fRadius, plUInt8 uiSubDivisions, const GeoOptions& options = GeoOptions());
200
209 void AddCylinder(float fRadiusTop, float fRadiusBottom, float fPositiveLength, float fNegativeLength, bool bCapTop, bool bCapBottom, plUInt16 uiSegments, const GeoOptions& options = GeoOptions(), plAngle fraction = plAngle::MakeFromDegree(360.0f));
210
214 void AddCylinderOnePiece(float fRadiusTop, float fRadiusBottom, float fPositiveLength, float fNegativeLength, plUInt16 uiSegments, const GeoOptions& options = GeoOptions());
215
219 void AddCone(float fRadius, float fHeight, bool bCap, plUInt16 uiSegments, const GeoOptions& options = GeoOptions());
220
225 void AddSphere(float fRadius, plUInt16 uiSegments, plUInt16 uiStacks, const GeoOptions& options = GeoOptions());
226
232 void AddHalfSphere(float fRadius, plUInt16 uiSegments, plUInt16 uiStacks, bool bCap, const GeoOptions& options = GeoOptions());
233
240 void AddCapsule(float fRadius, float fHeight, plUInt16 uiSegments, plUInt16 uiStacks, const GeoOptions& options = GeoOptions());
241
250 void AddTorus(float fInnerRadius, float fOuterRadius, plUInt16 uiSegments, plUInt16 uiSegmentDetail, bool bExtraVerticesForTexturing, const GeoOptions& options = GeoOptions());
251
253 void AddTexturedRamp(const plVec3& vSize, const GeoOptions& options = GeoOptions());
254
256 void AddStairs(const plVec3& vSize, plUInt32 uiNumSteps, plAngle curvature, bool bSmoothSloped, const GeoOptions& options = GeoOptions());
257
258 void AddArch(const plVec3& vSize, plUInt32 uiNumSegments, float fThickness, plAngle angle, bool bMakeSteps, bool bSmoothBottom, bool bSmoothTop, bool bCapTopAndBottom, const GeoOptions& options = GeoOptions());
259
261 // ThickLine
262 // Part of a Torus
263 // Arc
264 // Circle
265 // Curved cone (spotlight)
266 // flat arc / circle (ie. UE4 gizmo)
267 // ....
268
269 // Compounds:
270 // Arrow
271 // Cross ?
272 //
273
274
275private:
276 void TransformVertices(const plMat4& mTransform, plUInt32 uiFirstVertex);
277
278 plDeque<Vertex> m_Vertices;
279 plDeque<Polygon> m_Polygons;
280 plDeque<Line> m_Lines;
281};
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
static constexpr plAngle MakeFromDegree(float fDegree)
Creates an instance of plAngle that was initialized from degree. (Performs a conversion)
Definition Angle_inl.h:33
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
A 8bit per channel unsigned normalized (values interpreted as 0-1) color storage format that represen...
Definition Color8UNorm.h:61
Definition Deque.h:270
Provides functions to generate standard geometric shapes, such as boxes, spheres, cylinders,...
Definition Geometry.h:17
const plDeque< Line > & GetLines() const
Returns the entire line data.
Definition Geometry.h:86
plDeque< Line > & GetLines()
Returns the entire line data.
Definition Geometry.h:77
const plDeque< Vertex > & GetVertices() const
Returns the entire vertex data.
Definition Geometry.h:80
plUInt32 AddVertex(const plVec3 &vPos, const plVec3 &vNormal, const plVec2 &vTexCoord, const GeoOptions &options)
Adds a vertex with a single bone index, returns the index to the added vertex. Position and normal ar...
Definition Geometry.h:107
plDeque< Vertex > & GetVertices()
Returns the entire vertex data.
Definition Geometry.h:71
plUInt32 AddVertex(const plVec3 &vPos, const plVec3 &vNormal, const plVec2 &vTexCoord, const plColor &color, const plUInt16 uiBoneIndex, const plMat4 &mTransform)
Adds a vertex with a single bone index, returns the index to the added vertex. Position and normal ar...
Definition Geometry.h:101
plDeque< Polygon > & GetPolygons()
Returns the entire polygon data.
Definition Geometry.h:74
plUInt32 AddVertex(const plVec3 &vPos, const plVec3 &vNormal, const plVec2 &vTexCoord, const plColor &color, const plVec4U16 &vBoneIndices, const plColorLinearUB &boneWeights, const plMat4 &mTransform)
Adds a vertex, returns the index to the added vertex. Position and normal are transformed with the gi...
Definition Geometry.h:95
const plDeque< Polygon > & GetPolygons() const
Returns the entire polygon data.
Definition Geometry.h:83
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
static plMat4Template< float > MakeIdentity()
Definition Mat4_inl.h:75
const plVec3Template< Type > TransformPosition(const plVec3Template< Type > &v) const
Matrix-vector multiplication, assuming the 4th component of the vector is one (default behavior).
Definition Mat4_inl.h:347
const plVec3Template< Type > TransformDirection(const plVec3Template< Type > &v) const
Matrix-vector multiplication, assuming the 4th component of the vector is zero. So,...
Definition Mat4_inl.h:374
PL_DECLARE_IF_FLOAT_TYPE const plVec3Template< Type > GetNormalized() const
Returns a normalized version of this vector, leaves the vector itself unchanged.
Definition Vec3_inl.h:86
static plVec4Template< plUInt16 > MakeZero()
Definition Vec4.h:41
Options shared among all geometry creation functions.
Definition Geometry.h:58
plUInt16 m_uiBoneIndex
Which bone should influence this geometry, for single-bone skinning.
Definition Geometry.h:65
plMat4 m_Transform
An additional transform to apply to the geometry while adding it.
Definition Geometry.h:64
plColor m_Color
The color of the entire geometric object.
Definition Geometry.h:63
A line only references two vertices.
Definition Geometry.h:49
Each polygon has a face normal and a set of indices, which vertices it references.
Definition Geometry.h:39
The data that is stored per vertex.
Definition Geometry.h:21