[英]How can i generate multiple VBO with an ByteBuffer
According to lwjgl javadooc, there is a function : 根据lwjgl javadooc,有一个功能:
public static void glGenBuffers(int n, ByteBuffer buffer)
But i dont exactly understand how this works. 但我不完全明白这是如何工作的。 Do i create an ByteBuffer :
我创建一个ByteBuffer:
ByteBuffer buffer = ByteBuffer.allocate("how much???")
glGenBuffers(4, buffer);
And especially do i have to fill the Buffer with glBufferSubData
OR is it better to create 4 buffers and bind and fill them? 特别是我必须用
glBufferSubData
填充缓冲区或者更好地创建4个缓冲区并绑定并填充它们?
My thought was, that it is more efficient when i only create 1 Buffer ,which stores vertices,texturecoords,normals and indices. 我的想法是,当我只创建1个缓冲区时,它会更有效,它可以存储顶点,纹理,法线和索引。
glGenBuffers(int n, ByteBuffer buffer)
generates n vertex buffer objects (VBOs), which you can use to store your data, and puts them in the specified buffer. glGenBuffers(int n, ByteBuffer buffer)
生成n个顶点缓冲区对象(VBO),您可以使用它们来存储数据,并将它们放在指定的缓冲区中。 This buffer is not actually the one that holds your data , but the ids of the VBOs just generated. 这个缓冲区实际上不是保存数据的缓冲区,而是刚刚生成的VBO的ID 。 You have to manually define the VBO data with
glBufferData
. 您必须使用
glBufferData
手动定义VBO数据。
The function is useful if you want to create multiple VBOs with a single call. 如果要通过单个调用创建多个VBO,则此功能非常有用。 Each VBO id is an integer, and the buffer has to be large enough to hold n (in this case 4) buffers.
每个VBO id都是一个整数,缓冲区必须足够大才能容纳n个(在这种情况下为4个)缓冲区。
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * Integer.BYTES);
glGenBuffers(4, buffer);
However, to make things easier, you can also use an IntBuffer
. 但是,为了
IntBuffer
,您还可以使用IntBuffer
。
IntBuffer buffer = BufferUtils.createIntBuffer(4);
glGenBuffers(4, buffer);
You can then attach the data to each of the VBOs. 然后,您可以将数据附加到每个VBO。
glBindBuffer(GL_ARRAY_BUFFER, buffer.get(2); /*value from 0 to 3*/
glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW);
(If you are using a ByteBuffer
, you have to call buffer.getInt(2)
instead.) (如果您使用的是
ByteBuffer
,则必须调用buffer.getInt(2)
。)
It is generally more efficient to have all data in a single buffer 将所有数据放在单个缓冲区中通常更有效
, but note that in this case you have to use at least two because indices have to be in a . GL_ELEMENT_ARRAY_BUFFER
,但请注意,在这种情况下,您必须使用至少两个因为索引必须在
GL_ELEMENT_ARRAY_BUFFER
。 However, the performance difference is very small, so it may be easier to use several different VBOs.
但是,性能差异非常小,因此使用几种不同的VBO可能更容易。
It is preferred to tightly pack all the data in a buffer and upload that with glBufferData
instead of calling glBufferSubData
for each type of data. 最好将所有数据紧密打包在缓冲区中,并使用
glBufferData
上传,而不是为每种类型的数据调用glBufferSubData
。 Your data layout will then look something like this (P = position, T = texture coordinate, N = normal vector): 您的数据布局将看起来像这样(P =位置,T =纹理坐标,N =法向量):
PPPTTNNNPPPTTNNNPPPTTN...
Now you can setup the vertex attribute pointers to correctly read the 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
are the attribute locations of your vertex shader attributes. positionIndex
, texCoordIndex
和normalIndex
是顶点着色器属性的属性位置。 You can get them with glGetAttribLocation
. 你可以使用
glGetAttribLocation
获取它们。
stride
is the number of bytes between the value of one vertex and the value of the next one. stride
是一个顶点的值与下一个顶点的值之间的字节数。 We have 3 positions, 2 texture coordinates and 3 normals, which are all floats, so we multiply them with Float.BYTES
. 我们有3个位置,2个纹理坐标和3个法线,它们都是浮点数,所以我们将它们与
Float.BYTES
相乘。
The last parameter to glVertexAttribPointer
is the offset in bytes, ie the number of bytes from the start of the buffer to where the first value is located. glVertexAttribPointer
的最后一个参数是以字节为单位的偏移量,即从缓冲区起始处到第一个值所在位置的字节数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.