From: Svjatoslav Agejenko Date: Wed, 21 Jun 2023 21:19:36 +0000 (+0300) Subject: Improved code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=9f20345e99404017a98d3ae95b7588cabb31a9bf;p=sixth-3d.git Improved code readability --- diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java index dd706db..d491b0f 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java @@ -18,8 +18,22 @@ import eu.svjatoslav.sixth.e3d.math.Vertex; public class UserRelativityTracker { private final static int minimalSliceFactor = 5; + + /** + * Points to 0, 0, 0 in object own relative space. + */ public Vertex center = new Vertex(); + + /** + * Points to 10, 0, 0 in object own relative space if orientation tracking is enabled. + * It is used to determine the angle between the user and the object. + */ public Vertex right; + + /** + * Points to 0, 10, 0 in object own relative space if orientation tracking is enabled. + * It is used to determine the angle between the user and the object. + */ public Vertex down; public UserRelativityTracker() { @@ -31,32 +45,53 @@ public class UserRelativityTracker { center.calculateLocationRelativeToViewer(transformPipe, renderingContext); - if (right != null) { + if (right != null) { // If orientation tracking is enabled. right.calculateLocationRelativeToViewer(transformPipe, renderingContext); down.calculateLocationRelativeToViewer(transformPipe, renderingContext); } } + /** + * Initializes the orientation tracking. + * Orientation tracking is used to determine the angle between the user and the object. + * Orientation tracking is disabled by default and it is optional. + */ public void enableOrientationTracking() { right = new Vertex(new Point3D(10, 0, 0)); down = new Vertex(new Point3D(0, 10, 0)); } + /** + * Calculates the angle between the user and the object in the XY plane. + * @return the angle between the user and the object in the XY plane. + */ public double getAngleXY() { return center.transformedCoordinate .getAngleXY(down.transformedCoordinate); } + /** + * Calculates the angle between the user and the object in the XZ plane. + * @return the angle between the user and the object in the XZ plane. + */ public double getAngleXZ() { return center.transformedCoordinate .getAngleXZ(right.transformedCoordinate); } + /** + * Calculates the angle between the user and the object in the YZ plane. + * @return the angle between the user and the object in the YZ plane. + */ public double getAngleYZ() { return center.transformedCoordinate .getAngleYZ(down.transformedCoordinate); } + /** + * Calculates the distance between the user and the object. + * Distance to the user can be used to determine object detail level. + */ public double getDistanceToUser() { return center.transformedCoordinate.getVectorLength(); } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java index 0f40895..2cd60e7 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/texturedpolygon/TexturedPolygon.java @@ -15,13 +15,22 @@ import eu.svjatoslav.sixth.e3d.renderer.raster.texture.TextureBitmap; import java.awt.*; import static eu.svjatoslav.sixth.e3d.geometry.Polygon.pointWithinPolygon; +import static java.util.Arrays.sort; + +/** + * Textured polygon. + *

+ * + *

+ * This is how perspective-correct texture rendering is implemented:
+ * If polygon is sufficiently small, it is rendered without perspective correction.
+ * Otherwise, it is sliced into smaller polygons.
+ * 
+ */ public class TexturedPolygon extends AbstractCoordinateShape { public final Texture texture; - final PolygonBorderInterpolator[] is = new PolygonBorderInterpolator[]{ - new PolygonBorderInterpolator(), new PolygonBorderInterpolator(), - new PolygonBorderInterpolator()}; /** * Polygon texture coordinates. */ @@ -176,8 +185,7 @@ public class TexturedPolygon extends AbstractCoordinateShape { yBottom = renderBuffer.height - 1; // paint - double totalVisibleDistance = projectedPoint1 - .getDistanceTo(projectedPoint2); + double totalVisibleDistance = projectedPoint1.getDistanceTo(projectedPoint2); totalVisibleDistance += projectedPoint1.getDistanceTo(projectedPoint3); totalVisibleDistance += projectedPoint2.getDistanceTo(projectedPoint3); @@ -187,6 +195,10 @@ public class TexturedPolygon extends AbstractCoordinateShape { final TextureBitmap zoomedBitmap = texture.getZoomedBitmap(scaleFactor); + final PolygonBorderInterpolator[] is = new PolygonBorderInterpolator[]{ + new PolygonBorderInterpolator(), new PolygonBorderInterpolator(), + new PolygonBorderInterpolator()}; + is[0].setPoints(projectedPoint1, projectedPoint2, texturePoint1, texturePoint2); is[1].setPoints(projectedPoint1, projectedPoint3, texturePoint1, @@ -194,7 +206,7 @@ public class TexturedPolygon extends AbstractCoordinateShape { is[2].setPoints(projectedPoint2, projectedPoint3, texturePoint2, texturePoint3); - java.util.Arrays.sort(is); + sort(is); for (int y = yTop; y < yBottom; y++) if (is[0].containsY(y)) { diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/base/AbstractCompositeShape.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/base/AbstractCompositeShape.java index 55c81e8..52acbc2 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/base/AbstractCompositeShape.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/base/AbstractCompositeShape.java @@ -22,11 +22,6 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; -/** - * In order to get perspective correct textures, large textured polygons are - * sliced into smaller ones. - */ - public class AbstractCompositeShape extends AbstractShape { private final List originalSubShapes = new ArrayList<>(); private final UserRelativityTracker relativityTracker; @@ -87,19 +82,19 @@ public class AbstractCompositeShape extends AbstractShape { }); } - private boolean isReslicingNeeded(double sliceFactor1, double sliceFactor2) { + private boolean isReslicingNeeded(double proposedNewSliceFactor, double currentSliceFactor) { if (slicingOutdated) return true; - if (sliceFactor1 > sliceFactor2) { - final double tmp = sliceFactor1; - sliceFactor1 = sliceFactor2; - sliceFactor2 = tmp; + // reslice if there is significant difference between proposed and current slice factor + if (proposedNewSliceFactor > currentSliceFactor) { + final double tmp = proposedNewSliceFactor; + proposedNewSliceFactor = currentSliceFactor; + currentSliceFactor = tmp; } - return (sliceFactor2 / sliceFactor1) > 1.5d; - + return (currentSliceFactor / proposedNewSliceFactor) > 1.5d; } public void removeGroup(final String groupIdentifier) { @@ -123,12 +118,11 @@ public class AbstractCompositeShape extends AbstractShape { private void resliceIfNeeded() { - final double proposedSliceFactor = relativityTracker - .proposeSliceFactor(); + final double proposedSliceFactor = relativityTracker.proposeSliceFactor(); if (isReslicingNeeded(proposedSliceFactor, currentSliceFactor)) { currentSliceFactor = proposedSliceFactor; - slice(); + reslice(); } } @@ -175,7 +169,7 @@ public class AbstractCompositeShape extends AbstractShape { }); } - private void slice() { + private void reslice() { slicingOutdated = false; final List result = new ArrayList<>(); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/PolygonCoordinate.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/PolygonCoordinate.java index 328896d..eb4745a 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/PolygonCoordinate.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/PolygonCoordinate.java @@ -11,6 +11,8 @@ import eu.svjatoslav.sixth.e3d.geometry.Point3D; * Polygon coordinate. * This class is used to store coordinates of a point in 3D space and its * texture coordinate. + * + * TODO: Refactor this class out of existence. Use Vertex instead. Texture coordinates should be moved to Vertex. */ public class PolygonCoordinate {