简体   繁体   中英

Opengl VAO gets overwritten

I'm learning opengl and currently I'm struggeling with VAOs. I would like to draw a cube and a triangle using VAOs but unfortunately, only the object that I create later is drawn. This is what I do in the main loop:

void main()
{
  //loading shader, generate window, etc ...

  //generate a cube:
  GLuint cube_vao = generateCube();

  //next, generate a triangle:
  GLuint triangle_vao = generateTriangle();

  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LESS);

  // Clear the screen
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  do
  {
    //draw:
    glBindVertexArray(triangle_vao);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glBindVertexArray(cube_vao);
    glDrawArrays(GL_TRIANGLES, 0, 12*3);

    glfwPollEvents();
    glfwSwapBuffers(window);

  } while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && 
           glfwWindowShouldClose(window) == 0);
}

Both, generateCube() and generateTriangle() do basically the same thing: create the vertices, create vbo, create vao and set the attributes. Then they return the vao id. This is generateTriangle() for example:

generateTriangle()
{
  //generate the vertex positions:
  GLfloat triangle_pos[] = //not part of the snippet -> too long

  //generate vbo for the positions:
  GLuint pos_vbo;
  glGenBuffers(1, &pos_vbo);
  glBindBuffer(GL_ARRAY_BUFFER, pos_vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_pos), triangle_pos, GL_STATIC_DRAW);

  //next, generate the vertex colors:
  GLfloat triangle_color[] = //not part of the snippet -> too long

  //generate vbo for the colors:
  GLuint col_vbo;
  glGenBuffers(1, &col_vbo);
  glBindBuffer(GL_ARRAY_BUFFER, col_vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_color), triangle_color, GL_STATIC_DRAW);

  //generate VAO:
  GLuint vao;
  glGenVertexArrays(1, &vao);
  glBindVertexArray(vao);

  GLint pos_attrib_id = glGetAttribLocation(programID, "line_pos");
  glEnableVertexAttribArray(pos_attrib_id);
  glBindBuffer(GL_ARRAY_BUFFER, pos_vbo);
  glVertexAttribPointer(pos_attrib_id, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

  GLint col_attrib_id = glGetAttribLocation(programID, "color");
  glEnableVertexAttribArray(col_attrib_id);
  glBindBuffer(GL_ARRAY_BUFFER, col_vbo);
  glVertexAttribPointer(col_attrib_id, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);

  //function to set the perspective (argument is the model matrix)
  setPerspective(glm::mat4(1.0f));

  return vao;
}

With this code, only the cube gets drawn. Furthermore, if I comment out the lines: glBindVertexArray(cube_vao); and glDrawArrays(GL_TRIANGLES, 0, 12*3); in the main, the triangle gets drawn but has the color and position of the cube, this is driving me crazy.

I'm using OSX with the shader versions 120 if that helps.

VAOs were introduced as standard functionality in OpenGL 3.0. On Mac OS, the default context version is 2.1. So you will need to specifically request a 3.x context during setup.

The exact mechanics of getting a 3.x context will depend on the window system interface/toolkit you are using. For example in GLUT, you include the GLUT_3_2_CORE_PROFILE flag in the argument to glInitDisplayMode() . With Cocoa, you include NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core in the pixel format attributes.

Note that Mac OS only supports the Core Profile for 3.x and later contexts. So you will not be able to use deprecated functionality anymore.

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