Jump to content
  • Advertisement
Sign in to follow this  
Mihumihu

Directional lighting trouble.

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

Hi. I've stucked.

Point light source works fine, but directional not. Help please.

    pointLightPosition.xyz = modelViewMatrix * lightPos;
    pointLightPosition.w = 0.0;
    //vertex
    ...
    vPosition = modelViewMatrix * vec4(aVertexPosition, 1.0);
    gl_Position = projectionMatrix * vPosition;

    //fragment
    ...
    vec3 normal = normalize(uNMatrix * vNormal);

    vec3 lightDirection;

    if(pointLightsPositions[0].w == 0.0){
        lightDirection = normalize(pointLightPosition.xyz);
    }else{
        lightDirection = normalize(pointLightPosition.xyz - vPosition.xyz);
    }

    vec3 eyeDirection = normalize(-vPosition.xyz);
    
    float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
    vec3 lightWeighting = ambient + diffuse * diffuseLightWeighting;
    ...   
Edited by Turbochist

Share this post


Link to post
Share on other sites
Advertisement

Looks like you're putting the light's POSITION into your constant buffer CPU side. This is correct for a point light. But in the case of a directional light you just need a DIRECTION!

(If you normalize the lights position as you're doing here you'll get a directional vector form the origin to the lights position as your directional light direction.)

Share this post


Link to post
Share on other sites

don't "translate" the direction to model view, if the model has any position other than 0,0,0 then it will break the normalized direction.

 

use a m33 rather than an m44 to "transform" the light direction into the models space without taking any translations into account, i.e. only scales and rotations as they are the only things that matter for a directional light.

 

:D

Share this post


Link to post
Share on other sites

Use vertex normal and pass it to fragment shader, renormalise it, then use that.

If you use normal mapping you can use the normals from that.

So essentially just dot the normal against the transformed light direction (which should now be in the same space as the normal)

Share this post


Link to post
Share on other sites
attribute vec3 aVertexNormal;
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;

uniform mat4 uPMatrix;
uniform mat4 uMVMatrix;
uniform mat3 uNMatrix;

varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;

void main(void) {
        vTransformedNormal = uNMatrix * aVertexNormal;
        vTextureCoord = aTextureCoord;

        vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
        gl_Position = uPMatrix * vPosition;
}

And frag:

precision highp float;

varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
	
uniform vec4 uColor;
uniform sampler2D uSampler;
 
#define MAX_POINT_LIGHTS 1

uniform int pointLightsQuantity;
uniform vec4 pointLightsPositions[MAX_POINT_LIGHTS];
uniform vec3 pointLightsParamsv[MAX_POINT_LIGHTS * 3];
uniform float pointLightsParamsf[MAX_POINT_LIGHTS];

void main(void) {

    vec3 lightWeighting;
    vec3 lightDirection;
    vec3 normal;
    vec3 eyeDirection;
    vec3 reflectionDirection;
    float specularLightWeighting;
    float diffuseLightWeighting;


    lightDirection = normalize(pointLightsPositions[0].xyz);
    //float distance = length(dir);
    //float attenuation = 1.0/(1.0+0.1*distance+0.01*distance*distance);
    normal = normalize(vTransformedNormal);
    eyeDirection = normalize(-vPosition.xyz);
    reflectionDirection = reflect(-lightDirection, normal);	
    specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), pointLightsParamsf[0]);
    diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
    lightWeighting = pointLightsParamsv[0] + pointLightsParamsv[1] * diffuseLightWeighting + pointLightsParamsv[2] * specularLightWeighting;

    vec4 cc = texture2D( uSampler, vTextureCoord.st );
    gl_FragColor = vec4(lightWeighting, uColor.a) * cc * uColor;
}

uNMatrix is normal matrix.

I dont' know..where is a mistake?

Share this post


Link to post
Share on other sites

I've solved it! Thanks all. This is becouse I have to multyply direction to the normalMatrix. Mistake is I mul direction to modelView.

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!