Search code examples
c++openglglsl2dlighting

How do I convert my point light into an oval/ellipse?


I am looking to turn my current circular light into an ellipse by having a vec2 radius which can have different x and y values. Is there any way to do this based on my current code in the fragment shader?

uniform struct Light
{
vec4 colour;
vec3 position;
vec2 radius;
float intensity;
} allLights[MAX_LIGHTS];

vec4 calculateLight(Light light)
{
vec2 lightDir = fragmentPosition.xy - light.position.xy;
float lightDistance = length(lightDir);
if (lightDistance >= light.radius.x)
{
    return vec4(0, 0, 0, 1); //outside of radius make it black
}
return light.intensity * (1 - lightDistance / light.radius.x) * light.colour;
}

Solution

  • Divide the vector to the light source with the semi-axis of the ellipse and check whether the length of the vector is greater than 1.0:

    if (length(lightDir / light.radius) >= 1.0)
        return vec4(0, 0, 0, 1); //outside of radius make it black
    return light.intensity * (1 - length(lightDir / light.radius)) * light.colour;