Reputation: 363
I've compiled the Get Started example from the lwjgl.org webpage (http://www.lwjgl.org/guide) and now I wanted to continuously change to background-color. So, I wanted something like a blinking effect. But I don't get the background to switch its color permanently with glClearColor
, when the program is running.
I've tried to do this in the loop() function in the while loop with glClearColor
. But that seems wrong, as the glClearColor
Method is only called outside the while loop. For instance in the following code, the background color takes the color that was lastly called with glClearColor
.
...
private void loop() {
// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
// LWJGL detects the context that is current in the current thread,
// creates the ContextCapabilities instance and makes the OpenGL
// bindings available for use.
GLContext.createFromCurrent();
// Set the clear color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while ( glfwWindowShouldClose(window) == GL_FALSE ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
/* Switch permanently between red and green background color
- doesn't work, no blinking, just green background color */
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
}
...
I'm very new to lwjgl, thus I think I'm making some basic mistake and the background color shouldn't be changed in that way I did at all. Is there something to handle the background in lwjgl to achieve this?
I should mention that I'm using lwjgl 3, not lwjgl 2. And it seems to be that there is no Display Class anymore. GLFW seems to be the replacement for that.
Upvotes: 0
Views: 581
Reputation: 1034
In the while loop below, think that each iteration represent your 1 frame. In your scene you will see a change only after glfwPollEvents()
call after each iteration. That's why changing color 2 times at one iteration won't affect anything.
while ( glfwWindowShouldClose(window) == GL_FALSE ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
/* Switch permanently between red and green background color
- doesn't work, no blinking, just green background color */
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
At below, I wrote a simple solution for your problem. If you change the color at every frame you cannot see the change because it will be very fast, so I did a small trick that change your color at every 30 frame. You can write an alternative using time dependent color change instead of frame count.
Correct Version:
boolean redOrGreen = true; // true = Green false = Red
int counter = 0;
int COLORCHANGEAT = 30;
while ( glfwWindowShouldClose(window) == GL_FALSE ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
counter++;
if(counter == COLORCHANGEAT) {
redOrGreen = !redOrGreen;
counter = counter % COLORCHANGEAT;
}
if(redOrGreen == true)
glClearColor(1.0f, 1.0f, 0.0f, 0.0f);
else
glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
Upvotes: 1