<p>Problem fixed by RichieSams, trichoplax and xpicox. Thanks all of you for the answers. </p>

<p>I lower the roughness, change the color of material and reversed the ViewDirection then finally I start to see proper specular :).</p>

<p>Fixed Code:</p>

<pre><code>#version 330 core

in vec3 Position;
in vec2 TexCoord0;
in vec3 Normal;
in vec3 Tangent;
out vec4 FinalColor;

uniform vec3 uCameraPosition;

#define PI 3.1415926f
#define EPSILON 10e-5f
#define saturate(value) clamp(value, 0.0f, 1.0f);

float BRDF_Lambert(float NdotL)
{
    return NdotL;
}

// Got these BRDF formulas Moving Frostbite to PBR slide by Sebastien Lagarde &amp; Charles de Rousiers 
// http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr_v2.pdf
float BRDF_D_GGX(float NdotH, float Roughness)
{
    float Roughness2 = Roughness * Roughness;
    float f = (NdotH * Roughness2 - NdotH) * NdotH + 1.0f;
    return Roughness2 / (f * f + EPSILON);
}


float BRDF_F_FresnelSchlick(float LdotH, float F0)
{
    float f = F0 + (1.0f - F0) * pow((1.0f - LdotH), 5);
    return f;
}

float BRDF_G_SmithGGXCorrelated(float NdotL, float NdotV, float Roughness)
{
    float Roughness2 = Roughness * Roughness;
    float GV = NdotL * sqrt((-NdotV * Roughness2 + NdotV) * NdotV + Roughness2);
    float GL = NdotV * sqrt((-NdotL * Roughness2 + NdotL) * NdotL + Roughness2);

    return 0.5f / (GV + GL + EPSILON);
}

float BRDF_Specular(float NdotV, float NdotL, float NdotH, float LdotH, float Roughness, float F0)
{
    float D = BRDF_D_GGX(NdotH, Roughness);
    float F = BRDF_F_FresnelSchlick(LdotH, F0);
    float G = BRDF_G_SmithGGXCorrelated(NdotL, NdotV, Roughness);
    return (D * F * G) / PI;
}

void main()
{
    vec3 normal = normalize(Normal);

    vec4 BaseColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
    vec4 SpecularColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);

    vec3 LightDirection = normalize(vec3(0, 4, 4) - Position);
    vec3 ViewDirection = normalize(uCameraPosition - Position);
    vec3 HalfVector = normalize(ViewDirection + LightDirection);
    float Roughness = 0.04f;

    float RefractiveIndex = 0.24f; // RI for Gold materials. I got this from http://refractiveindex.info/
    float F0 = pow(((1.0f - RefractiveIndex) / (1.0f + RefractiveIndex)), 2);

    float NdotL = saturate(dot(LightDirection, normal));
    float NdotV = abs(dot(ViewDirection, normal)) + EPSILON; // Avoid artifact - Ref: SIGGRAPH14 - Moving Frosbite to PBR
    float LdotH = saturate(dot(LightDirection, HalfVector));
    float NdotH = saturate(dot(normal, HalfVector));

    float DiffuseFactor = BRDF_Lambert(NdotL);
    float SpecularFactor = 0.0f;
    if(DiffuseFactor &gt; 0.0f)
    {
        SpecularFactor = BRDF_Specular(NdotV, NdotL, NdotH, LdotH, Roughness, F0);
    }
    FinalColor = BaseColor * DiffuseFactor + SpecularColor * SpecularFactor;
}
</code></pre>

<p>Final Image:</p>

<p><a href="https://i.sstatic.net/j7Ft6.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/j7Ft6.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2015-08-11T07:53:30.297Z
2015-08-11T07:53:30.297Z
hmkum
<p>I am trying to implement microfacet BRDF model. I am reading <a href="http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr_v2.pdf" rel="noreferrer">Sebastien Lagarde's slides</a>. I implemented formulas to my code but i think result image is wrong. </p>

<p>Yellow is base color of material. Specular color is red to see properly.</p>

<p>My code:</p>

<pre><code>// Fragment Shader
#version 330 core

in vec3 Position;
in vec2 TexCoord0;
in vec3 Normal;
in vec3 Tangent;
out vec4 FinalColor;

uniform vec3 uCameraPosition; // init value: vec3(0, 0, 5)

#define PI 3.1415926f
#define EPSILON 10e-5f
#define saturate(value) clamp(value, 0.0f, 1.0f);

float BRDF_Lambert(float NdotL)
{
    return NdotL;
}

// Got these BRDF formulas Moving Frostbite to PBR slide by Sebastien Lagarde &amp; Charles de Rousiers 
// http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr_v2.pdf
float BRDF_D_GGX(float NdotH, float Roughness)
{
    float Roughness2 = Roughness * Roughness;
    float f = (NdotH * Roughness2 - NdotH) * NdotH + 1.0f;
    return Roughness2 / (f * f + EPSILON);
}


