Class SimpleBufferBuilder
- All Implemented Interfaces:
Closeable,AutoCloseable
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).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumSpecifies a vertex element with a specific data type, number of primitives and a size in bytes.static enumSpecifies a combination of vertex elements.static enumRepresents a primitive mode that this builder is capable of buffering in. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.lwjgl.system.MemoryUtil.MemoryAllocatorprivate ByteBufferprivate longprivate booleanprivate static intprivate static intprivate intprivate SimpleBufferBuilder.Formatprivate intprivate SimpleBufferBuilder.Modeprivate static final int[]private static final int[]private static final int[]private int -
Constructor Summary
ConstructorsConstructorDescriptionSimpleBufferBuilder(int capacity) Create a new SimpleBufferBuilder with an initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionbegin(SimpleBufferBuilder.Format format, SimpleBufferBuilder.Mode mode) Start building a new set of vertex data in the given format and mode.voidclose()Clear this builder's cached buffer.colour(byte r, byte g, byte b, byte a) Buffer a color element.colour(float r, float g, float b, float a) Buffer a color element.colour(int packedColor) static voiddestroy()voiddraw()Upload and draw this buffer using one of a number of re-usable set of buffers.End building the current vertex and prepare for the next.private static voidensureElementBufferLength(int vertices) private voidensureSpace(int newBytes) intUpload the current buffer.pos(float x, float y) Buffer a position element.tex(float u, float v) Buffer a texture element.
-
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
-
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
Buffer a position element.- Parameters:
x- The x.y- The y.z- The z.- Returns:
- The same builder.
-
tex
Buffer a texture element.- Parameters:
u- The u.v- The v.- Returns:
- The same builder.
-
colour
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
- Parameters:
packedColor- an ABGR packed int- Returns:
- the same buffer.
- See Also:
-
colour
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
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_BUFFERandGL15C.GL_ELEMENT_ARRAY_BUFFERThe 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-