Removed ViewListener interface. Renamed View to ViewPanel.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / ShapeCollection.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;
11
12 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
13 import eu.svjatoslav.sixth.e3d.math.Transform;
14 import eu.svjatoslav.sixth.e3d.math.TransformPipe;
15 import eu.svjatoslav.sixth.e3d.gui.Avatar;
16 import eu.svjatoslav.sixth.e3d.gui.RenderingContext;
17 import eu.svjatoslav.sixth.e3d.gui.ViewContext;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
24 public class ShapeCollection {
25
26     private final RenderAggregator aggregator = new RenderAggregator();
27     private final TransformPipe transformPipe = new TransformPipe();
28     private final List<AbstractShape> shapes = new ArrayList<>();
29
30     public synchronized void addShape(final AbstractShape shape) {
31         shapes.add(shape);
32     }
33
34     public Collection<AbstractShape> getShapes() {
35         return shapes;
36     }
37
38     public void clear() {
39         shapes.clear();
40     }
41
42     public synchronized void paint(final ViewContext viewContext,
43                                    final RenderingContext renderingContext) {
44
45         renderingContext.frameNumber++;
46
47         aggregator.reset();
48         transformPipe.clear();
49
50         // translate scene according to camera current location
51         final Avatar avatar = viewContext.getAvatar();
52
53         // rotate scene according to avatar looking direction
54         transformPipe.addTransform(new Transform(new Point3D(), avatar
55                 .getAngleXZ(), avatar.getAngleYZ()));
56
57         // translate scene according to avatar location in the world
58         final Point3D translation = new Point3D(-avatar.getLocation().x,
59                 -avatar.getLocation().y, -avatar.getLocation().z);
60
61         transformPipe.addTransform(new Transform(translation, 0, 0));
62
63         for (final AbstractShape shape : shapes)
64             shape.transform(transformPipe, aggregator, renderingContext);
65
66         aggregator.paint(renderingContext);
67     }
68 }