Class ShapeBatch

java.lang.Object
lol.trq.helios.batch.ShapeBatch
All Implemented Interfaces:
AutoCloseable

public final class ShapeBatch extends Object implements AutoCloseable
Draws many signed distance shapes with one program and as few draw calls as possible.

Call begin(float), add shapes, then end(). Shapes are drawn in the order added, in the current model-view-projection space (GUI units on a Minecraft screen). A batch flushes when it is full, when an image needs a different texture, and at end(). Each flush saves and restores every piece of GL state it touches. Every edge is anti-aliased over one framebuffer pixel; the pixel passed to begin is the size of that pixel in drawing units, and quads are grown by it so the outer half of the edge is not clipped.

Each shape is one quad of four vertices of 72 bytes: position, position relative to the shape centre, shape (half size, radius, kind), parameters, texture coordinates and four corner colours. Shapes are drawn as indexed triangles from the batch's own vertex array. Vertex data goes through a StreamBuffer, so a frame's shapes land in one buffer; call endFrame() once per frame.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final List<String>
    Attribute names, in the order of their layout(location) in shape.vsh.
    static final int
    Bytes per vertex.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ShapeBatch(GlBackend gl, GlStateCache cache, ShaderLoader shaders, int capacity)
    Creates a batch.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    alphaCutoff(float cutoff)
    Discards fragments whose alpha is at or below a cutoff, in the fragment shader.
    arc(float cx, float cy, float radius, float thickness, float startRadians, float sweepRadians, Paint paint)
    A ring segment with butt caps.
    begin(float pixel)
    Starts a batch.
    circle(float cx, float cy, float radius, Paint paint)
    A filled circle.
    void
    Deletes the program, the vertex array and the buffers.
    int
    Returns how many draw calls this batch has issued, for tests and profiling.
    boolean
    Returns whether the batch is between begin(float) and end().
    void
    end()
    Draws everything added since begin(float).
    void
    Marks the end of a frame for the streamed vertex buffers.
    glyph(int textureId, float x0, float y0, float x1, float y1, float skewTop, float skewBottom, float u0, float v0, float u1, float v1, int argb, float pxRange, float weight, float softness, int atlasWidth, int atlasHeight)
    One glyph from a multi-channel signed distance field atlas.
    image(int textureId, float x, float y, float width, float height, float radius, float u0, float v0, float u1, float v1, int tint)
    A textured rounded rectangle, multiplied by a tint.
    innerGlow(float x, float y, float width, float height, float radius, float glowWidth, int argb)
    A glow inside the edge of a rounded rectangle, strongest at the edge.
    line(float x0, float y0, float x1, float y1, float width, boolean roundCaps, Paint paint)
    A straight line.
    outline(float x, float y, float width, float height, float radius, float strokeWidth, Paint paint)
    The outline of a rounded rectangle, drawn inside its edge.
    float
    Returns the pixel size given to begin(float).
    polyline(float[] points, float width, Paint paint)
    Connected lines with round joins and caps.
    rect(float x, float y, float width, float height, float radius, int argb)
    A filled rounded rectangle in one colour.
    rect(float x, float y, float width, float height, float radius, Paint paint)
    A filled rounded rectangle.
    shadow(float x, float y, float width, float height, float radius, float softness, int argb)
    A soft drop shadow for a rounded rectangle.

    Methods inherited from class Object

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

    • STRIDE

      public static final int STRIDE
      Bytes per vertex.
      See Also:
    • ATTRIBUTES

      public static final List<String> ATTRIBUTES
      Attribute names, in the order of their layout(location) in shape.vsh.
  • Constructor Details

    • ShapeBatch

      public ShapeBatch(GlBackend gl, GlStateCache cache, ShaderLoader shaders, int capacity)
      Creates a batch.
      Parameters:
      gl - the backend
      cache - the host's state cache
      shaders - the loader that resolves the built-in shaders
      capacity - shapes per draw call
  • Method Details

    • begin

      public ShapeBatch begin(float pixel)
      Starts a batch.
      Parameters:
      pixel - the size of one framebuffer pixel in drawing units, such as 1 / guiScale
      Returns:
      this batch
    • end

      public void end()
      Draws everything added since begin(float).
    • alphaCutoff

      public void alphaCutoff(float cutoff)
      Discards fragments whose alpha is at or below a cutoff, in the fragment shader. Stencil masks use it so only the inside of a shape is written. 0 turns it off.
      Parameters:
      cutoff - the alpha below which fragments are dropped, in [0, 1)
    • pixel

      public float pixel()
      Returns the pixel size given to begin(float).
      Returns:
      one framebuffer pixel in drawing units
    • drawing

      public boolean drawing()
      Returns whether the batch is between begin(float) and end().
      Returns:
      true while drawing
    • glyph

      public ShapeBatch glyph(int textureId, float x0, float y0, float x1, float y1, float skewTop, float skewBottom, float u0, float v0, float u1, float v1, int argb, float pxRange, float weight, float softness, int atlasWidth, int atlasHeight)
      One glyph from a multi-channel signed distance field atlas. Text layout lives in TextRenderer; this is the primitive it draws with.
      Parameters:
      textureId - the atlas texture
      x0 - quad left
      y0 - quad top
      x1 - quad right
      y1 - quad bottom
      skewTop - horizontal offset of the top edge, for italics
      skewBottom - horizontal offset of the bottom edge
      u0 - atlas left
      v0 - atlas top
      u1 - atlas right
      v1 - atlas bottom
      argb - the colour
      pxRange - the atlas distance range in texels
      weight - extra thickness in field units (range / 2 texels is 0.5); 0 for regular
      softness - edge softness in framebuffer pixels, for soft shadows; 0 for a crisp one-pixel edge
      atlasWidth - atlas width in texels
      atlasHeight - atlas height in texels
      Returns:
      this batch
    • drawCalls

      public int drawCalls()
      Returns how many draw calls this batch has issued, for tests and profiling.
      Returns:
      the draw call count
    • rect

      public ShapeBatch rect(float x, float y, float width, float height, float radius, Paint paint)
      A filled rounded rectangle.
      Parameters:
      x - left
      y - top
      width - width
      height - height
      radius - corner radius, clamped to half the shorter side
      paint - the colour
      Returns:
      this batch
    • rect

      public ShapeBatch rect(float x, float y, float width, float height, float radius, int argb)
      A filled rounded rectangle in one colour.
      Parameters:
      x - left
      y - top
      width - width
      height - height
      radius - corner radius
      argb - the colour
      Returns:
      this batch
    • outline

      public ShapeBatch outline(float x, float y, float width, float height, float radius, float strokeWidth, Paint paint)
      The outline of a rounded rectangle, drawn inside its edge.
      Parameters:
      x - left
      y - top
      width - width
      height - height
      radius - corner radius
      strokeWidth - outline width
      paint - the colour
      Returns:
      this batch
    • circle

      public ShapeBatch circle(float cx, float cy, float radius, Paint paint)
      A filled circle.
      Parameters:
      cx - centre x
      cy - centre y
      radius - the radius
      paint - the colour
      Returns:
      this batch
    • arc

      public ShapeBatch arc(float cx, float cy, float radius, float thickness, float startRadians, float sweepRadians, Paint paint)
      A ring segment with butt caps.
      Parameters:
      cx - centre x
      cy - centre y
      radius - outer radius
      thickness - ring thickness, inward from the outer radius
      startRadians - start angle; 0 points along +x, angles grow clockwise on screen
      sweepRadians - swept angle; 2 * PI or more draws a full ring
      paint - the colour; radial paints are not supported on arcs
      Returns:
      this batch
    • line

      public ShapeBatch line(float x0, float y0, float x1, float y1, float width, boolean roundCaps, Paint paint)
      A straight line.
      Parameters:
      x0 - start x
      y0 - start y
      x1 - end x
      y1 - end y
      width - line width
      roundCaps - round ends reaching half the width past each end point, or square ends at the points
      paint - the colour; a horizontal paint runs from start to end
      Returns:
      this batch
    • polyline

      public ShapeBatch polyline(float[] points, float width, Paint paint)
      Connected lines with round joins and caps. Joins overlap, so a translucent colour is darker where segments meet.
      Parameters:
      points - x, y pairs; at least two points
      width - line width
      paint - the colour of every segment
      Returns:
      this batch
    • shadow

      public ShapeBatch shadow(float x, float y, float width, float height, float radius, float softness, int argb)
      A soft drop shadow for a rounded rectangle. Draw it before the shape, usually offset down and right.
      Parameters:
      x - left of the shape casting it
      y - top of the shape casting it
      width - width
      height - height
      radius - corner radius
      softness - half the width of the falloff band around the edge
      argb - shadow colour; its alpha is the opacity inside
      Returns:
      this batch
    • innerGlow

      public ShapeBatch innerGlow(float x, float y, float width, float height, float radius, float glowWidth, int argb)
      A glow inside the edge of a rounded rectangle, strongest at the edge.
      Parameters:
      x - left
      y - top
      width - width
      height - height
      radius - corner radius
      glowWidth - how far the glow reaches inward
      argb - glow colour
      Returns:
      this batch
    • image

      public ShapeBatch image(int textureId, float x, float y, float width, float height, float radius, float u0, float v0, float u1, float v1, int tint)
      A textured rounded rectangle, multiplied by a tint.
      Parameters:
      textureId - the GL texture
      x - left
      y - top
      width - width
      height - height
      radius - corner radius
      u0 - texture coordinate at the left
      v0 - texture coordinate at the top
      u1 - texture coordinate at the right
      v1 - texture coordinate at the bottom
      tint - colour the texture is multiplied by; 0xFFFFFFFF for none
      Returns:
      this batch
    • endFrame

      public void endFrame()
      Marks the end of a frame for the streamed vertex buffers. Helios.tick() calls it.
    • close

      public void close()
      Deletes the program, the vertex array and the buffers.
      Specified by:
      close in interface AutoCloseable