Can glDrawArrays and glDrawElements not render different models at one time?

I encountered a strange problem when rendering different models with OpenGL.

I want to render model A by calling function glDrawArrays and model B with
glDrawElements, but unfortunately, only the model B was rendered with glDrawArrays, and the function glDrawElements was failure to render model. BUT if I comment the code to render model B, the model was correctly rendered by glDrawElements.

Actually, if I draw one model no matter what function I calling, It will draw correctly, But if draw two models with glDrawElements and glDrawArrays simultaneously, only glDrawArrays will draw.

This is my code, please help me.

    // **model A**
    glUniformMatrix4fv(m_program->uniformLocation("model"), 1, GL_FALSE,;
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
                          (void*)(3 * sizeof(float)));
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);

   // **model B**
    glUniformMatrix4fv(program->uniformLocation("model"), 1, GL_FALSE,;
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
    glDrawArrays(GL_LINES, 0, 6);

What evidence are you seeing that supports that conclusion?

Model A was correctly drawn by glDrawElements? Ok.

Really? Or is that you only “see” the results of model A in the RGB channels of your render target?

A few things to try:

  1. Use a separate "model" transform for each model, so they don’t render on top of each other.
  2. If depth tests are enabled, disable them (glDisable( GL_DEPTH_TEST )).

With each change, what do you see?

You should be binding the VAO first. Attribute state and the current EBO binding are stored in the VAO. In the core profile, it’s not meaningful to bind an EBO without any VAO bound (although this case isn’t specified as generating an error, there’s nowhere to store the binding state).

That would explain why it works with one call but not with both. The first time the code is executed, the EBO binding is ignored. The second time it’s executed m_vao is still bound when m_ebo is bound. If you enable the code for model B, vao (not m_vao) is bound at the time m_ebo is bound.

It doesn’t matter for the VBO because it’s the GL_ARRAY_BUFFER binding at the time of the glVertexAttribPointer call which is stored in the VAO, not the current binding.

1 Like

Thank you for your reply, your answer solves my question, thank you very mush. :wink:

There is some references about when binding the OpenGL state? I refer to the glDrawElements in wiki, not finding some description about its state binding.

the two model’s size are not same, so one model can not block the other completely. The true reason is what @GClements pointed out.

There’s the specification, although it isn’t particularly easy to read.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.