OpenGL - Rendering from part of an index and vertex array depending on an element count

Posted by user1423893 on Game Development See other posts from Game Development or by user1423893
Published on 2012-09-01T14:31:40Z Indexed on 2012/09/01 15:49 UTC
Read the original article Hit count: 249

Filed under:
|

I'm currently drawing my shapes as lines by using a VAO and then assigning the dynamic vertices and indices each frame.

    // Bind VAO
    glBindVertexArray(m_vao);

    // Update the vertex buffer with the new data (Copy data into the vertex buffer object)
    glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(VertexPosition), m_vertices.data(), GL_DYNAMIC_DRAW);

    // Update the index buffer with the new data (Copy data into the index buffer object)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(unsigned short), indices.data(), GL_DYNAMIC_DRAW);

    glDrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));  

    // Unbind VAO
    glBindVertexArray(0);

What I would like to do is draw the lines using only part of the data stored in the index and vertex buffer objects.

The vertex buffer has its vertices set from an array of defined maximum size:

std::array<VertexPosition, maxVertices> m_vertices;

The index buffer has its elements set from an array of defined maximum size:

std::array<unsigned short, maxIndices> indices = { 0 };

A running total is kept of the number of vertices and indices needed for each draw call

numVertices numIndices

Can I not specify that the buffer data contain the entire array and only read from part of it when drawing?

For example using the vertex buffer object

glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(VertexPosition), m_vertices.data(), GL_DYNAMIC_DRAW);

m_vertices.data() = Entire array is stored numVertices * sizeof(VertexPosition) = Amount of data to read from the entire array

Is this not the correct way to approach this? I do not wish to use std::vector if possible.

© Game Development or respective owner

Related posts about c++

Related posts about opengl