-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
162 lines (141 loc) · 4.89 KB
/
Copy pathModel.cpp
File metadata and controls
162 lines (141 loc) · 4.89 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
#include "Globals.h"
#include "Model.h"
#include "gltf_utils.h"
#include <filesystem>
void Model::Load(const char* assetFileName)
{
tinygltf::TinyGLTF gltfContext;
tinygltf::Model model;
std::string error, warning;
bool loadOk = gltfContext.LoadASCIIFromFile(&model, &error, &warning, assetFileName);
if (loadOk)
{
std::filesystem::path path(assetFileName);
std::string basePath = path.parent_path().string() + "/";
// Load materials first
for (const auto& gltfMaterial : model.materials)
{
Material material;
material.Load(model, gltfMaterial, basePath.c_str());
materials.push_back(material);
}
// If no materials, create a default one
if (materials.empty())
{
Material defaultMat;
defaultMat.baseColor = DirectX::XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
materials.push_back(defaultMat);
}
// Load meshes
for (const auto& gltfMesh : model.meshes)
{
for (const auto& primitive : gltfMesh.primitives)
{
Mesh mesh;
mesh.Load(model, primitive);
meshes.push_back(mesh);
}
}
LOG("Loaded model %s: %zu meshes, %zu materials",
assetFileName, meshes.size(), materials.size());
}
else
{
LOG("Error loading %s: %s", assetFileName, error.c_str());
}
}
void Model::Render(ID3D12GraphicsCommandList* commandList, D3D12_GPU_VIRTUAL_ADDRESS materialBufferAddress) const
{
// Checking
if (!commandList) {
LOG("ERROR: Model::Render called with null command list!");
return;
}
if (meshes.empty()) {
LOG("WARNING: Model::Render called with empty meshes!");
return;
}
// Try-catch to handle invalid command list state
__try {
commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
}
__except (EXCEPTION_EXECUTE_HANDLER) {
LOG("ERROR: Command list is in invalid state (probably closed or corrupted)");
return;
}
for (const auto& mesh : meshes)
{
//Debug Print
char debugBuf[256];
sprintf_s(debugBuf, "Rendering Mesh: Verts %u, Indices %u, MatIdx %u\n",
mesh.getVertexCount(), mesh.getIndexCount(), mesh.getMaterialIndex());
OutputDebugStringA(debugBuf);
// Material Binding
UINT matIdx = mesh.getMaterialIndex();
if (matIdx < materials.size())
{
const Material& mat = materials[matIdx];
// Bind material constant buffer
__try {
commandList->SetGraphicsRootConstantBufferView(1, materialBufferAddress);
}
__except (EXCEPTION_EXECUTE_HANDLER) {
LOG("ERROR: Failed to set root constant buffer view");
return;
}
// BIND TEXTURE if it exists
if (mat.texture != nullptr && mat.srvIndex != 0)
{
LOG("Model::Render: Binding texture SRV index %u", mat.srvIndex);
__try {
// Get SRV handle
D3D12_GPU_DESCRIPTOR_HANDLE srvHandle = mat.getSRVHandle();
// Check if handle is valid
if (srvHandle.ptr != 0) {
commandList->SetGraphicsRootDescriptorTable(2, srvHandle);
LOG("Model::Render: Texture bound successfully");
}
else {
LOG("WARNING: Model::Render: Invalid SRV handle, skipping texture");
}
}
__except (EXCEPTION_EXECUTE_HANDLER) {
LOG("ERROR: Failed to set descriptor table for texture");
// Continue without texture
}
}
else
{
LOG("Model::Render: No texture to bind (texture=%p, srvIndex=%u)",
mat.texture.Get(), mat.srvIndex);
}
}
else
{
LOG("WARNING: Material index %u out of bounds (max %zu)", matIdx, materials.size());
}
// Bind Buffers with error handling
__try {
mesh.BindBuffers(commandList);
}
__except (EXCEPTION_EXECUTE_HANDLER) {
LOG("ERROR: Failed to bind mesh buffers");
return;
}
// Draw with error handling
__try {
if (mesh.getIndexCount() > 0) // Check if indices exist
{
commandList->DrawIndexedInstanced(mesh.getIndexCount(), 1, 0, 0, 0);
}
else
{
commandList->DrawInstanced(mesh.getVertexCount(), 1, 0, 0);
}
}
__except (EXCEPTION_EXECUTE_HANDLER) {
LOG("ERROR: Failed to draw mesh");
return;
}
}
}