osx - Metal normal doesn't interpolate -
i've been learning how metal works using swift , targeting macos. thing's have been going okay, now, close getting stuff done, i've hit problem cannot possibly understand ... hope guys me :)
i'm loading , displaying obj teapot, i'm lighting using ambiant+diffuse+specular light. lighting in works well, problem : normal vector not interpolated when going fragment shader, results in having flat lighting on supposedly curved surface ... not ...
i don't understand why normal not interpolated while other values (position + eye) ... here shader , image show result :
thanks in advance :)
struct vertex { float4 position; float4 normal; }; struct projectedvertex { float4 position [[position]]; float3 eye; float3 normal; }; vertex projectedvertex vertex_project(device vertex *vertices [[buffer(0)]], constant uniforms &uniforms [[buffer(1)]], uint vid [[vertex_id]]) { projectedvertex outvert; outvert.position = uniforms.modelviewprojectionmatrix * vertices[vid].position; outvert.eye = -(uniforms.modelviewprojectionmatrix * vertices[vid].position).xyz; outvert.normal = (uniforms.modelviewprojectionmatrix * float4(vertices[vid].normal)).xyz; return outvert; } fragment float4 fragment_light(projectedvertex vert [[stage_in]], constant uniforms &uniforms [[buffer(0)]]) { float3 ambientterm = light.ambientcolor * material.ambientcolor; float3 normal = normalize(vert.normal); float diffuseintensity = saturate(dot(normal, light.direction)); float3 diffuseterm = light.diffusecolor * material.diffusecolor * diffuseintensity; float3 specularterm(0); if (diffuseintensity > 0) { float3 eyedirection = normalize(vert.eye); float3 halfway = normalize(light.direction + eyedirection); float specularfactor = pow(saturate(dot(normal, halfway)), material.specularpower); specularterm = light.specularcolor * material.specularcolor * specularfactor; } return float4(ambientterm + diffuseterm + specularterm, 1); }
so problem using obj-c, when indexed vertices obj file, generated 1 vertex shared vertices between surfaces, kept 1 normal.
when translating swift, hash value used check if vertex @ same place 1 have wrong , couldn't detect shared vertices, resulted in keeping of normals, each surface flat.
i don't know if i'm clear enough that's happened, future reference, question making swift version of "metalbyexample" book obj-c only.
Comments
Post a Comment