5
votes

I'm trying out the Vertex Arrays stuff but for some reason the glDrawElements command doesn't draw anything for me. I can draw using glBegin/glEnd and glDrawElements in between, but glDrawElements doesn't work. Here's a code snippet:

These arrays get set up in the constructor:

double points[100];
GLint indices[50];

for (int i=0; i < 50; i++){
    points[2*i] = radius * cos(i*2*PI/50);
    points[2*i + 1] = radius * sin(i*2*PI/50);
    indices[i] = i;
}

Working code using only the points array with glArrayElement:

void GLCircle::draw()
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_DOUBLE, 0, points);

    glBegin(GL_POLYGON);
    for (int i=0; i < 50; i++){
        glArrayElement(i);
    }
    glEnd();
}

Also working code, using points array, specific indices accessed via indices array:

void GLCircle::draw()
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_DOUBLE, 0, points);

    glBegin(GL_POLYGON);
    for (int i=0; i < 50; i++){
        glArrayElement(indices[i]);
    }
    glEnd();
}

NON-working code, attempting to use glDrawElements:

void GLCircle::draw()
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_DOUBLE, 0, points);

    glDrawElements(GL_POLYGON, 4, GL_INT, indices);
}

Any advice? It's not entirely necessary for me to use it at this point, but it's disturbing that it doesn't work...

3

3 Answers

7
votes

The 2nd parameter of glDrawElements is count, so shouldn't 4 be the numbers of indices (50)?

5
votes

The parameters for

glDrawElements()

are as follows.. :

1st [mode] parameter is what kind of primitive to render.

2nd [count] parameter should be the number of elements to render. ie. the number of vertices

3rd [type] parameter should be the type of the value in the 4th parameter.. can ONLY be either

 GL_UNSIGNED_BYTE or GL_UNSIGNED_SHORT or GL_UNSIGNED_INT

4th [indices] parameter is a pointer to where the indices are stored.

You can read more on this here..

1
votes

I just faced the same problem.

Try GL_UNSIGNED_INT

glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_INT, indices);