2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base;
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.lighting.LightingManager;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.Line;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
19 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.texturedpolygon.TexturedPolygon;
20 import eu.svjatoslav.sixth.e3d.renderer.raster.slicer.Slicer;
22 import java.util.ArrayList;
23 import java.util.List;
26 * A composite shape that groups multiple sub-shapes into a single logical unit.
28 * <p>Use {@code AbstractCompositeShape} to build complex 3D objects by combining
29 * primitive shapes (lines, polygons, textured polygons) into a group that can be
30 * positioned, rotated, and manipulated as one entity. Sub-shapes can be organized
31 * into named groups for selective visibility toggling.</p>
33 * <p><b>Usage example - creating a custom composite shape:</b></p>
35 * // Create a composite shape at position (0, 0, 200)
36 * AbstractCompositeShape myObject = new AbstractCompositeShape(
37 * new Point3D(0, 0, 200)
41 * myObject.addShape(new Line(
42 * new Point3D(-50, 0, 0), new Point3D(50, 0, 0),
46 * // Add shapes to a named group for toggling visibility
47 * myObject.addShape(labelShape, "labels");
48 * myObject.hideGroup("labels"); // hide all shapes in "labels" group
49 * myObject.showGroup("labels"); // show them again
52 * viewPanel.getRootShapeCollection().addShape(myObject);
55 * <p><b>Level-of-detail slicing:</b></p>
56 * <p>Textured polygons within the composite shape are automatically sliced into smaller
57 * triangles based on distance from the viewer. This provides perspective-correct texture
58 * mapping without requiring hardware support. The slicing factor adapts dynamically.</p>
60 * <p><b>Extending this class:</b></p>
61 * <p>Override {@link #beforeTransformHook} to customize shape appearance or behavior
62 * on each frame (e.g., animations, dynamic geometry updates).</p>
64 * @see SubShape wrapper for individual sub-shapes with group and visibility support
65 * @see eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape the base shape class
66 * @see eu.svjatoslav.sixth.e3d.renderer.raster.slicer.Slicer the level-of-detail polygon slicer
68 public class AbstractCompositeShape extends AbstractShape {
69 private final List<SubShape> originalSubShapes = new ArrayList<>();
70 private final ViewSpaceTracker viewSpaceTracker;
71 double currentSliceFactor = 5;
72 private List<AbstractShape> renderedSubShapes = new ArrayList<>();
73 private boolean slicingOutdated = true;
74 private Transform transform;
75 private LightingManager lightingManager;
78 * Creates a composite shape at the world origin with no rotation.
80 public AbstractCompositeShape() {
81 this(new Transform());
85 * Creates a composite shape at the specified location with no rotation.
87 * @param location the position in world space
89 public AbstractCompositeShape(final Point3D location) {
90 this(new Transform(location));
94 * Creates a composite shape with the specified transform (position and orientation).
96 * @param transform the initial transform defining position and rotation
98 public AbstractCompositeShape(final Transform transform) {
99 this.transform = transform;
100 viewSpaceTracker = new ViewSpaceTracker();
104 * Adds a sub-shape to this composite shape without a group identifier.
106 * @param shape the shape to add
108 public void addShape(final AbstractShape shape) {
109 addShape(shape, null);
113 * Adds a sub-shape to this composite shape with an optional group identifier.
115 * <p>Grouped shapes can be shown, hidden, or removed together using
116 * {@link #showGroup}, {@link #hideGroup}, and {@link #removeGroup}.</p>
118 * @param shape the shape to add
119 * @param groupId the group identifier, or {@code null} for ungrouped shapes
121 public void addShape(final AbstractShape shape, final String groupId) {
122 final SubShape subShape = new SubShape(shape);
123 subShape.setGroup(groupId);
124 subShape.setVisible(true);
125 originalSubShapes.add(subShape);
126 slicingOutdated = true;
130 * This method should be overridden by anyone wanting to customize shape
131 * before it is rendered.
133 public void beforeTransformHook(final TransformStack transformPipe,
134 final RenderingContext context) {
138 * Returns the world-space position of this composite shape.
140 * @return the translation component of this shape's transform
142 public Point3D getLocation() {
143 return transform.getTranslation();
147 * Returns the list of all sub-shapes (including hidden ones).
149 * @return the internal list of sub-shapes
151 public List<SubShape> getOriginalSubShapes() {
152 return originalSubShapes;
156 * Returns the view-space tracker that monitors the distance
157 * and angle between the camera and this shape for level-of-detail adjustments.
159 * @return the view-space tracker for this shape
161 public ViewSpaceTracker getViewSpaceTracker() {
162 return viewSpaceTracker;
166 * Hides all sub-shapes belonging to the specified group.
167 * Hidden shapes are not rendered but remain in the collection.
169 * @param groupIdentifier the group to hide
170 * @see #showGroup(String)
171 * @see #removeGroup(String)
173 public void hideGroup(final String groupIdentifier) {
174 for (int i = 0; i < originalSubShapes.size(); i++) {
175 final SubShape subShape = originalSubShapes.get(i);
176 if (subShape.matchesGroup(groupIdentifier)) {
177 subShape.setVisible(false);
178 slicingOutdated = true;
183 private boolean isReslicingNeeded(double proposedNewSliceFactor, double currentSliceFactor) {
188 // reslice if there is significant difference between proposed and current slice factor
189 if (proposedNewSliceFactor > currentSliceFactor) {
190 final double tmp = proposedNewSliceFactor;
191 proposedNewSliceFactor = currentSliceFactor;
192 currentSliceFactor = tmp;
195 return (currentSliceFactor / proposedNewSliceFactor) > 1.5d;
199 * Permanently removes all sub-shapes belonging to the specified group.
201 * @param groupIdentifier the group to remove
202 * @see #hideGroup(String)
204 public void removeGroup(final String groupIdentifier) {
205 final java.util.Iterator<SubShape> iterator = originalSubShapes
208 while (iterator.hasNext()) {
209 final SubShape subShape = iterator.next();
210 if (subShape.matchesGroup(groupIdentifier)) {
212 slicingOutdated = true;
218 * Returns all sub-shapes belonging to the specified group.
220 * @param groupIdentifier the group identifier to match
221 * @return list of matching sub-shapes
223 public List<SubShape> getGroup(final String groupIdentifier) {
224 final List<SubShape> result = new ArrayList<>();
225 for (int i = 0; i < originalSubShapes.size(); i++) {
226 final SubShape subShape = originalSubShapes.get(i);
227 if (subShape.matchesGroup(groupIdentifier))
228 result.add(subShape);
233 private void resliceIfNeeded() {
235 final double proposedSliceFactor = viewSpaceTracker.proposeSliceFactor();
237 if (isReslicingNeeded(proposedSliceFactor, currentSliceFactor)) {
238 currentSliceFactor = proposedSliceFactor;
244 * Paint solid elements of this composite shape into given color.
246 public void setColor(final Color color) {
247 for (final SubShape subShape : getOriginalSubShapes()) {
248 final AbstractShape shape = subShape.getShape();
250 if (shape instanceof SolidPolygon)
251 ((SolidPolygon) shape).setColor(color);
253 if (shape instanceof Line)
254 ((Line) shape).color = color;
259 * Assigns a group identifier to all sub-shapes that currently have no group.
261 * @param groupIdentifier the group to assign to ungrouped shapes
263 public void setGroupForUngrouped(final String groupIdentifier) {
264 for (int i = 0; i < originalSubShapes.size(); i++) {
265 final SubShape subShape = originalSubShapes.get(i);
266 if (subShape.isUngrouped())
267 subShape.setGroup(groupIdentifier);
272 public void setMouseInteractionController(
273 final MouseInteractionController mouseInteractionController) {
274 super.setMouseInteractionController(mouseInteractionController);
276 for (final SubShape subShape : originalSubShapes)
277 subShape.getShape().setMouseInteractionController(
278 mouseInteractionController);
280 slicingOutdated = true;
285 * Replaces this shape's transform (position and orientation).
287 * @param transform the new transform to apply
289 public void setTransform(final Transform transform) {
290 this.transform = transform;
294 * Sets the lighting manager for this composite shape and enables shading on all SolidPolygon sub-shapes.
296 * @param lightingManager the lighting manager to use for shading calculations
298 public void setLightingManager(final LightingManager lightingManager) {
299 this.lightingManager = lightingManager;
300 applyShadingToPolygons();
304 * Enables or disables shading for all SolidPolygon sub-shapes.
306 * @param shadingEnabled true to enable shading, false to disable
308 public void setShadingEnabled(final boolean shadingEnabled) {
309 for (final SubShape subShape : getOriginalSubShapes()) {
310 final AbstractShape shape = subShape.getShape();
311 if (shape instanceof SolidPolygon) {
312 ((SolidPolygon) shape).setShadingEnabled(shadingEnabled, lightingManager);
317 private void applyShadingToPolygons() {
318 if (lightingManager == null)
321 for (final SubShape subShape : getOriginalSubShapes()) {
322 final AbstractShape shape = subShape.getShape();
323 if (shape instanceof SolidPolygon) {
324 ((SolidPolygon) shape).setShadingEnabled(true, lightingManager);
329 public void setBackfaceCulling(final boolean backfaceCulling) {
330 for (final SubShape subShape : getOriginalSubShapes()) {
331 final AbstractShape shape = subShape.getShape();
332 if (shape instanceof SolidPolygon) {
333 ((SolidPolygon) shape).setBackfaceCulling(backfaceCulling);
334 } else if (shape instanceof TexturedPolygon) {
335 ((TexturedPolygon) shape).setBackfaceCulling(backfaceCulling);
341 * Makes all sub-shapes belonging to the specified group visible.
343 * @param groupIdentifier the group to show
344 * @see #hideGroup(String)
346 public void showGroup(final String groupIdentifier) {
347 for (int i = 0; i < originalSubShapes.size(); i++) {
348 final SubShape subShape = originalSubShapes.get(i);
349 if (subShape.matchesGroup(groupIdentifier)) {
350 subShape.setVisible(true);
351 slicingOutdated = true;
356 private void reslice() {
357 slicingOutdated = false;
359 final List<AbstractShape> result = new ArrayList<>();
361 final Slicer slicer = new Slicer(currentSliceFactor);
362 for (int i = 0; i < originalSubShapes.size(); i++) {
363 final SubShape subShape = originalSubShapes.get(i);
364 if (subShape.isVisible()) {
365 if (subShape.getShape() instanceof TexturedPolygon)
366 slicer.slice((TexturedPolygon) subShape.getShape());
368 result.add(subShape.getShape());
372 result.addAll(slicer.getResult());
374 renderedSubShapes = result;
378 public void transform(final TransformStack transformPipe,
379 final RenderAggregator aggregator, final RenderingContext context) {
381 // add current composite shape transform to the end of the transform
383 transformPipe.addTransform(transform);
385 viewSpaceTracker.analyze(transformPipe, context);
387 beforeTransformHook(transformPipe, context);
389 // hack, to get somewhat perspective correct textures
392 // transform rendered subshapes
393 for (final AbstractShape shape : renderedSubShapes)
394 shape.transform(transformPipe, aggregator, context);
396 transformPipe.dropTransform();