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