Class LightingManager

java.lang.Object
eu.svjatoslav.sixth.e3d.renderer.raster.lighting.LightingManager

public class LightingManager extends Object
Manages light sources in the scene and calculates lighting for polygons.

This class implements flat shading using the Lambert cosine law. For each polygon face, it calculates the surface normal and determines how much light each source contributes based on the angle between the normal and the light direction.

The lighting calculation considers:

  • Distance from polygon center to each light source
  • Angle between surface normal and light direction
  • Color and intensity of each light source

Usage example:


 LightingManager lighting = new LightingManager();

 // Add light sources
 lighting.addLight(new LightSource(new Point3D(100, -50, 200), Color.YELLOW));
 lighting.addLight(new LightSource(new Point3D(-100, 50, 200), Color.BLUE));

 // Set ambient light (base illumination)
 lighting.setAmbientLight(new Color(30, 30, 30));

 // Calculate shaded color for a polygon (reusing result Color to avoid allocation)
 Color result = new Color();
 lighting.computeLighting(polygonCenter, surfaceNormal, baseColor, result);
 
See Also:
  • Constructor Details

    • LightingManager

      public LightingManager()
      Creates a new lighting manager with no light sources.
  • Method Details

    • addLight

      public void addLight(LightSource light)
      Adds a light source to the scene.
      Parameters:
      light - the light source to add
    • computeLighting

      public void computeLighting(Point3D polygonCenter, Point3D normal, Color baseColor, Color result)
      Computes lighting for a polygon and stores the result in an existing Color.

      This method avoids allocation by reusing an existing Color instance. Safe to call from multiple threads on the same result Color - the computation is deterministic (same polygon, same lights = same result).

      Parameters:
      polygonCenter - the center point of the polygon in world space
      normal - the surface normal vector (should be normalized)
      baseColor - the original color of the polygon
      result - the Color to receive the shaded result (modified in place)
    • getAmbientLight

      public Color getAmbientLight()
      Returns the ambient light color.
      Returns:
      the ambient light color
    • setAmbientLight

      public void setAmbientLight(Color ambientLight)
      Sets the ambient light color for the scene.

      Ambient light provides base illumination that affects all surfaces equally, regardless of their orientation.

      Parameters:
      ambientLight - the ambient light color
    • getLights

      public List<LightSource> getLights()
      Returns all light sources in the scene.
      Returns:
      list of light sources
    • removeLight

      public void removeLight(LightSource light)
      Removes a light source from the scene.
      Parameters:
      light - the light source to remove