Class MeshRenderer

java.lang.Object
lol.trq.helios.mesh.MeshRenderer
All Implemented Interfaces:
AutoCloseable

public final class MeshRenderer extends Object implements AutoCloseable
Draws 3D meshes written through the sinks: quads(), lines() and textured(). Vertices are in the current model-view space; inside Minecraft's world render (such as RenderWorldLastEvent) that is world coordinates relative to the camera, the space MinecraftWorld returns.

Each sink streams into its own buffer and flushes when it is full, when another sink is written to (so draws keep the order they were written in), when the texture or lighting changes, and at flush(). Call flush() when done drawing, and endFrame() once per frame (Helios.tick() does). Each flush saves and restores every piece of GL state it touches.

Meshes that do not change can be recorded once with a MeshRecorder and drawn from a StaticMesh with draw(StaticMesh, float[]), which uploads nothing.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final float
    Ambient light.
    static final float
    Diffuse strength of each light.
    static final float[]
    First light direction of Minecraft 1.8.9's entity lighting, in world space, normalized.
    static final float[]
    Second light direction of Minecraft 1.8.9's entity lighting, in world space, normalized.
    static final float
    Default alpha cutoff of textured meshes, as Minecraft's alpha test for entities.
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    alphaCutoff(float cutoff)
    Sets the alpha at or below which textured fragments are discarded, so transparent texels write no depth.
    void
    Deletes the programs, vertex arrays and buffers.
    cull(boolean on)
    Sets back-face culling for what follows.
    depth(boolean test, boolean write)
    Sets depth testing and writing for what follows.
    void
    draw(StaticMesh mesh, float[] model)
    Draws a recorded mesh, after flushing what was written so far.
    int
    Returns how many draw calls this renderer has issued, for tests and profiling.
    void
    Marks the end of a frame for the streamed vertex buffers.
    void
    Draws everything written so far.
    lighting(boolean on)
    Turns Minecraft 1.8.9 entity lighting on or off for the textured quads that follow: each fragment's colour is multiplied by min(1, AMBIENT + DIFFUSE * (max(0, n·LIGHT0) + max(0, n·LIGHT1))).
    Returns the sink for anti-aliased lines.
    Returns the sink for flat-coloured quads.
    texture(int textureId)
    Sets the texture of the textured quads that follow, flushing those already written with another.
    Returns the sink for textured, optionally lit quads; they use the texture set by texture(int).

    Methods inherited from class Object

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

    • LIGHT0

      public static final float[] LIGHT0
      First light direction of Minecraft 1.8.9's entity lighting, in world space, normalized.
    • LIGHT1

      public static final float[] LIGHT1
      Second light direction of Minecraft 1.8.9's entity lighting, in world space, normalized.
    • DIFFUSE

      public static final float DIFFUSE
      Diffuse strength of each light.
      See Also:
    • AMBIENT

      public static final float AMBIENT
      Ambient light.
      See Also:
    • TEXTURED_ALPHA_CUTOFF

      public static final float TEXTURED_ALPHA_CUTOFF
      Default alpha cutoff of textured meshes, as Minecraft's alpha test for entities.
      See Also:
  • Constructor Details

    • MeshRenderer

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

    • quads

      public QuadSink quads()
      Returns the sink for flat-coloured quads.
      Returns:
      the sink
    • lines

      public LineSink lines()
      Returns the sink for anti-aliased lines.
      Returns:
      the sink
    • textured

      public TexturedSink textured()
      Returns the sink for textured, optionally lit quads; they use the texture set by texture(int).
      Returns:
      the sink
    • texture

      public MeshRenderer texture(int textureId)
      Sets the texture of the textured quads that follow, flushing those already written with another.
      Parameters:
      textureId - the GL texture, or 0 for none
      Returns:
      this renderer
    • lighting

      public MeshRenderer lighting(boolean on)
      Turns Minecraft 1.8.9 entity lighting on or off for the textured quads that follow: each fragment's colour is multiplied by min(1, AMBIENT + DIFFUSE * (max(0, n·LIGHT0) + max(0, n·LIGHT1))). On by default. The light directions are in world space, so normals must be too, as they are inside Minecraft's world render.
      Parameters:
      on - whether lighting is on
      Returns:
      this renderer
    • alphaCutoff

      public MeshRenderer alphaCutoff(float cutoff)
      Sets the alpha at or below which textured fragments are discarded, so transparent texels write no depth. Defaults to 0.10000000149011612f. Coloured quads and lines discard only fully transparent fragments.
      Parameters:
      cutoff - the cutoff in [0, 1)
      Returns:
      this renderer
    • depth

      public MeshRenderer depth(boolean test, boolean write)
      Sets depth testing and writing for what follows. Both are on by default; turn writing off for translucent meshes, and testing off to draw through walls.
      Parameters:
      test - whether fragments are depth tested
      write - whether fragments write depth
      Returns:
      this renderer
    • cull

      public MeshRenderer cull(boolean on)
      Sets back-face culling for what follows. Off by default, so quads show from both sides. With culling on, quads wound counter-clockwise on screen face the viewer.
      Parameters:
      on - whether back faces are culled
      Returns:
      this renderer
    • flush

      public void flush()
      Draws everything written so far.
    • draw

      public void draw(StaticMesh mesh, float[] model)
      Draws a recorded mesh, after flushing what was written so far. Uses the current texture, lighting, depth and cull settings, as the streamed sinks do.
      Parameters:
      mesh - the mesh
      model - a transform applied on the GPU, or null for none
    • drawCalls

      public int drawCalls()
      Returns how many draw calls this renderer has issued, for tests and profiling.
      Returns:
      the draw call count
    • 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 programs, vertex arrays and buffers. Recorded meshes are closed separately.
      Specified by:
      close in interface AutoCloseable