Dynamic Packing Data for Vertex OpenGL Buffer Objects

I am trying to write a wrapper for VBOs in OOP that consists of the addVertex, addNormal .. addX, flush () and render () functions. First I keep the vertices, normals, indices in separate vectors, for example:

std::vector<glm::vec4> vertexBuffer; std::vector<glm::vec4> colorBuffer; std::vector<glm::vec3> normalBuffer; std::vector<glm::vec2> texCoordBuffer; std::vector<unsigned int> indexBuffer; 

But since I read somewhere, holding different VBO identifiers for each of them is absolutely inefficient, so it’s better to pack them in order, for example VertexNormalTexCoordColor - VNTC - VNTC ... and present them in one VBO identifier. Therefore, the buffer can be effectively used when loading onto the GPU. So this time I still keep them in vectors, but when I call flush (), I want to pack them with structs, and then load this struct vector in the GPU:

 struct VBOData { glm::vec4 vert; glm::vec3 normal; glm::vec4 color; glm::vec2 texcoord; }; std::vector<VBOData> vboBuffer; 

and then I load everything in flush ():

 vboBuffer.reserve(vertexBuffer.size()); for (int i = 0; i < vertexBuffer.size(); ++i) { VBOData data; data.vert = vertexBuffer[i]; data.color = colorBuffer[i]; data.normal = normalBuffer[i]; data.texcoord = texCoordBuffer[i]; vboBuffer.push_back(data); } glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, vboBuffer.size() * sizeof(VBOData), (GLchar*) &vboBuffer[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); 

Now there is this problem, what if my object does not contain colors, normals or tex-chords and consists only of vertices? Or just about vertices and normals? Or different combinations of data that will always have vertices? Can I dynamically pack them in V (N) (T) (C) order for efficiency?

+6
source share
1 answer

Now there is this problem, what if my object does not contain colors, normals or tex-chords and consists only of vertices? Or just about vertices and normals?

Then you set the offset to the components that you have glVertexAttribPointer , and not the set of components that you are not using.

By the way, you have a good opengl.org tutorial on VBOs.

+1
source

Source: https://habr.com/ru/post/956044/


All Articles