-1

问题

我无法使用 LWJGL 3 渲染任何东西。它不会渲染任何东西,同时创建 GLFW 显示并成功清除颜色。

工具

  • Eclipse 霓虹灯(Java IDE)
  • LWJGL 3.1.1 build 16 与 GLFW、JAWT 和 OPENGL 绑定、本地人。

编码

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();
        
    }

}

谢谢你。绝对任何帮助将不胜感激。

4

1 回答 1

5

好吧,我终于找到了答案。

问题的原因

问题是我在调用glfwSwapBuffers(this.display)之前调用了glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) :

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

这实质上意味着我在显示缓冲区之前清除它们。

修复

为了解决这个问题,我所要做的就是将glfwSwapBuffers(this.display)移到glPopMatrix()调用之后。下面是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();

大家,有一个美好的一天!

PS请不要介意FPS系统,我还在努力解决。

于 2017-05-04T08:00:57.053 回答