Search code examples
javaopengllwjglvbovao

VAO and VBO Crashing when drawing


I changed my code from DisplayLists to VBOs / VAOs. But when i run the application it crashes.

It crashes when at the first attempt to draw the VAOs. I'm drawing using no shaders (so it does not cause problems).

There are up to 12 faces (12 * 3 vertices) in one VAO and texture coordinates for those. There are up to 500 000 VAOs.

How i create a face:

tData.add(new float[]{textureX + 0.1249f, textureY+ 0.1249f});
vData.add(new float[]{x, y, z});
tData.add(new float[]{textureX+ 0.1249f, textureY+0.0001f});
vData.add(new float[]{x, y+1, z+1});
tData.add(new float[]{textureX+0.0001f, textureY+0.0001f});
vData.add(new float[]{x+1, y+1, z+1});

Creating the VBO/VAO:

if(vData.isEmpty())
        return;

    int vaoHandle = glGenVertexArrays();
    glBindVertexArray(vaoHandle);

    int vertexDataSize = vData.size() * 3;
    int textureDataSize = tData.size() * 2;
    FloatBuffer vertexData = BufferUtils.createFloatBuffer(vData.size() * 3);
    FloatBuffer textureData = BufferUtils.createFloatBuffer(tData.size() * 2);

    while(!vData.isEmpty())
    {
        vertexData.put(vData.remove(0));
    }
    while(!tData.isEmpty())
    {
        textureData.put(tData.remove(0));
    }

    vertexData.flip();
    textureData.flip();

    int vertexHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vertexHandle);
    glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
    glVertexAttribPointer(0, vertexDataSize, GL_FLOAT, false, 0, 0);

    int textureHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, textureHandle);
    glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
    glVertexAttribPointer(1, textureDataSize, GL_FLOAT, false, 0, 0);


    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    renderEngine.vaos.add(new VAO(vaoHandle, (int)((float)vertexDataSize / 3f)));

Rendering the VBO / VAO:

    glUseProgram(0);//TODO:REMOVE
    for(VAO vao : vaos)
    {
        glBindVertexArray(vao.getHandle());
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        System.out.println(vao.getVertices());//correct numeber
        glDrawArrays(GL_TRIANGLES, 0, vao.getVertices());//<-- CRASH at first time called

        glDisableVertexAttribArray(1);
        glDisableVertexAttribArray(0);
    }
    glBindVertexArray(0);

Here's the error:

# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000000b3fb610, pid=7736, tid=6224
#
# JRE version: Java(TM) SE Runtime Environment (8.0_20-b26) (build 1.8.0_20-b26)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.20-b23 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [ig75icd64.dll+0x8b610]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Geosearchef\workspaces\workspaceLWJGL\OrangeJuiceVBO\hs_err_pid7736.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

I don't think it's sensefull to post the whole error here. Do you have any idea why this is happening? I can't find anything about this error concerning VBOs.


Solution

  • You do not set the vertex attribute pointers correctly:

    int vertexDataSize = vData.size() * 3;
    int textureDataSize = tData.size() * 2;
    [...]
    glVertexAttribPointer(0, vertexDataSize, GL_FLOAT, false, 0, 0);
    [...]
    glVertexAttribPointer(1, textureDataSize, GL_FLOAT, false, 0, 0);
    

    The size parameter defines the number of elements in the vector of each vertex, and must be in the range of 1 to 4.

    YOur code will just generate a GL error - and you should definitively add some error checks, at least for debugging - and leaves the attribute pointer uninitialized.

    Another issue here: you use generic vertex attributes 0 and 1, but you don't use shaders. That is not going to work. The spec only guarantees that attribute index 0 will map to the classic glVertex attribute, but attribute 1 might be anything, or not work at all.