Global Display Lists?

In main() I want to draw some objects to a display list, then in display() (my DisplayFunc callback) I want to draw the things in the list.

I did this:



GLuint list = glGenLists(1);

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glCallList(list);
	glutSwapBuffers();
}

int main()
{
	glNewList(list,GL_COMPILE);//the EVERYTHING list

	glPushMatrix();
		glColor3f(1,0,0);
		glTranslatef(1 ,0, 0.0f);
		glutSolidSphere(1,20,20);
	glPopMatrix();

        glEndList();

        //all the openGL init stuff

        return 0;
}


The problem is, it doesn’t draw anything!! Clearly this is not the way to go about this? Can I pass the list to my display callback somehow?

Any tips would be appreciated!

Thanks,

Dave

As there is no opengl context when your application first initializes, all GL calls prior to OpenGl initialization are undefined. So initialize the GL context first.

Most likely, (list == 0) when glGenLists() is called without a valid context. This means you can check (list != 0) before you call it if you want to check its validity.

ahh sounded like a great idea, but no luck! Same problem!

Here’s the full code now that I changed the order of the init stuff relative to adding things to the display list:


#include "zpr.h"

#include <stdio.h>
#include <iostream>

using namespace std;

void display(void);

GLuint list = glGenLists(1); //need all lists global

int main(int argc, char *argv[])
{
	//Initialise GLUT and create a window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400,400);
	glutInitWindowPosition(100,0);
    glutCreateWindow("Test GLUT Application");

    //register GLUT callback functions
    glutDisplayFunc(display);
    glScalef(0.25,0.25,0.25);
	//glutKeyboardFunc(ProcessKeyboard);

	// Configure ZPR module
    zprInit();
    zprSelectionFunc(display); // Selection mode draw function

	// Initialise OpenGL
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);

	glNewList(list,GL_COMPILE);//the EVERYTHING list

		glPushMatrix();
			glColor3f(1,0,0);
			glTranslatef(1 ,0, 0.0f);
			glutSolidSphere(1,20,20);
		glPopMatrix();

		
		glPushMatrix();
			glColor3f(0,1,0);
			glTranslatef(0, 1, 0.0f);
			glutSolidSphere(1,20,20);
		glPopMatrix();

		glPushMatrix();
			glColor3f(0,0,1);
			glTranslatef(0 ,0, 1);
			glutSolidSphere(1,20,20);
		glPopMatrix();

	glEndList();

	glutMainLoop();

	return 0;
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	if(list==0)
		cout << "EMPTY!" << endl;

	glCallList(list);
	
	glutSwapBuffers();
}

Still the same problem.

Put
GLuint list;
as a global.

Then right before
glNewList
you can put
list = glGenLists(1);

Oh jeez, sorry I guess I wasn’t thinking!

All set now!