Michael Reeds
Michael Reeds

Reputation: 95

glDrawElements Index Buffer Object Indexing Problems

I am trying to draw a pyramid (which works fine), and a Y-Axis line (not working).

This is the relevant code:

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

The first call to glDrawElements seems to be working fine, even with varying values. The pyramid is drawn correctly.

The second call however, doesn't not draw the last two vertices, but instead somewhere inbetween. If I change the BUFFER_OFFSET, it will draw through the next points, but will never get the last two before crashing the display driver.

The final result should be a pyramid, with a vertical line off to the side of it.

I've spent probably more than 2 hours at this, trying many different combinations of settings and values.

Upvotes: 1

Views: 868

Answers (1)

derhass
derhass

Reputation: 45362

How is BUFFER_OFFSET defined in your code. My suspicion is that it is a byte offset, so you should use BUFFER_OFFSET(6*sizeof(GLuint)) to get the correct offset.

Upvotes: 3

Related Questions