Jump to content
  • Advertisement
Sign in to follow this  
shwasasin

OpenGL OpenGL 3.2/GLSL 1.5: Lighting Problems

This topic is 3012 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

Hey Folks,
I'm writing a lighting demo right now using OpenGL 3.2 / GLSL 1.5, and ran into a bit of a snag. I'm defining my static light position in object coordinates but for some reason it travels as I move. I've tried multiplying the current modelView against the lightPosition but no deal. To make things slightly more complicated I apply multiple transforms (1 to pos/rot camera, then 1 for each mesh instance) to place in the world. I tried multiplying the light-pos by the modelview (matCamera uniform) before mesh transformations are applied to each mesh, but no deal.

Any help would be greatly appreciated. The vertex/fragment shader code is written below.


**Vertex Shader*:

#version 150

uniform mat4 matModelView;
uniform mat4 matProjection;

in vec4 position;
in vec3 normal;
in vec2 texcoord;

out vec2 fragTexCoord;
out vec3 fragNormal;
out vec3 fragEye;

uniform mat4 matCamera;
uniform vec3 lightPosition;

void main(void)
{
fragTexCoord = texcoord;
fragEye = (matModelView * position).xyz;
gl_Position = matProjection * matModelView * position;
fragNormal = normalize(fragEye - lightPosition);
}




**Fragment Shader**:

#version 150

uniform vec3 lightPosition;
uniform vec4 lightColor;

uniform sampler2D textureUnit0;
uniform sampler2D textureUnit1;
uniform int textureUnitsUsed;
uniform vec4 tintColor;
uniform bool textureEnabled;

uniform mat4 matModelView;
uniform mat4 matProjection;
uniform mat4 matCamera;

in vec2 fragTexCoord;
in vec3 fragNormal;
in vec3 fragEye;

out vec4 fragColor;

void main(void)
{
// Get Texture Unit Colors
vec4 tu0 = texture(textureUnit0, fragTexCoord);

// Apply Tint As Base Color
fragColor = tintColor;

// If enabled apply texture
if ( textureUnitsUsed > 0 )
{
// Texture Unit 0 is Base Texture
fragColor *= tu0;

// If more than 1 Texture Unit is defined,
// use color-multiplication for blending.
if ( textureUnitsUsed > 1 )
{
vec4 tu1 = texture(textureUnit1, fragTexCoord);
fragColor *= tu1;
}
}


// Lighting
vec4 diffuse = vec4(1.0, 1.0, 1.0, 0.1);
float dp = dot(fragEye, fragNormal);
float range = clamp(distance(fragEye,lightPosition), 0.0, 10.0) / 10.0;
fragColor *= ((1.0-dp) * diffuse * range);
}

Share this post


Link to post
Share on other sites
Advertisement
@furrymaster: He is using glsl 1.5 where gl_FragColor is removed.

@shwasasin: Where are you actually using normal?

the fragNormal should be


fragNormal = normalize(lightPosition - fragEye); //which is actually the light direction

Share this post


Link to post
Share on other sites
You can do your lighting calculations in any space you like(there are benefits and downsides of course to which space you choose), but you must be aware that all the normals/positions must be in the same space. You chose object space, but it seems you are mixing spaces. In your vertex shader fragEye is in eye space but fragNormal is calculated with respect to the object space position of the light and the eye position of the vertex, resulting in something with not very much of a use. What you should do when calculating the normal is to have both fragEye and lightPosition in the same space.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!