OpenGL - Saar25/PlanetEngine GitHub Wiki

OpenGL binding

Opengl objects are saved as int in java code
it can cause many drawbacks like memory leaks, wrong parameters and unreadable code
to solve this issue, all opengl objects are wrapped with java classes

Buffer Objects

classes are located under package org.saar.lwjgl.opengl.objects.buffers

BufferObject is the wrapper class to opengl buffers that are usually created via GL15.glGenBuffers()

Creation

BufferObject.create() - create new BufferObject instance

Allocating memory

void allocate(BufferTarget target, long capacity, BufferUsage usage)

example:

bufferObject.allocate(BufferTarget.ELEMENT_ARRAY, 6, BufferUsage.STATIC_DRAW);

Storing data

void store(BufferTarget target, long offset, int[] buffer)
void store(BufferTarget target, long offset, float[] buffer)
void store(BufferTarget target, long offset, ByteBuffer buffer)
void store(BufferTarget target, long offset, IntBuffer buffer)
void store(BufferTarget target, long offset, FloatBuffer buffer)

example:

bufferObject.store(BufferTarget.ELEMENT_ARRAY, 0, new int[] {0, 1, 2, 0, 2, 3});

Mapping data

ByteBuffer map(BufferTarget target, BufferAccess access)
void unmap(BufferTarget target)

example:

bufferObject.map(BufferTarget.ELEMENT_ARRAY, BufferAccess.READ_ONLY);

Binding

void bind(BufferTarget target)
void unbind(BufferTarget target)

example:

bufferObject.bind(BufferTarget.ELEMENT_ARRAY);

Deleting

void delete()

Inheritance

ReadonlyBufferObject - Allows only read methods like binding
WriteableBufferObject - Allows allocating memory, storing byte data and buffer mapping, together with ReadonlyVbo methods
IBufferObject - Allows deleting the buffer object, together with WriteableBufferObject methods

Vertex Buffer Objects

classes are located under package org.saar.lwjgl.opengl.objects.vbos

Vbo is a wrapper class to BufferObject that used for vertices
DataBuffer is a wrapper class to BufferObject that used for vertices data
IndexBuffer is a wrapper class to BufferObject that used for vertices indices

Creation

Vbo.create(VboTarget target, VboUsage usage) - create new Vbo instance
new DataBuffer(VboUsage usage) - create new data buffer instance
new IndexBuffer(VboUsage usage) - create new index buffer instance

example:

Vbo.create(VboTarget.ELEMENT_ARRAY, VboUsage.STATIC_DRAW);

Allocating memory

void allocate(long capacity)
void allocateInt(long capacity)
void allocateFloat(long capacity)

example:

vbo.allocate(6);

Storing data

store(long offset, ByteBuffer buffer)
storeInt(long offset, int[] buffer)
storeInt(long offset, IntBuffer buffer)
storeFloat(long offset, float[] buffer)
storeFloat(long offset, FloatBuffer buffer)

example:

vbo.storeInt(0, new int[] {0, 1, 2, 0, 2, 3});

Mapping data

ByteBuffer map(BufferAccess access)
void unmap()

example:

vbo.map(BufferAccess.READ_ONLY);

Binding and Deleting

void bind()
void unbind()
void delete()

Inheritance

ReadonlyVbo - Allows only read methods like binding
WriteableVbo - Allows allocating and storing byte data, together with ReadonlyVbo methods
IVbo - Allows deleting the vbo, together with WriteableVbo methods

Vertex Array Objects

classes are located under package org.saar.lwjgl.opengl.objects.vaos

Vao is the wrapper class to opengl vertex arrays that are usually created via GL15.glGenVertexArrays()

Creation

Vao.create() - create new Vao instance

Loading Vbos

void loadVbo(ReadonlyVbo vbo, Attribute... attributes)

example:

Attribute position = Attribute.of(0, 3, DataType.FLOAT, false);
Attribute uvCoord  = Attribute.of(1, 2, DataType.FLOAT, false);
Attribute normal   = Attribute.of(2, 3, DataType.FLOAT, false);
vao.loadVbo(vbo, position, uvCoord, normal);

Enabling and disabling Attributes

void enableAttributes()
void disableAttributes()

Binding and Deleting

void bind()
void unbind()
void delete()

Inheritance

ReadonlyVao - Allows only read methods like binding and enabling attributes
WriteableVao - Allows loading vbos, together with ReadonlyVao methods
IVao - Allows deleting the vao, together with WriteableVao methods

Shaders Programs

classes are located under package org.saar.lwjgl.opengl.shaders

ShadersProgram is the wrapper class to opengl programs that are usually created via GL20.glCreateProgram()

Creation

create(Shader vertexShader, Shader fragmentShader)
create(Shader... shaders)

example:

ShadersProgram program = ShadersProgram.create(
    Shader.createVertex("/vertex.glsl"),
    Shader.createFragment("/fragment.glsl"));

Shader codes are loaded using a custom shader builder
using that builder some useful features can be implemented
like defining constant through java code, loading multiple shader sources together, and #include directive in glsl

example:

Shader.createFragment(GlslVersion.V400,
        ShaderCode.define("FXAA_REDUCE_MIN", (1.0 / 128.0).toString()),
        ShaderCode.define("FXAA_REDUCE_MUL", (1.0 / 8.0).toString()),
        ShaderCode.define("FXAA_SPAN_MAX", (8.0).toString()),
        ShaderCode.loadSource("/shaders/postprocessing/fxaa.pass.glsl"))

as well as #include inside glsl files

#include "/shaders/common/light/light"
#include "/shaders/common/transform/transform"

Binding attributes

void bindAttribute(int location, String name)
void bindAttributes(String... names)

example:

program.bindAttributes("in_position", "in_colour");

Binding fragment outputs

void bindFragmentOutput(int location, String name)
void bindFragmentOutputs(String... names)

example:

program.bindFragmentOutputs("f_colour");

Binding and Deleting

void bind()
void unbind()
void delete()

Frame Buffer Objects

Textures

Render Buffers

⚠️ **GitHub.com Fallback** ⚠️