JOGL 2 - BumpMap with Shader
Posted by greatdonking on Nov 12, 2012; 3:32pm
URL: https://forum.jogamp.org/JOGL-2-BumpMap-with-Shader-tp4026889.html
Hi,
I have implemented with success with success the BumpMap Dot3 with vertex, calculating TBN with CPU...
Now I'm trying to implement the bumpMap effect with Shader. I have weird results.
Looks like I displayed only normalMap...
Here is my Vertex Shader:
varying vec3 LightDir;
varying vec3 EyeDir;
uniform vec3 LightPosition;
attribute vec3 Tangent;
void main()
{
EyeDir = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
vec3 n = normalize(gl_NormalMatrix * gl_Normal);
vec3 t = normalize(gl_NormalMatrix * Tangent);
vec3 b = cross(n, t);
vec3 v;
v.x = dot(LightPosition, t);
v.y = dot(LightPosition, b);
v.z = dot(LightPosition, n);
LightDir = normalize(v);
v.x = dot(EyeDir, t);
v.y = dot(EyeDir, b);
v.z = dot(EyeDir, n);
EyeDir = normalize(v);
}
My Fragment Shader
varying vec3 LightDir;
varying vec3 EyeDir;
uniform vec3 SurfaceColor; // = (0.7, 0.6, 0.18)
uniform float BumpDensity; // = 16.0
uniform float BumpSize; // = 0.15
uniform float SpecularFactor; // = 0.5
void main()
{
vec3 litColor;
vec2 c = BumpDensity * gl_TexCoord[0].st;
vec2 p = fract(c) - vec2(0.5);
float d, f;
d = p.x * p.x + p.y * p.y;
f = 1.0 / sqrt(d + 1.0);
if (d >= BumpSize)
{ p = vec2(0.0); f = 1.0; }
vec3 normDelta = vec3(p.x, p.y, 1.0) * f;
litColor = SurfaceColor * max(dot(normDelta, LightDir), 0.0);
vec3 reflectDir = reflect(LightDir, normDelta);
float spec = max(dot(EyeDir, reflectDir), 0.0);
spec = pow(spec, 6.0)
spec *= SpecularFactor;
litColor = min(litColor + spec, vec3(1.0));
gl_FragColor = vec4(litColor, 1.0);
}
When I draw my scene :
1 - enable shader
2 - bind the texture
3 - bind the normalMap
4 - glUniform1i(texture_location, 0)
5 - glUniform1i(normalMap_location, 1)
6 - draw my VBO
7 - disable shader
Any ideas where I'm wrong ?