GGX shader creation

2019-07-25 10:04发布

问题:

I'm trying to create a ggx shader but when I run it at BRDF Explorer the main page remains white. I'm trying to find my error but I can't. Can anybody help me? below is my file

analytic

::begin parameters

float baseColor 0.001 .2 .1

float m 0.001 0.1 1.0 

::end parameters

::begin shader

    const float PI = 3.14;


    vec3 BRDF( vec3 N, vec3 V, vec3 L, float m, float baseColor)
        {

            vec3 H = normalize(V+L);

            float dotNL = clamp(dot(N,L), 0.0, 1.0);
            float dotLH = clamp(dot(L,H), 0.0, 1.0);
            float dotNH = clamp(dot(N,H), 0.0, 1.0);

            float DotNH2 = dotNH * dotNH;
            float m2 = m * m;
            float D =  m2 / (PI * pow(dotNH * dotNH * (m2 - 1) + 1, 2.0));


            float Fresnel(float baseColor, float l, float h)
        {
            return baseColor + ( 1 - baseColor ) * pow( 1 - (dotNH), 5);
        }
            float F = Fresnel(baseColor, dotNH);


            float ggx_visib(float dotNV, float m2) 
        { 
            return 1.0/(dotNV*(1.0-m2)+m2); 
        }                       

            float g1i =ggx_visib(m, dotLH);
            float g1o =ggx_visib(m, dotNV);
            float G = g1i * g1o;



        return D * G * F * (1.0 / (4.0 * dotNL * dotNV));


        }

::end shader

回答1:

There are 2 function declarations in the middle of the function BRDF. This is not allowed. In GLSL a function definition has to be global.

See GLSL - The OpenGL Shading Language 4.6; 6.1. Function Definitions ; page 122:

As indicated by the grammar above, a valid shader is a sequence of global declarations and function definitions.

Move the declarations of Fresnel and ggx_visib before the declaration of BRDF to solve your issue.

The major issue is, that the BRDF explorer requires the keyword analytic at the begin of the file, to identify the file as a light model.

After the keyword is add add the file reloaded, the application will trace error cause by your GLSL code.

Fixing the errors results in the following code:

analytic

::begin parameters

float baseColor 0.001 .2 .1

float m 0.001 0.1 1.0

::end parameters

::begin shader

const float PI = 3.14;

float Fresnel(float baseColor, float l, float h)
{
    return baseColor + ( 1 - baseColor ) * pow( 1 - (h), 5);
}

float ggx_visib(float dotNV, float m2) 
{ 
    return 1.0/(dotNV*(1.0-m2)+m2); 
} 

vec3 BRDF( vec3 N, vec3 V, vec3 L, vec3 X, vec3 Y)
{
    vec3 H = normalize(V+L);

    float dotNL = clamp(dot(N,L), 0.0, 1.0);
    float dotLH = clamp(dot(L,H), 0.0, 1.0);
    float dotNH = clamp(dot(N,H), 0.0, 1.0);

    float DotNH2 = dotNH * dotNH;
    float m2 = m * m;
    float D =  m2 / (PI * pow(dotNH * dotNH * (m2 - 1) + 1, 2.0));

    float F = Fresnel(baseColor, dotNL, dotNH);

    float g1i =ggx_visib(m, dotLH);
    float g1o =ggx_visib(m, dotNH);
    float G = g1i * g1o;

    float k = D * G * F * (1.0 / (4.0 * dotNL * dotNH));
    return vec3(k);
}
::end shader


标签: glsl shader