0
\$\begingroup\$

I just started to study openGL and I wrote this "Hello World" program to draw a triangle.

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1; 

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 640, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate(); 
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    
    /* Loop until the user closes th e window */
    while (!glfwWindowShouldClose(window)) 
    {

        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f( 0.0f,  0.5f); 
        glVertex2f( 0.5f, -0.5f);
        glEnd();

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

The problem is that I don't like the output since it seems to not match the resolution of my screen. The following pictures compare the output that I obtain by running the code above and a replica that I draw using inkscape.

enter image description here

enter image description here

As you can see, the diagonal edges of the triangle generated with openGL are not as smooth as the ones of the triangle generated with inkscape. I'm wonder if there is a way to fix this problem. Note that I'm an absolute beginner, so that I'm not so much interested in the solution, but rather I just want to know if in the future I will be able to fix the problem.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

This looks like a combination of small resolution + no anti-aliasing (multi-sampling). Try making these changes to your code:

// Add anti-aliasing
glfwWindowHint(GLFW_SAMPLES, 8);
// Increase window size
window = glfwCreateWindow(1920, 1080, "Hello World", NULL, NULL);

// Before you draw, right after glClear, enable multi-sampling. 
// Can't remember if this is required.
glEnable(GL_MULTISAMPLE);
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.