Application crashes at glDrawArrays(....)

Hello forum,

The application that I am trying to develop is crashing at the point glDrawArrays(…) . The snippet is as follows:


void Hilbert2DScene::render()
{
    if(!mInitialized)
        return;
    /*
     * Bind the vertex array object and vertex buffer object
     * */
    glBindVertexArray(mVaoID);
    /*
     * Now bind it to the context using the GL_ARRAY_BUFFER binding point
     * */
    glBindBuffer(GL_ARRAY_BUFFER,mVboVerticesID);

    mShader->Use();

    GL_CHECK_ERRORS;

    //allocate the buffer if the buffer is not allocated yet
    if(!mIsBufferAllocated)
    {
        //ALLOCATE THE BUFFER HERE
        unityCube(mIteration,mDim);

        //specify the amount of storage we want to use for the buffer
        glBufferData(GL_ARRAY_BUFFER,sizeof(glm::vec3) * mVertices.size(),&mVertices[0],GL_STATIC_DRAW);

        mIsBufferAllocated = true;

        mShowPoints = getNumberOfPointsForIteration(mIteration,mDim);
    }

    if(mClearBackground)
        glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );

    static const GLfloat color[] = {0.0f,0.0f,0.0f,1.0f};
    static const GLfloat depth = 1.0f;

    glClearBufferfv(GL_COLOR,0,color);
    glClearBufferfv(GL_DEPTH,0,&depth);


    //draw stuff

    //transfer the scene along the z-axis
    glm::mat4 T = glm::translate(glm::mat4(1.0f),glm::vec3(0.0f,0.0f,mDist));

    //the rotation matrix along X concatenated with the translation matrix
    glm::mat4 Rx = glm::rotate(T, glm::radians(static_cast<float>(mRX)),glm::vec3(1.0f,0.0f,0.0f));

    //rotation matrix along Y is concatenated with the rotation matrix along X
    glm::mat4 MV = glm::rotate(Rx,glm::radians(static_cast<float>(mRY)),glm::vec3(0.0f,1.0f,0.0f));

    mModelViewMatrix = mProjectionMatrix * MV;

    glEnableVertexAttribArray((GLuint)0);


    glUniformMatrix4fv(mShader->getUniform("MVP"),1,GL_FALSE,glm::value_ptr(mModelViewMatrix));

    if(mShowPoints < getNumberOfPointsForIteration(mIteration,mDim))
        ++mShowPoints;

    glDrawArrays(GL_LINE_STRIP,0,mShowPoints);


    glDisableVertexAttribArray(0);
    glBindVertexArray(0);

    GL_CHECK_ERRORS;

    mShader->UnUse();
}


I tried with gdb debugger and the backtrace leaves with nothing other than “??” . I am not sure where to start with . Some hint from the forum is eagerly awaited …

Thanks

Hello folks,

Do you need more information to provide me with hint to debug this issue ?

Thanks

As with all things like this, start removing things until you remove the problem. Or add things back until you produce the problem.

Scanning your code, it doesn’t appear that you understand how VAOs should be used. But the main thing I notice is the absence of a glVertexAttribPointer() call for your positions array.

I believe that I am doing it. Please check the following snippet:


void Hilbert2DScene::initialise()
{
    if(mInitialized)
        return;

    if(!initializeOpenGLFunctions())
    {
        std::cerr << "Modern OpenGL Functions could not be initialized" << std::endl;
        std::exit(EXIT_FAILURE);
    }

    GL_CHECK_ERRORS;

    //sets the polygon rasterization mode
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

    //load the shaders
    loadShaders();

    GL_CHECK_ERRORS;

    /*
     * Vertex array object represent the vertex fetch stage of the OpenGL pipeline
     * and used to supply the input to the vertex shader
     *
     * Vertex Array Object maintains all the state related to the input to the OpenGL pipeline
     * */
    glGenVertexArrays(1,&mVaoID);
    /*
     * generate the name of the buffer
     * */
    glGenBuffers(1,&mVboVerticesID);

    /*
     * Tell OpenGL where in the buffer object the data is
     * 1st - index to the vertex attribute
     * 2nd - the number of components that are stored in the buffer for each vertex
     * 3rd - the type of the data
     * 4th - tell if the data in the OpenGL buffer should be normalized or not before being passed to the vertex shader
     * 5th -
     * */
    glVertexAttribPointer((GLuint)0,3,GL_FLOAT,GL_FALSE,0,0);

    //make the initialization flag to true
    mInitialized = true;
}

I believe that I am doing it.

Wrongly.

There is neither a VAO nor a buffer object bound when you call that function. That is exactly why it crashes. It thinks you told it that your vertices are in CPU memory, starting at memory address NULL. So when you render, it will dutifully attempt to dereference NULL.