GGX着色器创建 [英] GGX shader creation

查看:99
本文介绍了GGX着色器创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个ggx着色器,但是当我在BRDF Explorer中运行它时,主页仍保持白色.我正在尝试查找错误,但是找不到. 有谁能够帮我? 下面是我的文件

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

分析

::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

推荐答案

在函数BRDF的中间有2个函数声明.这是不允许的.在GLSL中,函数定义必须是全局的.

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.

请参见 GLSL-OpenGL阴影语言4.6; 6.1.功能定义 ;第122页:

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.

Fresnelggx_visib的声明移到BRDF的声明之前,以解决您的问题.

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

主要问题是 BRDF浏览器需要关键字analytic在文件开头,将文件标识为灯光模型.

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.

添加关键字后,重新加载文件,应用程序将跟踪由您的GLSL代码引起的错误.

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

这篇关于GGX着色器创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