Before all

  1. OpenGL Site
  2. OpenGL Wiki

OpenGL drawing process

  1. Read obj file from vertices vector
  2. Vertex Buffer Object (VBO)
    1. Generate vertex buffer object
    2. Copy the vertices vector to the VBO (reside in GPU memory)
      1. First need to bind the buffer using glBindBuffer
      2. Then copy the data using glBufferData to load the vertices data to the buffer object
      3. After finishing the buffer loading, unbind using glBindBuffer with 0 as the object id.
  3. Vertex attribute specification
    1. Tell the OpenGL how to interpret the buffer data, which can be used in shader language
    2. glVertexAttribPointer: specifies the start and stride of an attribute
    3. glEnableVertexAttribArray(NUM): enable the NUMth attribute
    4. In GLSL, use layout(location = 0) to load the attribute 0
  4. Vertex Array Object (VAO)
    1. Store vertex specs and which VBO to use
    2. Use glGenVertexArrays to generate id
    3. glBindVertexArray to bind and unbind
    4. The VBO will bind to the VAO when specifying the buffer type to be GL_ARRAY_BUFFER, need to bind VBO after binding VAO
  5. Vertex shader
    1. write to gl_Position variable for the position of each vertex
    2. Get input from the VAO
    3. Can also have output
    #version 330 core
    layout (location = 0) in vec3 aPos;  // the first attribute
    layout (location = 1) in vec3 aColor;  // the second attribute
    out vec3 ourColor;  // pass the color data to the fragment shader
    void main() {
       gl_Position = vec4(aPos, 1.0);  // output
       ourColor = aColor;
    };
    
  6. Fragment shader
    1. Compute the color of the pixels in RGBA
    2. Also lighting and texture
    version 330 core
    out vec4 FragColor; // output color
    in vec3 ourColor; // vertex color, passed by the vertex shader
    void main() {
        FragColor = vec4(ourColor, 1.0f); 
    }
    
  7. Shader Compiling and Linking
    1. Compile shader source code at run time
      1. glCreateShader
      2. glShaderSource
      3. glCompileShader
    2. Link shaders outputs and inputs
      1. glCreateProgram
      2. glAttachShader: attach shader to the program
      3. glLinkProgram
      4. glDeleteShader: once linked, no longer needed
    3. glUseProgram: use the linked shader program
  8. Drawing!
    1. glDrawArrays(type, first, count): draw the surfaces using arrays of vertices, normals, and colors starting from first and end after drawing count vertices, along with type specified
    2. glDrawElements: draw VAO using the element buffer object (EBO), which specifies the indices of the vertices to form surfaces and draw.

OpenGL Shader

  1. glGetUniformLocation: get the location of the uniform variable
  2. glUniformMatrix4fv: send the uniform value to the shader