Turfa Auliarachman
Turfa Auliarachman

Reputation: 115

OpenGL doesn't create window with the desired size

So I have this code

GLFWwindow* window = glfwCreateWindow(1024, 768, "Hello Triangle", NULL, NULL);
glViewport(0, 0, 1024, 768);

But, when I created a triangle with coordinate (-1, -1), (0, 1), and (1,-1), it only drew a small triangle at the corner. If I resize the window, it draws the correct triangle.

So, I set a frame buffer size callback:

void FramebufferSizeCallback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
    cout << width << " " << height << endl;
}

I tried to resize the window a little, and it turns out the the window size is bigger than 1024 x 768.

So, why doesn't the glfwCreateWindow function create a window with size 1024 x 768? Or am I having a wrong understanding?

I am using macOS High Sierra.

Upvotes: 2

Views: 856

Answers (1)

Rabbid76
Rabbid76

Reputation: 210877

The first call of glViewport won't work, because at this point there is no current OpenGL context. Call glfwMakeContextCurrent to make the context of the window current, before any OpenGL instruction:

GLFWwindow* window = glfwCreateWindow(1024, 768, "Hello Triangle", NULL, NULL);

glfwMakeContextCurrent(wnd); // <-- make the context current before glViewport

glViewport(0, 0, 1024, 768);

Note, glfwCreateWindow creates a window and an associated OpenGL context, but it does not change which context is current.

Upvotes: 2

Related Questions