f456590cb7f96cc71aa7d5a94a76caf5e33d6b8e
[sixth-3d.git] /
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
9 import eu.svjatoslav.sixth.e3d.gui.ViewSpaceTracker;
10 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
11 import eu.svjatoslav.sixth.e3d.math.Transform;
12 import eu.svjatoslav.sixth.e3d.math.TransformStack;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.RenderAggregator;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.Line;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.texturedpolygon.TexturedPolygon;
19 import eu.svjatoslav.sixth.e3d.renderer.raster.slicer.Slicer;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 /**
25  * A composite shape that groups multiple sub-shapes into a single logical unit.
26  *
27  * <p>Use {@code AbstractCompositeShape} to build complex 3D objects by combining
28  * primitive shapes (lines, polygons, textured polygons) into a group that can be
29  * positioned, rotated, and manipulated as one entity. Sub-shapes can be organized
30  * into named groups for selective visibility toggling.</p>
31  *
32  * <p><b>Usage example - creating a custom composite shape:</b></p>
33  * <pre>{@code
34  * // Create a composite shape at position (0, 0, 200)
35  * AbstractCompositeShape myObject = new AbstractCompositeShape(
36  *     new Point3D(0, 0, 200)
37  * );
38  *
39  * // Add sub-shapes
40  * myObject.addShape(new Line(
41  *     new Point3D(-50, 0, 0), new Point3D(50, 0, 0),
42  *     Color.RED, 2.0
43  * ));
44  *
45  * // Add shapes to a named group for toggling visibility
46  * myObject.addShape(labelShape, "labels");
47  * myObject.hideGroup("labels");  // hide all shapes in "labels" group
48  * myObject.showGroup("labels");  // show them again
49  *
50  * // Add to scene
51  * viewPanel.getRootShapeCollection().addShape(myObject);
52  * }</pre>
53  *
54  * <p><b>Level-of-detail slicing:</b></p>
55  * <p>Textured polygons within the composite shape are automatically sliced into smaller
56  * triangles based on distance from the viewer. This provides perspective-correct texture
57  * mapping without requiring hardware support. The slicing factor adapts dynamically.</p>
58  *
59  * <p><b>Extending this class:</b></p>
60  * <p>Override {@link #beforeTransformHook} to customize shape appearance or behavior
61  * on each frame (e.g., animations, dynamic geometry updates).</p>
62  *
63  * @see SubShape wrapper for individual sub-shapes with group and visibility support
64  * @see eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape the base shape class
65  * @see eu.svjatoslav.sixth.e3d.renderer.raster.slicer.Slicer the level-of-detail polygon slicer
66  */
67 public class AbstractCompositeShape extends AbstractShape {
68     /**
69      * The original sub-shapes added to this composite, each wrapped with group
70      * identifier and visibility state. Shapes are stored in insertion order and
71      * remain in this collection even when hidden.
72      */
73     private final List<SubShape> originalSubShapes = new ArrayList<>();
74
75     /**
76      * Tracks the distance and angle between the camera and this shape to compute
77      * an appropriate slice factor for level-of-detail adjustments.
78      */
79     private final ViewSpaceTracker viewSpaceTracker;
80
81     /**
82      * The current slice factor used for tessellating textured polygons into smaller
83      * triangles for perspective-correct rendering. Higher values produce more triangles
84      * for distant objects; lower values for nearby objects. Updated dynamically based
85      * on view-space analysis.
86      */
87     double currentSliceFactor = 5;
88
89     /**
90      * The processed list of sub-shapes ready for rendering. Contains non-textured
91      * shapes directly, and sliced triangles for textured polygons. Regenerated when
92      * {@link #slicingOutdated} is true.
93      */
94     private List<AbstractShape> renderedSubShapes = new ArrayList<>();
95
96     /**
97      * Flag indicating whether the rendered sub-shapes need to be regenerated.
98      * Set to true when sub-shapes are added, removed, or when group visibility changes.
99      */
100     private boolean slicingOutdated = true;
101
102     /**
103      * The position and orientation transform for this composite shape.
104      * Applied to all sub-shapes during the rendering transform pass.
105      */
106     private Transform transform;
107
108     /**
109      * Creates a composite shape at the world origin with no rotation.
110      */
111     public AbstractCompositeShape() {
112         this(new Transform());
113     }
114
115     /**
116      * Creates a composite shape at the specified location with no rotation.
117      *
118      * @param location the position in world space
119      */
120     public AbstractCompositeShape(final Point3D location) {
121         this(new Transform(location));
122     }
123
124     /**
125      * Creates a composite shape with the specified transform (position and orientation).
126      *
127      * @param transform the initial transform defining position and rotation
128      */
129     public AbstractCompositeShape(final Transform transform) {
130         this.transform = transform;
131         viewSpaceTracker = new ViewSpaceTracker();
132     }
133
134     /**
135      * Adds a sub-shape to this composite shape without a group identifier.
136      *
137      * @param shape the shape to add
138      */
139     public void addShape(final AbstractShape shape) {
140         addShape(shape, null);
141     }
142
143     /**
144      * Adds a sub-shape to this composite shape with an optional group identifier.
145      *
146      * <p>Grouped shapes can be shown, hidden, or removed together using
147      * {@link #showGroup}, {@link #hideGroup}, and {@link #removeGroup}.</p>
148      *
149      * @param shape   the shape to add
150      * @param groupId the group identifier, or {@code null} for ungrouped shapes
151      */
152     public void addShape(final AbstractShape shape, final String groupId) {
153         originalSubShapes.add(new SubShape(shape, groupId, true));
154         slicingOutdated = true;
155     }
156
157     /**
158      * This method should be overridden by anyone wanting to customize the shape
159      * before it is rendered.
160      *
161      * @param transformPipe the current transform stack
162      * @param context       the rendering context for the current frame
163      */
164     public void beforeTransformHook(final TransformStack transformPipe,
165                                     final RenderingContext context) {
166     }
167
168     /**
169      * Returns the world-space position of this composite shape.
170      *
171      * @return the translation component of this shape's transform
172      */
173     public Point3D getLocation() {
174         return transform.getTranslation();
175     }
176
177     /**
178      * Returns the list of all sub-shapes (including hidden ones).
179      *
180      * @return the internal list of sub-shapes
181      */
182     public List<SubShape> getOriginalSubShapes() {
183         return originalSubShapes;
184     }
185
186     /**
187      * Returns the view-space tracker that monitors the distance
188      * and angle between the camera and this shape for level-of-detail adjustments.
189      *
190      * @return the view-space tracker for this shape
191      */
192     public ViewSpaceTracker getViewSpaceTracker() {
193         return viewSpaceTracker;
194     }
195
196     /**
197      * Hides all sub-shapes belonging to the specified group.
198      * Hidden shapes are not rendered but remain in the collection.
199      *
200      * @param groupIdentifier the group to hide
201      * @see #showGroup(String)
202      * @see #removeGroup(String)
203      */
204     public void hideGroup(final String groupIdentifier) {
205         for (final SubShape subShape : originalSubShapes) {
206             if (subShape.matchesGroup(groupIdentifier)) {
207                 subShape.setVisible(false);
208                 slicingOutdated = true;
209             }
210         }
211     }
212
213     /**
214      * Determines whether textured polygons need to be re-sliced based on slice factor change.
215      * <p>
216      * Re-slicing is needed if the slicing state is marked outdated, or if the ratio between
217      * the larger and smaller slice factor exceeds 1.5x. This threshold prevents frequent
218      * re-slicing for minor view changes while ensuring significant LOD changes trigger updates.
219      *
220      * @param proposedNewSliceFactor the slice factor computed from current view distance
221      * @param currentSliceFactor     the slice factor currently in use
222      * @return {@code true} if re-slicing should be performed
223      */
224     private boolean isReslicingNeeded(final double proposedNewSliceFactor, final double currentSliceFactor) {
225
226         if (slicingOutdated)
227             return true;
228
229         // reslice if there is significant difference between proposed and current slice factor
230         final double larger = Math.max(proposedNewSliceFactor, currentSliceFactor);
231         final double smaller = Math.min(proposedNewSliceFactor, currentSliceFactor);
232
233         return (larger / smaller) > 1.5d;
234     }
235
236     /**
237      * Permanently removes all sub-shapes belonging to the specified group.
238      *
239      * @param groupIdentifier the group to remove
240      * @see #hideGroup(String)
241      */
242     public void removeGroup(final String groupIdentifier) {
243         final java.util.Iterator<SubShape> iterator = originalSubShapes
244                 .iterator();
245
246         while (iterator.hasNext()) {
247             final SubShape subShape = iterator.next();
248             if (subShape.matchesGroup(groupIdentifier)) {
249                 iterator.remove();
250                 slicingOutdated = true;
251             }
252         }
253     }
254
255     /**
256      * Returns all sub-shapes belonging to the specified group.
257      *
258      * @param groupIdentifier the group identifier to match
259      * @return list of matching sub-shapes
260      */
261     public List<SubShape> getGroup(final String groupIdentifier) {
262         final List<SubShape> result = new ArrayList<>();
263         for (int i = 0; i < originalSubShapes.size(); i++) {
264             final SubShape subShape = originalSubShapes.get(i);
265             if (subShape.matchesGroup(groupIdentifier))
266                 result.add(subShape);
267         }
268         return result;
269     }
270
271     /**
272      * Checks if re-slicing is needed and performs it if so.
273      *
274      * @param context the rendering context for logging
275      */
276     private void resliceIfNeeded(final RenderingContext context) {
277
278         final double proposedSliceFactor = viewSpaceTracker.proposeSliceFactor();
279
280         if (isReslicingNeeded(proposedSliceFactor, currentSliceFactor)) {
281             currentSliceFactor = proposedSliceFactor;
282             reslice(context);
283         }
284     }
285
286     /**
287      * Paint solid elements of this composite shape into given color.
288      *
289      * @param color the color to apply to all solid sub-shapes
290      */
291     public void setColor(final Color color) {
292         for (final SubShape subShape : getOriginalSubShapes()) {
293             final AbstractShape shape = subShape.getShape();
294
295             if (shape instanceof SolidPolygon)
296                 ((SolidPolygon) shape).setColor(color);
297
298             if (shape instanceof Line)
299                 ((Line) shape).color = color;
300         }
301     }
302
303     /**
304      * Assigns a group identifier to all sub-shapes that currently have no group.
305      *
306      * @param groupIdentifier the group to assign to ungrouped shapes
307      */
308     public void setGroupForUngrouped(final String groupIdentifier) {
309         for (int i = 0; i < originalSubShapes.size(); i++) {
310             final SubShape subShape = originalSubShapes.get(i);
311             if (subShape.isUngrouped())
312                 subShape.setGroup(groupIdentifier);
313         }
314     }
315
316     @Override
317     public void setMouseInteractionController(
318             final MouseInteractionController mouseInteractionController) {
319         super.setMouseInteractionController(mouseInteractionController);
320
321         for (final SubShape subShape : originalSubShapes)
322             subShape.getShape().setMouseInteractionController(
323                     mouseInteractionController);
324
325         slicingOutdated = true;
326
327     }
328
329     /**
330      * Replaces this shape's transform (position and orientation).
331      *
332      * @param transform the new transform to apply
333      */
334     public void setTransform(final Transform transform) {
335         this.transform = transform;
336     }
337
338     /**
339      * Enables or disables shading for all SolidPolygon sub-shapes.
340      * When enabled, polygons use the global lighting manager from the rendering
341      * context to calculate flat shading based on light sources.
342      *
343      * @param shadingEnabled {@code true} to enable shading, {@code false} to disable
344      */
345     public void setShadingEnabled(final boolean shadingEnabled) {
346         for (final SubShape subShape : getOriginalSubShapes()) {
347             final AbstractShape shape = subShape.getShape();
348             if (shape instanceof SolidPolygon) {
349                 ((SolidPolygon) shape).setShadingEnabled(shadingEnabled);
350             }
351
352             // TODO: if shape is abstract composite, it seems that it would be good to enabled sharding recursively there too
353         }
354     }
355
356     /**
357      * Enables or disables backface culling for all SolidPolygon and TexturedPolygon sub-shapes.
358      *
359      * @param backfaceCulling {@code true} to enable backface culling, {@code false} to disable
360      */
361     public void setBackfaceCulling(final boolean backfaceCulling) {
362         for (final SubShape subShape : getOriginalSubShapes()) {
363             final AbstractShape shape = subShape.getShape();
364             if (shape instanceof SolidPolygon) {
365                 ((SolidPolygon) shape).setBackfaceCulling(backfaceCulling);
366             } else if (shape instanceof TexturedPolygon) {
367                 ((TexturedPolygon) shape).setBackfaceCulling(backfaceCulling);
368             }
369         }
370     }
371
372     /**
373      * Makes all sub-shapes belonging to the specified group visible.
374      *
375      * @param groupIdentifier the group to show
376      * @see #hideGroup(String)
377      */
378     public void showGroup(final String groupIdentifier) {
379         for (int i = 0; i < originalSubShapes.size(); i++) {
380             final SubShape subShape = originalSubShapes.get(i);
381             if (subShape.matchesGroup(groupIdentifier)) {
382                 subShape.setVisible(true);
383                 slicingOutdated = true;
384             }
385         }
386     }
387
388     /**
389      * Re-slices all textured polygons and rebuilds the rendered sub-shapes list.
390      * Logs the operation to the debug log buffer if available.
391      *
392      * @param context the rendering context for logging, may be {@code null}
393      */
394     private void reslice(final RenderingContext context) {
395         slicingOutdated = false;
396
397         final List<AbstractShape> result = new ArrayList<>();
398
399         final Slicer slicer = new Slicer(currentSliceFactor);
400         int texturedPolygonCount = 0;
401         int otherShapeCount = 0;
402
403         for (int i = 0; i < originalSubShapes.size(); i++) {
404             final SubShape subShape = originalSubShapes.get(i);
405             if (subShape.isVisible()) {
406                 if (subShape.getShape() instanceof TexturedPolygon) {
407                     slicer.slice((TexturedPolygon) subShape.getShape());
408                     texturedPolygonCount++;
409                 } else {
410                     result.add(subShape.getShape());
411                     otherShapeCount++;
412                 }
413             }
414         }
415
416         result.addAll(slicer.getResult());
417
418         renderedSubShapes = result;
419
420         // Log to developer tools console if available
421         if (context != null && context.debugLogBuffer != null) {
422             context.debugLogBuffer.log("reslice: " + getClass().getSimpleName()
423                     + " sliceFactor=" + String.format("%.2f", currentSliceFactor)
424                     + " texturedPolygons=" + texturedPolygonCount
425                     + " otherShapes=" + otherShapeCount
426                     + " resultingTexturedPolygons=" + slicer.getResult().size());
427         }
428     }
429
430     @Override
431     public void transform(final TransformStack transformPipe,
432                           final RenderAggregator aggregator, final RenderingContext context) {
433
434         // Add the current composite shape transform to the end of the transform
435         // pipeline.
436         transformPipe.addTransform(transform);
437
438         viewSpaceTracker.analyze(transformPipe, context);
439
440         beforeTransformHook(transformPipe, context);
441
442         resliceIfNeeded(context);
443
444         // transform rendered subshapes
445         for (final AbstractShape shape : renderedSubShapes)
446             shape.transform(transformPipe, aggregator, context);
447
448         transformPipe.dropTransform();
449     }
450
451 }