From Zero to Triangle : [Part 1]

From Zero to Triangle : [Part 1]

What is OpenGL and How to Create a window


OpenGL is an API which help us to communicate with the GPU and render pixels on screen with the help of the GPU.
This API is usually implemented by the GPU manufacturer itself. That is the very reason we can find some differences in the same thing rendered by different types of Graphic Cards, because it depends on the manufacturer how the API helps the GPU to interpret the code and render the pixels.

There are two Profiles of OpenGL to work with:

  • Immediate Profile : Easy to work with and understand but inefficient.

  • Core Profile : Difficult to get grasp on and master but very flexible and efficient.

To understand how OpenGL works we must know how it behaves.
OpenGL acts like a Huge State Machine and the OpenGL libraries are written in C language.


Basic Setup

To get similar results you have to setup :

  • GLFW [Graphics Library Framework] : Used for creating windows and managing input and other window related events.

  • GLAD : It is a library which helps to point at the OpenGL functions and load them when we need them during runtime.

  • Visual Studio Community : IDE to write the code and build the solution.

For better guidance I would suggest the following tutorials:


How to Create a Window?

As we move forward we come across our first hurdle, How can we get a window on our screen?

💡
From here onward there are steps given how to implement the code with inline explanation what the code is doing in form of the comments

So basically you need to follow these steps :

  • Step 1 : Include necessary libraries
#include <glad/glad.h> 
#include <GLFW/glfw3.h> 
#include <iostream>
using namespace std;
  • Step 2 : Declare important variables and function
void framebuffer_size_callback(GLFWwindow* window, int width, int height); // this to handle the window resizing
void process_input(GLFWwindow* window);//this manages the closing input from the window

const unsigned int SCR_WIDTH = 800; //WIDTH of the window
const unsigned int SCR_HEIGHT = 600; //HEIGHT of the window
  • Step 3 : Define the main driver code
int main()
{
    glfwInit(); //This is to initiate the glfw lib
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //hints are basically Configuration settings and here we are setting major
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //and minor version of OpenGL
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //this is to set our profile to core so that we can get more flexibility 

    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "triangle window", NULL, NULL); //here we allocate space and create window object

    if (window == NULL) {
        cout << "Failed To Create Window" << endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window); //here we make that window object run on the current thread 

    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);  // here we bind the callback fucntion with the window 

    if ((!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))) //here we are checking if glad can load all opengl function pointers or not
    {
        cout << "Failed to load the GLAD" << endl;
        return -1;
    }

    while (!glfwWindowShouldClose(window)) //to keep the window open we are running a while loop 
    {
        process_input(window);
        glClearColor(0.3f, 0.4f, 0.5f, 1.0f); // this is the color that will be brought up once you clear the screen
        glClear(GL_COLOR_BUFFER_BIT); 
        glfwSwapBuffers(window); // this function helps with buffer swaps
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}
💡
Buffer Swapping: It is a process of rendering pixel in background and flipping it with the frame in the front to avoid flickering and artifacts while rendering in real time.
  • Step 4 : Define the functions which were declared above
void process_input(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) 
    {
        glfwSetWindowShouldClose(window, true);
    }

}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) 
{
    glViewport(0, 0, width, height);
}

And hence we created our very first window.
For better look at code : Source code


Reference and Credit:

LearnOpenGL: Reference: LearnOpenGL.com