javaopengllwjglvao

Correct Use of glVertexAttribPointer?


I recently decided to start Learning OpenGL and got myself a book about OpenGL Core 3.3. The book is generally about C++.

So, after looking for a bit, I found a library in a language I was better in which provided almost the same functionality: lwjgl.

I followed the book's steps and translated the C++ syntax into java syntax, which worked until it got to actually drawing something.

There, the JVM just kept crashing, no matter what I changed about the code. After doing some debugging, I found out that the JVM crashed when I called either glVertexAttribPointer or glDrawArrays.

I am very new to OpenGL, and I am assuming this question must sound very stupid to someone more experienced, but: What do I need to change about this code?

        

        float[] vertices = {
                -0.5f, -0.5f, -0.0f,    
                0.5f, 0.5f, 0.0f,
                0.0f,0.5f,0.0f
        };
        FloatBuffer b = BufferUtils.createFloatBuffer(9);
        b.put(vertices);
        int VBO = glGenBuffers();
        int VAO = glGenVertexArrays();
        log.info("VBO:" + VBO + "VAO: " + VAO);
        // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
        glBindVertexArray(VAO);

            
        glVertexAttribPointer(0, 3, GL_FLOAT, false, 12,  0);
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
        glBindVertexArray(0);       

            

        // Run the rendering loop until the user has attempted to close
        // the window or has pressed the ESCAPE key.
        while (!glfwWindowShouldClose(window))
            {
                // input
                // -----

                // render
                // ------
                glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
                glClear(GL_COLOR_BUFFER_BIT);

                // draw our first triangle
                glUseProgram(shaderProgram);
                glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
                glDrawArrays(GL_TRIANGLES, 0, 3);
                 glBindVertexArray(0); // no need to unbind it every time 
         
                // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
                // -------------------------------------------------------------------------------
                glfwSwapBuffers(window);
                glfwPollEvents();
            }

I would be very thankful for any help i can get, if you need more info/ need to see more of my code please let me know. Thanks in advance


Solution

  • You have to bind the vertex buffer object to the target GL_ARRAY_BUFFER, before specifying the vertex attribute:

    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 12,  0);
    

    When glVertexAttribPointer is called, the buffer object currently bound to the ARRAY_BUFFER target is associated to the attribute (index) and a reference to the buffer object is stored in the state vector of the Vertex Array Object.