897a12b73a74f1befc9eb72661570f6a3cf969fc
[aukio-3d.git] /
1 package eu.svjatoslav.aukio.e3d.renderer.raster.shapes.basic.texturedpolygon;
2
3 import eu.svjatoslav.aukio.e3d.geometry.Point2D;
4 import eu.svjatoslav.aukio.e3d.renderer.raster.RenderingContext;
5 import eu.svjatoslav.aukio.e3d.renderer.raster.RenderAggregator;
6 import eu.svjatoslav.aukio.e3d.math.TransformStack;
7
8 /**
9  * Paint/sort handle for one triangle of a {@link TriangleMeshBlock}. The
10  * triangle's geometry lives in the block's flat arrays (SoA layout); this
11  * object exists only so the existing sort/bin/paint pipeline — typed on
12  * individual shapes — can address one triangle. Handles are allocated once
13  * at block build time and reused every frame; {@link #transform} is a
14  * no-op because the block transforms all its triangles in one tight loop.
15  *
16  * <p>Per-slot screen data is read from the block's arrays through the
17  * overridden accessors, so the Z-comparator and tile binning see exactly
18  * the values an object-backed {@link TexturedTriangle} would expose.</p>
19  */
20 final class MeshTriangle extends TexturedTriangle {
21
22     /**
23      * Scratch Point2D carriers for the flat paint call. Interpolators
24      * hold references to the screen/UV points they are given, so the
25      * objects must stay stable for the duration of one paint — but a
26      * paint never nests, so three screen + three UV points per thread
27      * suffice and nothing is allocated per triangle.
28      */
29     private static final ThreadLocal<Point2D[]> SCREEN_SCRATCH =
30             ThreadLocal.withInitial(() -> new Point2D[]{
31                     new Point2D(), new Point2D(), new Point2D()});
32     private static final ThreadLocal<Point2D[]> UV_SCRATCH =
33             ThreadLocal.withInitial(() -> new Point2D[]{
34                     new Point2D(), new Point2D(), new Point2D()});
35
36     private final TriangleMeshBlock block;
37     private final int index;
38
39     MeshTriangle(final TriangleMeshBlock block, final int index,
40                  final eu.svjatoslav.aukio.e3d.renderer.raster.texture.Texture texture) {
41         super(texture);
42         this.block = block;
43         this.index = index;
44     }
45
46     /**
47      * No-op: the owning block transforms all its triangles (including this
48      * one) in a single flat-array loop. Queued handles are never
49      * transformed through the shape path. Sort Z and screen bounds live in
50      * the inherited per-slot fields, written by the block via
51      * {@code setSlotScreenState}.
52      */
53     @Override
54     public void transform(final TransformStack transforms,
55                           final RenderAggregator aggregator,
56                           final RenderingContext renderingContext) {
57         // intentionally empty — see TriangleMeshBlock.transform
58     }
59
60     /**
61      * Publishes this triangle's per-slot screen state (called by the
62      * owning block during its flat transform loop). Trampoline: the
63      * inherited setter is protected, so the call must go through the
64      * subclass.
65      */
66     void publishSlotState(final int slot, final double z,
67                           final double minY, final double maxY,
68                           final double minX, final double maxX) {
69         setSlotScreenState(slot, z, minY, maxY, minX, maxX);
70     }
71
72     @Override
73     public void paint(final RenderingContext renderBuffer) {
74         final int slot = renderBuffer.vertexSlot;
75         final Point2D[] screen = SCREEN_SCRATCH.get();
76         final Point2D[] uvs = UV_SCRATCH.get();
77
78         // Per-triangle GI light (null while no GI system traces this
79         // block): the span loops multiply texels by it. Values are read
80         // racily from the GI threads — fine for converging estimates.
81         final float[] giLight = block.giLight();
82         if (giLight != null) {
83             final int i = index * 3;
84             giShadeR = giLight[i];
85             giShadeG = giLight[i + 1];
86             giShadeB = giLight[i + 2];
87             giShaded = true;
88         }
89
90         final int clip = block.clipOffset(slot, index);
91         if (clip >= 0) {
92             // Near-plane straddler: the block clipped to a loop of 3-4
93             // vertices, stored as (x, y, z, u, v, sx, sy) tuples. Paint
94             // as a fan, mirroring TexturedTriangle.paint's clipped path.
95             final int count = block.clipCount(slot, index);
96             final double[] store = block.clipStore(slot);
97             loadClipVertex(screen[0], uvs[0], store, clip);
98             for (int i = 1; i + 1 < count; i++) {
99                 loadClipVertex(screen[1], uvs[1], store, clip + i * 7);
100                 loadClipVertex(screen[2], uvs[2], store, clip + (i + 1) * 7);
101                 // Fan sub-triangle screen perimeter — same expression as
102                 // the object path (edge12 + edge13 + edge23); straddlers
103                 // are rare, so it is computed here rather than stored.
104                 final double dx01 = screen[0].x - screen[1].x;
105                 final double dy01 = screen[0].y - screen[1].y;
106                 final double dx02 = screen[0].x - screen[2].x;
107                 final double dy02 = screen[0].y - screen[2].y;
108                 final double dx12 = screen[1].x - screen[2].x;
109                 final double dy12 = screen[1].y - screen[2].y;
110                 final double visPerimeter = Math.sqrt(dx01 * dx01 + dy01 * dy01)
111                         + Math.sqrt(dx02 * dx02 + dy02 * dy02)
112                         + Math.sqrt(dx12 * dx12 + dy12 * dy12);
113                 paintFlat(renderBuffer, block.texture(index), block.backfaceCull(),
114                         screen[0], screen[1], screen[2],
115                         uvs[0], uvs[1], uvs[2],
116                         store[clip + 2],
117                         store[clip + i * 7 + 2],
118                         store[clip + (i + 1) * 7 + 2],
119                         visPerimeter,
120                         block.clipTtd(slot, clip));
121             }
122             return;
123         }
124
125         block.loadScreenVertex(screen[0], uvs[0], slot, index, 0, renderBuffer);
126         block.loadScreenVertex(screen[1], uvs[1], slot, index, 1, renderBuffer);
127         block.loadScreenVertex(screen[2], uvs[2], slot, index, 2, renderBuffer);
128         paintFlat(renderBuffer, block.texture(index), block.backfaceCull(),
129                 screen[0], screen[1], screen[2],
130                 uvs[0], uvs[1], uvs[2],
131                 block.camZ(slot, index, 0),
132                 block.camZ(slot, index, 1),
133                 block.camZ(slot, index, 2),
134                 block.screenPerim(slot, index),
135                 block.uvPerimeter(index));
136     }
137
138     /**
139      * Fills the scratch screen/UV points from one clip-loop entry; screen
140      * coordinates were stored at clip time with the exact
141      * {@code Vertex.setCameraSpaceCoordinate} expression.
142      */
143     private void loadClipVertex(final Point2D screen, final Point2D uv,
144                                 final double[] store, final int entry) {
145         screen.x = store[entry + 5];
146         screen.y = store[entry + 6];
147         uv.x = store[entry + 3];
148         uv.y = store[entry + 4];
149     }
150 }