函数glMultiDrawElements无法正常工作

时间:2014-06-04 16:35:06

标签: opengl glsl

我想使用函数'glMultiDrawElements'渲染一个简单的立方体(即使只有一个对象!)但是显示效果不好!但是,如果我使用'glDrawElements'功能,一切正常!我想精确地'glewInit'已被正确初始化。

这是使用'glDrawElements'的代码:

[...]

std::vector<std::vector<int32_t>> indexArray = pRenderBatch->GetIndexAttribArray();
std::vector<int32_t> countArray = pRenderBatch->GetCountElementArray(indexArray);

// indexArray[0] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 0, 2, 19, 3, 5, 20, 21, 22, 10, 23, 11, 13, 24, 14, 25, 15, 17}
//Size(indexArray) = 1, Size(indexArray[0])=36
// countArray = {36} //Size(countArray)=1

glDrawElements(GL_TRIANGLES, countArray[0], GL_UNSIGNED_INT, (const GLvoid *)&indexArray[0][0]);

[...]

现在,这是使用'glMultiDrawElements'的代码:

[...]

std::vector<std::vector<int32_t>> indexArray = pRenderBatch->GetIndexAttribArray();
std::vector<int32_t> countArray = pRenderBatch->GetCountElementArray(indexArray);

glMultiDrawElements(GL_TRIANGLES, &countArray[0], GL_UNSIGNED_INT, (const GLvoid **)&indexArray[0], countArray.size());

[...]

这是显示:

enter image description here

但是根据OpenGL文档(http://www.opengl.org/sdk/docs/man3/xhtml/glMultiDrawElements.xml),函数'glMultiDrawElements'具有以下签名:

void glMultiDrawElements(GLenum mode, const GLsizei * count, GLenum type, const GLvoid ** indices, GLsizei primcount);

我不明白为什么显示不正确。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

std::vector<std::vector<int32_t>> indexArray

glMultiDrawElements()期待指向索引数组的指针。它对std::vector<int32_t>的布局一无所知。

请改为尝试:

std::vector< int32_t* > indexArray2

您可以像这样构建它:

std::vector< int32_t* > indexArray2( indexArray.size() );
for( size_t i = 0; i < indexArray.size(); ++i )
{
    indexArray2[i] = &indexArray[i][0];
}

编辑:完整示例:

#include <GL/glew.h>
#include <GL/glut.h>

#include <vector>
using namespace std;

void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -2, 2, -2, 2, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    GLfloat verts[] = 
    {
         0,  0,
         1,  0,
         1,  1,
         0,  0,
        -1,  0,
        -1, -1,
    };

    vector< GLsizei > counts;
    vector< vector< unsigned int > > indexes;
    {
        vector< unsigned int > index;
        index.push_back( 0 );
        index.push_back( 1 );
        index.push_back( 2 );
        counts.push_back( index.size() );
        indexes.push_back( index );
    }
    {
        vector< unsigned int > index;
        index.push_back( 3 );
        index.push_back( 4 );
        index.push_back( 5 );
        counts.push_back( index.size() );
        indexes.push_back( index );
    }

    vector< unsigned int* > indexes2( indexes.size() );
    for( size_t i = 0; i < indexes.size(); ++i )
    {
        indexes2[i] = &indexes[i][0];
    }

    glEnableClientState( GL_VERTEX_ARRAY );
    glVertexPointer( 2, GL_FLOAT, 0, verts );

    glMultiDrawElements
        (
        GL_TRIANGLES, 
        &counts[0], 
        GL_UNSIGNED_INT, 
        (const GLvoid **)&indexes2[0], 
        counts.size() 
        );

    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitWindowSize( 600, 600 );
    glutCreateWindow( "GLUT" );
    glewInit();
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}