Search code examples
directxhlslspecular

Specular Lighting breaks at origin (0, 0, 0)


im having problem to get specular lighting to work. It looks like im having some kind of bug in my application which im not able to trace.

Light is coming from the front (screenshots shows camera looking at -z direction, on the left is -x).

A simple specular output shows the following failure:

enter image description here

Code used:

    float3 n = normalize(input.normal); // world space normal, mul(float4(normal, 0.0), modelMatrix)
    float3 l = normalize(-sDirection); // constant direction (like 0.7, -0.8, -0.7)
    float3 v = normalize(viewPos.xyz - input.vertexWorldSpace.xyz); // viewpos = world space camera position
    float3 LightReflect = normalize(reflect(n,l));
    float SpecularFactor = dot(v, LightReflect);

    color = float4(SpecularFactor, SpecularFactor, SpecularFactor, 1.0);

To check for possible variable errors i checked the input.VertexWorldSpace:

enter image description here

to also check lightdirection and normal, i checked the diffuse term:

enter image description here

and the camera to vertex view vector (v): enter image description here

For me, all parts look fine, but still the specular gets black at origin(0,0,0) and perpendicular to the light direction.

Ive also made a gif showing what happens with the view direction vector at origin(0, 0, 0)

https://i.sstatic.net/P2iI5.jpg

And another gif showing camera position and where the specular goes black: http://i.imgur.com/ajUaekA.gifv

Am i using a wrong calculation for v?


Solution

  • Ok, the problem was with my buffer alignment: https://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx

    Instead of passing

    Matrix
    Matrix 
    Vec3
    Vec3
    

    hlsl wants 16 byte alignment vectors so i had to use

    Matrix
    Vec3
    float1 // 16 byte alignment
    Vec3
    float1 // 16 byte alignment