Question

I've been developing a 3D creator but I'm still stuck in the first stages since I haven't been able to draw with VBO.

This is a piece of my glwidget.cpp code, where all the objects are drawn

void GLWidget::initializeGL()
{
    #define PROGRAM_VERTEX_ATTRIBUTE 0
    #define PROGRAM_NORMALS_ATTRIBUTE 1

    //glEnable(GL_DEPTH_TEST);
    //glEnable(GL_CULL_FACE);


    vShader= new QGLShader (QGLShader::Vertex, this);
    vShader->compileSourceFile("../src/shaders/editorVshader.glsl");

    fShader= new QGLShader (QGLShader::Fragment, this);
    fShader->compileSourceFile("../src/shaders/editorFshader.glsl");

    editor= new QGLShaderProgram (this);
    editor->addShader(vShader);
    editor->addShader(fShader);
    editor->bindAttributeLocation("vertices", PROGRAM_VERTEX_ATTRIBUTE);
    editor->bindAttributeLocation("normals", PROGRAM_NORMALS_ATTRIBUTE);
    editor->link();
    editor->bind();
}

void GLWidget::paintGL()
{
    glClearColor(0.4765625, 0.54296875, 0.6171875, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    /*
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -10.0f);
    */

    editor->setUniformValue("projectMatrix", controller->getProjectionMatrix());
    editor->setUniformValue("viewMatrix", controller->getViewMatrix());


    /** Ahora para las operaciones especificas de cada objeto **/

    for (int i=0; i<Objects.size(); i++)
    {
        Objects[i].modelmatrix.scale(1.0, 1.0, 1.0);
        Objects[i].modelmatrix.rotate(1.0, 0.0, 0.0, 1.0);
        editor->setUniformValue("modelMatrix", Objects.at(i).modelmatrix);

        glEnableClientState(GL_VERTEX_ARRAY);
        Objects[i].vertexbuffer->bind();
        glVertexPointer(3, GL_FLOAT, 0, Objects.at(i).indexed_vertices.data());
        editor->enableAttributeArray("vertices");
        editor->setAttributeBuffer("vertices", GL_FLOAT, 0, Objects[i].indexed_vertices.size() * sizeof(vertex)); // (PROGRAM_VERTEX_ATTRIBUTE, Objects[i].vertices.data());
    /*
        Objects[i].normalbuffer.bind();
        //glVertexPointer(3, GL_FLOAT, 3, Objects.at(i).indexed_normals.data());
        glEnableClientState(GL_NORMAL_ARRAY);
        editor->enableAttributeArray(PROGRAM_NORMALS_ATTRIBUTE);
        editor->setAttributeBuffer (PROGRAM_NORMALS_ATTRIBUTE, GL_FLOAT, 0, Objects[i].indexed_normals.size() * sizeof(vertex));*/

        //glDrawArrays(GL_QUADS, 0, Objects[i].vertices.size());
        Objects[i].elementbuffer->bind();
        glDrawElements(GL_QUADS, Objects[i].indices.size(), GL_UNSIGNED_SHORT, (void*)0);
    }
}

It explodes when it tries to execute the glDrawElements instruction. I've been tracking down the problem but I can't find what's wrong. I'm even doubting about the right way to use QGLBuffer in Qt. Can anyone help me?

Was it helpful?

Solution

I solved it. The problem was the “tuplasize” that I was using with the setAttributeBuffer instruction.

It was

Objects[i].indexed_vertices.size() * sizeof(vertex)

Now I changed it to 3 and started passing normals as vertices too, the result code is:

void GLWidget::paintGL()
{
    glClearColor(0.4765625, 0.54296875, 0.6171875, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    /*
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -10.0f);
    */

    editor->setUniformValue("projectMatrix", controller->getProjectionMatrix());
    editor->setUniformValue("viewMatrix", controller->getViewMatrix());


    /** Ahora para las operaciones especificas de cada objeto **/

    for (int i=0; i<Objects.size(); i++)
    {
        Objects[i].modelmatrix.scale(1.0, 1.0, 1.0);
        Objects[i].modelmatrix.rotate(1.0, 0.0, 0.0, 1.0);
        editor->setUniformValue("modelMatrix", Objects.at(i).modelmatrix);

        glEnableClientState(GL_VERTEX_ARRAY);
        editor->enableAttributeArray("vertices");
        Objects[i].vertexbuffer->bind();
        //glVertexPointer(3, GL_FLOAT, 0, Objects.at(i).indexed_vertices.data());
        editor->setAttributeBuffer("vertices", GL_FLOAT, 0, 3); // (PROGRAM_VERTEX_ATTRIBUTE, Objects[i].vertices.data());
        //glDisableClientState(GL_VERTEX_ARRAY);

        //glEnableClientState(GL_NORMAL_ARRAY);
        editor->enableAttributeArray("normals");
        Objects[i].normalbuffer->bind();
        //glVertexPointer(3, GL_FLOAT, 3, Objects.at(i).indexed_normals.data());
        editor->setAttributeBuffer ("normals", GL_FLOAT, 0, 3);
        glDisableClientState(GL_VERTEX_ARRAY);

        //glDrawArrays(GL_QUADS, 0, Objects[i].vertices.size());
        Objects[i].elementbuffer->bind();
        glDrawElements(GL_QUADS, Objects[i].indices.size(), GL_UNSIGNED_SHORT, (void*)0);
    }
}

This can be marked as Solved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top