Sign in to follow this  
Khaos Dragon

can't get my surface tangent light vector to vary across pixels in glsl

Recommended Posts

Khaos Dragon    196
My problem is that my surface_light vector always seems to be the same, even when I pass different values via glUniform3fARB. Specifically, I am rotating a quad about the z axis, and despite the different tangent light vector generated, my quad always looks the if the tangent light vector were not changing. Basically for each vertex I pass to my glsl shader, I do this:
handle_to_light = glGetUniformLocationARB( glsl_program, "light" );
glUniform3fARB( handle_to_light, surface_light.x, surface_light.y, surface_light.z );
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, A.texX, A.texY);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, A.texX, A.texY);
glVertex3f( A.x, A.y, A.z );
And so the values of the light vector should be passed to the vertex shader.
uniform vec3 light;
varying vec2 texture_coordinate; 
varying vec3 surface_light;

void main()
surface_light = light;
gl_Position = ftransform();
texture_coordinate = vec2(gl_MultiTexCoord0); 

And then I make it so the surface_light is interpolated, until the next vertex by setting it to a varying variable, so this is the corresponding pixel shader used.
uniform sampler2D bump;
uniform sampler2D perpen;
varying vec2 texture_coordinate;
varying vec3 surface_light;

void main ()
surface_light = 0.5f*surface_light + 0.25f;
float scale = dot( surface_light,texture2D(perpen, texture_coordinate)); 
gl_FragColor = scale*texture2D(bump, texture_coordinate );

Share this post

Link to post
Share on other sites
zedzeek    528
looking quickly at your code. (which i cant really make sense of but anyways)

the lightvectorshould be a normalized vector thus from ( -1.0 -> 1.0 ) thus no need to scale it by half + add a quarter
though if the vectors not normallized eg youre using light_vector = light_position - vertex_position then u will want ot normalize it first.
though the texturemap (containing the normals) is usually in the region of 0.0 -> 1.0 but u want this in the same region as the light vector ie -1.0 -> 1.0. thus u can do texture.rgb*2.0 - 1.0. now these are the two vectors that u dotproduct together to get the fragments shade.

though to get standard bumpmapping in your shader up and running youre gonna have to supply a lot more info per vertice eg for each vertice u will need the normal,bitangent + tangent (see some bumpmapping papers about what these are) as well as the lights direction

ps/ using uniforms for stuff that changes per vertice is gonna be slow. use one of the inbuilt things eg colors/texcoords/normals

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this