Jump to content
  • Advertisement
Sign in to follow this  
Enalis

OpenGL Lighting Shader Transformations Problem

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font=arial, verdana, tahoma, sans-serif][size=2]The issue is that my objects are getting transformed correctly however the lighting is not. EG... I have a sphere rolling on some terrain, as it rolls the triangles/textures transform correctly, however the lighting does not, and the lit side of the sphere rolls with the sphere. I am posting my vert shader which is pretty straight forward...

As you can see, the modelview_matrix is used for the gl_Position variable and is working quite nicely...
The normal_matrix uniform is just

- Take the top left 3x3 of the modelview matrix
- Then calculate the Inverse Transpose of that.

That's it.

I've also tried sending just the modelview_matrix in for lighting as well, even though the normal scaling problem would exist, at least it might rotate properly?
Anyway, this is the last little issue with this new lighting model in which I've replaced opengls matrices.

I'm hoping it's quickly solvable, thanks in advance!
CODE BAM!


varying vec3 normal, tangent, bitangent;
varying vec3 light, view;
varying float light_distance;

uniform vec3 light_position;

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;
uniform mat3 normal_matrix;

void main(){
normal = normalize(normal_matrix * gl_Normal);
tangent = normalize(normal_matrix * gl_MultiTexCoord1.xyz);
bitangent = normalize(normal_matrix * gl_MultiTexCoord2.xyz);

vec4 eye_position = modelview_matrix * gl_Vertex;
view = normalize(-eye_position.xyz);
vec3 transformed_light_position = vec3(modelview_matrix * vec4(light_position, 1.0));
vec3 temp = vec3(transformed_light_position - eye_position.xyz);
light = normalize(temp);
light_distance = length(temp);

gl_Position = projection_matrix * modelview_matrix * gl_Vertex;
gl_TexCoord[0].st = gl_MultiTexCoord0.st;
gl_FrontColor = gl_Color;
}
[/font]

Share this post


Link to post
Share on other sites
Advertisement
[font=arial, verdana, tahoma, sans-serif][size=2]sorry for posting so much, I should have just kept hammering on the code... once I really broke down and did a bunch of math I figured out what I needed to do...

here's the code for reference if anyone's interested:[/font]



varying vec3 normal, tangent, bitangent;
varying vec3 light, view;
varying float light_distance;

uniform vec3 light_position;
uniform vec3 camera_position;

uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
uniform mat4 normal_matrix;

void main(){
normal = normalize(normal_matrix * vec4(gl_Normal, 0.0)).xyz;
tangent = normalize(normal_matrix * vec4(gl_MultiTexCoord1.xyz, 0.0)).xyz;
bitangent = normalize(normal_matrix * vec4(gl_MultiTexCoord2.xyz, 0.0)).xyz;

vec3 vertex_position = (model_matrix * gl_Vertex).xyz;
vec3 light_vector = vertex_position - light_position;

view = normalize(camera_position - vertex_position);

light_distance = length(light_vector);
light = -normalize(light_vector);

gl_Position = projection_matrix * view_matrix * model_matrix * gl_Vertex;
gl_TexCoord[0].st = gl_MultiTexCoord0.st;
gl_FrontColor = gl_Color;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!