5151import java .io .IOException ;
5252
5353/**
54- * PointLightShadowRenderer renders shadows for a point light
54+ * Renders shadows for a {@link PointLight}. This renderer uses six cameras,
55+ * one for each face of a cube map, to capture shadows from the point light's
56+ * perspective.
5557 *
5658 * @author Nehon
5759 */
5860public class PointLightShadowRenderer extends AbstractShadowRenderer {
5961
62+ /**
63+ * The fixed number of cameras used for rendering point light shadows (6 for a cube map).
64+ */
6065 public static final int CAM_NUMBER = 6 ;
66+
6167 protected PointLight light ;
6268 protected Camera [] shadowCams ;
63- private Geometry [] frustums = null ;
64- private final Vector3f X_NEG = Vector3f .UNIT_X .mult (-1f );
65- private final Vector3f Y_NEG = Vector3f .UNIT_Y .mult (-1f );
66- private final Vector3f Z_NEG = Vector3f .UNIT_Z .mult (-1f );
69+ protected Geometry [] frustums = null ;
70+ protected final Vector3f X_NEG = Vector3f .UNIT_X .mult (-1f );
71+ protected final Vector3f Y_NEG = Vector3f .UNIT_Y .mult (-1f );
72+ protected final Vector3f Z_NEG = Vector3f .UNIT_Z .mult (-1f );
6773
6874 /**
6975 * For serialization only. Do not use.
@@ -73,10 +79,11 @@ protected PointLightShadowRenderer() {
7379 }
7480
7581 /**
76- * Creates a PointLightShadowRenderer.
82+ * Creates a new {@code PointLightShadowRenderer} instance .
7783 *
78- * @param assetManager the application's asset manager
79- * @param shadowMapSize the size of the rendered shadow maps (512, 1024, 2048, etc...)
84+ * @param assetManager The application's asset manager.
85+ * @param shadowMapSize The size of the rendered shadow maps (e.g., 512, 1024, 2048).
86+ * Higher values produce better quality shadows but may impact performance.
8087 */
8188 public PointLightShadowRenderer (AssetManager assetManager , int shadowMapSize ) {
8289 super (assetManager , shadowMapSize , CAM_NUMBER );
@@ -85,7 +92,7 @@ public PointLightShadowRenderer(AssetManager assetManager, int shadowMapSize) {
8592
8693 private void init (int shadowMapSize ) {
8794 shadowCams = new Camera [CAM_NUMBER ];
88- for (int i = 0 ; i < CAM_NUMBER ; i ++) {
95+ for (int i = 0 ; i < shadowCams . length ; i ++) {
8996 shadowCams [i ] = new Camera (shadowMapSize , shadowMapSize );
9097 }
9198 }
@@ -106,24 +113,20 @@ protected void updateShadowCams(Camera viewCam) {
106113 return ;
107114 }
108115
109- //bottom
110- shadowCams [0 ].setAxes (X_NEG , Z_NEG , Y_NEG );
111- //top
112- shadowCams [1 ].setAxes (X_NEG , Vector3f .UNIT_Z , Vector3f .UNIT_Y );
113- //forward
114- shadowCams [2 ].setAxes (X_NEG , Vector3f .UNIT_Y , Z_NEG );
115- //backward
116- shadowCams [3 ].setAxes (Vector3f .UNIT_X , Vector3f .UNIT_Y , Vector3f .UNIT_Z );
117- //left
118- shadowCams [4 ].setAxes (Vector3f .UNIT_Z , Vector3f .UNIT_Y , X_NEG );
119- //right
120- shadowCams [5 ].setAxes (Z_NEG , Vector3f .UNIT_Y , Vector3f .UNIT_X );
121-
122- for (int i = 0 ; i < CAM_NUMBER ; i ++) {
123- shadowCams [i ].setFrustumPerspective (90f , 1f , 0.1f , light .getRadius ());
124- shadowCams [i ].setLocation (light .getPosition ());
125- shadowCams [i ].update ();
126- shadowCams [i ].updateViewProjection ();
116+ // Configure axes for each of the six cube map cameras (positive/negative X, Y, Z)
117+ shadowCams [0 ].setAxes (X_NEG , Z_NEG , Y_NEG ); // -Y (bottom)
118+ shadowCams [1 ].setAxes (X_NEG , Vector3f .UNIT_Z , Vector3f .UNIT_Y ); // +Y (top)
119+ shadowCams [2 ].setAxes (X_NEG , Vector3f .UNIT_Y , Z_NEG ); // +Z (forward)
120+ shadowCams [3 ].setAxes (Vector3f .UNIT_X , Vector3f .UNIT_Y , Vector3f .UNIT_Z ); // -Z (backward)
121+ shadowCams [4 ].setAxes (Vector3f .UNIT_Z , Vector3f .UNIT_Y , X_NEG ); // -X (left)
122+ shadowCams [5 ].setAxes (Z_NEG , Vector3f .UNIT_Y , Vector3f .UNIT_X ); // +X (right)
123+
124+ // Set perspective and location for all shadow cameras
125+ for (Camera shadowCam : shadowCams ) {
126+ shadowCam .setFrustumPerspective (90f , 1f , 0.1f , light .getRadius ());
127+ shadowCam .setLocation (light .getPosition ());
128+ shadowCam .update ();
129+ shadowCam .updateViewProjection ();
127130 }
128131 }
129132
@@ -153,16 +156,17 @@ protected void doDisplayFrustumDebug(int shadowMapIndex) {
153156 if (frustums == null ) {
154157 frustums = new Geometry [CAM_NUMBER ];
155158 Vector3f [] points = new Vector3f [8 ];
156- for (int i = 0 ; i < 8 ; i ++) {
159+ for (int i = 0 ; i < points . length ; i ++) {
157160 points [i ] = new Vector3f ();
158161 }
159162 for (int i = 0 ; i < CAM_NUMBER ; i ++) {
160163 ShadowUtil .updateFrustumPoints2 (shadowCams [i ], points );
161164 frustums [i ] = createFrustum (points , i );
162165 }
163166 }
164- if (frustums [shadowMapIndex ].getParent () == null ) {
165- ((Node ) viewPort .getScenes ().get (0 )).attachChild (frustums [shadowMapIndex ]);
167+ Geometry geo = frustums [shadowMapIndex ];
168+ if (geo .getParent () == null ) {
169+ ((Node ) viewPort .getScenes ().get (0 )).attachChild (geo );
166170 }
167171 }
168172
0 commit comments