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.solid;
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
9 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
13 * A solid (filled) rectangular box composed of 12 triangular polygons (2 per face,
14 * covering all 6 faces). Each face is rendered as a pair of {@link SolidPolygon}
15 * triangles with the same color.
17 * <p>The box can be constructed either from a center point and a uniform size
18 * (producing a cube), or from two diagonally opposite corner points (producing
19 * an arbitrary axis-aligned rectangular box).</p>
21 * <p>The vertices are labeled p1 through p8, representing the eight corners of
22 * the box. The triangles are arranged to cover the bottom, top, front, back,
23 * left, and right faces.</p>
25 * <p><b>Usage example:</b></p>
27 * // From center and size:
28 * SolidPolygonRectangularBox box1 = new SolidPolygonRectangularBox(
29 * new Point3D(0, 0, 200), 100, Color.RED);
31 * // From two corner points:
32 * SolidPolygonRectangularBox box2 = new SolidPolygonRectangularBox(
33 * new Point3D(-50, -25, 100), new Point3D(50, 25, 200), Color.BLUE);
35 * shapeCollection.addShape(box1);
38 * @see SolidPolygonCube
40 * @see AbstractCompositeShape
42 public class SolidPolygonRectangularBox extends AbstractCompositeShape {
45 * Constructs a solid rectangular box between two diagonally opposite corner
46 * points in 3D space. The eight vertices of the box are derived from the
47 * coordinate components of {@code p1} and {@code p7}. All six faces are
48 * tessellated into two triangles each, for a total of 12 solid polygons.
50 * @param p1 the first corner point (minimum coordinates by convention)
51 * @param p7 the diagonally opposite corner point (maximum coordinates)
52 * @param color the fill color applied to all 12 triangular polygons
54 public SolidPolygonRectangularBox(final Point3D p1, final Point3D p7, final Color color) {
57 final Point3D p2 = new Point3D(p7.x, p1.y, p1.z);
58 final Point3D p3 = new Point3D(p7.x, p1.y, p7.z);
59 final Point3D p4 = new Point3D(p1.x, p1.y, p7.z);
61 final Point3D p5 = new Point3D(p1.x, p7.y, p1.z);
62 final Point3D p6 = new Point3D(p7.x, p7.y, p1.z);
63 final Point3D p8 = new Point3D(p1.x, p7.y, p7.z);
65 // Bottom face (y = minY)
66 addShape(new SolidPolygon(p1, p2, p3, color));
67 addShape(new SolidPolygon(p1, p3, p4, color));
69 // Top face (y = maxY)
70 addShape(new SolidPolygon(p5, p8, p7, color));
71 addShape(new SolidPolygon(p5, p7, p6, color));
73 // Front face (z = minZ)
74 addShape(new SolidPolygon(p1, p5, p6, color));
75 addShape(new SolidPolygon(p1, p6, p2, color));
77 // Back face (z = maxZ)
78 addShape(new SolidPolygon(p3, p7, p8, color));
79 addShape(new SolidPolygon(p3, p8, p4, color));
81 // Left face (x = minX)
82 addShape(new SolidPolygon(p1, p4, p8, color));
83 addShape(new SolidPolygon(p1, p8, p5, color));
85 // Right face (x = maxX)
86 addShape(new SolidPolygon(p2, p6, p7, color));
87 addShape(new SolidPolygon(p2, p7, p3, color));
89 setBackfaceCulling(true);