Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Bump Mapping GLSL problems

Started by Mach5Mach Feb 27, 2008 at 4:52 PM 0 replies 550+ views
Original Post
Mach5Mach
Mach5Mach
I've been working on this bump mapping project for a while now. I've gotten it to transfer the normal map and the regular color texture correctly so I'm pretty sure the problem is with my vectors. I'm still not sure if it's because I'm passing them in wrong or what but hopefully somebody can find the error in my logic. Thanks. vert shader: varying vec3 v_normal; varying vec3 light_vector; varying vec3 view_vector; attribute vec3 Tangent; attribute vec3 Binormal; //need to transform light and eye vectors into tangent space //and then pass them to fragment vector void main(){ gl_TexCoord[0] = gl_MultiTexCoord0; //color map gl_TexCoord[1] = gl_MultiTexCoord1; //normal map v_normal = normalize(gl_NormalMatrix * gl_Normal); vec4 light_pos = gl_LightSource[0].position; vec4 pos_in_eye = gl_ModelViewMatrix*gl_Vertex; //light vector vec3 light_vector = normalize(vec3(light_pos - pos_in_eye)); //eye vector vec3 view_vector = normalize(vec3(vec4(0,0,0,1)-pos_in_eye)); //transform into tangent space [T,B,N,homogeneous] Tangent = normalize(gl_NormalMatrix * Tangent); Binormal = normalize(gl_NormalMatrix * Binormal); light_vector = normalize(vec3(dot(Tangent,light_vector), dot(Binormal,light_vector), dot(v_normal,light_vector))); view_vector = normalize(vec3(dot(Tangent,view_vector), dot(Binormal,view_vector), dot(v_normal,view_vector))); gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } frag shader: varying vec3 v_normal; varying vec3 light_vector; varying vec3 view_vector; uniform sampler2D ColorMap; uniform sampler2D NormalMap; //receives transformed light and eye vector, compute phong illumination using modified normal from normal map (du, dv, 1). void main() { vec4 cMap = texture2D(ColorMap, vec2(gl_TexCoord[0])); //cMap is float rgb values of texture vec4 nMap = texture2D(NormalMap, vec2(gl_TexCoord[1])); //nMap is float rgb values of bump map // light_vector = normalize(vec3(1,1,0)); vec3 halfv = normalize(light_vector+view_vector); //to go from range [0,1], subtract 0.5 to get [-0.5,0.5], multiply by 2 to get [-1,1] vec3 perturbed = normalize(vec3(((vec2(nMap[0],nMap[1]) - 0.5)*2),nMap[2])); //nMap[2] = 1 // perturbed = normalize(vec3(0,1,0)); vec4 ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient; float ndotl = max(dot(perturbed, light_vector), 0.0); vec4 diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse* vec4(ndotl); float ndoth = max(dot(perturbed, halfv), 0.0); float shine = pow(ndoth, 8.0); vec4 specular = gl_FrontMaterial.specular* gl_LightSource[0].specular*vec4(shine, shine, shine, 1); vec4 color = ambient*cMap+diffuse*cMap+specular; gl_FragColor = diffuse; }

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.