[英]Make background blink with lwjgl
我已经从lwjgl.org网页( http://www.lwjgl.org/guide )编译了“入门”示例,现在我想不断地更改为背景色。 所以,我想要一个闪烁效果。 但是在程序运行时,我没有背景使用glClearColor
永久地切换其颜色。 我尝试使用glClearColor
在while循环的loop()函数中执行此glClearColor
。 但这似乎是错误的,因为glClearColor
方法仅在while循环之外调用。 例如,在以下代码中,背景色采用的是最后用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();
}
}
...
我是lwjgl的新手,因此我认为我犯了一些基本的错误,完全不应以这种方式更改背景色。 是否可以处理lwjgl中的背景来实现这一目标?
我应该提及的是,我使用的是lwjgl 3,而不是lwjgl2。而且似乎不再有Display Class。 GLFW似乎可以代替它。
在下面的while循环中,认为每个迭代代表1帧。 在您的场景中,只有在每次迭代后调用glfwPollEvents()
之后,您才会看到更改。 这就是为什么一次迭代更改两次颜色不会产生任何影响的原因。
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();
}
在下面,我为您的问题写了一个简单的解决方案。 如果在每一帧更改颜色,您将看不到更改,因为更改很快,所以我做了一个小技巧,每30帧更改一次颜色。 您可以使用随时间变化的颜色而不是帧数来编写替代方案。
正确版本:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.