如何在此特定的OpenGL 2.0法线/视差着色器上添加​​镜面贴图

时间:2019-07-04 00:39:00

标签: opengl glsl shader opengl-2.0

我试图在此处通过dhpoware在现有的GLSL 2.0 Normal / Parallax映射实现上添加镜面映射。 http://www.dhpoware.com/demos/glslParallaxNormalMapping.html

我不太熟悉高级GLSL着色器,并希望获得一些帮助来进行设置并使其与下面的GLSL代码一起使用。

这是顶点着色器

#version 110

varying vec3 lightDir;
varying vec3 halfVector;

void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;

    vec3 n = normalize(gl_NormalMatrix * gl_Normal);
    vec3 t = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);
    vec3 b = cross(n, t) * gl_MultiTexCoord1.w;

    mat3 tbnMatrix = mat3(t.x, b.x, n.x,
                          t.y, b.y, n.y,
                          t.z, b.z, n.z);

    lightDir = gl_LightSource[0].position.xyz;
    lightDir = tbnMatrix * lightDir;

    halfVector = gl_LightSource[0].halfVector.xyz;
    halfVector = tbnMatrix * halfVector;
}

然后是碎片着色器

#version 110

uniform sampler2D colorMap;
uniform sampler2D normalMap;
uniform sampler2D heightMap;

uniform bool enableParallax;
uniform float scale;
uniform float bias;

varying vec3 lightDir;
varying vec3 halfVector;

void main()
{
    vec2 newTexCoord;
    vec3 h = normalize(halfVector);

    if (enableParallax == true)
    {
        float height = texture2D(heightMap, gl_TexCoord[0].st).r;

        height = height * scale + bias;
        newTexCoord = gl_TexCoord[0].st + (height * h.xy);
    }
    else
    {
        newTexCoord = gl_TexCoord[0].st;
    }

    vec3 n = normalize(texture2D(normalMap, newTexCoord).rgb * 2.0 - 1.0);
    vec3 l = normalize(lightDir);

    float nDotL = max(0.0, dot(n, l));
    float nDotH = max(0.0, dot(n, h));
    float power = (nDotL == 0.0) ? 0.0 : pow(nDotH, gl_FrontMaterial.shininess);

    vec4 ambient = gl_FrontLightProduct[0].ambient;
    vec4 diffuse = gl_FrontLightProduct[0].diffuse * nDotL;
    vec4 specular = gl_FrontLightProduct[0].specular * power;
    vec4 color = gl_FrontLightModelProduct.sceneColor + ambient + diffuse + specular;

    gl_FragColor = color * texture2D(colorMap, newTexCoord);
}

0 个答案:

没有答案