Skip to content

Commit c1d7eef

Browse files
committed
fixed ggx shading
1 parent 48b9954 commit c1d7eef

2 files changed

Lines changed: 5 additions & 9 deletions

File tree

src/engine/Material.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ void Material::load(const cJSON *json)
1111
this->data.albedo = glm::vec3(cJSON_GetArrayItem(diffuse, 0)->valuedouble, cJSON_GetArrayItem(diffuse, 1)->valuedouble, cJSON_GetArrayItem(diffuse, 2)->valuedouble);
1212
this->data.metalness = (float)cJSON_GetObjectItem(json, "metalness")->valuedouble;
1313
this->data.roughness = (float)cJSON_GetObjectItem(json, "roughness")->valuedouble;
14-
15-
// normalize diffuse BRDF
16-
this->data.albedo /= 3.1415926535f;
1714
}
1815

1916
void Material::unload()

src/engine/shaders/basic.ps.hlsl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ PS_OUTPUT main(BASIC_PS_INPUT input)
2424
float3 blinn_phong = diffuse * d + s;*/
2525

2626
// blend between dielectric and metal
27-
const float f0 = 0.03;
28-
float3 specularColor = lerp(float3(1.0, 1.0, 1.0), albedo, metalness);
27+
float3 specularColor = metalness * lerp(metalness.xxx, albedo, metalness);
2928
float3 finalAlbedo = albedo * (1.0 - metalness);
3029

3130
// precompute all cosines
@@ -38,7 +37,7 @@ PS_OUTPUT main(BASIC_PS_INPUT input)
3837
float3 diffuse = dotNL * finalAlbedo;
3938

4039
// schlick fresnel approximation
41-
float fresnel = f0 + (1.0 - f0) * pow(1.0 - dotNV, 5.0);
40+
float3 fresnel = specularColor + (1.0 - specularColor) * pow(1.0 - dotNV, 5.0);
4241

4342
float alpha = roughness * roughness;
4443
float alphaSquared = alpha * alpha;
@@ -49,12 +48,12 @@ PS_OUTPUT main(BASIC_PS_INPUT input)
4948

5049
// schlick approximation for geometry factor
5150
float k = alpha * 0.5;
52-
float geometryFactor = g1v(dotNL, k) * g1v(dotNV, k);
51+
float visibility = g1v(dotNL, k) * g1v(dotNV, k);
5352

5453
// cook-torrance microfacet model
55-
float3 specular = specularColor * dotNL * fresnel * normalDistribution * geometryFactor;
54+
float3 specular = fresnel * normalDistribution * visibility;
5655

57-
output.color = float4((diffuse + specular) * 10.0, 1.0);
56+
output.color = float4(diffuse + specular, 1.0);
5857

5958
return output;
6059
}

0 commit comments

Comments
 (0)