0
\$\begingroup\$

Basically, i'm just trying to find out where to insert the model scale into the lighting calculations. Ive searched the web, but amazingly, couldn't find anything dealing with scale + GLSL.

This is because the lighting is just plain wrong if i use scale on a model, which kinda makes sense.

I'm passing the model matrix as the transformation matrix of the model * a scale matrix. Should i pass them separated?

this is my frag shader:

#version 300 es

precision mediump float;

struct SPointLight
    {
    vec3 Color;
    vec3 Position;
    float Intensity;
    float ConstantAttenuation;
    float LinearAttenuation;
    float ExponentialAttenuation;
    float Cutoff;
    };

struct SAmbientLight
    {
    vec3 Color;
    float Intensity;
    };

struct SMaterial
    {
    vec4 Diffuse, Ambient, Specular;
    float Shininess;
    };

uniform mat4 ModelMatrix;
uniform bool IsTextured;
uniform sampler2D TextureSampler;
uniform int PointLightCount;
uniform SPointLight PointLights[MAX_POINT_LIGHTS];
uniform SAmbientLight AmbientLight;
uniform SMaterial Material;
uniform vec3 CameraPositionWorld;

in vec2 fragTexCoord;
in vec3 WorldFragPosition;
in vec3 fragVertexNormalWorld;

out vec4 FragColor;

vec3 CalculateLights ( void )
    {
    vec3 OverallResult = (AmbientLight.Color * AmbientLight.Intensity * Material.Ambient.xyz );
    vec3 SurfaceToCamera = WorldFragPosition - CameraPositionWorld;

    // Apply point lights
    for ( int LightIndex = 0; LightIndex < PointLightCount; ++LightIndex )
        {
        vec3 LightResult = vec3 ( 0, 0, 0 );
        vec3 SurfaceToLight = PointLights[LightIndex].Position - WorldFragPosition;

        float Distance = length ( SurfaceToLight );
        if ( Distance > PointLights[LightIndex].Cutoff )
            continue;

        // Calculate normalized vectors and Lambert term
        vec3 NormalizedFragVertexNormalWorld = normalize( fragVertexNormalWorld );
        vec3 NormalizedSurfaceToLight = normalize( SurfaceToLight );
        float LambertTerm = max( dot( NormalizedSurfaceToLight,                         NormalizedFragVertexNormalWorld ), 0 ); // Should I use normalized here?

        // Compute the diffuse term.
        vec3 DiffuseResult = LambertTerm * PointLights[LightIndex].Intensity * PointLights[LightIndex].Color * Material.Diffuse.xyz;
        LightResult += DiffuseResult;

    // Compute specular
//  float SpecularCoefficient = 0.0;
//  if ( LambertTerm > 0.0 )
//      SpecularCoefficient = pow ( max ( 0.0, dot ( SurfaceToCamera, reflect(-SurfaceToLight, NormalizedFragVertexNormalWorld))), Material.Shininess);
//  vec3 SpecularResult = SpecularCoefficient * Material.Specular.xyz * PointLights[LightIndex].Color;
//  LightResult += SpecularResult;

        // Compute attenuation
        float Attenuation = PointLights[LightIndex].ConstantAttenuation + PointLights[LightIndex].LinearAttenuation * Distance + PointLights[LightIndex].ExponentialAttenuation * pow ( Distance, 2 );

    //  float Attenuation = 1.0 / (1.0 + PointLights[LightIndex].ConstantAttenuation * pow(Distance, 2));
        LightResult /= Attenuation;
        OverallResult += LightResult;
        }

    return OverallResult;
    }

void main()
    {
    vec4 TempFragColor = Material.Diffuse;
    vec3 LightResult = AmbientLight.Color * AmbientLight.Intensity;

    if ( IsTextured )
        TempFragColor *= texture(TextureSampler, fragTexCoord);
    LightResult = CalculateLights();

    FragColor = TempFragColor * vec4 ( LightResult, 1.0f );
    }

and this is the vert shader:

#version 300 es

in vec3 VertexPosition;
in vec3 VertexNormal;
in vec2 TexCoord;

uniform mat4 ModelViewProjectionMatrix;
uniform mat4 ModelMatrix;

out vec2 fragTexCoord;
out vec3 fragVertexNormalWorld;

out vec3 WorldFragPosition;

void main()
    {
    gl_Position = ModelViewProjectionMatrix * vec4(VertexPosition, 1.0);
    fragTexCoord = TexCoord;
    WorldFragPosition = (ModelMatrix * vec4 (VertexPosition,1.0f)).xyz;
    fragVertexNormalWorld = (ModelMatrix * vec4 (VertexNormal,0.0f)).xyz;
    }

where should i insert the new code? what should it be?

thanks.

\$\endgroup\$
  • \$\begingroup\$ Do it in the vertex shader. \$\endgroup\$ – Soapy Jul 2 '15 at 13:20
  • \$\begingroup\$ Edited: added the vertex shader code. \$\endgroup\$ – Joao Pincho Jul 2 '15 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.