Theory - How OpenGL works?

Drawing objects on a monitor is not that easy. You need to create specific set of instruction for what do you want to do. It’s not that easy as saying “Draw me a rectangle”. This process is called the Graphic Pipeline.

  1. Input (CPU): Vertices and Indices:
    1. Vertices: basic elements of 3D objects. All vertices are just specific points in space (x,y,z). They can have additional attributes such as texture, color etc.
    2. Indices: rather than duplicate vertices, which are same as others, we create unique vertices and a list of indices to specify the order of connections to draw between them. It saves us a lot of memory and boosts performance.
  2. Transfer data to GPU:
    1. Data defined on the CPU (in RAM memory) needs to be transfer on the GPU (VRAM memory).
      1. VBO (Vertex Buffer Object)- a buffer (a specific place in VRAM) where we transfer vertex data from the CPU.
      2. EBO (Element Buffer Object)/IBO (Index Buffer Object) - special buffers used for indices.
  3. Shaders:
    1. Shaders are small programs in GLSL (OpenGL ShadingLanguage), which run directly on the GPU. They give us full control over visual content and the position of objects. Two basic types of shaders:
      1. Vertex Shader: It processes all vertices from the VBO. The main task of this shader is to transform 3D vertex data into 2D coordinates on the monitor.
      2. Fragment Shader (Pixel Shader): It works for each pixel on our monitor. The main task of this shader is to determine the color of each pixel.
  4. Shader Program:
    1. The compiled Vertex Shader and Fragment Shader are linked into single Shader Program. This program is then used to draw elements on screen.
  5. VAO (Vertex Array Objects):
    1. It’s a special object, that stores all the current configuration needed to draw an object:
      1. VBO - vertex data
      2. Ebo - index data
      3. How to interpret data from the VBO
    2. With a VAO, when we want to draw an object, there is no need to configure VBO, EBO and other attribute one more time. Just bind the VAO and it will automatically restore the previous configuration.