Improved code readability.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / math / Vertex.java
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 (file)
index 0000000..0c77868
--- /dev/null
@@ -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);
+    }
+}