簡體   English   中英

Opengl VAO被覆蓋

[英]Opengl VAO gets overwritten

我正在學習opengl,目前正在與VAO斗爭。 我想使用VAO繪制立方體和三角形,但不幸的是,僅繪制了稍后創建的對象。 這是我在主循環中所做的:

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);
}

generateCube()generateTriangle()兩者基本上都具有相同的作用:創建頂點,創建vbo,創建vao和設置屬性。 然后,他們返回vao id。 例如,這是generateTriangle()

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;
}

使用此代碼,僅繪制多維數據集。 此外,如果我注釋掉以下行: glBindVertexArray(cube_vao); glDrawArrays(GL_TRIANGLES, 0, 12*3); 主要是繪制三角形,但是具有立方體的顏色和位置,這讓我發瘋。

如果有幫助,我將OSX與着色器版本120一起使用。

VAO是OpenGL 3.0中的標准功能。 在Mac OS上,默認上下文版本為2.1。 因此,在安裝過程中,您將需要特別請求3.x上下文。

獲取3.x上下文的確切機制將取決於您所使用的窗口系統界面/工具包。 例如,在GLUT中,您可以在glInitDisplayMode()的參數中包含GLUT_3_2_CORE_PROFILE標志。 使用Cocoa,您可以在像素格式屬性中包括NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core

請注意,Mac OS僅對3.x和更高版本的上下文支持Core Profile。 因此,您將不再能夠使用不推薦使用的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM