LWJGL 3在成功创建显示时不渲染任何内容

时间:2017-05-04 07:18:41

标签: java opengl 3d lwjgl 3d-engine

问题

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

工具

  • Eclipse Neon(Java IDE)
  • LWJGL 3.1.1 build 16 with GLFW,JAWT和OPENGL bindings,natives。

代码

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

    }

}

谢谢。绝对会得到任何帮助。

1 个答案:

答案 0 :(得分:2)

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

问题的原因

问题是我在调用 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();

每个人,祝你有个美好的一天!

<强> P.S。请不要介意FPS系统,我仍然想弄明白。