# Need help with bump-mapping

This topic is 2572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey there, I usually try to help people out but this time I just cant figure out what Im doing wrong
Im not terribly good at matrixes and coordinate-systems, but I have read all the tutorials I could find, all the problem thread... everything
Ive tried for a week now to get my shaders to display bump mapping in the correct place

To make sure the light position wasnt the problem i also now draw a cube where the light position is presently set
My world is being constructed using translate and rotate only:
rotate x
rotate y
translate -camerax, -cameray, -cameraz

I did this only because i couldnt get a directional light to work, but it doesnt really matter which is used I guess
for simplicity ive even abandoned the light and im setting a hardcoded position for light

 vec4 position = vec4(in_vertex.xyz, 1.0); position = gl_ModelViewMatrix * position; vec3 tangent, binormal; if (in_normal.x > 0.5) { tangent = vec3( 0.0, 0.0, -1.0); binormal = vec3( 0.0, -1.0, 0.0); } else if (in_normal.x < -0.5) { tangent = vec3( 0.0, 0.0, 1.0); binormal = vec3( 0.0, -1.0, 0.0); } else if (in_normal.y > 0.5) { tangent = vec3( 1.0, 0.0, 0.0); binormal = vec3( 0.0, 0.0, 1.0); } else if (in_normal.y < -0.5) { tangent = vec3( -1.0, 0.0, 0.0); binormal = vec3( 0.0, 0.0, 1.0); } else if (in_normal.z > 0.5) { tangent = vec3( 1.0, 0.0, 0.0); binormal = vec3( 0.0, -1.0, 0.0); } else if (in_normal.z < -0.5) { tangent = vec3(-1.0, 0.0, 0.0); binormal = vec3( 0.0, -1.0, 0.0); } tbn = gl_NormalMatrix * mat3(tangent, binormal, in_normal); v_ldir = normalize(gl_LightSource[0].position - position.xyz) * tbn; v_eye = normalize(-position.xyz) * tbn; v_half = normalize(position.xyz + lightVector.xyz) * tbn; 

im using cubes btw. lots of them, so the normals are all completely directional =)

the fragment shader is pretty standard, exactly what you'd expect
just to be on the same page: bump is sampler2D() * 2.0 - vec3(1.0);
and the lamberfactor: [font="Consolas,"]max(dot(bump, normalize(v_ldir)), 0.0);[/font]
ive written these things so many times in so many ways i remember it by heart now =)

i also tried changing it over: converting the bump normal to tangent space, instead of light vector

oh, before i forget, the vectors (vec3) used in the fragment shader (v_ldir and v_eye) are being normalized() one extra time on the fragment side just in case!

last but not least i dont actually use v_eye and v_half yet, since i cant even get normal bumpmapping to work =)

if it somehow comes across as if i know what im doing here: i dont
this is why i need help =)
ANY help is appreciated!

##### Share on other sites

Hey there, I usually try to help people out but this time I just cant figure out what Im doing wrong
Im not terribly good at matrixes and coordinate-systems, but I have read all the tutorials I could find, all the problem thread... everything
Ive tried for a week now to get my shaders to display bump mapping in the correct place

To make sure the light position wasnt the problem i also now draw a cube where the light position is presently set
My world is being constructed using translate and rotate only:
rotate x
rotate y
translate -camerax, -cameray, -cameraz

I did this only because i couldnt get a directional light to work, but it doesnt really matter which is used I guess
for simplicity ive even abandoned the light and im setting a hardcoded position for light

 vec4 position = vec4(in_vertex.xyz, 1.0); position = gl_ModelViewMatrix * position; vec3 tangent, binormal; if (in_normal.x > 0.5) { tangent = vec3( 0.0, 0.0, -1.0); binormal = vec3( 0.0, -1.0, 0.0); } else if (in_normal.x < -0.5) { tangent = vec3( 0.0, 0.0, 1.0); binormal = vec3( 0.0, -1.0, 0.0); } else if (in_normal.y > 0.5) { tangent = vec3( 1.0, 0.0, 0.0); binormal = vec3( 0.0, 0.0, 1.0); } else if (in_normal.y < -0.5) { tangent = vec3( -1.0, 0.0, 0.0); binormal = vec3( 0.0, 0.0, 1.0); } else if (in_normal.z > 0.5) { tangent = vec3( 1.0, 0.0, 0.0); binormal = vec3( 0.0, -1.0, 0.0); } else if (in_normal.z < -0.5) { tangent = vec3(-1.0, 0.0, 0.0); binormal = vec3( 0.0, -1.0, 0.0); } tbn = gl_NormalMatrix * mat3(tangent, binormal, in_normal); v_ldir = normalize(gl_LightSource[0].position - position.xyz) * tbn; v_eye = normalize(-position.xyz) * tbn; v_half = normalize(position.xyz + lightVector.xyz) * tbn; 

im using cubes btw. lots of them, so the normals are all completely directional =)

the fragment shader is pretty standard, exactly what you'd expect
just to be on the same page: bump is sampler2D() * 2.0 - vec3(1.0);
and the lamberfactor: [font="Consolas,"]max(dot(bump, normalize(v_ldir)), 0.0);[/font]
ive written these things so many times in so many ways i remember it by heart now =)

i also tried changing it over: converting the bump normal to tangent space, instead of light vector

oh, before i forget, the vectors (vec3) used in the fragment shader (v_ldir and v_eye) are being normalized() one extra time on the fragment side just in case!

last but not least i dont actually use v_eye and v_half yet, since i cant even get normal bumpmapping to work =)

if it somehow comes across as if i know what im doing here: i dont
this is why i need help =)
ANY help is appreciated!

I'd suggest you first try to get bump mapping working on a quad and don't use transformations yet. Once you can do that, then try doing the cube and adding transformations. I think it may be a better idea to pre-compute your normals and tangents and send them as attributes instead of trying to use if statements in the vertex shader. Here is a vertex shader that I use that works for me (for a quad):

#version 410 uniform mat4 mvMatrix; uniform mat4 mvpMatrix; uniform vec3 LightPosition; uniform mat3 normalMatrix; in vec4 vVertex; in vec3 normal; in vec3 tangent; out vec3 LightDir; out vec3 EyeDir; out vec2 TexCoord; void main(void) { // convert normal and tangent (from main program) into eye space vec3 u = vec3(mvMatrix * vVertex); vec3 n = normalize(normalMatrix * normal); vec3 t = normalize(normalMatrix * tangent); // compute b in eye space vec3 b = cross(n, t); // convert light direction from eye space to tangent space vec3 v; v.x = dot(LightPosition, t); v.y = dot(LightPosition, b); v.z = dot(LightPosition, n); LightDir = normalize(v); // convert eye direction from eye space to tangent space v.x = dot(u, t); v.y = dot(u, b); v.z = dot(u, n); EyeDir = normalize(v); // compute tex coordinates TexCoord = vVertex.xy * vec2(0.5) + vec2(0.5); // transform geometry gl_Position = mvpMatrix * vVertex; }

##### Share on other sites
i have no idea what i have been doing wrong, but it clearly works now...
you probably helped me out (haha), super thanks =)

1. 1
2. 2
Rutin
20
3. 3
khawk
17
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633757
• Total Posts
3013711
×