Class SolidPolygon
This class serves as the unified polygon type for both rendering and CSG operations. It renders convex polygons by decomposing them into triangles using fan triangulation, and supports CSG operations directly without conversion to intermediate types.
Rendering:
- Fan triangulation for N-vertex polygons (N-2 triangles)
- Scanline rasterization with alpha blending
- Backface culling and flat shading support
- Mouse interaction via point-in-polygon testing
CSG Support:
- Lazy-computed plane for BSP operations
flip()for inverting polygon orientationdeepClone()for creating independent copies
Usage examples:
// Create a triangle
SolidPolygon triangle = new SolidPolygon(
new Point3D(0, 0, 0),
new Point3D(50, 0, 0),
new Point3D(25, 50, 0),
Color.RED
);
// Create a quad
SolidPolygon quad = SolidPolygon.quad(
new Point3D(-50, -50, 0),
new Point3D(50, -50, 0),
new Point3D(50, 50, 0),
new Point3D(-50, 50, 0),
Color.BLUE
);
// Use with CSG (via AbstractCompositeShape)
SolidPolygonRectangularBox box = new SolidPolygonRectangularBox(...);
box.subtract(sphere);
- See Also:
-
Field Summary
Fields inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape
onScreenZ, shapeId, verticesFields inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape
cachedBoundingBox, mouseInteractionController -
Constructor Summary
ConstructorsConstructorDescriptionSolidPolygon(Point3D[] vertices, Color color) Creates a solid polygon with the specified vertices and color.SolidPolygon(Point3D point1, Point3D point2, Point3D point3, Color color) Creates a solid triangle with the specified vertices and color.SolidPolygon(List<Point3D> points, Color color) Creates a solid polygon from a list of points and color. -
Method Summary
Modifier and TypeMethodDescriptionCreates a deep clone of this polygon.static voiddrawTriangle(RenderingContext context, Point2D onScreenPoint1, Point2D onScreenPoint2, Point2D onScreenPoint3, MouseInteractionController mouseInteractionController, Color color) Renders a triangle using scanline rasterization.voidflip()Flips the orientation of this polygon.static SolidPolygonfromVertices(List<Vertex> vertices, Color color) Creates a solid polygon from existing vertices.static SolidPolygonfromVertices(List<Vertex> vertices, Color color, boolean shadingEnabled) Creates a solid polygon from existing vertices with specified shading.getColor()Returns the fill color of this polygon.getPlane()Returns the plane containing this polygon.intReturns the number of vertices in this polygon.booleanChecks if backface culling is enabled for this polygon.booleanChecks if shading is enabled for this polygon.voidpaint(RenderingContext renderBuffer) Renders this polygon to the screen.static SolidPolygonCreates a quad (4-vertex polygon).voidsetBackfaceCulling(boolean backfaceCulling) Enables or disables backface culling for this polygon.voidSets the fill color of this polygon.voidsetShadingEnabled(boolean shadingEnabled) Enables or disables shading for this polygon.voidtransform(TransformStack transforms, RenderAggregator aggregator, RenderingContext renderingContext) Transforms vertices to screen space and computes lighting once per frame.static SolidPolygonCreates a triangle (3-vertex polygon).Methods inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractCoordinateShape
getBoundingBox, getZ, translateMethods inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape
invalidateBounds, setMouseInteractionController
-
Constructor Details
-
SolidPolygon
Creates a solid polygon with the specified vertices and color.- Parameters:
vertices- the vertices defining the polygon (must have at least 3)color- the fill color of the polygon- Throws:
IllegalArgumentException- if vertices is null or has fewer than 3 vertices
-
SolidPolygon
Creates a solid polygon from a list of points and color.- Parameters:
points- the list of points defining the polygon (must have at least 3)color- the fill color of the polygon- Throws:
IllegalArgumentException- if points is null or has fewer than 3 points
-
SolidPolygon
Creates a solid triangle with the specified vertices and color.- Parameters:
point1- the first vertex positionpoint2- the second vertex positionpoint3- the third vertex positioncolor- the fill color
-
-
Method Details
-
fromVertices
Creates a solid polygon from existing vertices.Used for CSG operations and cloning where vertices already exist. The vertex list is used directly (not copied), so callers should not modify the list after passing it to this method.
- Parameters:
vertices- the list of Vertex objects (used directly, not copied)color- the fill color of the polygon- Returns:
- a new SolidPolygon with the given vertices (shading disabled by default)
- Throws:
IllegalArgumentException- if vertices is null or has fewer than 3 vertices
-
fromVertices
Creates a solid polygon from existing vertices with specified shading.Used for CSG operations and cloning where vertices already exist. The vertex list is used directly (not copied), so callers should not modify the list after passing it to this method.
- Parameters:
vertices- the list of Vertex objects (used directly, not copied)color- the fill color of the polygonshadingEnabled- whether shading is enabled for this polygon- Returns:
- a new SolidPolygon with the given vertices and shading setting
- Throws:
IllegalArgumentException- if vertices is null or has fewer than 3 vertices
-
triangle
Creates a triangle (3-vertex polygon).- Parameters:
p1- the first vertexp2- the second vertexp3- the third vertexcolor- the fill color- Returns:
- a new SolidPolygon with 3 vertices
-
quad
Creates a quad (4-vertex polygon).- Parameters:
p1- the first vertexp2- the second vertexp3- the third vertexp4- the fourth vertexcolor- the fill color- Returns:
- a new SolidPolygon with 4 vertices
-
drawTriangle
public static void drawTriangle(RenderingContext context, Point2D onScreenPoint1, Point2D onScreenPoint2, Point2D onScreenPoint3, MouseInteractionController mouseInteractionController, Color color) Renders a triangle using scanline rasterization.This static method handles:
- Rounding vertices to integer screen coordinates
- Mouse hover detection via point-in-triangle test
- Viewport clipping
- Scanline rasterization with alpha blending
- Parameters:
context- the rendering contextonScreenPoint1- the first vertex in screen coordinatesonScreenPoint2- the second vertex in screen coordinatesonScreenPoint3- the third vertex in screen coordinatesmouseInteractionController- optional controller for mouse events, or nullcolor- the fill color
-
getVertexCount
public int getVertexCount()Returns the number of vertices in this polygon.- Returns:
- the vertex count
-
getColor
Returns the fill color of this polygon.- Returns:
- the polygon color
-
setColor
Sets the fill color of this polygon.- Parameters:
color- the new color
-
isShadingEnabled
public boolean isShadingEnabled()Checks if shading is enabled for this polygon.- Returns:
- true if shading is enabled, false otherwise
-
setShadingEnabled
public void setShadingEnabled(boolean shadingEnabled) Enables or disables shading for this polygon.- Parameters:
shadingEnabled- true to enable shading, false to disable
-
isBackfaceCullingEnabled
public boolean isBackfaceCullingEnabled()Checks if backface culling is enabled for this polygon.- Returns:
trueif backface culling is enabled
-
setBackfaceCulling
public void setBackfaceCulling(boolean backfaceCulling) Enables or disables backface culling for this polygon.- Parameters:
backfaceCulling-trueto enable backface culling
-
getPlane
Returns the plane containing this polygon.Computed from the first three vertices and cached for reuse. Used by BSP tree construction for spatial partitioning.
- Returns:
- the Plane containing this polygon
-
flip
public void flip()Flips the orientation of this polygon.Reverses the vertex order and negates vertex normals. Also flips the cached plane if computed. Used during CSG operations when inverting solids.
-
deepClone
Creates a deep clone of this polygon.Clones all vertices and preserves the color, shading, and backface culling settings. Used by CSG operations to create independent copies before modification.
- Returns:
- a new SolidPolygon with cloned data and preserved settings
-
paint
Renders this polygon to the screen.- Specified by:
paintin classAbstractCoordinateShape- Parameters:
renderBuffer- the rendering context containing the pixel buffer
-
transform
public void transform(TransformStack transforms, RenderAggregator aggregator, RenderingContext renderingContext) Transforms vertices to screen space and computes lighting once per frame.Overrides parent to add lighting computation during the single-threaded transform phase. This ensures lighting is calculated only once per polygon per frame, rather than once per render thread.
- Overrides:
transformin classAbstractCoordinateShape- Parameters:
transforms- the transform stack to applyaggregator- the render aggregator to queue shapes intorenderingContext- the rendering context
-