Updated readability of the code.
[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  * It is used to store coordinates of vertices of 3D objects.
13  * It is also used to store coordinates of points in 3D space.
14  */
15 public class Vertex {
16
17     /**
18      * Vertex coordinate in 3D space.
19      */
20     public Point3D coordinate;
21
22     /**
23      * Vertex coordinate relative to the viewer after transformation.
24      * Visible vertices have positive z coordinate.
25      * Viewer is located at (0, 0, 0).
26      */
27     public Point3D transformedCoordinate;
28
29     /**
30      * Vertex coordinate in pixels relative to the top left corner of the screen after transformation.
31      */
32     public Point2D onScreenCoordinate;
33
34     /**
35      * The frame number when this vertex was last transformed.
36      */
37     private int lastTransformedFrame;
38
39     public Vertex() {
40         coordinate = new Point3D();
41         transformedCoordinate = new Point3D();
42         onScreenCoordinate = new Point2D();
43     }
44
45     public Vertex(final Point3D location) {
46         coordinate = location;
47         transformedCoordinate = new Point3D();
48         onScreenCoordinate = new Point2D();
49     }
50
51
52     /**
53      * Transforms vertex coordinate to calculate its location relative to the viewer.
54      * It also calculates its location on the screen.
55      *
56      * @param transforms Transforms pipeline.
57      * @param renderContext Rendering context.
58      */
59     public void calculateLocationRelativeToViewer(final TransformsStack transforms,
60                                                   final RenderingContext renderContext) {
61
62         if (lastTransformedFrame == renderContext.frameNumber)
63             return;
64
65         lastTransformedFrame = renderContext.frameNumber;
66
67         transforms.transform(coordinate, transformedCoordinate);
68
69         onScreenCoordinate.x = ((transformedCoordinate.x / transformedCoordinate.z) * renderContext.zoom);
70         onScreenCoordinate.y = ((transformedCoordinate.y / transformedCoordinate.z) * renderContext.zoom);
71         onScreenCoordinate.add(renderContext.centerCoordinate);
72     }
73 }