Getting Started guide for LWJGL 3 contains a bug

Started by spdfNerd, October 26, 2020, 12:45:06

Previous topic - Next topic

spdfNerd

I have only just fixed this problem, and it has been bugging me for the past few days now. I thought it would be a good idea to post on here just to let people know about this bug, if they ever run into it.
This is the loop() method of the guide:
private void loop() {
    GL.createCapabilities();
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}

The problem here is that the buffer is being cleared before it has a chance of getting rendered, causing nothing to show up when the user tries to render anything. The way to fix this bug is to just move the glClear() method call to below the glfwSwapBuffers() method call. So the code would look like this:
private void loop() {
    GL.createCapabilities();
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    while (!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window); // This line and the next are swapped
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glfwPollEvents();
    }
}

It would also be great to see the code changed so that this bug does not exist in the guide!

Btw, first time poster here, does anyone know how to post an image? I couldn't figure it out :( Any help would be greatly appreciated!

KaiHH

QuoteThe problem here is that the buffer is being cleared before it has a chance of getting rendered, causing nothing to show up when the user tries to render anything.
The typical phases in a render loop is:
1. glClear()
2. draw calls
3. swap buffers.
You did not show how you think that a user would add their rendering to the existing Getting Started code, but the general phases are always as shown above.
1. clear screen (you HAVE to glClear before in order to invalidate anything rendered in a swapbuffer image in previous draw calls).
2. make any draw calls
3. swap the backbuffer to the frontbuffer
So: There is no issue with the current Getting Started code. You just seem to think that user code would come before the glClear, which it DOES NOT!