Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL: glDrawElements doesn't draw

Tags:

opengl

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...

like image 218
mayonnaise_clinic Avatar asked Jul 15 '12 03:07

mayonnaise_clinic


3 Answers

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

like image 58
wquist Avatar answered Nov 02 '22 22:11

wquist


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..

like image 32
iKlsR Avatar answered Nov 03 '22 00:11

iKlsR


I just faced the same problem.

Try GL_UNSIGNED_INT

glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_INT, indices);
like image 34
Shashwat Avatar answered Nov 02 '22 23:11

Shashwat