【问题标题】:Color oscillates between black and red in open glopengl中的颜色在黑色和红色之间摆动
【发布时间】: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


【解决方案1】:

根据上面的 cmets,在您的原始代码中,您似乎只在创建窗口时清除一次,而通常您会在每帧清除一次。 (我猜你看到的闪烁是由于多个帧缓冲区中只有一个被清除为红色,但这只是一个猜测。)

请注意,虽然 update() 中的清除(如上所述)可能会起作用,但取决于环境的设置方式,render() 函数可能更适合执行此操作。很可能在调用 update() 时上下文是当前的,因此您可以摆脱在那里进行渲染,但至少从概念上讲,最好将更新逻辑和渲染保留在各自的专用函数中。

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 2012-08-04
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多