float BRDF_F_FresnelSchlick(float LdotH, float F0)
{
    float f = F0 + (1.0f - F0) * pow((1.0f - LdotH), 5);
    return f;
}

float BRDF_G_SmithGGXCorrelated(float NdotL, float NdotV, float Roughness)
{
    float Roughness2 = Roughness * Roughness;
    float GV = NdotL * sqrt((-NdotV * Roughness2 + NdotV) * NdotV + Roughness2);
    float GL = NdotV * sqrt((-NdotL * Roughness2 + NdotL) * NdotL + Roughness2);

    return 0.5f / (GV + GL + EPSILON);
}

float BRDF_Specular(float NdotV, float NdotL, float NdotH, float LdotH, float Roughness, float F0)
{
    float D = BRDF_D_GGX(NdotH, Roughness);
    float F = BRDF_F_FresnelSchlick(LdotH, F0);
    float G = BRDF_G_SmithGGXCorrelated(NdotL, NdotV, Roughness);
    return (D * F * G) / PI;
}

void main()
{
    FinalColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
    vec4 BaseColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);
    vec4 SpecularColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
    vec3 LightDirection = normalize(vec3(0, 4, 4));
    vec3 ViewDirection = normalize(Position - uCameraPosition);
    vec3 HalfVector = normalize(ViewDirection + LightDirection);
    float Roughness = 0.9f; // [0.04 - 0.1f] -&gt; Dielectric, [0.7, 1.0f] -&gt; Metallic

    float RefractiveIndex = 0.27049f; // RI for Gold materials. I got this from http://refractiveindex.info/
    float F0 = pow(((1.0f - RefractiveIndex) / (1.0f + RefractiveIndex)), 2);

    float NdotL = saturate(dot(LightDirection, Normal));
    float NdotV = abs(dot(ViewDirection, Normal)) + EPSILON; // Avoid artifact - Ref: SIGGRAPH14 - Moving Frosbite to PBR
    float LdotH = saturate(dot(LightDirection, HalfVector));
    float NdotH = saturate(dot(Normal, HalfVector));

    float DiffuseFactor = BRDF_Lambert(NdotL);
    float SpecularFactor = BRDF_Specular(NdotV, NdotL, NdotH, LdotH, Roughness, F0);

    FinalColor = BaseColor * DiffuseFactor + SpecularColor * SpecularFactor;
}
</code></pre>

<p><a href="https://i.sstatic.net/vwRZ8.png" rel="noreferrer"><img src="https://i.sstatic.net/vwRZ8.png" alt="Wrong result"></a></p>

<p><strong>EDIT</strong></p>

<p>Roughness = 0.2f;
<a href="https://i.sstatic.net/89aAW.png" rel="noreferrer"><img src="https://i.sstatic.net/89aAW.png" alt="enter image description here"></a></p>

<p>Roughness = 0.04f;
<a href="https://i.sstatic.net/SlEtb.png" rel="noreferrer"><img src="https://i.sstatic.net/SlEtb.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2015-08-09T18:49:36.253Z
I haven't looked in too much detail at the code, but the image seems to be alright. The Fresnel effect shows up as a red ring. With the roughness so high (0.9), it makes sense that the rest of the image is mostly yellow (ie. mostly diffuse). If you lower the roughness, you may get a red specular highlight
RichieSams
CC BY-SA 3.0
2015-08-09T20:24:29.623Z
@RichieSams I added new images for different roughness values but can't see red shiny specular highlight yet.
hmkum
CC BY-SA 3.0
2015-08-10T01:26:42.160Z
Your 2nd and 3rd images do appear to have less red in general (in the yellow diffuse area) than your original image. This isn't very apparent because adding a little red to a yellow area leaves it a similar colour (orange-yellow rather than yellow). Do you see any more detail of the red distribution if you reduce the yellow significantly? Omitting the yellow altogether may help in identifying what is going wrong.
trichoplax is on Codidact now
CC BY-SA 3.0
2015-08-10T08:32:09.737Z
@trichoplax i reduced the yellow but again no way to see red specular. I just see red ring(fresnel) effect. Doesnt matter what i set value for roughness I cant see specular effect that focused a point.
hmkum
CC BY-SA 3.0
2015-08-10T11:35:59.267Z
First normalise the Normal vector before using it and second the viewDirection is the outgoing vector from the Position to the camera: uCameraPosition - Position.
xpicox
CC BY-SA 3.0
2015-08-10T20:05:33.543Z
Thanks all of you(RichieSams, trichoplax and xpicox ) for the answers. I lower the roughness, change the color of material and reversed the ViewDirection then i start to see shiny red specular :). Actually i was using right ViewDirection but i could not see the specular because of the color and roughness value. All answers help to fix my problem.
hmkum
CC BY-SA 3.0
2015-08-11T01:28:24.907Z
If the various different suggestions came together into a solution, you could write them up and post it as a self-answer. That's generally encouraged. Then you can include the working final image too...
trichoplax is on Codidact now
<rendering><glsl><shading><specular>
Trying to implement Microfacet BRDF but my result images are wrong
hmkum
# Trying to implement Microfacet BRDF but my result images are wrong

(Asked by: username_0 on 2015-08-09)

I am trying to implement microfacet BRDF model. I am reading [Sebastien Lagarde's slides](http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr_v2.pdf). I implemented formulas to my code but i think result image is wrong.

Yellow is base color of material. Specular color is red to see properly.

My code:

```
// Fragment Shader
#version 330 core

in vec3 Position;
in vec2 TexCoord0;
in vec3 Normal;
in vec3 Tangent;
out vec4 FinalColor;

uniform vec3 uCameraPosition; // init value: vec3(0, 0, 5)

#define PI 3.1415926f
#define EPSILON 10e-5f
#define saturate(value) clamp(value, 0.0f, 1.0f);

float BRDF_Lambert(float NdotL)
{
    return NdotL;
}

// Got these BRDF formulas Moving Frostbite to PBR slide by Sebastien Lagarde & Charles de Rousiers 
// http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr_v2.pdf
float BRDF_D_GGX(float NdotH, float Roughness)
{
    float Roughness2 = Roughness * Roughness;
    float f = (NdotH * Roughness2 - NdotH) * NdotH + 1.0f;
    return Roughness2 / (f * f + EPSILON);
}

float BRDF_F_FresnelSchlick(float LdotH, float F0)
{
    float f = F0 + (1.0f - F0) * pow((1.0f - LdotH), 5);
    return f;
}

float BRDF_G_SmithGGXCorrelated(float NdotL, float NdotV, float Roughness)
{
    float Roughness2 = Roughness * Roughness;
    float GV = NdotL * sqrt((-NdotV * Roughness2 + NdotV) * NdotV + Roughness2);
    float GL = NdotV * sqrt((-NdotL * Roughness2 + NdotL) * NdotL + Roughness2);

    return 0.5f / (GV + GL + EPSILON);
}

float BRDF_Specular(float NdotV, float NdotL, float NdotH, float LdotH, float Roughness, float F0)
{
    float D = BRDF_D_GGX(NdotH, Roughness);
    float F = BRDF_F_FresnelSchlick(LdotH, F0);
    float G = BRDF_G_SmithGGXCorrelated(NdotL, NdotV, Roughness);
    return (D * F * G) / PI;
}

void main()
{
    FinalColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
    vec4 BaseColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);
    vec4 SpecularColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
    vec3 LightDirection = normalize(vec3(0, 4, 4));
    vec3 ViewDirection = normalize(Position - uCameraPosition);
    vec3 HalfVector = normalize(ViewDirection + LightDirection);
    float Roughness = 0.9f; // [0.04 - 0.1f] -> Dielectric, [0.7, 1.0f] -> Metallic

    float RefractiveIndex = 0.27049f; // RI for Gold materials. I got this from http://refractiveindex.info/
    float F0 = pow(((1.0f - RefractiveIndex) / (1.0f + RefractiveIndex)), 2);

    float NdotL = saturate(dot(LightDirection, Normal));
    float NdotV = abs(dot(ViewDirection, Normal)) + EPSILON; // Avoid artifact - Ref: SIGGRAPH14 - Moving Frosbite to PBR
    float LdotH = saturate(dot(LightDirection, HalfVector));
    float NdotH = saturate(dot(Normal, HalfVector));

    float DiffuseFactor = BRDF_Lambert(NdotL);
    float SpecularFactor = BRDF_Specular(NdotV, NdotL, NdotH, LdotH, Roughness, F0);

    FinalColor = BaseColor * DiffuseFactor + SpecularColor * SpecularFactor;
}

```

[![Wrong result](https://i.sstatic.net/vwRZ8.png)](https://i.sstatic.net/vwRZ8.png)

**EDIT**

Roughness = 0.2f;
[![enter image description here](https://i.sstatic.net/89aAW.png)](https://i.sstatic.net/89aAW.png)

Roughness = 0.04f;
[![enter image description here](https://i.sstatic.net/SlEtb.png)](https://i.sstatic.net/SlEtb.png)

**Tags:** rendering, glsl, shading, specular

> **Comments:**
> **username_1**: I haven't looked in too much detail at the code, but the image seems to be alright. The Fresnel effect shows up as a red ring. With the roughness so high (0.9), it makes sense that the rest of the image is mostly yellow (ie. mostly diffuse). If you lower the roughness, you may get a red specular highlight  
> **username_0**: @username_1 I added new images for different roughness values but can't see red shiny specular highlight yet.  
> **username_2**: Your 2nd and 3rd images do appear to have less red in general (in the yellow diffuse area) than your original image. This isn't very apparent because adding a little red to a yellow area leaves it a similar colour (orange-yellow rather than yellow). Do you see any more detail of the red distribution if you reduce the yellow significantly? Omitting the yellow altogether may help in identifying what is going wrong.  
> **username_0**: @username_2 i reduced the yellow but again no way to see red specular. I just see red ring(fresnel) effect. Doesnt matter what i set value for roughness I cant see specular effect that focused a point.  
> **username_3**: First normalise the Normal vector before using it and second the viewDirection is the outgoing vector from the Position to the camera: uCameraPosition - Position.  
> **username_0**: Thanks all of you(username_1, username_2 and username_3 ) for the answers. I lower the roughness, change the color of material and reversed the ViewDirection then i start to see shiny red specular :). Actually i was using right ViewDirection but i could not see the specular because of the color and roughness value. All answers help to fix my problem.  
> **username_2**: If the various different suggestions came together into a solution, you could write them up and post it as a self-answer. That's generally encouraged. Then you can include the working final image too...  

---

## 1 answers

### Answer by username_0 on 2015-08-11. Score: 3 | Accepted answer

Problem fixed by username_1, username_2 and username_3. Thanks all of you for the answers.

I lower the roughness, change the color of material and reversed the ViewDirection then finally I start to see proper specular :).

Fixed Code:

```
#version 330 core

in vec3 Position;
in vec2 TexCoord0;
in vec3 Normal;
in vec3 Tangent;
out vec4 FinalColor;

uniform vec3 uCameraPosition;

#define PI 3.1415926f
#define EPSILON 10e-5f
#define saturate(value) clamp(value, 0.0f, 1.0f);

float BRDF_Lambert(float NdotL)
{
    return NdotL;
}

// Got these BRDF formulas Moving Frostbite to PBR slide by Sebastien Lagarde & Charles de Rousiers 
// http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr_v2.pdf
float BRDF_D_GGX(float NdotH, float Roughness)
{
    float Roughness2 = Roughness * Roughness;
    float f = (NdotH * Roughness2 - NdotH) * NdotH + 1.0f;
    return Roughness2 / (f * f + EPSILON);
}

float BRDF_F_FresnelSchlick(float LdotH, float F0)
{
    float f = F0 + (1.0f - F0) * pow((1.0f - LdotH), 5);
    return f;
}

float BRDF_G_SmithGGXCorrelated(float NdotL, float NdotV, float Roughness)
{
    float Roughness2 = Roughness * Roughness;
    float GV = NdotL * sqrt((-NdotV * Roughness2 + NdotV) * NdotV + Roughness2);
    float GL = NdotV * sqrt((-NdotL * Roughness2 + NdotL) * NdotL + Roughness2);

    return 0.5f / (GV + GL + EPSILON);
}

float BRDF_Specular(float NdotV, float NdotL, float NdotH, float LdotH, float Roughness, float F0)
{
    float D = BRDF_D_GGX(NdotH, Roughness);
    float F = BRDF_F_FresnelSchlick(LdotH, F0);
    float G = BRDF_G_SmithGGXCorrelated(NdotL, NdotV, Roughness);
    return (D * F * G) / PI;
}

void main()
{
    vec3 normal = normalize(Normal);

    vec4 BaseColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
    vec4 SpecularColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);

    vec3 LightDirection = normalize(vec3(0, 4, 4) - Position);
    vec3 ViewDirection = normalize(uCameraPosition - Position);
    vec3 HalfVector = normalize(ViewDirection + LightDirection);
    float Roughness = 0.04f;

    float RefractiveIndex = 0.24f; // RI for Gold materials. I got this from http://refractiveindex.info/
    float F0 = pow(((1.0f - RefractiveIndex) / (1.0f + RefractiveIndex)), 2);

    float NdotL = saturate(dot(LightDirection, normal));
    float NdotV = abs(dot(ViewDirection, normal)) + EPSILON; // Avoid artifact - Ref: SIGGRAPH14 - Moving Frosbite to PBR
    float LdotH = saturate(dot(LightDirection, HalfVector));
    float NdotH = saturate(dot(normal, HalfVector));

    float DiffuseFactor = BRDF_Lambert(NdotL);
    float SpecularFactor = 0.0f;
    if(DiffuseFactor > 0.0f)
    {
        SpecularFactor = BRDF_Specular(NdotV, NdotL, NdotH, LdotH, Roughness, F0);
    }
    FinalColor = BaseColor * DiffuseFactor + SpecularColor * SpecularFactor;
}

```

Final Image:

[![enter image description here](https://i.sstatic.net/j7Ft6.png)](https://i.sstatic.net/j7Ft6.png)


---

