LWJGL 3 不渲染任何东西,同时成功创建显示

LWJGL 3 Not rendering anything, while successfully creating display

问题

我在使用 LWJGL 3 渲染任何东西时遇到问题。 它不会渲染任何东西,同时创建 GLFW 显示并清除 上色成功

工具

代码

package init;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.nio.IntBuffer;

import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryStack;

import exception.ExceptionHandler;
import util.Time;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;

public class DisplayInstance {
    
    public String title = "The SuperMatrix";
    public GraphicsDevice gd;
    public Game game;
    public Config conf;
    private long display;
    
    private GLFWErrorCallback glfwerrorcallback;
    
    public DisplayInstance(Game game) {
    
        this.gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        this.game = game;
        this.conf = Config.returnConfig(this);
        this.glfwerrorcallback = GLFWErrorCallback.createPrint(System.err);
        this.glfwerrorcallback.set();
        this.start();
        
    }
    
    public void start() {
        
        if (!glfwInit()) 
            ExceptionHandler.handleException(new IllegalStateException("Cannot initialize GLFW"));
        
        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
        
        this.display = glfwCreateWindow(this.conf.width, this.conf.height, this.title, 0, 0);
        if (this.display == 0L) {
            
            ExceptionHandler.handleException(new RuntimeException("Cannot create GLFW window"));
            
        }
        
        System.out.println(this.display);
        
        try (MemoryStack stack = stackPush()) {
            
            IntBuffer pWidth = stack.mallocInt(1);
            IntBuffer pHeight = stack.mallocInt(1);
            
            glfwGetWindowSize(this.display, pWidth, pHeight);
            GLFWVidMode mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
            
            glfwSetWindowPos(this.display,
                    (mode.width()-pWidth.get(0))/2,
                    (mode.height()-pHeight.get(0))/2
                    );
            
        } catch (Exception e) {
            ExceptionHandler.handleException(e);
        }
        
        glfwMakeContextCurrent(this.display);
        glfwSwapInterval(1);
        glfwShowWindow(this.display);
        
        this.loop();
        
    }
    
    public void loop() {
        
        GL.createCapabilities();
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0,this.conf.width, 0, this.conf.height, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glDisable(GL_DEPTH_TEST);
        
        Time time = new Time();
        
        while(!glfwWindowShouldClose(this.display)) {
            
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glfwSwapBuffers(this.display);
            glfwPollEvents();
            glPushMatrix();
            
            glBegin(GL_QUADS);
            
            glColor3f(1,0,1);
            glVertex2f(0, 0);
            glVertex2f(0, 64);
            glVertex2f(64, 64);
            glVertex2f(64, 0);
            
            glEnd();
            
            glPopMatrix();
            
            float deltaSeconds = time.getDelta()/Time.SECOND;
            float fps = deltaSeconds;
            System.out.println(fps);
            
        }
        
        this.destroy();
        
    }
    
    public void destroy() {
        
        glfwFreeCallbacks(this.display);
        glfwDestroyWindow(this.display);
        
        glfwTerminate();
        this.glfwerrorcallback.free();
        
        this.game.stopGame();
        
    }

}

谢谢。绝对感谢任何帮助。

好的,终于找到答案了

问题原因

问题是我在调用 glfwSwapBuffers(this.display):

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(this.display);

这实际上意味着我在显示缓冲区之前就清除了缓冲区。

修复

要解决这个问题,我所要做的就是将 glfwSwapBuffers(this.display) 向下移动到 glPopMatrix()[=35= 之后] 称呼。下面是 loop() 函数现在的样子:

    GL.createCapabilities();

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,this.conf.width, 0, this.conf.height, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glDisable(GL_DEPTH_TEST);

    Time time = new Time();

    while(!glfwWindowShouldClose(this.display)) {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glfwPollEvents();
        glPushMatrix();

        glBegin(GL_QUADS);

        glColor3f(1,0,1);
        glVertex2f(0, 0);
        glVertex2f(0, 64);
        glVertex2f(64, 64);
        glVertex2f(64, 0);

        glEnd();

        glPopMatrix();
        glfwSwapBuffers(this.display);

        float deltaSeconds = time.getDelta()/Time.SECOND;
        float fps = deltaSeconds;
        System.out.println(fps);

    }

    this.destroy();

大家,祝大家有个愉快的一天!

P.S。请不要介意FPS系统,我还在想办法。