Commit e7bff9cc authored by Matthias Betz's avatar Matthias Betz
Browse files

Add HighlightOverlay dynamic marker buffers

parent 861dafdf
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;
/**
* Dynamic overlay for highlight markers: points for vertices, lines for edges. Positions are in the same
* recentred world space as the scene. Colour rides in the color attribute; id attribute is 0.
*/
public class HighlightOverlay {
private int pointVao;
private int pointPos;
private int pointColor;
private int pointId;
private int pointCount;
private int lineVao;
private int linePos;
private int lineColor;
private int lineId;
private int lineVertexCount;
private boolean created;
public void init(GL3 gl) {
int[] v = new int[2];
gl.glGenVertexArrays(2, v, 0);
pointVao = v[0];
lineVao = v[1];
int[] b = new int[6];
gl.glGenBuffers(6, b, 0);
pointPos = b[0]; pointColor = b[1]; pointId = b[2];
linePos = b[3]; lineColor = b[4]; lineId = b[5];
created = true;
}
/** points: xyz triplets; pointColors: rgb triplets (one per point). */
public void setPoints(GL3 gl, float[] points, float[] pointColors) {
pointCount = points.length / 3;
configure(gl, pointVao, pointPos, points, pointColor, pointColors, pointId, pointCount);
}
/** lines: xyz pairs of endpoints (6 floats per segment); lineColors: rgb per line vertex. */
public void setLines(GL3 gl, float[] lines, float[] lineColors) {
lineVertexCount = lines.length / 3;
configure(gl, lineVao, linePos, lines, lineColor, lineColors, lineId, lineVertexCount);
}
private void configure(GL3 gl, int vao, int posVbo, float[] pos, int colVbo, float[] col,
int idVbo, int vertexCount) {
gl.glBindVertexArray(vao);
bindFloat(gl, posVbo, pos, 0, 3);
bindFloat(gl, colVbo, col, 1, 3);
// id attribute all zeros so overlay never matches uSelectedId
int[] zeros = new int[vertexCount];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, idVbo);
gl.glBufferData(GL.GL_ARRAY_BUFFER, (long) zeros.length * Integer.BYTES, toInt(zeros),
GL.GL_DYNAMIC_DRAW);
gl.glEnableVertexAttribArray(2);
gl.glVertexAttribIPointer(2, 1, GL3.GL_INT, 0, 0L);
gl.glBindVertexArray(0);
}
private void bindFloat(GL3 gl, int vbo, float[] data, int loc, int comp) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo);
gl.glBufferData(GL.GL_ARRAY_BUFFER, (long) data.length * Float.BYTES, toFloat(data),
GL.GL_DYNAMIC_DRAW);
gl.glEnableVertexAttribArray(loc);
gl.glVertexAttribPointer(loc, comp, GL.GL_FLOAT, false, 0, 0L);
}
public void draw(GL3 gl, float pointSize) {
if (!created) {
return;
}
if (lineVertexCount > 0) {
gl.glBindVertexArray(lineVao);
gl.glDrawArrays(GL.GL_LINES, 0, lineVertexCount);
}
if (pointCount > 0) {
gl.glPointSize(pointSize);
gl.glBindVertexArray(pointVao);
gl.glDrawArrays(GL.GL_POINTS, 0, pointCount);
}
gl.glBindVertexArray(0);
}
public void clear() {
pointCount = 0;
lineVertexCount = 0;
}
private static FloatBuffer toFloat(float[] d) {
FloatBuffer fb = ByteBuffer.allocateDirect(Math.max(1, d.length) * Float.BYTES)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
fb.put(d).flip();
return fb;
}
private static IntBuffer toInt(int[] d) {
IntBuffer ib = ByteBuffer.allocateDirect(Math.max(1, d.length) * Integer.BYTES)
.order(ByteOrder.nativeOrder()).asIntBuffer();
ib.put(d).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