LWJGL 3.1.6 在 Win 10 上崩溃

LWJGL 3.1.6 crashes on Win 10

即使这个问题被问了多次(我阅读了所有这些但没有解决方案适合我),我正在尝试使用 LWJGL 和 OpenGL 建模矩形 ,但每次都会崩溃。这是我的 PC 统计数据:

AMD-锐龙 1600x | 微星英伟达 GTX 1060 (6GB) | 微星 x370 Carbon Pro 主板

我也在 Intel-Setup 上试过这个,有 i7 处理器和 Nvidia Quadro K 1000M 设置,但你可以在下面看到同样的错误:

https://hastebin.com/ayiqiritov.makefile

我的画法:

public void render(RawModel model){
    GL30.glBindVertexArray(model.getVaoID());
    GL20.glEnableVertexAttribArray(0);
    GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, model.getVertexCount());
    GL20.glDisableVertexAttribArray(0);
    GL30.glBindVertexArray(0);
}

在这个 class 中,我创建了 VAO 和 VBO 并将数据存储到其中:

    private List<Integer> vaos = new ArrayList<Integer>();
private List<Integer> vbos = new ArrayList<Integer>();

public RawModel loadToVAO(float[] positions) {
    int vaoID = createVAO();
    storeDataInAttributeList(0, positions);
    unbindVAO();
    return new RawModel(vaoID, positions.length / 3);
}

public void cleanUp() {
    for (int vao : vaos) {
        GL30.glDeleteVertexArrays(vao);
    }
    for (int vbo : vbos) {
        GL15.glDeleteBuffers(vbo);
    }
}

private int createVAO() {
    int vaoID = GL30.glGenVertexArrays();
    vaos.add(vaoID);
    GL30.glBindVertexArray(vaoID);
    return vaoID;
}

private void storeDataInAttributeList(int attributeNumber, float[] data) {
    int vboID = GL15.glGenBuffers();
    vbos.add(vboID);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
    FloatBuffer buffer = storeDataInFloatBuffer(data);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
    GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}

private void unbindVAO() {
    GL30.glBindVertexArray(0);
}

private FloatBuffer storeDataInFloatBuffer(float[] data) {
    FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
    buffer.put(data).position(0);
    buffer.flip();
    return buffer;
}

我的主要方法:

        public static void main(String[] args){
    if(!glfwInit()){
        throw new IllegalStateException("Failed");
    }

    System.out.println(GL11.glGetString(GL11.GL_VERSION));

    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

    GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3);
    GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 4);

    long window = GLFW.glfwCreateWindow(640, 480, "Hello World", 0, 0);

    if(window == 0){
        throw new IllegalStateException("Failed to create Window");
    }

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - 640) / 2, (vidmode.height() - 480) / 2);

    glfwShowWindow(window);

    Loader loader = new Loader();
    Renderer renderer = new Renderer();

    float[] vertices = {
        -0.5f, 0.5f, 0f,
        -0.5f, -0.5f, 0f,
        0.5f, -0.5f, 0f,

        0.5f, -0.5f, 0f,
        0.5f, 0.5f, 0f,
        -0.5f, 0.5f, 0f
    };

    RawModel model = loader.loadToVAO(vertices);

    while(!glfwWindowShouldClose(window)){
        renderer.prepare();
        renderer.render(model);
        glfwPollEvents();
    }

    loader.cleanUp();
    GLFW.glfwTerminate();

}

所以我已经试过了:

更新显卡驱动程序,更新 java,更新 Windows,设置新的 eclipse,重新安装 java 并删除 eclipse 中的 .metadata。

谁能帮帮我?

根据评论

I dont have implemented a shader yet

OpenGL 中最先进的渲染方式是使用 Shader

如果您不使用着色器,则必须通过 glVertexPointer. glVertexPointer specifies a array for Fixed-Function vertex coordinate attribute. If you don't have a shader program, then you have to use the Fixed Function Pipeline.

定义顶点数据数组
private void storeDataInAttributeList(int attributeNumber, float[] data) {
    int vboID = GL15.glGenBuffers();
    vbos.add(vboID);
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
    FloatBuffer buffer = storeDataInFloatBuffer(data);
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);

    GL11.glVertexPointer( 3, GL11.GL_FLOAT, 0, 0 ); // <---------

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}

此外,您必须通过 glEnableClientState( GL_VERTEX_ARRAY ):

启用顶点坐标的客户端功能
public void render(RawModel model){
    GL30.glBindVertexArray(model.getVaoID());

    GL11.glEnableClientState( GL11.GL_VERTEX_ARRAY );   // <---------

    GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, model.getVertexCount());

    GL11.glDisableClientState( GL11.GL_VERTEX_ARRAY );  // <---------

    GL30.glBindVertexArray(0);
}

进一步注意,在使用 GL30.glGenVertexArrays() 之类的 OpenGL 指令之前,您必须创建 "GLCapabilities" 实例并使 OpenGL 绑定可用,并且您必须确保 OpenGL 上下文是当前。

在创建 window 之后和任何 OpenGL 指令之前调用 glfwMakeContextCurrent(window) and GL.createCapabilities()

long window = GLFW.glfwCreateWindow(640, 480, "Hello World", 0, 0);
if(window == 0){
    throw new IllegalStateException("Failed to create Window");
}

GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - 640) / 2, (vidmode.height() - 480) / 2);

glfwMakeContextCurrent(window);  // <-----

glfwShowWindow(window);

GL.createCapabilities();         // <-----

.....

最后,您缺少 glfwSwapBuffers 渲染循环。 glfwSwapBuffers 交换指定 window 的前缓冲区和后缓冲区。换句话说,简单地说,它会将您渲染到的缓冲区加载到屏幕上:

while(!glfwWindowShouldClose(window)){
    renderer.prepare();
    renderer.render(model);

    glfwSwapBuffers(window); // <-----

    glfwPollEvents();
}

另见 LWJGL 3 Guide - Getting Started