简体   繁体   English

我应该使用相同的VBO传递不同的顶点属性吗? 还是我应该使用2?

[英]Should I use the same VBO for passing different vertex attributes? Or should I use 2?

I know the glVertexAttribPointer will use the values from the VBO that was bound when it was called. 我知道glVertexAttribPointer将使用VBO调用时绑定的值。 But can you buffer twice onto the same object? 但是您可以在同一对象上缓冲两次吗? Would it replace what was in? 它会取代里面的东西吗? Or can you clear a buffer? 还是可以清除缓冲区? I don't know if this approach is correct: 我不知道这种方法是否正确:

glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO); // shared VBO

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(posLoc);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),(GLvoid*)0);


glBufferData(GL_ARRAY_BUFFER,  sizeof(colours),  colours, GL_STATIC_DRAW);
glEnableVertexAttribArray(colLoc);
glVertexAttribPointer(colLoc, 4, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat),(GLvoid*)0);

glBindBuffer(GL_ARRAY_VERTEX, 0);
glBindVertexArray(0);

Or if I should be using 2 VBOs for buffering the data. 或者,如果我应该使用2个VBO缓冲数据。 What would happen if you call the glBufferData function twice to the same bound vertex array object? 如果对相同的绑定顶点数组对象调用glBufferData函数两次,将会发生什么? This is the other way I would think of for doing this: 这是我想到的另一种方式:

glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO1); // VBO for vertices

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(posLoc);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),(GLvoid*)0);

glBindBuffer(GL_ARRAY_BUFFER, VBO2); // VBO for colours

glBufferData(GL_ARRAY_BUFFER,  sizeof(colours),  colours, GL_STATIC_DRAW);
glEnableVertexAttribArray(colLoc);
glVertexAttribPointer(colLoc, 4, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat),(GLvoid*)0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

The top example won't work as the second glBufferData call will overwrite all of the buffer space in the second one. 最上面的示例将不起作用,因为第二个glBufferData调用将覆盖第二个glBufferData中的所有缓冲区空间。 To properly do that, you have to use the stride and pointer arguments properly, so that the data is interleaved. 为了正确地做到这一点,您必须正确使用stride和pointer参数,以便对数据进行交织。 It's easier (and cleaner imo) to just have multiple VBO's, each storing a separate set of data. 拥有多个VBO(每个存储单独的数据集)会更容易(也更干净imo)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM