【问题标题】:Opengl GLSL Normal Mapping IssueOpengl GLSL法线映射问题
【发布时间】:2016-01-29 02:35:34
【问题描述】:

我的法线贴图有问题,我被困在哪里出错了。地图似乎在模型上,但不在正确的空间中。可变眼睛只是相机位置。切线是在程序中计算出来的,而且是正确的。

顶点着色器:

void main() 
{     

    vec3 EyeSpaceNormal = normalize(vec3(NormalMatrix * VertexNormal));
    vec3 EyeSpaceTangent = normalize(vec3(NormalMatrix * vec3(VertexTangent)));

    TexCoord = VertexUV;
    vec3 bitangent = normalize(cross( EyeSpaceNormal, EyeSpaceTangent)) * VertexTangent.w;

    mat3 TBN = mat3(EyeSpaceTangent, bitangent, EyeSpaceNormal);

    TangentLightDirection = vec3( normalize(LightDirection) * TBN );
    TangentEye = vec3(normalize( eye) * TBN );

    Normal = EyeSpaceNormal;   
    VertPosition = vec3( ModelViewMatrix * vec4(VertexPosition,1.0));     

    gl_Position = MVP * vec4(VertexPosition,1.0); 
}

片段着色器:

void main() 
{    
    vec3 ReturnColour;
    vec3 TextureNormal_tangentspace = normalize(texture2D( NormalMap, TexCoord ).rgb * 2.0 - 1.0);

    vec3 diffuse =  intensity * vec3(0.0,1.0,0.0) * max(0,dot(normalize(TextureNormal_tangentspace), normalize(-TangentLightDirection)));
    vec3 specular;

    //Specular
    vec3 VertexToEye = normalize(TangentEye - VertPosition);
    vec3 LightReflect = normalize(reflect(normalize(TangentLightDirection), TextureNormal_tangentspace));
    float SpecularFactor = dot(VertexToEye, LightReflect);
    SpecularFactor = pow(SpecularFactor, Shininess);

    if(SpecularFactor > 0)
    {
        specular = intensity * vec3(0.0,1.0,0.0) * SpecularFactor;  
    }

    ReturnColour = diffuse + specular;       

    FragColor = vec4(ReturnColour, 1.0); 
}

【问题讨论】:

  • 您没有详细说明结果与您的预期有何不同。我看到的一个问题是您应该在pow() 调用之前检查SpecularFactor 是否是肯定的。否则pow() 的结果是未定义的。

标签: opengl glsl


【解决方案1】:

我的最后一个芒斯纳:Normal mapping and phong shading with incorrect specular component 看看,镜面反射因子是如何计算的。

1) 你的代码:

dot(VertexToEye, LightReflect)

必须是:

max(dot(VertexToEye, LightReflect), 0.0)

这对于将负值钳制为零是必需的,因为在镜面反射计算中我们有指数(比如Reto Koradi)!

2) 如果您在编译着色器时没有看到错误,请尝试使用 glGetProgramInfoLog 函数。看看吧:

vec3 specular;
...
if(SpecularFactor > 0)
{
    specular = intensity * vec3(0.0,1.0,0.0) * SpecularFactor;  
}

如果 specular 等于 0 我们有 variable is undefined 错误。

替换:

vec3 specular;

作者:

vec3 specular = vec3(0.0);

附:更好地将浮点值(0.0)用于浮点变量,如浮点、vec2、vec3... 大概是这样:

if(SpecularFactor > 0) -> if(SpecularFactor > 0.0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多