Fixed git clone URL
[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     /**
39      * Coordinate within texture.
40      */
41     public Point2D textureCoordinate;
42
43
44     /**
45      * The frame number when this vertex was last transformed.
46      */
47     private int lastTransformedFrame;
48
49     public Vertex() {
50         this(new Point3D());
51     }
52
53     public Vertex(final Point3D location) {
54         this(location, null);
55     }
56
57     public Vertex(final Point3D location, Point2D textureCoordinate) {
58         coordinate = location;
59         transformedCoordinate = new Point3D();
60         onScreenCoordinate = new Point2D();
61         this.textureCoordinate = textureCoordinate;
62     }
63
64
65     /**
66      * Transforms vertex coordinate to calculate its location relative to the viewer.
67      * It also calculates its location on the screen.
68      *
69      * @param transforms    Transforms pipeline.
70      * @param renderContext Rendering context.
71      */
72     public void calculateLocationRelativeToViewer(final TransformsStack transforms,
73                                                   final RenderingContext renderContext) {
74
75         if (lastTransformedFrame == renderContext.frameNumber)
76             return;
77
78         lastTransformedFrame = renderContext.frameNumber;
79
80         transforms.transform(coordinate, transformedCoordinate);
81
82         onScreenCoordinate.x = ((transformedCoordinate.x / transformedCoordinate.z) * renderContext.zoom);
83         onScreenCoordinate.y = ((transformedCoordinate.y / transformedCoordinate.z) * renderContext.zoom);
84         onScreenCoordinate.add(renderContext.centerCoordinate);
85     }
86 }