2014-03-04 83 views
-1

我一直在開發一個3D創作者,但由於我一直無法使用VBO繪製,所以我仍然陷入第一階段。頂點緩衝區對象 - >分段錯誤

這是一段我glwidget.cpp代碼,所有的對象都繪製

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

當它試圖執行glDrawElements指令它爆炸。我一直在追查這個問題,但我找不到有什麼問題。我甚至懷疑在Qt中使用QGLBuffer的正確方法。誰能幫我?

+0

當我創建對象時,它們被綁定,讓我發佈它... – BlastDV

回答

0

我解決了它。問題在於我用setAttributeBuffer指令使用的「tuplasize」。

這是

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

現在我把它改成3,並開始通過法線爲頂點過,結果代碼:

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

這可以爲解決被標記。