Efficiently filling a FloatBuffer from part of a ByteBuffer

449 views Asked by At

Currently I am filling a FloatBuffer from a Mapped Byte Buffer in the following way:

/**
 * The vertex lump (Lump 3) is an array of coordinates of all the vertices (corners)    of brushes in the map geometry. 
 * Each vertex is a Vector of 3 floats (x, y, and z), giving 12 bytes per vertex. 
 * @author Jurian
 *
 */
public class VertexLump extends Lump {
    public final FloatBuffer vertexes;

    /**
     * @param buffer Little-Endian buffer with actual data
     * @param bsp Used for referencing the header with position and length of vertex data
     */
    public VertexLump(MappedByteBuffer buffer, ValveBSP bsp) {
        super(buffer, bsp.headers[LumpType.LUMP_VERTEXES.index]);
        //MappedByteBuffer is set to correct position in super()
        vertexes = FloatBuffer.allocate(header.filelen / (Buffers.SIZEOF_FLOAT));
        for(int i = 0; i < vertexes.capacity(); i++) {
            vertexes.put(buffer.getFloat());
        }
        vertexes.flip();
    }
}

But I have a suspicion that there is a better way to do this, instead of looping over all positions. There can be quite a few vertexes in the buffer (a maximum of 65536).

This seems to work:

public VertexLump(MappedByteBuffer buffer, ValveBSP bsp) {
    super(buffer, bsp.headers[LumpType.LUMP_VERTEXES.index]);
    vertexes = buffer.asFloatBuffer();
    vertexes.limit(header.filelen / Buffers.SIZEOF_FLOAT);
}

However will this function in OpenGL send too much data to the GPU? Or just from the current position to the limit?

gl.glBufferData(GL3.GL_ARRAY_BUFFER, vertexes.limit(), vertexes, GL3.GL_STATIC_DRAW);
0

There are 0 answers