Class SimpleBufferBuilder

java.lang.Object
net.minecraftforge.fml.earlydisplay.SimpleBufferBuilder
All Implemented Interfaces:
Closeable, AutoCloseable

public class SimpleBufferBuilder extends Object implements Closeable
A very simple, Mojang inspired BufferBuilder. This has been customized for 2d rendering such as text and simple planar textures

Not bound to any specific format, ideally should be held onto for re-use.

Can be used for 'immediate mode' style rendering using draw(), or upload to external vertex arrays for proper instancing using finishAndUpload().

This is a Triangles only buffer, all data uploaded is in Triangles. Quads are converted to triangles using 0, 1, 2, 0, 2, 3.

Any given SimpleBufferBuilder.Format should have its individual SimpleBufferBuilder.Element components buffered in the order specified by the SimpleBufferBuilder.Format, followed by an endVertex() call to prepare for the next vertex.

It is illegal to buffer primitives in any format other than the one specified to begin(Format, Mode).

Author:
covers1624
  • Field Details

    • ALLOCATOR

      private static final org.lwjgl.system.MemoryUtil.MemoryAllocator ALLOCATOR
    • VERTEX_ARRAYS

      private static final int[] VERTEX_ARRAYS
    • VERTEX_BUFFERS

      private static final int[] VERTEX_BUFFERS
    • VERTEX_BUFFER_LENGTHS

      private static final int[] VERTEX_BUFFER_LENGTHS
    • elementBuffer

      private static int elementBuffer
    • elementBufferVertexLength

      private static int elementBufferVertexLength
    • bufferAddr

      private long bufferAddr
    • buffer

      private ByteBuffer buffer
    • format

    • mode

    • building

      private boolean building
    • elementIndex

      private int elementIndex
    • index

      private int index
    • vertices

      private int vertices
  • Constructor Details

    • SimpleBufferBuilder

      public SimpleBufferBuilder(int capacity)
      Create a new SimpleBufferBuilder with an initial capacity.

      The buffer will be doubled as required.

      Generally picking a small number, around 128/256 should be a safe bet. Provided you cache your buffers, it should not mean much overall.

      Parameters:
      capacity - The initial capacity in bytes.
  • Method Details

    • destroy

      public static void destroy()
    • ensureElementBufferLength

      private static void ensureElementBufferLength(int vertices)
    • begin

      Start building a new set of vertex data in the given format and mode.
      Parameters:
      format - The format to start building in.
      mode - The mode to start building in.
    • pos

      public SimpleBufferBuilder pos(float x, float y)
      Buffer a position element.
      Parameters:
      x - The x.
      y - The y.
      z - The z.
      Returns:
      The same builder.
    • tex

      public SimpleBufferBuilder tex(float u, float v)
      Buffer a texture element.
      Parameters:
      u - The u.
      v - The v.
      Returns:
      The same builder.
    • colour

      public SimpleBufferBuilder colour(float r, float g, float b, float a)
      Buffer a color element.
      Parameters:
      r - The red component. (0-1)
      g - The green component. (0-1)
      b - The blue component. (0-1)
      a - The alpha component. (0-1)
      Returns:
      The same buffer.
    • colour

      public SimpleBufferBuilder colour(int packedColor)
      Parameters:
      packedColor - an ABGR packed int
      Returns:
      the same buffer.
      See Also:
    • colour

      public SimpleBufferBuilder colour(byte r, byte g, byte b, byte a)
      Buffer a color element.
      Parameters:
      r - The red component. (0-255)
      g - The green component. (0-255)
      b - The blue component. (0-255)
      a - The alpha component. (0-255)
      Returns:
      The same buffer.
    • endVertex

      public SimpleBufferBuilder endVertex()
      End building the current vertex and prepare for the next.
      Returns:
      The same builder.
    • ensureSpace

      private void ensureSpace(int newBytes)
    • finishAndUpload

      public int finishAndUpload()
      Upload the current buffer.

      This will bind a GL15C.GL_ARRAY_BUFFER and GL15C.GL_ELEMENT_ARRAY_BUFFER

      The vertex data and index data is uploaded to their respective buffers.

      Uploading the buffers finishes drawing and resets for the next buffer operation.

      This should not be called in conjunction with draw()

      Returns:
      The number of indexes that were uploaded.
    • draw

      public void draw()
      Upload and draw this buffer using one of a number of re-usable set of buffers.

      This will immediately upload the buffer, resetting this builder for the next buffer operation, and draw the uploaded data.

      You will need to bind shaders, textures, etc, before calling this function.

    • close

      public void close()
      Clear this builder's cached buffer.

      If you are completely done, call destroy()

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable