I´ve tried hard to implement correct lighting/bumpmapping with the possibility of moving around in the world, but i´m lost.
Given by the app is:
-light position
-tangent
-bitangent
-normal
-camera postion if needed
-samplers (base & bump)
The vertex program should compute the following values and send them to fragment program:
-light direction
-viewing direction
The application should not
-compute any inverse matrices
-if possible no matrix stuff at all
I tried a way the Orange Book uses:
n = normalize(gl_NormalMatrix * gl_Normal);
t = normalize(gl_NormalMatrix * tangent);
b = normalize(gl_NormalMatrix * bitangent);
vec3 lVec = light_pos;
light_dir.x = dot(t, lVec);
light_dir.y = dot(b, lVec);
light_dir.z = dot(n, lVec);
vec3 vVec = vec3(gl_ModelViewMatrix * gl_Vertex);
view_dir.x = dot(t, vVec);
view_dir.y = dot(b, vVec);
view_dir.z = dot(n, vVec);
result:
A nearly totally lit sphere from all sides, only the edges are black if the view-angle is big.
Then I tried different (totally min. 10) other “possibilities”, with mat3 multiplications, gl_Vertex - or gl_ModelViewMatrix - or gl_ModelViewProjectionMatrix - computations; no luck.
Then I found this in a demo from Nvidia:
attribute vec4 position;
attribute mat3 tangentBasis;
attribute vec2 texcoord;
uniform vec3 light;
uniform vec3 halfAngle;
uniform mat4 modelViewI;
varying vec2 uv;
varying vec3 lightVec;
varying vec3 halfVec;
varying vec3 eyeVec;
void main()
{
// output vertex position
gl_Position = gl_ModelViewProjectionMatrix * position;
// output texture coordinates for decal and normal maps
uv = texcoord;
// transform light and half angle vectors by tangent basis
lightVec = light * tangentBasis;
halfVec = halfAngle * tangentBasis;
eyeVec = modelViewI[3].xyz - position.xyz;
eyeVec = eyeVec * tangentBasis;
}
But, as I said above, I don´t want any matrix-stuff in my app.
Then i actually wanted to post this Humus´portaldemo-shader like code and wanted to say that it does not work for me too:
n = normalize(gl_NormalMatrix * gl_Normal);
t = normalize(gl_NormalMatrix * tangent);
b = normalize(gl_NormalMatrix * bitangent);
vec3 lVec = light_pos - gl_Vertex.xyz;
light_dir.x = dot(t, lVec);
light_dir.y = dot(b, lVec);
light_dir.z = dot(n, lVec);
vec3 vVec = cam_pos - gl_Vertex.xyz;
view_dir.x = dot(t, vVec);
view_dir.y = dot(b, vVec);
view_dir.z = dot(n, vVec);
I saw a lit sphere with a black spot in the middle wich was moving with me around.
But while writing this post, i found out, that Humus is not making any computation with gl_NormalMatrix, so i deleted this, tested it, and voila, it works!!!
Nevertheless i dicided to post this, because of two things:
- Is there a mistake in the Orange Book´s code?
- actually i need a directional light. I changed the code above and don´t substract ‘gl_Vertex.xyz’
from ‘light_pos’
I have a correct half-side bright sphere now, but it seems that the light direction is some kind of disturbed, beacause the lit side is more on top of the sphere with a light_pos of (0.0, 0.0, 1.0).
So eighter this way isn’t totally correct too or there is another thing in my code i´m not thinking about at the moment.
Thank you for helping (and reading this long text of course )