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 * It is used to store coordinates of vertices of 3D objects.
13 * It is also used to store coordinates of points in 3D space.
18 * Vertex coordinate in 3D space.
20 public Point3D coordinate;
23 * Vertex coordinate relative to the viewer after transformation.
24 * Visible vertices have positive z coordinate.
25 * Viewer is located at (0, 0, 0).
27 public Point3D transformedCoordinate;
30 * Vertex coordinate in pixels relative to the top left corner of the screen after transformation.
32 public Point2D onScreenCoordinate;
35 * The frame number when this vertex was last transformed.
37 private int lastTransformedFrame;
40 coordinate = new Point3D();
41 transformedCoordinate = new Point3D();
42 onScreenCoordinate = new Point2D();
45 public Vertex(final Point3D location) {
46 coordinate = location;
47 transformedCoordinate = new Point3D();
48 onScreenCoordinate = new Point2D();
53 * Transforms vertex coordinate to calculate its location relative to the viewer.
54 * It also calculates its location on the screen.
56 * @param transforms Transforms pipeline.
57 * @param renderContext Rendering context.
59 public void calculateLocationRelativeToViewer(final TransformsStack transforms,
60 final RenderingContext renderContext) {
62 if (lastTransformedFrame == renderContext.frameNumber)
65 lastTransformedFrame = renderContext.frameNumber;
67 transforms.transform(coordinate, transformedCoordinate);
69 onScreenCoordinate.x = ((transformedCoordinate.x / transformedCoordinate.z) * renderContext.zoom);
70 onScreenCoordinate.y = ((transformedCoordinate.y / transformedCoordinate.z) * renderContext.zoom);
71 onScreenCoordinate.add(renderContext.centerCoordinate);