# Incorrect lighting

I’ve run into troubles in making per-pixel lighting in Cg. First of all, here’s the shaders I have written so far:
http://www.nomorepasting.com/paste.php?pasteID=16351

I think the problem must be in the shaders. The tangent space vectors should be ok, I think. Here’s a shot so you can see them rendered over vertices:
http://koti.mbnet.fi/blender/poista/basis.jpg

I calculate the BiNormal in the vp atm, though I use VBO so it might be wiser to pre-calculate it too.

I also used the normalization cubemap to normalize the interpolated light and half vectors, but to exclude the possibility for the error to be in the cubemaps, I just normalize per-pixel until this problem is solved.

So… the actual problem. First of all, I can’t see any specular hilights. I’ve tried everything but it just won’t work. I hoped that maybe the solution is too obvious for mee to see after all this struggling and you could notice it.

In addition, isn’t it right, that e.g. the light vector should be LightPosition-VertexPosition?
However, by doing this, the object is illuminated from the opposite side relative to light position.
Also, I took that object-to-tangent space code from some tutorial, which said that (0, 0, 1) is staring up. Does it matter, that in my code (which provides the light and viewer positions), (0, 1, 0) is straight up in object space?

Huh, if you consider my overview of the problem too confusing, just tell me about it. -Thanks.

I think you answered that yourself and it was the first I suspected when looking a the RGB axis in you JPG.
If tangent space is not what you think it is in the shaders, then expect lighting to be wrong.
Your shader uses binormals as y and normals as z but they are z resp. y in tangent space in your illustration, so what?
Having the normal as z-axis is used often.
You should follow that convention in you geometry if you copy-paste shaders which do it this way.
For an experiment you could exchange the normal and binormal identifier in the shaders, but I’d go for the vertex attrib exchange.

In bumpmaps the up base vector is (0, 0, 1) and my tangent space basis is (tangent, binormal, normal) and normal is always up from the surface i.e. in the direction of up in the bumpmap. Tangent is in the direction of U texture coordinate and binormal in the direction of V, so I think they are correct.

Diffuse lighting works (at least it seems to). I’m a bit confused about normalizing light vectors though. Isn’t it, that when the light vectors are interpolated across the surface, they get biased and thus should be normalized before calculating the dot product? However, if I do, I get weird results; when I move the light around the object, the fade between dark and lit ares gets sharp at some points. Without normalizing, it look fairly ok.

Specular lighting is a pain for some reason. It seems to appear in correct ares, but it look really weird. It’s not a smooth hilight, but “grain” looking, and it’s not because of over dense bumpmaps.