PDA

View Full Version : My attempt of a normalmap-shader



Pong_Use
01-27-2016, 02:39 AM
Hello guys :)
I am new to GLSL and tried to write my own normalmap-shader. It seems to work fine, but somehow the highlights are dark, when I look into the light, and they are white, when I don't look into it.
It is hard to explain. I wanted to post some screenshots, but the forum won't let me. :/

I am trying to find the mistake. I hope you can help me.
Vertex-Shader:

varying vec3 lightVec;
varying vec3 eyeVec;
varying vec2 texCoord;
varying vec3 vTangent;


void main(void) {
gl_Position = ftransform();
texCoord = gl_MultiTexCoord0.xy;
vec3 n = normalize(gl_NormalMatrix * gl_Normal);
vec3 c1 = cross( gl_Normal, vec3(0.0, -1.0, -1.0) );
vec3 c2 = cross( gl_Normal, vec3(0.0, 1.0, 0.0) );
if( length(c1)>length(c2) ) {
vTangent = c1;
}
else
{
vTangent = c2;
}
vTangent = -normalize(vTangent);
vec3 t = normalize(gl_NormalMatrix * vTangent);
vec3 b = normalize(cross(n, t));
b = -b;
vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
vec3 lv = vec3(gl_LightSource[0].position.x,gl_LightSource[0].position.y,gl_LightSource[0].position.z);
vec3 tmpVec = lv - vVertex;
lightVec.x = dot(tmpVec, t);
lightVec.y = dot(tmpVec, b);
lightVec.z = dot(tmpVec, n);
tmpVec = -vVertex;
eyeVec.x = dot(tmpVec, t);
eyeVec.y = dot(tmpVec, b);
eyeVec.z = dot(tmpVec, n);
}
Fragment-Shader:

varying vec3 lightVec;
varying vec3 eyeVec;
varying vec2 texCoord;
uniform sampler2D tex;
uniform sampler2D norm;

void main (void) {
float distSqr = dot(lightVec, lightVec);
vec3 lVec = lightVec * inversesqrt(distSqr);
vec3 vVec = normalize(eyeVec);
vec4 base = texture2D(tex, texCoord);
vec3 bump = normalize( texture2D(norm, texCoord).xyz * 2.0 - 1.0);
vec4 vAmbient = gl_LightSource[0].ambient;
float diffuse = max( dot(lVec, bump), 0.0 );
vec4 vDiffuse = gl_LightSource[0].diffuse * diffuse;
vec4 vSpecular = gl_LightSource[0].specular;
gl_FragColor = ( vAmbient*base + vDiffuse*base );
}