Commit 20b56607 authored by Matthias Betz's avatar Matthias Betz
Browse files

Add GlScene global VBO upload and indexed draw

parent 65b6eb39
package de.hft.stuttgart.citydoctor2.gui.gl;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL3;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
/** GPU resources for the global scene mesh. Single VBO (pos+color+id), one index buffer redrawn per frame. */
public class GlScene {
private int vao = 0;
private int vboPos;
private int vboColor;
private int vboId;
private int ebo;
private int indexCount;
private boolean uploaded;
public void upload(GL3 gl, SceneData data) {
if (!uploaded) {
int[] ids = new int[3];
gl.glGenVertexArrays(1, ids, 0);
vao = ids[0];
int[] buffers = new int[4];
gl.glGenBuffers(4, buffers, 0);
vboPos = buffers[0];
vboColor = buffers[1];
vboId = buffers[2];
ebo = buffers[3];
}
gl.glBindVertexArray(vao);
uploadFloat(gl, vboPos, data.positions(), 0, 3);
uploadFloat(gl, vboColor, data.colors(), 1, 3);
uploadInt(gl, vboId, data.ids(), 2);
gl.glBindVertexArray(0);
uploaded = true;
}
private void uploadFloat(GL3 gl, int vbo, float[] data, int location, int components) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo);
FloatBuffer fb = toFloatBuffer(data);
gl.glBufferData(GL.GL_ARRAY_BUFFER, (long) data.length * Float.BYTES, fb, GL.GL_STATIC_DRAW);
gl.glEnableVertexAttribArray(location);
gl.glVertexAttribPointer(location, components, GL.GL_FLOAT, false, 0, 0L);
}
private void uploadInt(GL3 gl, int vbo, int[] data, int location) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo);
IntBuffer ib = toIntBuffer(data);
gl.glBufferData(GL.GL_ARRAY_BUFFER, (long) data.length * Integer.BYTES, ib, GL.GL_STATIC_DRAW);
gl.glEnableVertexAttribArray(location);
gl.glVertexAttribIPointer(location, 1, GL3.GL_INT, 0, 0L); // integer attribute, not normalized
}
/** Re-uploads which triangles to draw (element indices). Cheap; called on filter/LOD changes. */
public void setIndexSet(GL3 gl, int[] indices) {
gl.glBindVertexArray(vao);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, ebo);
IntBuffer ib = toIntBuffer(indices);
gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, (long) indices.length * Integer.BYTES, ib,
GL.GL_DYNAMIC_DRAW);
gl.glBindVertexArray(0);
indexCount = indices.length;
}
public void draw(GL3 gl) {
if (vao == 0 || indexCount == 0) {
return;
}
gl.glBindVertexArray(vao);
gl.glDrawElements(GL.GL_TRIANGLES, indexCount, GL.GL_UNSIGNED_INT, 0L);
gl.glBindVertexArray(0);
}
public boolean hasData() { return uploaded; }
public void dispose(GL3 gl) {
if (vao != 0) {
gl.glDeleteVertexArrays(1, new int[]{vao}, 0);
gl.glDeleteBuffers(4, new int[]{vboPos, vboColor, vboId, ebo}, 0);
vao = 0;
uploaded = false;
}
}
private static FloatBuffer toFloatBuffer(float[] data) {
ByteBuffer bb = ByteBuffer.allocateDirect(data.length * Float.BYTES).order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(data).flip();
return fb;
}
private static IntBuffer toIntBuffer(int[] data) {
ByteBuffer bb = ByteBuffer.allocateDirect(data.length * Integer.BYTES).order(ByteOrder.nativeOrder());
IntBuffer ib = bb.asIntBuffer();
ib.put(data).flip();
return ib;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment