CLOSE
Updated on 04 Jul, 202511 mins read 65 views

Now that we understand what computer graphics is and why OpenGL is so widely used, it's time to get hands-on. In this section, we will walk through setting up a modern OpenGL development environment and creating our first window using GLFW and GLAD.

What is OpenGL?

OpenGL (Open Graphics Library) is a cross-platform API for rendering 2D and 3D graphics. It abstracts the complexity of interacting directly with GPU hardware. OpenGL doesn't handle things like window creation or input—that's where GLFW comes in.

Key traits:

  • Cross-platform (Windows, Mac, Linux)
  • Hardware-accelerated
  • Real-time rendering
  • C-based API, works well with C++

OpenGL vs Vulkan vs DirectX

FeatureOpenGLVulkanDirectX
ComplexityMediumHighMedium
PerformanceGoodVery highHigh
Cross-platformYesYesWindows/Xbox only
Learning CurveEasierSteepModerate

For learning and building projects quickly, OpenGL is ideal. Vulkan is better suited for advanced systems programming or game engines that need low-level control.

Step 1: Setting Up the Environment

You will need the following tools to begin working with OpenGL:

Required Libraries:

  1. GLFW – For creating windows and handling input
  2. GLAD – For loading OpenGL functions
  3. GLM – For math (used later)

Recommended Tools:

  • C++ compiler (e.g., MSVC, g++, clang++)
  • CMake (build system generator)
  • IDE (e.g., Visual Studio, CLion, VSCode)

Step 2: Create a Basic OpenGL Project

With CMake:

Folder Structure

OpenGLProject/
├── CMakeLists.txt
├── src/
│   └── main.cpp

main.cpp

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

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

int main() {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // OpenGL 3.3+
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Window", NULL, NULL);
    if (window == NULL) {
        std::cerr << "Failed to create GLFW window\n";
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cerr << "Failed to initialize GLAD\n";
        return -1;
    }

    glViewport(0, 0, 800, 600);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    while (!glfwWindowShouldClose(window)) {
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Step 3: CMake Configuration

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(OpenGLProject)

set(CMAKE_CXX_STANDARD 17)

# Add your paths to GLFW and GLAD
add_subdirectory(libs/glfw)
add_library(glad src/glad.c)

include_directories(
    libs/glfw/include
    libs/glad/include
)

add_executable(OpenGLProject src/main.cpp)
target_link_libraries(OpenGLProject glfw glad)

What This Code Does

  • GLFW creates a window and sets an OpenGL context.
  • GLAD loads the necessary OpenGL function pointers.
  • glViewport ensures your scene scales with the window size.
  • glClearColor sets the background color each frame.

When you run this, you'll see a blank window with a teal background—your first rendered OpenGL output!

Leave a comment

Your email address will not be published. Required fields are marked *