-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterial.cpp
More file actions
152 lines (133 loc) · 4.64 KB
/
Copy pathMaterial.cpp
File metadata and controls
152 lines (133 loc) · 4.64 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
#include "Globals.h"
#include "Material.h"
#include "Application.h"
#include "ModuleResource.h"
#include "gltf_utils.h"
#include <algorithm>
#include <cmath>
void Material::Load(const tinygltf::Model& model, const class tinygltf::Material& gltfMaterial, const char* basePath)
{
ModuleResource* resources = app->getResources();
ModuleShaderDescriptors* shaderDescriptors = app->getShaderDescriptors();
if (!shaderDescriptors) {
LOG("ERROR: Material::Load: shaderDescriptors is null!");
return;
}
// LOAD BASE COLOR
if (gltfMaterial.pbrMetallicRoughness.baseColorFactor.size() >= 4)
{
baseColor = DirectX::XMFLOAT4(
(float)gltfMaterial.pbrMetallicRoughness.baseColorFactor[0],
(float)gltfMaterial.pbrMetallicRoughness.baseColorFactor[1],
(float)gltfMaterial.pbrMetallicRoughness.baseColorFactor[2],
(float)gltfMaterial.pbrMetallicRoughness.baseColorFactor[3]
);
}
else
{
// Default color
baseColor = DirectX::XMFLOAT4(1.0f, 1.0f, 0.0f, 1.0f);
LOG("COULDN'T SHOW TEXTURE BUT HERE IS YELLOWWWW");
}
// LOAD PBR PROPERTIES from glTF
if (gltfMaterial.pbrMetallicRoughness.metallicFactor >= 0)
{
metallic = (float)gltfMaterial.pbrMetallicRoughness.metallicFactor;
LOG("Loaded metallic from glTF: %.2f", metallic);
}
else
{
metallic = 0.0f;
LOG("No metallic in glTF, using default: 0.0");
}
if (gltfMaterial.pbrMetallicRoughness.roughnessFactor >= 0)
{
roughness = (float)gltfMaterial.pbrMetallicRoughness.roughnessFactor;
LOG("Loaded roughness from glTF: %.2f", roughness);
}
else
{
roughness = 0.7f;
LOG("No roughness in glTF, duck is rubber/plastic so using: %.2f", roughness);
}
// Set Kd and Ks based on metallicness
if (metallic > 0.5f)
{
Kd = 0.1f;
Ks = 0.9f;
}
else
{
Kd = 0.8f;
Ks = 0.5f;
}
shininess = exp2(10.0f * (1.0f - roughness));
shininess = std::max(1.0f, std::min(256.0f, shininess));
LOG("Duck material (corrected for non-metal):");
LOG(" metallic: %.2f (non-metallic)", metallic);
LOG(" roughness: %.2f", roughness);
LOG(" Kd: %.2f (diffuse)", Kd);
LOG(" Ks: %.2f (specular - THIS WAS 0.0 BEFORE!)", Ks);
LOG(" shininess: %.2f", shininess);
// LOAD BASE COLOR TEXTURE
if (gltfMaterial.pbrMetallicRoughness.baseColorTexture.index >= 0)
{
const tinygltf::Texture& tex = model.textures[gltfMaterial.pbrMetallicRoughness.baseColorTexture.index];
if (tex.source >= 0 && tex.source < model.images.size())
{
const tinygltf::Image& image = model.images[tex.source];
if (!image.uri.empty())
{
// Build full texture path
std::string texturePath = std::string(basePath) + image.uri;
// Create texture from file
texture = resources->createTextureFromFile(texturePath, false);
if (texture)
{
LOG("Texture loaded, creating SRV...");
// CREATE SRV DESCRIPTOR for the texture
srvIndex = shaderDescriptors->createSRV(texture.Get());
LOG("SRV created at index: %u", srvIndex);
if (srvIndex == 0) {
LOG("ERROR: Failed to create SRV for texture!");
}
}
else
{
LOG("ERROR: Failed to load texture: %s", texturePath.c_str());
srvIndex = 0;
}
}
}
}
else
{
LOG("WARNING: No texture found for material");
texture = nullptr;
srvIndex = 0; // Will use null descriptor
}
}
D3D12_GPU_DESCRIPTOR_HANDLE Material::getSRVHandle() const
{
if (!texture) {
LOG("DEBUG: No texture, returning null handle");
return { 0 };
}
if (srvIndex == 0) {
LOG("DEBUG: SRV index is 0, returning null handle");
return { 0 };
}
ModuleShaderDescriptors* shaderDescriptors = app->getShaderDescriptors();
if (!shaderDescriptors) {
LOG("ERROR: getSRVHandle: shaderDescriptors is null");
return { 0 };
}
D3D12_GPU_DESCRIPTOR_HANDLE handle = shaderDescriptors->getSRVGPUHandle(srvIndex);
if (handle.ptr == 0) {
LOG("ERROR: getSRVHandle: Got null handle from shaderDescriptors for index %u", srvIndex);
}
else {
LOG("DEBUG: getSRVHandle: Returning valid handle %llu for index %u", handle.ptr, srvIndex);
}
return handle;
}