Improved code readability
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / math / Vertex.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.math;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
8 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
9 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
10
11 /**
12  * Vertex is a point where two or more lines, line segments, or rays come together.
13  * In other words, it's a corner of a polygon, polyhedron, or other geometric shape.
14  * For example, a triangle has three vertices, a square has four, and a cube has eight.
15  */
16 public class Vertex {
17
18     /**
19      * Vertex coordinate in 3D space.
20      */
21     public Point3D coordinate;
22
23     /**
24      * Vertex coordinate relative to the viewer after transformation.
25      * Visible vertices have positive z coordinate.
26      * Viewer is located at (0, 0, 0).
27      * No perspective correction is applied.
28      */
29     public Point3D transformedCoordinate;
30
31     /**
32      * Vertex coordinate in pixels relative to the top left corner of the screen after transformation
33      * and perspective correction.
34      */
35     public Point2D onScreenCoordinate;
36
37     /**
38      * The frame number when this vertex was last transformed.
39      */
40     private int lastTransformedFrame;
41
42     public Vertex() {
43         coordinate = new Point3D();
44         transformedCoordinate = new Point3D();
45         onScreenCoordinate = new Point2D();
46     }
47
48     public Vertex(final Point3D location) {
49         coordinate = location;
50         transformedCoordinate = new Point3D();
51         onScreenCoordinate = new Point2D();
52     }
53
54
55     /**
56      * Transforms vertex coordinate to calculate its location relative to the viewer.
57      * It also calculates its location on the screen.
58      *
59      * @param transforms    Transforms pipeline.
60      * @param renderContext Rendering context.
61      */
62     public void calculateLocationRelativeToViewer(final TransformsStack transforms,
63                                                   final RenderingContext renderContext) {
64
65         if (lastTransformedFrame == renderContext.frameNumber)
66             return;
67
68         lastTransformedFrame = renderContext.frameNumber;
69
70         transforms.transform(coordinate, transformedCoordinate);
71
72         onScreenCoordinate.x = ((transformedCoordinate.x / transformedCoordinate.z) * renderContext.zoom);
73         onScreenCoordinate.y = ((transformedCoordinate.y / transformedCoordinate.z) * renderContext.zoom);
74         onScreenCoordinate.add(renderContext.centerCoordinate);
75     }
76 }