java - 使用 lwjgl 让背景闪烁

标签 java opengl lwjgl glfw

我已经从 lwjgl.org 网页 ( http://www.lwjgl.org/guide ) 编译了入门示例,现在我想不断更改背景颜色。所以,我想要一些像闪烁效果的东西。但当程序运行时,我无法使用 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,而不是lwjgl 2。而且似乎不再有显示类了。 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();
}

关于java - 使用 lwjgl 让背景闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717945/

相关文章:

java - LWJGL 的几个问题

java - 使用 lwjgl 将值从我的 Java 程序传递到 GLSL

java - 为什么这段java代码表现出奇怪的行为?

c# - 本类(class)中使用的 OOP 概念

Java 8 Lambda 重载

java - 奇怪的 Spring 事务行为

c++ - OpenGL 中的着色器问题

c++ - 哪个用于 OpenGL 客户端等待 : glGetSynciv vs. glClientWaitSync?

c++ - Vbo如何绘制基元

java - LWJGL 按住一个键