OpenGL or GLFW only show when the window is moved

I am just trying to draw a line between the bottom left corner and the top right corner of the screen. The result is quite annoying as it displays a line that goes from the bottom left corner to the midpoint of the screen ... However, if I move the window position with my mouse, it suddenly changes and displays correctly! What's going on and how can I solve it?

I am running the code on a macOS system and building with Xcode.

Before moving the window:

Before moving the window

After moving the window:

After moving the window

This is the code:

#include <GLFW/glfw3.h>

#define SCREEN_W 640
#define SCREEN_H 480

int main(int argc, char * argv[]) {

    glfwInit();

    GLFWwindow* window = glfwCreateWindow(SCREEN_W, SCREEN_H, "GLFW Window", NULL, NULL);

    glfwMakeContextCurrent(window);

    float lineVertices[] = {
        0, 0, 0,
        SCREEN_W, SCREEN_H, 0
    };

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0.0f, 0.0f, SCREEN_W, SCREEN_H);
    glOrtho(0, SCREEN_W, 0, SCREEN_H, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

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

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, lineVertices);
        glDrawArrays(GL_LINES, 0, 2);
        glDisableClientState(GL_VERTEX_ARRAY);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();

    return 0;
}

      

+3


source to share





All Articles