Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Vertex Buffer Object -> Segmentation Fault
Forum Update on Monday, May 27th 2025

[Solved]Vertex Buffer Object -> Segmentation Fault

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BlastDV
    wrote on 4 Mar 2014, 02:39 last edited by
    #1

    Hi everyone, 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?

    (8) Just live your life blind like me (8)

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Nexelen
      wrote on 4 Mar 2014, 10:37 last edited by
      #2

      bq. Warning: Apart from the QGLWidget class, this module should not be used anymore for new code. Please use the corresponding OpenGL classes in Qt Gui.

      Use new classes for OpenGL application: QOpenGLBuffer, QOpenGLShaderProgram...

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BlastDV
        wrote on 4 Mar 2014, 14:33 last edited by
        #3

        Hi, thanks for your answer.

        I'm using QGLShaderProgram and QGLBuffer is called inside of QtOpenGL.

        Do you mean that Qt4.8's OpenGL classes are so deprecated that they don't even work?

        (8) Just live your life blind like me (8)

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BlastDV
          wrote on 4 Mar 2014, 15:09 last edited by
          #4

          Hi, 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);
          }
          

          }@

          I will mark this as SOLVED.

          (8) Just live your life blind like me (8)

          1 Reply Last reply
          0

          1/4

          4 Mar 2014, 02:39

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved