Advertisement

Nested display lists

Started by October 08, 2002 08:36 PM
5 comments, last by CaptainJester 22 years, 4 months ago
First, I am using the NeHe basecode from lesson 3 for this. I am creating a map board made up of hexes in OpenGL. To do this, I made a hex class that sets up the shape of a hex and a color and stores that in a display list. I then created a board class that would hold multiple instances of the hex class, one for each hex on the board. For the board I create a single display list that calls each hex display list within it. So whatever size I define the board as should draw that many hexes. However, it only draws a single hex. I call the routine that creates the board display list in InitGL(). When I move the call to DrawGLScene() after the glClear call, it works fine. Can anyone tell me why this is not working? If I have to move it into DrawGLScene(), it kinda defeats the purpose of display lists if I have to create them each frame. I checked all the display list variables for validity and they are all valid. Anyway, you can download all my files here http://members.rogers.com/mark.bernard/SquadLeader/. It''s only 7KB. Or you can look at the code snippets below. Hexagon.cpp
  
GLuint Hexagon::compile() {
    if(hex!=0) {
        glDeleteLists(hex,2);
    }
    hex=glGenLists(2);
    if(hex!=0) {
        glNewList(hex,GL_COMPILE);
            glColor3f(red,green,blue);
            glBegin(GL_TRIANGLE_FAN);
                glVertex3f(0.0f,0.0f,0.0f);
                glVertex3f(-0.25f,-0.433f,0.0f);
                glVertex3f( 0.25f,-0.433f,0.0f);
                glVertex3f( 0.25f,-0.433f,0.0f);
                glVertex3f( 0.5f , 0.0f  ,0.0f);
                glVertex3f( 0.5f , 0.0f  ,0.0f);
                glVertex3f( 0.25f, 0.433f,0.0f);
                glVertex3f( 0.25f, 0.433f,0.0f);
                glVertex3f(-0.25f, 0.433f,0.0f);
                glVertex3f(-0.25f, 0.433f,0.0f);
                glVertex3f(-0.5f , 0.0f  ,0.0f);
                glVertex3f(-0.5f , 0.0f  ,0.0f);
                glVertex3f(-0.25f,-0.433f,0.0f);
            glEnd();
        glEndList();
        glNewList(hex+1,GL_COMPILE);
            glBegin(GL_LINE_LOOP);
                glVertex3f(-0.25f,-0.433f,0.0f);
                glVertex3f( 0.25f,-0.433f,0.0f);
                glVertex3f( 0.5f , 0.0f  ,0.0f);
                glVertex3f( 0.25f, 0.433f,0.0f);
                glVertex3f(-0.25f, 0.433f,0.0f);
                glVertex3f(-0.5f , 0.0f  ,0.0f);
            glEnd();
        glEndList();
    }
    return hex;
}
  
Board.cpp
  
GLuint Board::compile() {
    int i,j;
    GLuint tempList;

    if(board!=0) {
        glDeleteLists(board,1);
    }
    board=glGenLists(1);
    if(board!=0) {
        glNewList(board,GL_COMPILE);
      	    glTranslatef(0.0f,-0.433f,0.0f);
            for(i=0;i<width;i++) {
            	if(i%2==1) {
            	    glTranslatef(0.0f,-0.433f,0.0f);
            	}
            	else {
            	    glTranslatef(0.0f,0.433f,0.0f);
            	}
                for(j=0;j<height;j++) {
                    tempList=hex[i][j].compile();
                    glCallList(tempList);
                   glTranslatef(0.0f,-0.866f,0.0f);
                }
               	glTranslatef(0.75f,(0.866f*(float)height),0.0f);
           	}
        glEndList();
    }
    return board;
}
  
InitGL() et al...
  
int InitGL(GLvoid) // All Setup For OpenGL Goes Here

{
	glShadeModel(GL_SMOOTH);					// Enable Smooth Shading

	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background

	glClearDepth(1.0f);						// Depth Buffer Setup

	glEnable(GL_DEPTH_TEST);					// Enables Depth Testing

	glDepthFunc(GL_LEQUAL);						// The Type Of Depth Testing To Do

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

	board.init(3,3);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer


	// ***  \/ \/ if I put it here it does not work  ***

	boardList=board.compile();

	return TRUE;							// Initialization Went OK

}

int DrawGLScene(GLvoid)	// Here''s Where We Do All The Drawing

{
	// ***  \/ \/ if I put it here it does not work  ***

	//boardList=board.compile();


	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer


	// ***  \/ \/ if I put it here it works  ***

	//boardList=board.compile();


	glLoadIdentity();						// Reset The Current Modelview Matrix

	glTranslatef(0.0f,0.0f,-5.0f);					// Move Left 1.5 Units And Into The Screen 6.0

	glCallList(boardList);
	return TRUE;							// Keep Going

}
  

Make it work. Make it fast. "I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Quick and dirty solution would be to skip your hexagon display lists. Just call draw inside your board::compile routine. You won''t lose any speed over it.

You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
Advertisement
The problem is that once I have this working, each hex will have its own texture and it would be easier to manage this way.



Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
I think this text from MSDN explains my problem:
quote:

The glCallList and glCallLists functions can be entered into display lists. The commands in the display list or lists executed by glCallList or glCallLists are not included in the display list being created, even if the list creation mode is GL_COMPILE_AND_EXECUTE.



So instead of nesting display lists, I will just call the drawing commands in the Hexagon class and create a single display list in the Board class.



Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
I am pretty sure I came across this before:
You can call display lists from within other lists, but you can''t CREATE a display list inside another list.

When you call the hex class'' compile routine from within the board list, you create a list inside a list. Maybe try creating all the hex lists before the board list, then call them inside the board list.
quote:
Original post by CaptainJester
The problem is that once I have this working, each hex will have its own texture and it would be easier to manage this way.



I realy don''t know what does that has to do with anything. Call hexagon.draw insted of hexagon.compile. Will there be any diffrence?



You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
Advertisement
quote:
Original post by _DarkWIng_
I realy don''t know what does that has to do with anything. Call hexagon.draw insted of hexagon.compile. Will there be any diffrence?



No. That is what I changed it to. I don''t know what I was thinking doing it the hard way.

Thanks.



Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement