I have data stored in an object file. It consists of vertex, normal, and face data. I have loaded the data into 3 vectors. two vector where each element represents a 3-element vertex/normal, and one vector<vector> where each vector represents a face and has 6 elements which index into the other vectors as follow [normIdx, vtxIdx, normIdx, vtxIdx, normIdx, vtxIdx]. I am currently using glBegin(GL_TRIANGLES) and looping over the faces to draw the vertices. I want to modify my code to use a VBO to improve its efficiency.
I have tried writing a function to generate the buffers I need and then I have another to draw the object.
Current Render (It should be sphere)
void createBuffers()
{
vector<Vector3f> interleavedData;
for (u_int i = 0; i < vecv.size(); i++) {
interleavedData.push_back(vecn[i]);
interleavedData.push_back(vecv[i]);
}
size_t dataSize = interleavedData.size() * sizeof(Vector3f);
// Generate VAO
glGenVertexArrays(1, &vaoId);
glBindVertexArray(vaoId);
// Bind vertex buffer
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, dataSize, interleavedData.data(), GL_STATIC_DRAW);
// Setup attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(Vector3f), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(Vector3f), (void*)(sizeof(Vector3f)));
glEnableVertexAttribArray(1);
// Generate IBO
glGenBuffers(1, &iboId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW);
// Unbind buffers
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void drawBuffers()
{
// Bind VAO, Draw, Unbind VAO
glBindVertexArray(vaoId);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
user26730950 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.