【发布时间】:2020-11-18 13:09:53
【问题描述】:
我用 lwjgl 3 和 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);
}}
我怎样才能停止振荡
【问题讨论】:
-
在创建窗口时,您似乎只清除了一次。你试过清除每一帧吗?
-
如果我清除每一帧,我在渲染或更新中添加清除代码? @scg
-
@scg clearing in update 方法解决问题,写答案
标签: java user-interface lwjgl glfw