Directional lighting trouble.

Started by
6 comments, last by Mihumihu 7 years, 10 months ago

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;
    ...   
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.)

Check out my project @ www.exitearth.com

I made only direction that translate to model view, but nothing.

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

Ok I dont translate direction, but how to direct the light to fragment now?

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)


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?

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

This topic is closed to new replies.

Advertisement