c++openglglut

OpenGL using glNewList and glDrawElements


I have my code compiling and working but I am not sure if it is working just by chance. The thing is, In my init function I enable the vertex array, then define a glNewList and at the end I disable the vertex array.

void init(){

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertices);

    doubleStar = glGenLists(1);
    glNewList(doubleStar, GL_COMPILE);
        glPushAttrib(GL_CURRENT_BIT);
        glPushMatrix();

        glColor3f(0, 0, 0.7);

        // outside
        glPushMatrix();
            glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_INT, indices);
            glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_INT, &indices[8]);
        glPopMatrix();

        // inside
        glPushMatrix();
            glRotatef(30, 0, 0, 1);
            glScalef(0.4, 0.4, 0.4);            
            glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_INT, indices);
            glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_INT, &indices[8]);
        glPopMatrix();

        glPopMatrix();
        glPopAttrib();
    glEndList();

    glDisableClientState(GL_VERTEX_ARRAY);

    glutDisplayFunc(display);

}

In my display function I call glCallList:

void display(){

    glClear(GL_COLOR_BUFFER_BIT);
    glClear(GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    // top left
    glPushMatrix();
        glTranslatef(-0.5, 0.5, 0);
        glRotatef(15, 0, 0, 1);
        glScalef(0.5, 0.5, 0.5);
        glCallList(doubleStar);
    glPopMatrix();

    // top right
    glPushMatrix();
        glTranslatef(0.5, 0.5, 0);
        glRotatef(-15, 0, 0, 1);
        glScalef(0.5, 0.5, 0.5);
        glCallList(doubleStar);
    glPopMatrix();

    // bot left
    glPushMatrix();
        glTranslatef(-0.5, -0.5, 0);
        glRotatef(-15, 0, 0, 1);
        glScalef(0.5, 0.5, 0.5);
        glCallList(doubleStar);
    glPopMatrix();

    // bot right    
    glPushMatrix();
        glTranslatef(0.5, -0.5, 0);
        glRotatef(15, 0, 0, 1);
        glScalef(0.5, 0.5, 0.5);
        glCallList(doubleStar);
    glPopMatrix();

    glFlush();
    glutSwapBuffers();

}

When display is executed the array is already closed! So I am not sure if that would work in any case. On the other hand, I have read that if you call glNewList with the GL_COMPILE flag the status at compile time is saved. So, may be since at compile time the vertex array was enabled then when calling the list would be enabled.
So, the question is: would this work in any case? Is it a good approach?

Here the full code:

#include <GL/glut.h>
#include <cmath>

void init();
void diplay();

static const GLfloat rInt = 0.7;
static const GLfloat rExt = 1;

static const GLuint indices[] = 
{

    4, 1, 3, 0, 5, 2, 4, 1,
    10, 7, 9, 6, 11, 8, 10, 7

};

static const GLfloat vertices[] =
{
    0, rInt, 0,
    -cos(M_PI_2/3)*rInt, -sin(M_PI_2/3)*rInt, 0,
    cos(M_PI_2/3)*rInt, -sin(M_PI_2/3)*rInt, 0,

    0, rExt, 0,
    -cos(M_PI_2/3)*rExt, -sin(M_PI_2/3)*rExt, 0,
    cos(M_PI_2/3)*rExt, -sin(M_PI_2/3)*rExt, 0,

    // 6

    0, -rInt, 0,
    -cos(M_PI_2/3)*rInt, sin(M_PI_2/3)*rInt, 0,
    cos(M_PI_2/3)*rInt, sin(M_PI_2/3)*rInt, 0,

    0, -rExt, 0,
    -cos(M_PI_2/3)*rExt, sin(M_PI_2/3)*rExt, 0,
    cos(M_PI_2/3)*rExt, sin(M_PI_2/3)*rExt, 0

};

static GLuint doubleStar;

int main(int argc, char** argv){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(64, 64);
    glutCreateWindow("Mosaico estrella");

    init();

    glutMainLoop();

    return 0;

}

void display(){

    glClear(GL_COLOR_BUFFER_BIT);
    glClear(GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    // top left
    glPushMatrix();
        glTranslatef(-0.5, 0.5, 0);
        glRotatef(15, 0, 0, 1);
        glScalef(0.5, 0.5, 0.5);
        glCallList(doubleStar);
    glPopMatrix();

    // top right
    glPushMatrix();
        glTranslatef(0.5, 0.5, 0);
        glRotatef(-15, 0, 0, 1);
        glScalef(0.5, 0.5, 0.5);
        glCallList(doubleStar);
    glPopMatrix();

    // bot left
    glPushMatrix();
        glTranslatef(-0.5, -0.5, 0);
        glRotatef(-15, 0, 0, 1);
        glScalef(0.5, 0.5, 0.5);
        glCallList(doubleStar);
    glPopMatrix();

    // bot right    
    glPushMatrix();
        glTranslatef(0.5, -0.5, 0);
        glRotatef(15, 0, 0, 1);
        glScalef(0.5, 0.5, 0.5);
        glCallList(doubleStar);
    glPopMatrix();

    glFlush();
    glutSwapBuffers();

}

void init(){

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertices);

    doubleStar = glGenLists(1);
    glNewList(doubleStar, GL_COMPILE);
        glPushAttrib(GL_CURRENT_BIT);
        glPushMatrix();

        glColor3f(0, 0, 0.7);

        // outside
        glPushMatrix();
            glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_INT, indices);
            glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_INT, &indices[8]);
        glPopMatrix();

        // inside
        glPushMatrix();
            glRotatef(30, 0, 0, 1);
            glScalef(0.4, 0.4, 0.4);            
            glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_INT, indices);
            glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_INT, &indices[8]);
        glPopMatrix();

        glPopMatrix();
        glPopAttrib();
    glEndList();

    glDisableClientState(GL_VERTEX_ARRAY);

    glutDisplayFunc(display);

}

Solution

  • So, the question is: would this work in any case?

    No.

    Is it a good approach?

    No!

    Display Lists only store the OpenGL commands and their parameters, but not the data of vertex arrays. In fact if you've read any decent tutorial on OpenGL display lists, it should have explicitly warned of not mixing vertex arrays and display lists, because this is impossible to get right.

    In fact you should not use display lists at all. Please forget whatever you know about them and ignore them. If you have to ask why? Because: Display Lists have been abandoned completely removed from modern OpenGL. Don't use them, they're a thing from the past. Their last useful application was sometimes 15 years or so ago.