I am wondering about the strategy of using direct buffers with glVertexPointer. With GL4Java, I had code like
gl.glVertexPointer(2, gl.GL_FLOAT, 0, series.getArray());
where “series” was an instance of my DataSeries class, which basically wraps a float array and provides getArray() and setArray() along with other functionality.
JOGL uses a Buffer with glVertexPointer but, apparently, it requires it to be direct. Therefore, FloatBuffer.wrap(array) does not work, as it creates a non-direct buffer. I found that the following code works as a functional replacement
float[] array = series.getArray();
ByteBuffer bBuf = ByteBuffer.allocateDirect(4 * array.length);
bBuf.order(ByteOrder.nativeOrder());
FloatBuffer fBuf = bBuf.asFloatBuffer();
fBuf.put(array);
gl.glVertexPointer(2, gl.GL_FLOAT, 0, fBuf);
The problem is that the array can be any size. On occasion, the application is memory bound because of it. I’m not sure I like being forced to take an existing array, allocate new space for it, and copy it in, when space is already tight.
The only alternative I see is to rewrite the DataSeries class to be based around a Buffer instead of an array. Any code that allocates an array and creates a DataSeries from it would need to be modified to allocate a Buffer instead. This would be a considerable effort. Is there any alternative to this?
Is there a reason that JOGL can’t work with a non-direct buffer? According to the Java documentation for ByteBuffer, “In general it is best to allocate direct buffers only when they yield a measureable gain in program performance.” Would giving the option of a non-direct wrapped array be a big drain on performance or be harder to implement?