Class TriangleMeshBlock

java.lang.Object
eu.svjatoslav.aukio.e3d.renderer.raster.shapes.AbstractShape
eu.svjatoslav.aukio.e3d.renderer.raster.shapes.basic.texturedpolygon.TriangleMeshBlock

public final class TriangleMeshBlock extends AbstractShape
A block of textured triangles stored as flat primitive arrays (struct-of-arrays) instead of one object graph per triangle. Built once (off the render thread), then every frame a single tight loop applies the composed camera transform to all vertices — sequential memory access instead of pointer chasing through Vertex/Point3D soup, which is what made the transform phase memory-latency-bound.

Interaction with the rest of the pipeline is unchanged: each visible triangle is queued as a thin preallocated MeshTriangle handle into the same RenderAggregator, sorted by the same comparator, binned into the same tile grid, and painted by the same TexturedTriangle.paintFlat(RenderingContext, Texture, boolean, Point2D, Point2D, Point2D, Point2D, Point2D, Point2D, double, double, double, double, double) core — bit-exact with the object-backed path.

Subpixel-cull verdicts are cached per triangle (cullEpoch): while the verdict epoch holds, a culled triangle costs one integer compare per frame — no vertex math at all.

Global illumination: the GI system traces block triangles like any other scene triangle and writes per-triangle light totals (ambient + direct with shadows + indirect bounce) into giLight. The paint path multiplies texture texels by that per-triangle light; while no GI system has stamped the block the array is null and painting is bit-identical to the unlit path. Render-side cost: one array read and three multiplies per triangle — ray casting happens only on the dedicated GI threads.

