2015-01-26 98 views
0

我的法线贴图存在问题,并且卡在我出错的地方。地图似乎在模型上,但不在正确的空间中。可变的眼睛就是相机的位置。切线在程序中计算,它们是正确的。Opengl GLSL法线贴图问题

顶点着色器:

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); 
} 

enter image description here

+0

你不给的结果究竟是怎么与你的期望不同的细节。我看到的一个问题是你应该在** pow()调用之前检查'SpecularFactor'是否为正**。否则'pow()'的结果是未定义的。 – 2015-01-27 07:53:57

回答

0

我最后awnsner: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; 
} 

如果镜面等于我们变量未定义错误。

替换:

vec3 specular; 

由:

vec3 specular = vec3(0.0); 

附:像浮,VEC 2,VEC 3浮点型变量更好地利用浮点值(0.0)......这是关于:

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

我没有翻转正常的地图图像....这是问题 – Student123 2016-01-30 20:23:45