0

I'm still relatively new to OpenGL, just finished the "Getting Started" section on learnopengl and decided to go ahead and make a simple renderer. I have a question tho.

I've always noticed the author putting all the vertex data (position and texture) in one array in this manner (pos0,texCoord0,pos1,texCoord1,....)

I feel like this might not be practical when it comes to what people actually do, since there can be some redundancy in the data. So my question is this, can I have 2 separate VBOs (one for positions and one for texCoords) and link between them using one (or two, I don't know) EBOs?

I've already tried having 2 VBOs and one EBO. The structure of the indices in the EBO is as follows: first x indices map to the positions, the second x indices map to the texCoords

Here's the code snippet:

unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);


unsigned int posVBO;
glGenBuffers(1, &posVBO);
glBindBuffer(GL_ARRAY_BUFFER, posVBO);
glBufferData(GL_ARRAY_BUFFER, 3 * cubeMesh->position_count * sizeof(float), cubeMesh->positions, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);


unsigned int texVBO;
glGenBuffers(1, &texVBO);
glBindBuffer(GL_ARRAY_BUFFER, texVBO);
glBufferData(GL_ARRAY_BUFFER, 2 * cubeMesh->texcoord_count * sizeof(float), cubeMesh->texcoords, GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(2);

unsigned int cubeEBO;
glGenBuffers(1, &cubeEBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, cnt *2* sizeof(unsigned int), cubeIndices, GL_STATIC_DRAW);

cnt here is the size of the vertices (calculated by looping through the faces, if the face has 3 vertices cnt+=3, if it has 4 vertices cnt+=6)

I'm using this library for model loading:

glDrawElements(GL_TRIANGLES, cnt, GL_UNSIGNED_INT, 0);

glDrawElements(GL_TRIANGLES, cnt, GL_UNSIGNED_INT, (void*)(cnt * sizeof(GLuint)));

The result isn't quite right when I do that, If you need any more information or code I'll provide it.

So TLDR: Given that the vertex data are separated in more than 1 array, is it possible to draw an object without creating one big array combining both data? and how? (i.e using multiple VBOs or EBOs)

Thank you

0 Answers0