Limitations vs object-backed triangles: no mouse picking, no SDF textures (rejected at build). Per-texel lightmaps exist only inside the GI stage-3 radius bubble (see lightmapAt(int)); outside it GI lands per triangle — the right granularity for far game meshes.

  • Field Summary

  • Constructor Summary

    Constructors
    Constructor
    Description
    TriangleMeshBlock(double[] world, double[] uv, Texture[] textures, boolean backfaceCull)
    Builds a block from baked world-space triangle soup.
  • Method Summary

    Modifier and Type
    Method
    Description
    albedo(int tri)
    Average color of the triangle's texture (albedo for GI bounce rays), sampled from the primary bitmap on a coarse stride and cached per texture.
    attachLightmapIfAbsent(int tri, Lightmap lightmap)
    Attaches a lightmap to a triangle unless one is already attached.
    void
    Allocates and publishes the per-triangle GI light array if absent (called by the GI system when this block enters a scene snapshot).
    Returns the axis-aligned bounding box for this shape in local coordinates.
    int
    Estimated cost of transforming this shape, in arbitrary units where a leaf primitive counts 1.
    float[]
    The per-triangle GI light array (3 floats per triangle), or null when no GI system has stamped this block — the paint path then renders the plain texture.
    lightmapAt(int tri)
    The lightmap attached to a triangle, or null when the triangle is outside the stage-3 bubble (or GI has not reached it yet).
    void
    setGiLight(int tri, float r, float g, float b)
    Writes one triangle's GI light total (light units, may exceed 255 — the paint path clamps).
    texture(int tri)
    The triangle's base texture (also read by the GI system when it attaches a lightmap: the composite resamples this texture into lightmap space).
    void
    transform(TransformStack transforms, RenderAggregator aggregator, RenderingContext renderingContext)
    Transforms every triangle of the block with the composed top transform of the stack (hoisted out of the loop), culls (near plane, subpixel with verdict cache, viewport) and queues a thin handle per surviving triangle.
    void
    triangleUv(int tri, double[] out)
    Copies the triangle's 6 UV coordinates (primary-texture pixels).
    void
    triangleVertices(int tri, float[] out)
    Copies the triangle's 9 world-space vertex coordinates into out (x0,y0,z0, x1,y1,z1, x2,y2,z2).
    void
    triangleWorld(int tri, double[] out)
    Copies the triangle's 9 world-space vertex coordinates at full double precision (GI lightmap construction and cache keys).
    int
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TriangleMeshBlock

      public TriangleMeshBlock(double[] world, double[] uv, Texture[] textures, boolean backfaceCull)
      Builds a block from baked world-space triangle soup.
      Parameters:
      world - 9 doubles per triangle (x0,y0,z0,x1,...), world space; the array is adopted, not copied
      uv - 6 doubles per triangle (u0,v0,...) in primary texture pixels; adopted
      textures - one texture per triangle
      backfaceCull - cull clockwise triangles on screen
  • Method Details

    • triCount

      public int triCount()
    • ensureGiLight

      public void ensureGiLight()
      Allocates and publishes the per-triangle GI light array if absent (called by the GI system when this block enters a scene snapshot). Fresh arrays start at 255 per channel — the historical full-texture look — so enabling GI shows no dark age before the first estimates land. Idempotent and thread-safe.
    • setGiLight

      public void setGiLight(int tri, float r, float g, float b)
      Writes one triangle's GI light total (light units, may exceed 255 — the paint path clamps). Called from GI worker threads only, and only after ensureGiLight().
    • giLight

      public float[] giLight()
      The per-triangle GI light array (3 floats per triangle), or null when no GI system has stamped this block — the paint path then renders the plain texture.
      Returns:
      the light array or null
    • albedo

      public Color albedo(int tri)
      Average color of the triangle's texture (albedo for GI bounce rays), sampled from the primary bitmap on a coarse stride and cached per texture. Cost is paid once per distinct texture, not per triangle.
      Parameters:
      tri - triangle index
      Returns:
      the average texture color
    • triangleVertices

      public void triangleVertices(int tri, float[] out)
      Copies the triangle's 9 world-space vertex coordinates into out (x0,y0,z0, x1,y1,z1, x2,y2,z2). Used by the GI system at snapshot build.
      Parameters:
      tri - triangle index
      out - destination array of at least 9 floats
    • triangleWorld

      public void triangleWorld(int tri, double[] out)
      Copies the triangle's 9 world-space vertex coordinates at full double precision (GI lightmap construction and cache keys).
      Parameters:
      tri - triangle index
      out - destination array of at least 9 doubles
    • triangleUv

      public void triangleUv(int tri, double[] out)
      Copies the triangle's 6 UV coordinates (primary-texture pixels). Used by the GI system when attaching a lightmap.
      Parameters:
      tri - triangle index
      out - destination array of at least 6 doubles
    • lightmapAt

      public Lightmap lightmapAt(int tri)
      The lightmap attached to a triangle, or null when the triangle is outside the stage-3 bubble (or GI has not reached it yet).
      Parameters:
      tri - triangle index
      Returns:
      the lightmap or null
    • attachLightmapIfAbsent

      public Lightmap attachLightmapIfAbsent(int tri, Lightmap lightmap)
      Attaches a lightmap to a triangle unless one is already attached. Called from GI threads; idempotent and thread-safe.
      Parameters:
      tri - triangle index
      lightmap - the candidate lightmap
      Returns:
      the lightmap actually attached to the slot (the argument, or the pre-existing one on a lost race)
    • getBoundingBox

      public Box getBoundingBox()
      Description copied from class: AbstractShape
      Returns the axis-aligned bounding box for this shape in local coordinates.

      The bounding box is used for frustum culling to determine if the shape is potentially visible before expensive vertex transformations.

      Conservative default: Returns a very large box that ensures the shape is always considered visible. Subclasses should override to provide tight bounds computed from their geometry.

      Caching: The bounding box is cached after first computation. If geometry changes, call AbstractShape.invalidateBounds() to trigger recomputation on next call.

      Overrides:
      getBoundingBox in class AbstractShape
      Returns:
      the axis-aligned bounding box in local coordinates
    • getTransformWeight

      public int getTransformWeight(RenderingContext renderingContext)
      Description copied from class: AbstractShape
      Estimated cost of transforming this shape, in arbitrary units where a leaf primitive counts 1. Composites override this with their total subtree weight. Used by the parallel transform fork to decide where splitting pays off.
      Overrides:
      getTransformWeight in class AbstractShape
      Parameters:
      renderingContext - the rendering context (frame identity for caching)
      Returns:
      transform weight, always at least 1
    • transform

      public void transform(TransformStack transforms, RenderAggregator aggregator, RenderingContext renderingContext)
      Transforms every triangle of the block with the composed top transform of the stack (hoisted out of the loop), culls (near plane, subpixel with verdict cache, viewport) and queues a thin handle per surviving triangle. All expressions replicate TransformStack.transform / Vertex.calculateLocationRelativeToViewer exactly, so output is bit-identical with the object-backed path.
      Specified by:
      transform in class AbstractShape
      Parameters:
      transforms - the current stack of transforms (world-to-camera transformations)
      aggregator - collects transformed shapes for depth-sorted rendering
      renderingContext - provides frame dimensions, graphics context, and frame metadata
    • texture

      public Texture texture(int tri)
      The triangle's base texture (also read by the GI system when it attaches a lightmap: the composite resamples this texture into lightmap space).