0

I am doing A raytraycer with c# and I am trying to add lights. My scene has one Spherical light in front and one behind the Spheres(I haven't implemented shadows yet). However, it doesn't seem to look right when multiple light sources are turned on. Are there any mistakes in my implementation ?

Scene with two light sources turned on

Scene with one light source

The code for computing the light

     protected Color Shading(Vector3 position, List<Lightsource> lightSources, Color 
    color, Vector3 normal, float albedo)
    {
        var finalColor = Color.Black;
        
        foreach (var lightSource in lightSources)
        {
            var posToLightVector = lightSource.Position - position;
            var lightDir = Vector3.Normalize(posToLightVector);
            var lightDot = Math.Max(Vector3.Dot(lightDir,normal), 0);
            var lightReflected = albedo / Math.PI;
            var lightPower = lightDot * lightSource.Intensity;
            var newColor = calculateColorValue(color, lightPower, lightReflected);
            finalColor = AddColors(finalColor, newColor);
        }

        return finalColor;
    }

    private Color calculateColorValue(Color colorValue, float lightPower, double lightReflected)
    {
        var r = ((float)colorValue.R / 255) * lightPower * lightReflected;
        var g = ((float)colorValue.G / 255) * lightPower * lightReflected;
        var b = ((float)colorValue.B / 255) * lightPower * lightReflected;
        return Color.FromArgb(Math.Min((int)(r * 255), 255), Math.Min((int)(g * 255), 255), Math.Min((int)(b * 255), 255));
    }


    private static Color AddColors(Color color1, Color color2)
    {
        return Color.FromArgb(Math.Min(255, color1.R + color2.R), Math.Min(255, color1.G + color2.G), Math.Min(255, color1.B + color2.B));
    }
  • It seems you are not factoring light distance into the equation, or at least not giving it enough weight. It's been years, but I used `1/dist * factor` as a factor to make distance scaleable. In addition, you need to calculate the intersections between the object and the light sources to see if any other object is shadowing the current object and you also need to trace the reflecting/refracting rays as well. My raytracer(with comments): https://stackoverflow.com/questions/33054399/raytracing-lighting-equations/33091767#33091767. Hope this helps! – Koto Feb 27 '22 at 11:28
  • I do not see a check if the light is visible from the position. Like @Koto mentioned you need to scale the light intensity. Most common is to use the invers squared distance to the light source. – theVortr3x Mar 09 '22 at 11:00

0 Answers0