LWJGL 3成功创建显示时未呈现任何内容 [英] LWJGL 3 Not rendering anything, while successfully creating display

查看:135
本文介绍了LWJGL 3成功创建显示时未呈现任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用LWJGL 3渲染任何东西. 在创建GLFW显示并清除 成功上色.

I am having trouble rendering absolutely anything with LWJGL 3. It will not render anything, whilst creating the GLFW display and clearing the color successfully.

  • Eclipse Neon(Java IDE)
  • 具有GLFW,JAWT和OPENGL绑定的LWJGL 3.1.1内部版本16.
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();

    }

}

谢谢.绝对可以的,我们将不胜感激.

Thank you. Absolutely any help would be appreciated.

推荐答案

好的,我终于找到了答案.

Alright, I finally found the answer.

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

The problem was that I called glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) right before I called glfwSwapBuffers(this.display):

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

从本质上讲,这意味着我在显示缓冲区之前立即清除了缓冲区.

This essentially means that I clear the buffers right before I show them.

要解决此问题,我要做的就是在 glPopMatrix()调用之后将 glfwSwapBuffers(this.display)下移.这是 loop()函数现在的样子:

To fix this, all I had to do is move glfwSwapBuffers(this.display) down to after the glPopMatrix() call. Here is how the loop() function looks like now:

    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系统,我仍在努力弄清楚.

这篇关于LWJGL 3成功创建显示时未呈现任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