2014/11/12

Brightness by inner product of normal and light vectors

Continuation of : Add texture in Metal / iPhone App Development Memo

I succeeded to add texture to a object in Metal, but the texture looks dark.
The pixels’ brightness are decided by inner product of normal and light vectors, this is a common way to draw object in 3D. So there must be something wrong in code around here.

The inner product is calculated in the Apple sample code ‘MetalBasic3D’ as bellow :
{
    float3 normal = normal_array[vid].position;//(1)
    float4 eye_normal = normalize(constants.normalMatrix * float4(normal, 0.0));//(2)
    float3 light_position = float3(1.0 , 1.0 , 1.0);//(3)
    float n_dot_l = dot(eye_normal.rgb, normalize(light_position));//(4)
}
(1) Argument normal receives by float3
(2) The normal is added one element and calculated with normal matrix
(3) Light vector
(4) Getting inner product normal and light vectors

There seems be nothing wrong...
But I found it, eye_normal vector was normalized by 4 elements, though this should be made from 3 elements, because fourth elements may not be zero.

So I changed the code like this :
{
    float4 raw_eye_normal = constants.normalMatrix * float4(normal, 0.0);
    float3 eye_normal = normalize(row_eye_normal.rgb);
}
Then the image was displayed as I expected!

textured robot in Metal
Develop | Comments(0) | Trackback(0)
Comment

管理者のみに表示