2015-01-26 14 views
0

我計算我的模型視圖矩陣,在我的主程序正常矩陣:法線倒

private void setMV() 
     { 
      modelViewMat = Matrix4.Mult(modelMat,viewMat); // model * view because opentk is row major order 
      GL.UniformMatrix4(uModelViewIndex, false, ref modelViewMat); 

      // normal matrix = transpose(inverse(modelView)) 
      normalMat = Matrix4.Invert(modelViewMat); 
      normalMat = Matrix4.Transpose(normalMat); 
      GL.UniformMatrix4(uNormalIndex, false, ref normalMat); 
     } 

在我的頂點着色器:

void main() 
{ 
    texcoord = VertexTexcoord; 
     Normal = normalize((uNormalMatrix * vec4(VertexNormal,0.0)).xyz); 
    Position = (uModelViewMatrix * vec4(VertexPosition,1.0)).xyz; 

    gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(VertexPosition,1.0); 
} 

片段着色器:

vec3 phong(vec3 n, vec3 s, vec3 v, vec3 r) 
{ 
    vec3 diffC = texture2D(diff, texcoord.st).rgb; 
    vec3 specC = texture2D(spec, texcoord.st).rgb; 
    return uKa + uKd*max(dot(s,n),0.0)*diffC + uKs*pow(max(dot(r,v),0.0),uShininess)*specC; 

} 

void main() { 

    vec3 n = blendNormals(Normal * 0.5 + 0.5, texture2D(ddn, texcoord.st).rgb); 
    //vec3 n = normalize(Normal); 
    vec3 s = normalize(uLightPosition - Position); 
    vec3 v = normalize(-Position); 
    vec3 r = reflect(-s,n); 

    fragColour = vec4(phong(n,s,v,r),1.0); 
} 

不知道我在做什麼錯,下面是它的樣子: enter image description here

回答

2

如果您的目標是爲相同的gemetry和法線的正面和背面獲得相同的結果。如果三角形的方向相反,那麼您需要在着色器中正常翻轉,而不是正常值。

在片段着色器你可以:

if (gl_FrontFacing) 
    myNormal = - myNormal; 
+0

謝謝,這是一個快速解決。爲了獲得完整的解決方案,我需要顛倒我的垂直陣列的順序,以便繞組與原來的相反。 – Infodayne 2015-01-27 03:41:23