From: Svjatoslav Agejenko Date: Sun, 26 Feb 2023 07:49:34 +0000 (+0200) Subject: Improved code readability. X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=de8fb260a5e99922231b1d0f437916e796ec6ccb;p=sixth-3d.git Improved code readability. --- diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java index b6417e5..ce8c4a0 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java @@ -4,6 +4,7 @@ */ package eu.svjatoslav.sixth.e3d.gui; +import eu.svjatoslav.sixth.e3d.geometry.Point2D; import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseEvent; import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController; @@ -19,8 +20,12 @@ public class RenderingContext { public final byte[] pixels; public final int width; public final int height; - public final int xCenter; - public final int yCenter; + /** + * Center of the screen in screen space (pixels). + * This is the point where (0,0) coordinate of the world space is rendered. + */ + public final Point2D centerCoordinate; + public final double zoom; final BufferedImage bufferedImage; public int frameNumber = 0; @@ -65,10 +70,7 @@ public class RenderingContext { public RenderingContext(final int width, final int height) { this.width = width; this.height = height; - - this.xCenter = width / 2; - this.yCenter = height / 2; - + this.centerCoordinate = new Point2D(width / 2d, height / 2d); this.zoom = width / 3d; bufferedImage = new BufferedImage(width, height, bufferedImageType); 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 4ebca1a..06363ea 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java @@ -5,15 +5,15 @@ package eu.svjatoslav.sixth.e3d.gui; import eu.svjatoslav.sixth.e3d.geometry.Point3D; -import eu.svjatoslav.sixth.e3d.math.GeometryCoordinate; +import eu.svjatoslav.sixth.e3d.math.Vertex; import eu.svjatoslav.sixth.e3d.math.TransformsPipeline; public class UserRelativityTracker { private final static int minimalSliceFactor = 5; - public GeometryCoordinate center = new GeometryCoordinate(); - public GeometryCoordinate right; - public GeometryCoordinate down; + public Vertex center = new Vertex(); + public Vertex right; + public Vertex down; public UserRelativityTracker() { @@ -31,8 +31,8 @@ public class UserRelativityTracker { } public void enableOrientationTracking() { - right = new GeometryCoordinate(new Point3D(10, 0, 0)); - down = new GeometryCoordinate(new Point3D(0, 10, 0)); + right = new Vertex(new Point3D(10, 0, 0)); + down = new Vertex(new Point3D(0, 10, 0)); } public double getAngleXY() { diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/math/GeometryCoordinate.java b/src/main/java/eu/svjatoslav/sixth/e3d/math/GeometryCoordinate.java deleted file mode 100644 index af3d3d3..0000000 --- a/src/main/java/eu/svjatoslav/sixth/e3d/math/GeometryCoordinate.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Sixth 3D engine. Author: Svjatoslav Agejenko. - * This project is released under Creative Commons Zero (CC0) license. - */ -package eu.svjatoslav.sixth.e3d.math; - -import eu.svjatoslav.sixth.e3d.geometry.Point2D; -import eu.svjatoslav.sixth.e3d.geometry.Point3D; -import eu.svjatoslav.sixth.e3d.gui.RenderingContext; - -/** - * A point in 3D space with a transformed and on-screen coordinates. - */ -public class GeometryCoordinate { - - /** - * The original coordinate. - */ - public Point3D coordinate; - - /** - * The transformed coordinate. - */ - public Point3D transformedCoordinate; - - /** - * The on-screen coordinate. - */ - public Point2D onScreenCoordinate; - - private int lastTransformedFrame; - - public GeometryCoordinate() { - coordinate = new Point3D(); - transformedCoordinate = new Point3D(); - onScreenCoordinate = new Point2D(); - } - - public GeometryCoordinate(final Point3D location) { - coordinate = location; - transformedCoordinate = new Point3D(); - onScreenCoordinate = new Point2D(); - } - - /** - * Transforms the coordinate. - * - * @param transforms - * The transform pipe. - * @param renderContext - * The rendering context. - */ - public void transform(final TransformsPipeline transforms, - final RenderingContext renderContext) { - - if (lastTransformedFrame == renderContext.frameNumber) - return; - - lastTransformedFrame = renderContext.frameNumber; - - transforms.transform(coordinate, transformedCoordinate); - - onScreenCoordinate.x = ((transformedCoordinate.x / transformedCoordinate.z) * renderContext.zoom) - + renderContext.xCenter; - onScreenCoordinate.y = ((transformedCoordinate.y / transformedCoordinate.z) * renderContext.zoom) - + renderContext.yCenter; - } -} diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java b/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java new file mode 100644 index 0000000..0c77868 --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java @@ -0,0 +1,66 @@ +/* + * Sixth 3D engine. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. + */ +package eu.svjatoslav.sixth.e3d.math; + +import eu.svjatoslav.sixth.e3d.geometry.Point2D; +import eu.svjatoslav.sixth.e3d.geometry.Point3D; +import eu.svjatoslav.sixth.e3d.gui.RenderingContext; + +/** + * It is used to store coordinates of vertices of 3D objects. + * It is also used to store coordinates of points in 3D space. + */ +public class Vertex { + + /** + * Vertex coordinate in 3D space. + */ + public Point3D coordinate; + + /** + * Vertex coordinate relative to the viewer after transformation. + */ + public Point3D transformedCoordinate; + + /** + * Vertex coordinate on screen after transformation. + */ + public Point2D onScreenCoordinate; + + private int lastTransformedFrame; + + public Vertex() { + coordinate = new Point3D(); + transformedCoordinate = new Point3D(); + onScreenCoordinate = new Point2D(); + } + + public Vertex(final Point3D location) { + coordinate = location; + transformedCoordinate = new Point3D(); + onScreenCoordinate = new Point2D(); + } + + /** + * Transforms the coordinate. + * + * @param transforms The transforms pipeline. + * @param renderContext The rendering context. + */ + public void transform(final TransformsPipeline transforms, + final RenderingContext renderContext) { + + if (lastTransformedFrame == renderContext.frameNumber) + return; + + lastTransformedFrame = renderContext.frameNumber; + + transforms.transform(coordinate, transformedCoordinate); + + onScreenCoordinate.x = ((transformedCoordinate.x / transformedCoordinate.z) * renderContext.zoom); + onScreenCoordinate.y = ((transformedCoordinate.y / transformedCoordinate.z) * renderContext.zoom); + onScreenCoordinate.add(renderContext.centerCoordinate); + } +} diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/AbstractCoordinateShape.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/AbstractCoordinateShape.java index 649e546..f2fdde0 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/AbstractCoordinateShape.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/AbstractCoordinateShape.java @@ -6,7 +6,7 @@ package eu.svjatoslav.sixth.e3d.renderer.raster.shapes; import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.gui.RenderingContext; -import eu.svjatoslav.sixth.e3d.math.GeometryCoordinate; +import eu.svjatoslav.sixth.e3d.math.Vertex; import eu.svjatoslav.sixth.e3d.math.TransformsPipeline; import eu.svjatoslav.sixth.e3d.renderer.raster.RenderAggregator; @@ -16,22 +16,22 @@ public abstract class AbstractCoordinateShape extends AbstractShape { private static final AtomicInteger lastShapeId = new AtomicInteger(); public final int shapeId; - public final GeometryCoordinate[] coordinates; + public final Vertex[] coordinates; public double onScreenZ; public AbstractCoordinateShape(final int pointsCount) { - coordinates = new GeometryCoordinate[pointsCount]; + coordinates = new Vertex[pointsCount]; for (int i = 0; i < pointsCount; i++) - coordinates[i] = new GeometryCoordinate(); + coordinates[i] = new Vertex(); shapeId = lastShapeId.getAndIncrement(); } public AbstractCoordinateShape(final Point3D... vertexes) { - coordinates = new GeometryCoordinate[vertexes.length]; + coordinates = new Vertex[vertexes.length]; for (int i = 0; i < vertexes.length; i++) - coordinates[i] = new GeometryCoordinate(vertexes[i]); + coordinates[i] = new Vertex(vertexes[i]); shapeId = lastShapeId.getAndIncrement(); } @@ -50,7 +50,7 @@ public abstract class AbstractCoordinateShape extends AbstractShape { double accumulatedZ = 0; boolean paint = true; - for (final GeometryCoordinate geometryPoint : coordinates) { + for (final Vertex geometryPoint : coordinates) { geometryPoint.transform(transforms, renderingContext); accumulatedZ += geometryPoint.transformedCoordinate.z;