Reputation: 911
I am computing my modelview matrix, and normal matrix in my main program:
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);
}
And in my vertex shader:
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);
}
Fragment shader:
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);
}
Not sure what I am doing wrong, here is what it looks like:
Upvotes: 0
Views: 4711
Reputation: 2172
If your objective is to achieve same result for front and backfaces of same gemetry and normals. You need to flip normal in shader in case triangles are in opposite direction than normals were calculated for them.
In fragment shader you could:
if (gl_FrontFacing)
myNormal = - myNormal;
Upvotes: 3