OpenGL - Saar25/PlanetEngine GitHub Wiki
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
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()
BufferObject.create()
- create new BufferObject instance
void allocate(BufferTarget target, long capacity, BufferUsage usage)
example:
bufferObject.allocate(BufferTarget.ELEMENT_ARRAY, 6, BufferUsage.STATIC_DRAW);
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});
ByteBuffer map(BufferTarget target, BufferAccess access)
void unmap(BufferTarget target)
example:
bufferObject.map(BufferTarget.ELEMENT_ARRAY, BufferAccess.READ_ONLY);
void bind(BufferTarget target)
void unbind(BufferTarget target)
example:
bufferObject.bind(BufferTarget.ELEMENT_ARRAY);
void delete()
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
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
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);
void allocate(long capacity)
void allocateInt(long capacity)
void allocateFloat(long capacity)
example:
vbo.allocate(6);
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});
ByteBuffer map(BufferAccess access)
void unmap()
example:
vbo.map(BufferAccess.READ_ONLY);
void bind()
void unbind()
void delete()
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
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()
Vao.create()
- create new Vao instance
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);
void enableAttributes()
void disableAttributes()
void bind()
void unbind()
void delete()
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
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()
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"
void bindAttribute(int location, String name)
void bindAttributes(String... names)
example:
program.bindAttributes("in_position", "in_colour");
void bindFragmentOutput(int location, String name)
void bindFragmentOutputs(String... names)
example:
program.bindFragmentOutputs("f_colour");
void bind()
void unbind()
void delete()