Summit
Summit

Reputation: 2268

Do i need to create two different VAO an VBO for data which is very similar

I am drawing a circle this is my code to extract the data for the circle.

// Vertices
    data.push_back(0.0f);
    data.push_back(0.0f);
    data.push_back(0.0f);
    // Normal
    data.push_back(0.0f);
    data.push_back(0.0f);
    data.push_back(1.0f);
    // Texture Coord
    data.push_back(0.5f);
    data.push_back(0.5f);

    for (int i = 0; i < iSegments + 1; i++)
    {
    //  float angle = 2.0f * M_PI * i / iSegments;
        float initialAngle = startAngle * (M_PI / 180);
        float angle = initialAngle + ((angleCircle *( M_PI / 180)) * i / iSegments);
        // vertex data
        float x, y, z ,tx,ty ,tz;
        x = cos(angle)  * 50.0;
        y = sin(angle)  * 50.0;
        z = 0.0;
        tx = cos(angle) * 0.5 + 0.5f;
        ty = sin(angle) * 0.5 + 0.5f;
        data.push_back(x);
        data.push_back(y);
        data.push_back(z);
        data.push_back(0.0f);
        data.push_back(0.0f);
        data.push_back(1.0f);
        data.push_back(tx);
        data.push_back(ty);
    }

    glGenVertexArrays(1, &m_VAO);
    glGenBuffers(1, &m_VBO);
    glBindVertexArray(m_VAO);
    glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
    glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof( float ) , &data[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
    glBindVertexArray(0);
    //glEnableVertexAttribArray(0);
    isInited = true;

if i want to draw another cirle where only difference in data would be the z position of that circle , which would be at 50.0 instead of 0.0.

Do i need to create another VAO and VBO for the new data or can i update the existing data in the VBO object ?

Upvotes: 1

Views: 104

Answers (1)

Rabbid76
Rabbid76

Reputation: 210978

Do i need to create another VAO and VBO for the new data or can i update the existing data in the VBO object

No. Add a model transformation matrix to the shader program and set the individual position, orientation and scale of the circles by the model matrix. e.g:

in vec3 vertex;
layout (location = 0) uniform mat4 u_model;

void main()
{
    vec4 worldPos = u_model * vec4(vertex.xyz, 1.0);

    // [...]
}
glUniformMatrix4fv(0, 1, GL_FALSE, model1);
glDrawArrays( ... );

glUniformMatrix4fv(0, 1, GL_FALSE, model2);
glDrawArrays( ... );

Upvotes: 3

Related Questions