1

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.stack.imgur.com/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?

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
Raphael Mayer
  • 667
  • 6
  • 19
  • 1
    Looking at https://www.opengl.org/sdk/docs/man/html/reflect.xhtml It appears that reflect(n,l) would use N as the INCIDENT and L as the NORMAL vector, so I think they should be swapped. Not sure if that's the entire problem though. – Henk De Boer Sep 16 '15 at 08:17
  • BTW the first gif looks really cool with the color compression, you should make that the artstyle. – Henk De Boer Sep 16 '15 at 08:23
  • thanks for the hint. but im usually using a physically based shading model someone else wrote and it just consumes the vectors, so that reflect was not the error. I just changed the direction of the light. same here: it only shows specular as soon as viewPos (camera position) is in -x or-z range. – Raphael Mayer Sep 16 '15 at 08:33
  • ok i just output my viewpos alone in the shader and i get a green color if my z value is positive? the value is wrong (at least in the shader)! and if z and y are negative and x is positive (which would result in a green color normally), its just black. – Raphael Mayer Sep 16 '15 at 09:20
  • ok the error is related to the shader. the last variable of my constant buffer is always read wrong. – Raphael Mayer Sep 16 '15 at 10:49
  • Please do not tag your question as both OpenGL and DirectX; tag only the one you are using. Since you say you are using HLSL, and that is specific to DirectX, I have removed the OpenGL tag. – Colonel Thirty Two Sep 16 '15 at 15:02

1 Answers1

0

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
Raphael Mayer
  • 667
  • 6
  • 19