在opengl es2中绘制两个三角形(使用两个绘制调用和两个位置)

时间:2019-08-07 09:06:46

标签: opengl-es-2.0 opengl-es-3.0

我知道如何在opengl 3中使用VAO绘制多对象。但是在opengl es2中没有VAO(glBindVertexArray仅在gl3,gl4和gles3上具有值)。 我可以在opengl es2中绘制两个三角形,如下所示:

    //1 triangle
     float positions[] =
    {
        0.0f    ,0.5f   ,1.0f   ,0.0f   ,0.0f,
        0.5f    ,-0.5f  ,0.0f   ,1.0f   ,0.0f,
        -0.5f   ,-0.5f  ,0.0f   ,0.0f   ,1.0f
    };
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
    auto positionLocation = glGetAttribLocation(program, "a_Position");
    assert(positionLocation >= 0);
    glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
    glEnableVertexAttribArray(positionLocation);
    auto colorLocation = glGetAttribLocation(program, "a_Color");
    assert(colorLocation >= 0);
    glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
    glEnableVertexAttribArray(colorLocation);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    //2 triangle
     float positions2[] =
    {
        0.5f    ,0.0f   ,1.0f   ,0.0f   ,0.0f,
        0.75f   ,0.5f   ,0.0f   ,1.0f   ,0.0f,
        1.0f    ,0.0f   ,0.0f   ,0.0f   ,1.0f

    };
    glGenBuffers(1, &VBO2);
    glBindBuffer(GL_ARRAY_BUFFER, VBO2);
    glBufferData(GL_ARRAY_BUFFER, sizeof(positions2), positions2, GL_STATIC_DRAW);

    auto positionLocation2 = glGetAttribLocation(program, "a_Position");
    assert(positionLocation2 >= 0);
    glVertexAttribPointer(positionLocation2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
    glEnableVertexAttribArray(positionLocation2);

    auto colorLocation2 = glGetAttribLocation(program, "a_Color");
    assert(colorLocation2 >= 0);
    glVertexAttribPointer(colorLocation2, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
    glEnableVertexAttribArray(colorLocation2);
    glDrawArrays(GL_TRIANGLES, 0, 3);

我可以用其他方式绘制两个三角形吗?

0 个答案:

没有答案