简体   繁体   中英

LWJGL crashes on glDrawElements

I have run into this error and i have no Idea what it could be. So iam initalizing GLFW, creating the Capability for OpenGL, loading the Shader and the VAO into VRAM.

I really have no Idea what it could be at this point.

public static void main(String[] args){
        GLFWErrorCallback.createPrint(System.err).set();
        if(!glfwInit()) {
            assert false : "failed to inialize GLFW";
        }
        
        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
        
        long windowID = glfwCreateWindow(800, 600, "title", NULL, NULL);
        if(windowID == NULL) {
            assert false : "Failed to create GLFW-Window";
        }
        
        glfwMakeContextCurrent(windowID);
        glfwSwapInterval(0);
        glfwShowWindow(windowID);
        GL.createCapabilities();
        
        
        
        Shader shader = new Shader(
                FileReader.readRaw("assets/shaders/basicMesh.vs"),
                FileReader.readRaw("assets/shaders/basicMesh.fs"));
        
        
        
        int vaoID = glGenVertexArrays();
        glBindVertexArray(vaoID);
        
        int vbo = glGenBuffers();
        glBindBuffer(GL_VERTEX_ARRAY, vbo);
        glBufferData(GL_VERTEX_ARRAY, new float[] {
                0.0f, 0.0f, 0.0f,
                0.0f, 1.0f, 0.0f,
                1.0f, 1.0f, 0.0f,
                1.0f, 0.0f, 0.0f
        }, GL_STATIC_DRAW);
        
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
        
        glBindBuffer(GL_VERTEX_ARRAY, 0);
        
        int ebo = glGenBuffers();
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, new int[] {
                0,1,2,
                2,3,0
        }, GL_STATIC_DRAW);
        
        glBindVertexArray(0);
        
        while(!glfwWindowShouldClose(windowID)) {
            
            glfwPollEvents();
            
            glUseProgram(shader.getID());
            
            glBindVertexArray(vaoID);
            //crahes here!!!
            glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
            
            glBindVertexArray(0);
            
            glUseProgram(0);
            
            glfwSwapBuffers(windowID);
            
        }
}

Shader code is pretty simple for the Moment. Just to answer any questions here is the Code.

basicMesh.vs

#version 420 core

layout(location=0) in vec3 position;

void main() {
    gl_Position = vec4(position, 1.0);
}

basicMesh.fs

#version 420 core

out vec4 color;

void main(){
    color = vec4(1.0f, 1.0f, 0.0f, 1.0f);
}

GL_VERTEX_ARRAY is not a valid buffer binding point for eg glBindBuffer . The reason why it crashes is because you enabled generic vertex attribute 0 without having specified any vertex data for that, since you bind to an illegal binding point. glVertexAttribPointer looks for a buffer bound to GL_ARRAY_BUFFER , not GL_VERTEX_ARRAY .

What you meant to bind to is not GL_VERTEX_ARRAY but GL_ARRAY_BUFFER .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM