Skip to content

Commit 2db89f0

Browse files
committed
Start a GlBuffer class.
1 parent 8c7bc2d commit 2db89f0

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.nullprogram.lwjgl;
2+
3+
import java.nio.Buffer;
4+
import lombok.Getter;
5+
import org.lwjgl.opengl.GL15;
6+
7+
/**
8+
* An OpenGL buffer object, the abstract base class. Because the LWJGL
9+
* API depends highly on the <i>type</i> of the underlying Buffer, it
10+
* is generic and each subclass takes on a different Buffer subclass.
11+
* @param <T> the type of buffer backing this OpenGL buffer
12+
*/
13+
public abstract class GlBuffer<T extends Buffer> {
14+
15+
/** OpenGL handle for this buffer. */
16+
@Getter
17+
private final int handle;
18+
19+
/** Native buffer that backs this buffer. */
20+
@Getter
21+
private final T buffer;
22+
23+
/** True of this buffer has been disposed. */
24+
private boolean disposed = false;
25+
26+
/**
27+
* Create a new buffer.
28+
* @param id the buffer's handle
29+
* @param data the native buffer backing this buffer
30+
*/
31+
protected GlBuffer(final int id, final T data) {
32+
handle = id;
33+
buffer = data;
34+
}
35+
36+
37+
/**
38+
* Disposes of this shader and the system resources it is
39+
* using. This can be safely called multiple times, but it is not
40+
* thread-safe.
41+
*/
42+
public void dispose() {
43+
if (!disposed) {
44+
GL15.glDeleteBuffers(handle);
45+
disposed = true;
46+
}
47+
}
48+
49+
@Override
50+
protected void finalize() {
51+
dispose();
52+
}
53+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.nullprogram.lwjgl;
2+
3+
import java.nio.ByteBuffer;
4+
import org.lwjgl.opengl.GL15;
5+
6+
/**
7+
* OpenGL buffer based on a ByteBuffer.
8+
*/
9+
public class GlByteBuffer extends GlBuffer<ByteBuffer> {
10+
11+
/**
12+
* Create a new OpenGL buffer for the given data. The usage hint
13+
* defaults to GL_STATIC_DRAW.
14+
* @param data the data array
15+
* @param target the type of buffer
16+
*/
17+
public GlByteBuffer(final byte[] data, final int target) {
18+
this(Lwjgl.toBuffer(data), target, GL15.GL_STATIC_DRAW);
19+
}
20+
21+
/**
22+
* Create a new OpenGL buffer for the given data.
23+
* @param data the data array
24+
* @param target the type of buffer
25+
* @param usage usage hint for this buffer
26+
*/
27+
public GlByteBuffer(final ByteBuffer data,
28+
final int target, final int usage) {
29+
super(GL15.glGenBuffers(), data);
30+
GL15.glBindBuffer(target, getHandle());
31+
GL15.glBufferData(target, getBuffer(), usage);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.nullprogram.lwjgl;
2+
3+
import java.nio.FloatBuffer;
4+
import org.lwjgl.opengl.GL15;
5+
6+
/**
7+
* OpenGL buffer based on a FloatBuffer.
8+
*/
9+
public class GlFloatBuffer extends GlBuffer<FloatBuffer> {
10+
11+
/**
12+
* Create a new OpenGL buffer for the given data. The usage hint
13+
* defaults to GL_STATIC_DRAW.
14+
* @param data the data array
15+
* @param target the type of buffer
16+
*/
17+
public GlFloatBuffer(final float[] data, final int target) {
18+
this(Lwjgl.toBuffer(data), target, GL15.GL_STATIC_DRAW);
19+
}
20+
21+
/**
22+
* Create a new OpenGL buffer for the given data.
23+
* @param data the data array
24+
* @param target the type of buffer
25+
* @param usage usage hint for this buffer
26+
*/
27+
public GlFloatBuffer(final FloatBuffer data,
28+
final int target, final int usage) {
29+
super(GL15.glGenBuffers(), data);
30+
GL15.glBindBuffer(target, getHandle());
31+
GL15.glBufferData(target, getBuffer(), usage);
32+
}
33+
}

0 commit comments

Comments
 (0)