glDrawElements索引缓冲区对象索引问题

时间:2013-11-10 17:31:53

标签: opengl vbo

我正在尝试绘制金字塔(工作正常)和Y轴线(不工作)。

这是相关代码:

class Example 
{

    bool init();
    void render();

    GLuint m_vboV;
    GLuint m_vboC;
    GLuint m_vboI;

    vector<GLfloat> m_vertex;
    vector<GLfloat> m_colour;
    vector<GLuint>  m_indice;

    };

#define BUFFER_OFFSET(i) ((char *) NULL + (i))

bool Example::init()
{    
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);

    m_vertex.push_back( 0.0F); m_vertex.push_back( 0.5F); m_vertex.push_back( 0.0F); // PYRAMID
    m_vertex.push_back(-0.5F); m_vertex.push_back( 0.0F); m_vertex.push_back(-0.5F);
    m_vertex.push_back(-0.5F); m_vertex.push_back( 0.0F); m_vertex.push_back( 0.5F);    
    m_vertex.push_back( 0.5F); m_vertex.push_back( 0.0F); m_vertex.push_back( 0.5F);
    m_vertex.push_back( 0.5F); m_vertex.push_back( 0.0F); m_vertex.push_back(-0.5F);        
    m_vertex.push_back( 0.0F); m_vertex.push_back(-1.0F); m_vertex.push_back( 0.0F); // LINE
    m_vertex.push_back( 0.0F); m_vertex.push_back( 1.0F); m_vertex.push_back( 0.0F);

    m_indice.push_back(0); // PYRAMID       
    m_indice.push_back(1);
    m_indice.push_back(2);      
    m_indice.push_back(3);
    m_indice.push_back(4);      
    m_indice.push_back(1);
    m_indice.push_back(5); // LINE
    m_indice.push_back(6);

    m_colour.push_back(0.0F);   m_colour.push_back(0.0F);   m_colour.push_back(0.0F); // PYRAMID    
    m_colour.push_back(0.0F);   m_colour.push_back(0.0F);   m_colour.push_back(1.0F);   
    m_colour.push_back(0.0F);   m_colour.push_back(1.0F);   m_colour.push_back(0.0F);   
    m_colour.push_back(0.0F);   m_colour.push_back(1.0F);   m_colour.push_back(1.0F);
    m_colour.push_back(1.0F);   m_colour.push_back(0.0F);   m_colour.push_back(0.0F);   
    m_colour.push_back(1.0F);   m_colour.push_back(0.0F);   m_colour.push_back(1.0F);   
    m_colour.push_back(1.0F);   m_colour.push_back(1.0F);   m_colour.push_back(0.0F); // LINE
    m_colour.push_back(1.0F);   m_colour.push_back(1.0F);   m_colour.push_back(1.0F);

    glGenBuffers(1, &m_vboC);
    glGenBuffers(1, &m_vboI);
    glGenBuffers(1, &m_vboV);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboV);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * m_vertex.size(), &m_vertex[0], GL_STATIC_DRAW);
    glVertexPointer(3, GL_FLOAT, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboC);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * m_colour.size(), &m_colour[0], GL_STATIC_DRAW);
    glColorPointer(3, GL_FLOAT, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vboI);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * m_indice.size(), &m_indice[0], GL_STATIC_DRAW);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    return true;
}

void Example::render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    gluLookAt(2, 5, -4, 0, 0, 0, 0, 1, 0);

    glDrawElements(GL_TRIANGLE_FAN, 8, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

    glTranslatef(1.5F, 0.0F, 0.0F);
    glDrawElements(GL_LINES, 2, GL_UNSIGNED_INT, BUFFER_OFFSET(6));
}

第一次调用glDrawElements似乎工作正常,即使值不同。金字塔绘制正确。

然而,第二个调用不会绘制最后两个顶点,而是绘制介于两者之间的某个顶点。如果我更改BUFFER_OFFSET,它将绘制下一个点,但在崩溃显示驱动程序之前永远不会得到最后两个。

最终结果应该是金字塔,其旁边有一条垂直线。

我花了大约2个多小时,尝试了许多不同的设置和值组合。

1 个答案:

答案 0 :(得分:3)

如何在代码中定义BUFFER_OFFSET。我怀疑它是一个字节偏移量,因此你应该使用BUFFER_OFFSET(6*sizeof(GLuint))来获得正确的偏移量。