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;
39 * Coordinate within texture.
41 public Point2D textureCoordinate;
45 * The frame number when this vertex was last transformed.
47 private int lastTransformedFrame;
53 public Vertex(final Point3D location) {
57 public Vertex(final Point3D location, Point2D textureCoordinate) {
58 coordinate = location;
59 transformedCoordinate = new Point3D();
60 onScreenCoordinate = new Point2D();
61 this.textureCoordinate = textureCoordinate;
66 * Transforms vertex coordinate to calculate its location relative to the viewer.
67 * It also calculates its location on the screen.
69 * @param transforms Transforms pipeline.
70 * @param renderContext Rendering context.
72 public void calculateLocationRelativeToViewer(final TransformsStack transforms,
73 final RenderingContext renderContext) {
75 if (lastTransformedFrame == renderContext.frameNumber)
78 lastTransformedFrame = renderContext.frameNumber;
80 transforms.transform(coordinate, transformedCoordinate);
82 onScreenCoordinate.x = ((transformedCoordinate.x / transformedCoordinate.z) * renderContext.zoom);
83 onScreenCoordinate.y = ((transformedCoordinate.y / transformedCoordinate.z) * renderContext.zoom);
84 onScreenCoordinate.add(renderContext.centerCoordinate);