How can I generate multiple VBOs with ByteBuffer

According to lwjgl javadooc there is a function:

public static void glGenBuffers(int n, ByteBuffer buffer)

      

But I don't understand exactly how it works. Create ByteBuffer:

ByteBuffer buffer = ByteBuffer.allocate("how much???")
glGenBuffers(4, buffer);

      

And especially I need to fill the Buffer with glBufferSubData

OR is it better to create 4 buffers and bind and fill them?

My thought was that it is more efficient when I only create 1 buffer that stores vertices, texture sockets, normals and indices.

+3


source to share


1 answer


glGenBuffers(int n, ByteBuffer buffer)

generates n vertex buffer objects (VBOs) that you can use to store your data and places them in the specified buffer. This buffer is actually not the one that stores your data, but only VBO ids. You have to manually define the VBO data with glBufferData

.

This function is useful if you want to create multiple VBOs with one call. Each VBO ID is an integer and the buffer must be large enough to hold n buffers (4 in this case).

ByteBuffer buffer = BufferUtils.createByteBuffer(4 * Integer.BYTES);
glGenBuffers(4, buffer);

      

However, to keep things simple, you can also use IntBuffer

.

IntBuffer buffer = BufferUtils.createIntBuffer(4);
glGenBuffers(4, buffer);

      

Then you can attach data to each of the VBOs.

glBindBuffer(GL_ARRAY_BUFFER, buffer.get(2); /*value from 0 to 3*/
glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW);

      

(If you are using ByteBuffer

, you need to call buffer.getInt(2)

.)




It is generally more efficient to have all the data in one buffer , but note that in this case, you need to use at least two, because the indices must be in GL_ELEMENT_ARRAY_BUFFER

... However, the performance difference is very small, so it might be easier to use several different VBOs.

It is preferable to densely pack all data into a buffer and load it with glBufferData

instead of calling glBufferSubData

for each data type. Then your data layout will look something like this (P = position, T = texture coordinate, N = normal vector):

PPPTTNNNPPPTTNNNPPPTTN...

      

You can now set up pointers to vertex attributes to correctly read values ​​from this buffer.

int vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, tightlyPackedData, GL_STATIC_DRAW);

int stride = (3 + 2 + 3) * Float.BYTES;

glVertexAttribPointer(positionIndex, 3, GL_FLOAT, false, stride, 0);
glVertexAttribPointer(texCoordIndex, 2, GL_FLOAT, false, stride, 3 * Float.BYTES);
glVertexAttribPointer(normalIndex,   3, GL_FLOAT, false, stride, (3 + 2) * Float.BYTES);

      

positionIndex

, texCoordIndex

and normalIndex

is the location of your vertex shader attributes. You can get them with glGetAttribLocation

.

stride

- the number of bytes between the value of one vertex and the value of the next. We have 3 positions, 2 texture coordinates and 3 normals that are all floating, so we multiply them by Float.BYTES

.

The last parameter glVertexAttribPointer

is the byte offset, that is, the number of bytes from the beginning of the buffer to the location of the first value.

+2


source







All Articles