微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OpenGL 4.3 VAO不会在场景中绘制任何内容

如何解决OpenGL 4.3 VAO不会在场景中绘制任何内容

我正在QT上使用某些OpenGL 4.3应用程序,但是在使用VAO绘制场景时遇到了一些麻烦。

所以我的基本程序结构如下:

Create():
{
*Create VAO
*Bind VAO
*Gen buffer
*Bind buffer
*EnableAttribArray(0)
*AttribPointer(0,....)
*BufferData()
}

Draw():
{
*Bind VAO
*DrawArrays()
}

具体来说,代码部分位于此处:

//This function generates vertex information,all of them are vec3 stored in a vector,nothing wrong with this part.
void Surfaces::SetInfo(QString equa[5],float info[4])
{...}

//This function was called at the end of SetInfo(),after vertex info was generated.
int Surfaces::GenObject()
{
    qInfo("Start generating the obejct");

    //Generate the vertex array object and start store information
    glGenVertexArrays(1,&vertexArrayID);

    //Bind the VAO
    glBindVertexArray(vertexArrayID);

    qInfo("Create surface vertex buffer");

    //Generate the vertex buffer object for the surface mesh
    glGenBuffers(1,&surfaceBufferID);

    //Bind the current buffer
    glBindBuffer(GL_ARRAY_BUFFER,surfaceBufferID);

    //Enable the attribute array
    glEnabLevertexAttribArray(0);

    //Set the vertex pointer to the VAO
    glVertexAttribPointer(
       0,//Attribute 0. No particular reason for 0,but must match the layout in the shader.
       3,//Size
       GL_FLOAT,//Type
       GL_FALSE,//normalized?
       0,//Stride
       (void*)0            //Array buffer offset
    );

    //Load information into the buffer
    glBufferData(GL_ARRAY_BUFFER,indicesSurface.size() * sizeof(vec3),&indicesSurface[0],GL_STATIC_DRAW);

    /*
    qInfo("Create line vertex buffer");

    //Generate another buffer for the line mesh
    glGenBuffers(1,&lineBufferID);
    glBindBuffer(GL_ARRAY_BUFFER,lineBufferID);
    //Load information into the buffer
    glBufferData(GL_ARRAY_BUFFER,indicesLines.size() * sizeof(vec3),&indicesLines[0],GL_STATIC_DRAW);

    //Set the vertex pointer to the VAO
    glVertexAttribPointer(
       0,//Stride
       (void*)0            //Array buffer offset
    );
    glEnabLevertexAttribArray(0);
    */

    //Detach the VAO
    glBindVertexArray(0);

    //Create the shader program
    shaderProgID = loader.LoadShader("Simple.vert","Simple.frag");

    qInfo("Object created");

    return 0;
}

然后在绘制场景时,功能为:

void Surfaces::Draw(mat4 &Translation,mat4 &mvp)
{
    qInfo("Draw the surface.");

    //for(int i = 0; i < 50; i++)
        //qInfo("Surface vertices info: %f %f %f",indicesSurface[i].x,indicesSurface[i].y,indicesSurface[i].z);

    //Bind the vertex array
    glBindVertexArray(vertexArrayID);

    //Bind the shader
    gluseProgram(shaderProgID);

    //Set attribute of shader
    gluint MatrixID = glGetUniformlocation(shaderProgID,"MVP");  //Set the view matrix
    gluniformMatrix4fv(MatrixID,1,GL_FALSE,&mvp[0][0]);

    GLint uniTrans = glGetUniformlocation(shaderProgID,"trans");
    gluniformMatrix4fv(uniTrans,value_ptr(Translation));


    //glEnabLevertexAttribArray(0);
    //glBindBuffer(GL_ARRAY_BUFFER,surfaceBufferID);
    /*
    glVertexAttribPointer(
       0,//Stride
       (void*)0            //Array buffer offset
    );
    */



    //Draw the scene
    glDrawArrays(GL_TRIANGLES,indicesSurface.size()); //Starting from vertex 0
    //glDrawArrays(GL_TRIANGLES,indicesSurface.size());

    /* Clean up */
    //gluseProgram(0);

    //Detach the VAO
    //glBindVertexArray(0);

    qInfo("Finish drawing\n");
}

如果我这样绘制场景,屏幕上将不会显示任何内容。但是,如果我不推荐该部分:

    //glEnabLevertexAttribArray(0);
    //glBindBuffer(GL_ARRAY_BUFFER,//Stride
       (void*)0            //Array buffer offset
    );
    */

然后场景将按照我的意图绘制。

实际的OpenGL绘图函数在另一个类的函数中,但是由于它仅包含一些缓冲区清除内容和矩阵构造过程,因此我认为问题不在于那些部分。在这里,我认为问题在于,当我重新调用bind VAO命令时,状态没有正确地回调,例如attrib指针和缓冲区绑定(就像我在draw函数中直接调用它们那样,然后一切正常)。

那么我对VAO和VBO的顺序或用法有误吗?还是和QT OpenGL本身有关?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。