1+ package scene .light ;
2+
3+ import math .Color ;
4+ import math .Vector3f ;
5+
6+ /**
7+ * Represents a directional light source in a 3D scene.
8+ *
9+ * <p>
10+ * A directional light simulates light emitted from a distant source, such as
11+ * the sun or moon. Unlike point lights or spotlights, directional lights have
12+ * no specific position, and their light rays travel in a uniform direction
13+ * throughout the scene. This makes them ideal for creating consistent lighting
14+ * over large areas.
15+ * </p>
16+ *
17+ * <pre>
18+ * Key characteristics of a directional light:
19+ * - The light's direction is defined by a normalized vector.
20+ * - It emits light uniformly in the specified direction, without attenuation
21+ * (intensity does not decrease with distance).
22+ * - It is commonly used to simulate natural light sources like sunlight during
23+ * the day or moonlight at night.
24+ * </pre>
25+ *
26+ * This class provides methods to configure the light's direction, color, and
27+ * intensity, as well as integration with rendering systems via the
28+ * {@link LightRenderer}.
29+ */
30+ public class DirectionalLight implements Light {
31+
32+ /**
33+ * The color of the light emitted by the directional light source.
34+ */
35+ private Color color ;
36+
37+ /**
38+ * The direction of the light source.
39+ */
40+ private Vector3f direction ;
41+
42+ /**
43+ * The intensity of the light emitted by the directional light source.
44+ */
45+ private float intensity ;
46+
47+ /**
48+ * Creates a new DirectionalLight instance with default settings.
49+ *
50+ * <p>
51+ * This constructor initializes the light with the following defaults: -
52+ * Color: White light. RGB(255, 255, 255) - Direction: A downward-facing
53+ * vector (0, 1, 0), simulating overhead light. - Intensity: 1.0 (full
54+ * strength).
55+ * </p>
56+ */
57+ public DirectionalLight () {
58+ this (Color .WHITE , new Vector3f (0 , 1 , 0 ), 1.0f );
59+ }
60+
61+ /**
62+ * Creates a new DirectionalLight instance.
63+ *
64+ * @param color The color of the light emitted by the directional light
65+ * source. Represents the RGB values of the light's color.
66+ * This parameter cannot be null.
67+ * @param direction The direction of the light source. This vector determines
68+ * the direction in which the light rays travel. The provided
69+ * vector is automatically normalized during construction,
70+ * ensuring the direction's magnitude is always 1. This
71+ * parameter cannot be null.
72+ * @param intensity The intensity of the light emitted by the directional
73+ * light source. This value must be non-negative.
74+ *
75+ * @throws IllegalArgumentException if the direction or color is null, or if
76+ * the intensity is negative.
77+ */
78+ public DirectionalLight (Color color , Vector3f direction , float intensity ) {
79+ setColor (color );
80+ setDirection (direction );
81+ setIntensity (intensity );
82+ }
83+
84+ /**
85+ * Gets the direction of the light source.
86+ *
87+ * @return The direction of the light source.
88+ * @see #setDirection(Vector3f)
89+ */
90+ public Vector3f getDirection () {
91+ return direction ;
92+ }
93+
94+ /**
95+ * Sets the direction of the directional light source.
96+ *
97+ * The provided vector is normalized to ensure the light's direction always
98+ * has a magnitude of 1, which maintains consistent light behavior. This
99+ * method validates that the input is not null to avoid runtime errors.
100+ *
101+ * @param direction The new direction vector for the light source. This vector
102+ * defines the direction in which the light rays travel.
103+ *
104+ * @throws IllegalArgumentException if the provided direction vector is null.
105+ */
106+ public void setDirection (Vector3f direction ) {
107+ if (direction == null )
108+ throw new IllegalArgumentException ("Direction cannot be null." );
109+ this .direction = direction .normalize ();
110+ }
111+
112+ /**
113+ * Gets the color of the light emitted by the directional light source.
114+ *
115+ * @return The color of the light.
116+ * @see #setColor(Color)
117+ */
118+ @ Override
119+ public Color getColor () {
120+ return color ;
121+ }
122+
123+ /**
124+ * Sets the color of the directional light source.
125+ *
126+ * This method updates the light's emitted color. It validates that the
127+ * provided color is not null to ensure the light's color is always valid.
128+ *
129+ * @param color The new color of the light to set. Represents the RGB values
130+ * of the light's color.
131+ *
132+ * @throws IllegalArgumentException if the provided color is null.
133+ */
134+ public void setColor (Color color ) {
135+ if (color == null )
136+ throw new IllegalArgumentException ("Color cannot be null." );
137+ this .color = color ;
138+ }
139+
140+ /**
141+ * Gets the intensity of the light emitted by the directional light source.
142+ *
143+ * @return The intensity of the light.
144+ * @see #setIntensity(float)
145+ */
146+ public float getIntensity () {
147+ return intensity ;
148+ }
149+
150+ /**
151+ * Sets the intensity of the light emitted by the directional light source.
152+ *
153+ * The intensity value determines how bright the light appears in the scene.
154+ * This method ensures that the value is non-negative, as negative intensity
155+ * does not make logical sense in this context.
156+ *
157+ * @param intensity The new intensity value to set for the light source. Must
158+ * be non-negative to represent valid light brightness.
159+ *
160+ * @throws IllegalArgumentException if the provided intensity is negative.
161+ */
162+ public void setIntensity (float intensity ) {
163+ if (intensity < 0 )
164+ throw new IllegalArgumentException ("Intensity must be non-negative." );
165+ this .intensity = intensity ;
166+ }
167+
168+ /**
169+ * Gets the type of the light source.
170+ *
171+ * @return The type of the light source, which is `LightType.DIRECTIONAL`.
172+ */
173+ @ Override
174+ public LightType getType () {
175+ return LightType .DIRECTIONAL ;
176+ }
177+
178+ /**
179+ * Renders the directional light source using the provided renderer.
180+ *
181+ * @param renderer The renderer to use for rendering the light source.
182+ */
183+ @ Override
184+ public void render (LightRenderer renderer ) {
185+ renderer .render (this );
186+ }
187+
188+ /**
189+ * Provides a string representation of this directional light instance for
190+ * debugging.
191+ *
192+ * @return String describing the current state of the directional light.
193+ */
194+ @ Override
195+ public String toString () {
196+ return "DirectionalLight [color=" + color + ", direction=" + direction
197+ + ", intensity=" + intensity + "]" ;
198+ }
199+
200+ }
0 commit comments