-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
136 lines (115 loc) · 3.83 KB
/
Copy pathMesh.cpp
File metadata and controls
136 lines (115 loc) · 3.83 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
#include "Globals.h"
#include "Mesh.h"
#include "Application.h"
#include "ModuleResource.h"
#include "gltf_utils.h"
void Mesh::Load(const tinygltf::Model& model, const tinygltf::Primitive& primitive)
{
ModuleResource* resources = app->getResources();
const auto& posIt = primitive.attributes.find("POSITION");
if (posIt == primitive.attributes.end())
return;
// Get vertex count from position accessor
vertexCount = UINT(model.accessors[posIt->second].count);
// Allocate temporary vertex array
std::unique_ptr<Vertex[]> vertices = std::make_unique<Vertex[]>(vertexCount);
uint8_t* vertexData = reinterpret_cast<uint8_t*>(vertices.get());
// Load positions
if (!loadAccessorData(vertexData + offsetof(Vertex, position),
sizeof(DirectX::XMFLOAT3),
sizeof(Vertex),
vertexCount,
model,
posIt->second))
{
LOG("Failed to load vertex positions");
return;
}
// Load normals
loadAccessorData(vertexData + offsetof(Vertex, normal),
sizeof(DirectX::XMFLOAT3),
sizeof(Vertex),
vertexCount,
model,
primitive.attributes,
"NORMAL");
// Load texture coordinates
loadAccessorData(vertexData + offsetof(Vertex, texCoord0),
sizeof(DirectX::XMFLOAT2),
sizeof(Vertex),
vertexCount,
model,
primitive.attributes,
"TEXCOORD_0");
vertexBuffer = resources->createDefaultBuffer(
vertices.get(),
vertexCount * sizeof(Vertex),
"MeshVB");
if (!vertexBuffer)
{
LOG("Failed to create vertex buffer");
return;
}
//SETUP VERTEX BUFFER VIEW
vertexBufferView.BufferLocation = vertexBuffer->GetGPUVirtualAddress();
vertexBufferView.StrideInBytes = sizeof(Vertex);
vertexBufferView.SizeInBytes = vertexCount * sizeof(Vertex);
//LOAD INDEX DATA
if (primitive.indices >= 0)
{
// Determine index type
const ::tinygltf::Accessor& indexAccessor = model.accessors[primitive.indices];
indexCount = UINT(indexAccessor.count);
switch (indexAccessor.componentType)
{
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
indexFormat = DXGI_FORMAT_R8_UINT;
break;
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
indexFormat = DXGI_FORMAT_R16_UINT;
break;
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
indexFormat = DXGI_FORMAT_R32_UINT;
break;
default:
LOG("Unsupported index format");
return;
}
// Calculate element size
size_t elementSize = tinygltf::GetComponentSizeInBytes(indexAccessor.componentType);
// Allocate temporary index buffer
std::unique_ptr<uint8_t[]> indices = std::make_unique<uint8_t[]>(indexCount * elementSize);
// Load index data using your utility function
if (!loadAccessorData(indices.get(),
elementSize,
elementSize,
indexCount,
model,
primitive.indices))
{
LOG("Failed to load index data");
return;
}
//CREATE INDEX BUFFER
indexBuffer = resources->createDefaultBuffer(
indices.get(),
indexCount * elementSize,
"MeshIB");
if (!indexBuffer)
{
LOG("Failed to create index buffer");
return;
}
//SETUP INDEX BUFFER VIEW
indexBufferView.BufferLocation = indexBuffer->GetGPUVirtualAddress();
indexBufferView.Format = indexFormat;
indexBufferView.SizeInBytes = indexCount * elementSize;
hasIndices = true;
}
else
{
hasIndices = false;
}
//STORE MATERIAL INDEX
materialIndex = (primitive.material >= 0) ? UINT(primitive.material) : 0;
}