Search code examples
javaopengllwjglvbovao

LWJGL 3.1.6 crashes on Win 10


even if this question was ask multiple times (i readed all of that and no solution worked for me), I am trying to model a rectangle with LWJGL and OpenGL , but it crashes every time. Here my PC-Stats:

AMD-Ryzen 1600x | MSI Nvidia GTX 1060 (6GB) | MSI x370 Carbon Pro Motherboard

I also tried this on an Intel-Setup, with an i7 Processor and a Nvidia Quadro K 1000M setup, but same Error you can see in the following:

https://hastebin.com/ayiqiritov.makefile

My Drawing Method:

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

In this class I create VAOs and the VBOs and store data into those:

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

And my main Method:

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

}

So I have already tried:

Update drivers for Graphic-card, update java, update Windows, setting up a new eclipse, reinstall java and deleting .metadata in eclipse.

Can anyone pls help me?


Solution

  • According to the comment

    I dont have implemented a shader yet

    The state of the art way of rendering in OpenGL, would be to use a Shader.

    If you don't use a shader, than you have to define the array of vertex data by 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);
    }
    

    Further you have to enable the client-side capability for vertex coordinates by 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);
    }
    

    Further note, that you have to creates the "GLCapabilities" instance and makes the OpenGL bindings available for use before you use an OpenGL instruction like GL30.glGenVertexArrays() and you have to ensure the the OpenGL context is current.

    Call glfwMakeContextCurrent(window) and GL.createCapabilities() after creating the window and before any OpenGL instruction:

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

    Finally you are missing glfwSwapBuffers int the render loop. glfwSwapBuffers Swaps the front and back buffers of the specified window. In other, simple words it binges the buffer where you rendered to, onto the screen:

    while(!glfwWindowShouldClose(window)){
        renderer.prepare();
        renderer.render(model);
    
        glfwSwapBuffers(window); // <-----
    
        glfwPollEvents();
    }
    

    See also LWJGL 3 Guide - Getting Started