0c77868b426dc844cd666376f5eeb24b3fa106d0
[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      */
25     public Point3D transformedCoordinate;
26
27     /**
28      * Vertex coordinate on screen after transformation.
29      */
30     public Point2D onScreenCoordinate;
31
32     private int lastTransformedFrame;
33
34     public Vertex() {
35         coordinate = new Point3D();
36         transformedCoordinate = new Point3D();
37         onScreenCoordinate = new Point2D();
38     }
39
40     public Vertex(final Point3D location) {
41         coordinate = location;
42         transformedCoordinate = new Point3D();
43         onScreenCoordinate = new Point2D();
44     }
45
46     /**
47      * Transforms the coordinate.
48      *
49      * @param transforms The transforms pipeline.
50      * @param renderContext The rendering context.
51      */
52     public void transform(final TransformsPipeline transforms,
53                           final RenderingContext renderContext) {
54
55         if (lastTransformedFrame == renderContext.frameNumber)
56             return;
57
58         lastTransformedFrame = renderContext.frameNumber;
59
60         transforms.transform(coordinate, transformedCoordinate);
61
62         onScreenCoordinate.x = ((transformedCoordinate.x / transformedCoordinate.z) * renderContext.zoom);
63         onScreenCoordinate.y = ((transformedCoordinate.y / transformedCoordinate.z) * renderContext.zoom);
64         onScreenCoordinate.add(renderContext.centerCoordinate);
65     }
66 }