2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.math;
7 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
8 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
9 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
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.
19 * Vertex coordinate in 3D space.
21 public Point3D coordinate;
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.
29 public Point3D transformedCoordinate;
32 * Vertex coordinate in pixels relative to the top left corner of the screen after transformation
33 * and perspective correction.
35 public Point2D onScreenCoordinate;
38 * The frame number when this vertex was last transformed.
40 private int lastTransformedFrame;
43 coordinate = new Point3D();
44 transformedCoordinate = new Point3D();
45 onScreenCoordinate = new Point2D();
48 public Vertex(final Point3D location) {
49 coordinate = location;
50 transformedCoordinate = new Point3D();
51 onScreenCoordinate = new Point2D();
56 * Transforms vertex coordinate to calculate its location relative to the viewer.
57 * It also calculates its location on the screen.
59 * @param transforms Transforms pipeline.
60 * @param renderContext Rendering context.
62 public void calculateLocationRelativeToViewer(final TransformsStack transforms,
63 final RenderingContext renderContext) {
65 if (lastTransformedFrame == renderContext.frameNumber)
68 lastTransformedFrame = renderContext.frameNumber;
70 transforms.transform(coordinate, transformedCoordinate);
72 onScreenCoordinate.x = ((transformedCoordinate.x / transformedCoordinate.z) * renderContext.zoom);
73 onScreenCoordinate.y = ((transformedCoordinate.y / transformedCoordinate.z) * renderContext.zoom);
74 onScreenCoordinate.add(renderContext.centerCoordinate);