-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMesh.h
More file actions
329 lines (278 loc) · 10.9 KB
/
Copy pathMesh.h
File metadata and controls
329 lines (278 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#pragma once
#include "Reflection.hpp" // CT3: was transitive via Core.Minimal.h
#include "Core.Minimal.h"
#include "MetaPolymorphic.h"
#include "EngineResourceCensus.h"
#include <assimp/mesh.h>
#include <mathematics/bounds.hpp>
#include <mathematics/matrix4x4.hpp>
#include <mathematics/vector2.hpp>
#include <mathematics/vector3.hpp>
#include <mathematics/vector4.hpp>
#include <cstddef>
#include <type_traits>
struct ModelNode : public meta::polymorphic
{
std::string m_name;
math::matrix4x4 m_transform{ math::matrix4x4::identity() };
uint32 m_index{};
uint32 m_parentIndex{};
std::vector<uint32> m_childrenIndex;
uint32 m_numChildren{};
uint32 m_numMeshes{};
std::vector<uint32> m_meshes;
ModelNode() = default;
ModelNode(std::string_view name) : m_name(name) {}
};
struct Vertex
{
math::vector3 position{};
math::vector3 normal{};
math::vector2 uv0{};
math::vector2 uv1{};
math::vector3 tangent{};
math::vector3 bitangent{};
math::vector4 boneIndices{};
math::vector4 boneWeights{};
Vertex() = default;
Vertex(
const math::vector3& _position,
const math::vector3& _normal,
const math::vector2& _uv0,
const math::vector2& _uv1,
const math::vector3& _tangent,
const math::vector3& _bitangent,
const math::vector4& _boneIndices,
const math::vector4& _boneWeights
) :
position(_position),
normal(_normal),
uv0(_uv0),
uv1(_uv1),
tangent(_tangent),
bitangent(_bitangent),
boneIndices(_boneIndices),
boneWeights(_boneWeights) {}
Vertex(const math::vector3& _position, const math::vector3& _normal, const math::vector2& _uv) :
position(_position), normal(_normal), uv0(_uv) {}
static Vertex ConvertToAiMesh(aiMesh* mesh, uint32 i)
{
if (!mesh->HasPositions() || !mesh->HasNormals() || !mesh->HasTangentsAndBitangents())
{
throw std::runtime_error("Mesh does not have required vertex attributes.");
}
if (mesh->mVertices == nullptr || mesh->mNormals == nullptr || mesh->mTangents == nullptr || mesh->mBitangents == nullptr)
{
throw std::runtime_error("Mesh vertex data is null.");
}
bool hasTexCoords = mesh->mTextureCoords[0] != nullptr;
bool hasTexCoords1 = mesh->mTextureCoords[1] != nullptr;
Vertex vertex;
vertex.position = { mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z };
vertex.normal = { mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z };
vertex.tangent = { mesh->mTangents[i].x, mesh->mTangents[i].y, mesh->mTangents[i].z };
vertex.bitangent = { mesh->mBitangents[i].x, mesh->mBitangents[i].y, mesh->mBitangents[i].z };
if (hasTexCoords)
{
vertex.uv0 = { mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y };
if (hasTexCoords1)
{
vertex.uv1 = { mesh->mTextureCoords[1][i].x, mesh->mTextureCoords[1][i].y };
}
else
{
vertex.uv1 = vertex.uv0;
}
}
return vertex;
}
};
static_assert(std::is_same_v<decltype(ModelNode::m_transform), math::matrix4x4>);
static_assert(sizeof(math::matrix4x4) == 64u);
static_assert(std::is_standard_layout_v<Vertex>);
static_assert(std::is_trivially_copyable_v<Vertex>);
static_assert(alignof(Vertex) == alignof(float));
static_assert(sizeof(Vertex) == 96u, "CEMA v2/GPU vertex stride changed");
static_assert(offsetof(Vertex, position) == 0u);
static_assert(offsetof(Vertex, normal) == 12u);
static_assert(offsetof(Vertex, uv0) == 24u);
static_assert(offsetof(Vertex, uv1) == 32u);
static_assert(offsetof(Vertex, tangent) == 40u);
static_assert(offsetof(Vertex, bitangent) == 52u);
static_assert(offsetof(Vertex, boneIndices) == 64u);
static_assert(offsetof(Vertex, boneWeights) == 80u);
class Texture;
class Material;
class ModelLoader;
class MeshOptimizer;
class Camera;
class Mesh : public meta::polymorphic, public std::enable_shared_from_this<Mesh>,
private Diagnostics::CountedResource<Diagnostics::EngineResource::Mesh>
{
public:
static consteval auto reflect()
{
using Self = Mesh;
return meta::schema<Self>(
meta::field<&Self::m_name>,
meta::field<&Self::m_materialIndex>,
meta::field<&Self::m_LODThresholds>);
}
public:
// 각 LOD 레벨의 GPU 리소스를 관리하는 구조체
struct LODResource
{
uint32 indexCount;
};
public:
Mesh() = default;
Mesh(std::string_view _name, const std::vector<Vertex>& _vertices, const std::vector<uint32>& _indices);
Mesh(std::string_view _name, std::vector<Vertex>&& _vertices, std::vector<uint32>&& _indices);
Mesh(Mesh&& _other) noexcept;
~Mesh();
bool operator==(const Mesh& _other) const
{
return m_hashingMesh == _other.m_hashingMesh;
}
void AssetInit();
// DX11 드로우 8종은 T5에서, 정점·인덱스 버퍼는 D4에서, 그림자 최적화
// 계통은 여기(A-③)에서 걷었다 — .cpp 주석 참고.
// 이제 Mesh는 CPU 배열이 전부다. GPU 올리기는 DX12 쪽 소유다.
// LOD 생성 함수
bool HasLODs() const;
void GenerateLODs(const std::vector<float>& lodThresholds);
const std::vector<float>& GetLODThresholds() const { return m_LODThresholds; }
std::string GetName() const { return m_name; }
std::string GetModelName() const { return m_modelName; }
uint32 GetMaterialIndex() const { return m_materialIndex; }
const std::vector<Vertex>& GetVertices() { return m_vertices; }
const std::vector<uint32>& GetIndices() { return m_indices; }
const math::aabb& GetBoundingBox() const noexcept { return m_boundingBox; }
const math::sphere& GetBoundingSphere() const noexcept { return m_boundingSphere; }
/// 정점에서 로컬 바운드를 다시 계산한다. ModelLoader가 임포트 때 하는
/// 계산과 같다 — 로더를 거치지 않는 절차 생성 메시는 바운드가 기본값
/// (empty AABB/반지름 0)으로 남아 그림자 캐스터 컬링 같은 판정이 조용히 틀어진다.
void RecalculateBounds();
std::vector<Vertex> GetVertices() const{ return m_vertices; }
std::vector<uint32> GetIndices() const { return m_indices; }
uint32 GetStride() { return m_stride; }
HashedGuid m_hashingMesh{ make_guid() };
private:
friend class ModelLoader;
friend class MeshOptimizer;
std::string m_name;
uint32 m_materialIndex{};
std::string m_modelName;
std::vector<Vertex> m_vertices;
std::vector<uint32> m_indices;
math::aabb m_boundingBox{};
math::sphere m_boundingSphere{};
static_assert(std::is_same_v<decltype(m_boundingBox), math::aabb>);
static_assert(std::is_same_v<decltype(m_boundingSphere), math::sphere>);
static_assert(sizeof(math::aabb) == 24u);
static_assert(sizeof(math::sphere) == 16u);
// --- LOD 관련 멤버 변수 ---
// 인덱스 0: 원본(LOD0), 1: LOD1, ...
std::vector<LODResource> m_LODs;
// 화면 공간 크기 기반의 LOD 전환 임계값
std::vector<float> m_LODThresholds;
// ---
static constexpr const uint32 m_stride = sizeof(Vertex);
};
class PrimitiveCreator
{
public:
static inline std::vector<Vertex> CubeVertices()
{
std::vector<Vertex> cube;
cube.reserve(24);
cube.push_back({ { -1.0f, -1.0f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 0.0f, 0.0f } });
cube.push_back({ { 1.0f, -1.0f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 1.0f, 0.0f } });
cube.push_back({ { 1.0f, 1.0f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 1.0f, 1.0f } });
cube.push_back({ { -1.0f, 1.0f, -1.0f }, { 0.0f, 0.0f, -1.0f }, { 0.0f, 1.0f } });
cube.push_back({ { 1.0f, -1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 0.0f, 0.0f } });
cube.push_back({ { -1.0f, -1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f } });
cube.push_back({ { -1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f } });
cube.push_back({ { 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f } });
cube.push_back({ { -1.0f, -1.0f, 1.0f }, { -1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f } });
cube.push_back({ { -1.0f, -1.0f, -1.0f }, { -1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f } });
cube.push_back({ { -1.0f, 1.0f, -1.0f }, { -1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f } });
cube.push_back({ { -1.0f, 1.0f, 1.0f }, { -1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f } });
cube.push_back({ { 1.0f, -1.0f, -1.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f } });
cube.push_back({ { 1.0f, -1.0f, 1.0f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f } });
cube.push_back({ { 1.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f } });
cube.push_back({ { 1.0f, 1.0f, -1.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f } });
cube.push_back({ { -1.0f, -1.0f, -1.0f }, { 0.0f, -1.0f, 0.0f }, { 0.0f, 0.0f } });
cube.push_back({ { -1.0f, -1.0f, 1.0f }, { 0.0f, -1.0f, 0.0f }, { 1.0f, 0.0f } });
cube.push_back({ { 1.0f, -1.0f, 1.0f }, { 0.0f, -1.0f, 0.0f }, { 1.0f, 1.0f } });
cube.push_back({ { 1.0f, -1.0f, -1.0f }, { 0.0f, -1.0f, 0.0f }, { 0.0f, 1.0f } });
cube.push_back({ { -1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } });
cube.push_back({ { -1.0f, 1.0f, -1.0f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 0.0f } });
cube.push_back({ { 1.0f, 1.0f, -1.0f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f } });
cube.push_back({ { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } });
return cube;
}
static inline std::vector<uint32> CubeIndices()
{
return std::vector<uint32>
{
0, 2, 1, 0, 3, 2,
4, 6, 5, 4, 7, 6,
8, 10, 9, 8, 11, 10,
12, 14, 13, 12, 15, 14,
16, 18, 17, 16, 19, 18,
20, 22, 21, 20, 23, 22
};
}
static inline std::vector<Vertex> QuadVertices()
{
std::vector<Vertex> quad;
quad.reserve(4);
quad.push_back({ { -1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } });
quad.push_back({ { -1.0f, 0.0f, -1.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } });
quad.push_back({ { 1.0f, 0.0f, -1.0f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f } });
quad.push_back({ { 1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 0.0f } });
return quad;
}
static inline std::vector<uint32> QuadIndices()
{
return std::vector<uint32>{ 0, 2, 1, 0, 3, 2 };
}
};
struct UIvertex
{
math::vector3 position{};
math::vector2 texCoord{};
};
static_assert(std::is_standard_layout_v<UIvertex>);
static_assert(std::is_trivially_copyable_v<UIvertex>);
static_assert(sizeof(UIvertex) == 20u);
static_assert(offsetof(UIvertex, position) == 0u);
static_assert(offsetof(UIvertex, texCoord) == 12u);
class UIMesh
{
public:
UIMesh();
~UIMesh();
const std::string& GetName() { return m_name; }
private:
friend class ModelLoader;
std::string m_name;
std::vector<UIvertex> m_vertices;
std::vector<uint32> m_indices;
uint32 m_materialIndex{};
std::string m_nodeName;
// UIMesh는 아직 bounds를 계산하거나 직렬화하지 않는다. 미계산 AABB는
// Mathematics의 empty sentinel로, sphere는 radius 0으로 명시한다.
math::aabb m_boundingBox{};
math::sphere m_boundingSphere{};
static constexpr uint32 m_stride = sizeof(UIvertex);
std::vector<UIvertex> UIQuad
{
{ {-1.0f, 1.0f, 0.0f}, { 0.0f, 0.0f} }, // 좌상단
{ { 1.0f, 1.0f, 0.0f}, { 1.0f, 0.0f} }, // 우상단
{ { 1.0f, -1.0f, 0.0f}, { 1.0f, 1.0f} }, // 우하단
{ {-1.0f, -1.0f, 0.0f}, { 0.0f, 1.0f} }, // 좌하단
};
std::vector<uint32> UIIndices = { 0, 1, 2, 0, 2, 3 };
};