Another glDrawElements thread

Started by
1 comment, last by Dougie 19 years, 7 months ago
Ok, I'm trying to use glDrawElements to replace the calls in between the /* */ comment tags. However, my replacement code don't work; I end up with a blank screen, were there should be a multicoloured square. I'm using SDL to control the windows, etc.

static const Uint8      Indice[] = { 0, 1, 2, 3};
    static const GLfloat    Vertice[] = { -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 
                                             -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f };
    static const GLfloat    Colours[] = {1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 
                                            1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f };

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    PerformCameraTransform();
    
    /*
    glBegin(GL_TRIANGLE_STRIP);

    glColor3f(Colours[0], Colours[1], Colours[2]);
    glVertex3f(Vertice[0], Vertice[1], Vertice[2]);

    glColor3f(Colours[3], Colours[4], Colours[5]);
    glVertex3f(Vertice[3], Vertice[4], Vertice[5]);

    glColor3f(Colours[6], Colours[7], Colours[8]);
    glVertex3f(Vertice[6], Vertice[7], Vertice[8]);

    glColor3f(Colours[9], Colours[10], Colours[11]);
    glVertex3f(Vertice[9], Vertice[10], Vertice[11]);

    glEnd();

    glLoadIdentity();
    */
    
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_INDEX_ARRAY_POINTER);
    glIndexPointer(GL_FLOAT, 0, Colours);
    glVertexPointer(3, GL_FLOAT, 0, Vertice);
    glDrawElements(GL_TRIANGLE_STRIP, 2, GL_UNSIGNED_BYTE, Indice);

    glLoadIdentity();

    SDL_GL_SwapBuffers();

Btw, I checked the code between the tags and it works
Advertisement
The second parameter is the number of indices you want to draw (usually, the number of indices in the index array). You're only drawing 2 vertices, but a triangle needs atleast 3 to form a complete triangle. 4 is the correct value in this case.

And why are you using the index color array for the RGB color array? You should probably use glColorPointer instead.
Ahh, much better now. It now works correctly. The reason why I used glIndexPointer was because of the msdn library statement:

The glIndexPointer function defines an array of color indexes.

Which confused me. Anyway, thanks for your help

This topic is closed to new replies.

Advertisement