opengl中的颜色在黑色和红色之间摆动

bvn4nwqk  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(380)

我用lwjgl3和opengl编写了几个java类,它们创建了一个红色的窗口,但是颜色实际上可能在红色和后面的代码之间摆动
这个窗口类在具有游戏循环的主类中用作对象

package engine.io.output;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;

import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.*;

import engine.io.input.*;

public class Window {
public int width , height;
public String title;
long time;
int frames = 0;
private long window;
public KeyInput keyInputCallBack = new KeyInput();
MouseButtonInput mouseButtonInput = new MouseButtonInput();
MousePositionInput mPositionInput = new MousePositionInput();
public Window(int width , int height , String title) {
    this.width = width;
    this.height = height;
    this.title = title;
}
public void create() {
    time = System.currentTimeMillis();
    if (!glfwInit()) {
        System.err.println("Couldnt init");
        System.exit(-1);
    }
    window = glfwCreateWindow(width, height, title,0, 0);

    if (window == 0) {
        System.err.println("cannot create window");
        System.exit(-1);
    }
    GLFWVidMode vm = glfwGetVideoMode(glfwGetPrimaryMonitor());

    int windowXPos = vm.width()/2 - width/2;
    int windowYPos = vm.height()/2 - height/2;

    glfwSetWindowPos(window, windowXPos,windowYPos);

    addCallback(window);
    glfwMakeContextCurrent(window);

    GL.createCapabilities();
    GL11.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glfwShowWindow(window);
    glfwSwapInterval(1);

}
private void addCallback(long window2) {
    glfwSetKeyCallback(window,keyInputCallBack.getKeyCallback());
    glfwSetMouseButtonCallback(window2, mouseButtonInput.getbuttonCallback());
    glfwSetCursorPosCallback(window2, mPositionInput.getMousePositionCallback());
}
public void update() {
    frames++;
    if (System.currentTimeMillis() >= time + 1000) {
        glfwSetWindowTitle(window,"fps is " + frames);
        frames = 0;
        time = System.currentTimeMillis();
    }
    glfwPollEvents();
}
public void render() {
    glfwSwapBuffers(window);
}
public boolean shouldClose() {
    return glfwWindowShouldClose(window);

}}

我怎样才能停止振荡

2j4z5cfb

2j4z5cfb1#

根据上面的注解,在您的原始代码中,在创建窗口时,似乎只清除一次,而通常情况下,每帧清除一次(我猜您看到的闪烁是由于多个帧缓冲区中只有一个被清除为红色,但这只是猜测。)
请注意,尽管在update()中清除(如上所述)可能会起作用,但这取决于环境的设置方式,render()函数可能更适合执行此操作。在调用update()时,上下文很可能是最新的,因此可以在那里进行渲染,但至少在概念上,最好将update逻辑和渲染保留在各自的专用函数中。

相关问题