LWJGL 3.1.6在Win 10上崩溃

时间:2018-07-30 16:26:07

标签: java opengl lwjgl vbo vao

即使这个问题被问了多次(我读了所有这些,但没有解决方案对我有用),我还是试图用LWJGL和OpenGL为矩形建模 ,但每次都会崩溃。这是我的PC统计信息:

AMD锐龙1600x | 微星Nvidia GTX 1060(6GB)| MSI x370 Carbon Pro主板

我也在具有i7处理器和Nvidia Quadro K 1000M设置的Intel-Setup上尝试过此操作,但在以下内容中可能看到相同的错误:

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

在此类中,我创建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。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

根据评论

  

我还没有实现着色器

在OpenGL中进行渲染的最先进方法是使用Shader

如果不使用着色器,则必须通过glVertexPointer定义顶点数据数组。 glVertexPointer指定固定功能顶点坐标属性的数组。如果您没有着色器程序,则必须使用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上下文是最新的。

在创建窗口之后和任何OpenGL指令之前调用glfwMakeContextCurrent(window)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();         // <-----

.....

最后,您在渲染循环中缺少glfwSwapBuffersglfwSwapBuffers交换指定窗口的前后缓冲区。换句话说,它会将您渲染到的缓冲区绑定到屏幕上:

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

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

    glfwPollEvents();
}

另请参阅LWJGL 3 Guide - Getting Started