0

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);
Blight
  • 87
  • 1
  • 10
  • 1
    Why are you copying the data? I would use `buffer.asFloatBuffer()` – Peter Lawrey Sep 01 '14 at 13:57
  • @PeterLawrey Because I don't want to copy the entire bytebuffer but only the bytes from `header.fileofs` to `header.fileofs + header.filelen`. – Blight Sep 01 '14 at 14:02
  • 1
    buffer.slice().asFloatBuffer()? – nablex Sep 01 '14 at 14:11
  • I think you misread the question; you don't need to copy the data at all (not even a portion of it) – Peter Lawrey Sep 01 '14 at 14:12
  • When I use `buffer.asFloatBuffer()`, the new `FloatBuffer` also contains all the bytes that come after the data I need. I'd like to send the `FloatBuffer` to OpenGL, with only the vertex data. – Blight Sep 01 '14 at 14:32
  • Can I use `vertexes.limit(header.filelen / Buffers.SIZEOF_FLOAT)` to stop JOGL from reading too many bytes? – Blight Sep 01 '14 at 14:49
  • JOGL takes into account the position and the limit of a buffer. – gouessej Sep 03 '14 at 17:47
  • @PeterLawrey If you want you can add your first comment as an answer and I will accept it. – Blight Sep 05 '14 at 15:15

0 Answers0