Drawing a circle in OpenGL

Drawing a circle in OpenGL

Do you ever stop and wonder how graphics have come so far? In the early days of computer graphics, creating curves and circles involved complex mathematical calculations and programming. Today, thanks to the incredible advancements in technology, we can draw circles and curves with unparalleled precision and ease.

It's crazy to think that just a few decades ago, even drawing a simple circle in OpenGL was a major challenge. It required a deep understanding of the graphics pipeline, including the coordinate system, vertex buffers, and shaders.

So, to commemorate mankind's undying spirit toward knowledge and advancement - let's draw a circle in OpenGL.

#include <iostream>
using namespace std;
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cmath>

void drawCircle(float x, float y, float r){
    float theta = 3.1415926535897 * 2 / 360;
    float tanTheta = tanf(theta);
    float cosTheta = cosf(theta);

    float i = r, j = 0;

    glLineWidth(5);
    glPointSize(7.0f);
    glBegin(GL_POINTS);

    for(int index = 0; index < 360; index++){
        glVertex2f(x + i, y + j);

        float ti = - j, tj = i;

        i += ti * tanTheta;
        i *= cosTheta;
        j += tj * tanTheta;
        j *= cosTheta;
    }

    glEnd();
}

int main(void)
{
    GLFWwindow* window;

    if (!glfwInit())    
        return -1;

    window = glfwCreateWindow(640, 480, "Drawing a circle in OpenGL", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);

        glColor3f(0.078f, 0.75f, 0.078f);
        drawCircle(0.0, 0.0, 0.5);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}