简体   繁体   中英

Multiple VBO(s) in one VAO

How would you have, an example an VBO for position, an VBO for texture-coords and lastly a VBO for normals? Currently I can only create one VBO for one VAO, and I have no idea how to make it multiple VBO(s)

float vertices[] = {
    // Pos  
 0.0f,  0.0f,
 1.0f,  0.0f,
 0.0f,  1.0f,
 0.0f,  1.0f,
 1.0f,  1.0f,
 1.0f,  0.0f
};
float texcords[] = {
    0.0f, 0.0f,
    1.0f, 0.0f,
    0.0f, 1.0f,
    0.0f, 1.0f,
    1.0f, 1.0f,
    1.0f, 0.0f
};

unsigned int VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);

glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

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

The buffer object which is currently bound to the ARRAY_BUFFER is associated to the vertex attribute (index), when glVertexAttribPointer is called. A reference to the Vertex Buffer Object (object id) is stored in the state vector of the Vertex Array Object .
Just bid the proper VBO, before specifying the vertex attribute:

unsigned int VBO[2];
glGenBuffers(2, VBO);

glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(texcords), texcords, GL_STATIC_DRAW);

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

glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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