4e36d2ebd249403b183ec1e94017705b059fa680
[sixth-3d.git] / AbstractCoordinateShape.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes;
11
12 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
13 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
14 import eu.svjatoslav.sixth.e3d.math.GeometryCoordinate;
15 import eu.svjatoslav.sixth.e3d.math.TransformPipe;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.RenderAggregator;
17
18 import java.util.concurrent.atomic.AtomicInteger;
19
20 public abstract class AbstractCoordinateShape extends AbstractShape {
21
22     public static final AtomicInteger lastShapeId = new AtomicInteger();
23     public final int shapeId;
24     public final GeometryCoordinate[] coordinates;
25     public double onScreenZ;
26
27     public AbstractCoordinateShape(final int pointsCount) {
28         coordinates = new GeometryCoordinate[pointsCount];
29         for (int i = 0; i < pointsCount; i++)
30             coordinates[i] = new GeometryCoordinate();
31
32         shapeId = lastShapeId.getAndIncrement();
33     }
34
35     public AbstractCoordinateShape(final Point3D... locationPoints) {
36         coordinates = new GeometryCoordinate[locationPoints.length];
37
38         for (int i = 0; i < locationPoints.length; i++)
39             coordinates[i] = new GeometryCoordinate(locationPoints[i]);
40
41         shapeId = lastShapeId.getAndIncrement();
42     }
43
44     public double getZ() {
45         return onScreenZ;
46     }
47
48     public abstract void paint(RenderingContext renderBuffer);
49
50     @Override
51     public void transform(final TransformPipe transforms,
52                           final RenderAggregator aggregator,
53                           final RenderingContext renderingContext) {
54
55         double accumulatedZ = 0;
56         boolean paint = true;
57
58         for (final GeometryCoordinate geometryPoint : coordinates) {
59             geometryPoint.transform(transforms, renderingContext);
60
61             accumulatedZ += geometryPoint.transformedCoordinate.z;
62
63             if (!geometryPoint.transformedCoordinate.isVisible())
64                 paint = false;
65         }
66
67         if (paint) {
68             onScreenZ = accumulatedZ / coordinates.length;
69             aggregator.queueShapeForRendering(this);
70         }
71     }
72
73 }