2015-01-26 86 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