OpenGL渲染管道,Shader,VAO&VBO&EBO

OpenGL渲染管線

(也就是)OpenGL渲染一幀圖形的流程

  • 以下列舉最簡單的,渲染一個三角形的流程,你可以將它視為 精簡版OpenGL渲染管線

  更複雜的流程也僅僅就是:在此基礎上的各個流程中 添加額外的內容,而這些會在後續補充。

    • CPU將三角形的  [點坐標,及其索引資訊]  傳入GPU顯示記憶體
    • CPU調用 gl函數編譯鏈接掛載 shader
      • shader是使用  類C語言: glsl 編寫的程式 
      • GPU會執行shader中的內容,並根據shader程式進行繪圖。
      • GPU會   匯總shader提供的數據&CPU提供的數據,並根據他們進行繪圖。
    • CPU調用 gl函數激活 GPU執行shader並繪製圖
    • 以下是相關程式碼:
 // build and compile our shader program
 // ------------------------------------
    // vertex shader
    unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    // check for shader compile errors
    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // fragment shader
    unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    // check for shader compile errors
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // link shaders
    unsigned int shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    // check for linking errors
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
// -----------------------------以上:Shader編譯,編譯檢查,鏈接,鏈接檢查-----------------------------

// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------ //三角形頂點,及其索引資訊:
  float vertices[] = { 0.5f, 0.5f, 0.0f, // top right 0.5f, -0.5f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, // bottom left -0.5f, 0.5f, 0.0f // top left }; unsigned int indices[] = { // note that we start from 0! 0, 1, 3, // first Triangle 1, 2, 3 // second Triangle };
//向GPU傳入相關數據 unsigned
int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
   glBindVertexArray(VAOID) glVertexAttribPointer(
0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind glBindBuffer(GL_ARRAY_BUFFER, 0); // remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound. //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary. glBindVertexArray(0);

   while (!glfwWindowShouldClose(window)) {
   // input
   // -----
     processInput(window);
  
   // render
// ------
     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
     glClear(GL_COLOR_BUFFER_BIT);
  
   // draw our first triangle
     glUseProgram(shaderProgram);
     glBindVertexArray(VAO);

   // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
     glDrawArrays(GL_TRIANGLES, 0, 6);
     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

   // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
   // -------------------------------------------------------------------------------
     glfwSwapBuffers(window);
     glfwPollEvents();
  }
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
  glDeleteVertexArrays(1, &VAO);
  glDeleteBuffers(1, &VBO);
  glDeleteBuffers(1, &EBO);
  glDeleteProgram(shaderProgram);

 

VAO&VBO&EBO

    1. VBO:一個抽象對象,OpenGL沒有固定它的實現,

     因此你不必了解它的實現,你只需知道VBO是  OpenGL用來管理  來自CPU數據的 一種工具:

    unsigned int VBOID, VAOID, EBOID;
    //申請三個整形值,用以存放 申請的VBO,VAB,EBO的 ID    
    
    glGenBuffers(1, &VBOID);
    glGenVertexArrays(1, &VAOID);
    glGenBuffers(1, &EBOID);
    //向OpenGL申請: 1*VBO, 1*VAO, 1*EBO  並將其ID傳入各自的 地址
  //[此地址可以是連續數組空間的首地址,也可以是單一的一個數據]
glBindVertexArray(VAOID)
//將 glBindBuffer(GL_ARRAY_BUFFER, VBOID);
//將VBOID指向的VBO 與flag:GL_ARRAY_BUFFER綁定。

 

    2. 

 

Tags: