PDA

View Full Version : Call List Issues



Jack2606
04-15-2011, 09:26 AM
Hi there,

I've been trying out call lists in openGL, i created them fine and was able to display them fine at first, however, when i started to put them in Push/Pop matrixes I only seem to be able to display one call list (the stickList in the code shown below)? :S I'm assuming this is a rookie error but I cant seem to find what i'm doing wrong, posted the relevant code below:



//My displaylist method i'm using to generate, called in the init() function
void generateLists()
{
//START OF GATE LIST CREATE
gateList = glGenLists(1);
glNewList(gateList,GL_COMPILE);
glPushMatrix();
GLUquadric *quad;
quad = gluNewQuadric();

//Make gate straddle origin
glTranslatef(-0.5,0,0);

//Left Upright
glPushMatrix();
glRotatef(-90,1,0,0);
gluCylinder(quad, 0.1f, 0.1f, 1.0f, 18, 8);
glPopMatrix();

//Crossbeam
glPushMatrix();
glRotatef(90,0,1,0);
glTranslatef(0,0.925,0);
gluCylinder(quad, 0.1f, 0.1f, 1.0f, 18, 8);
glPopMatrix();

//Right Upright
glPushMatrix();
glRotatef(-90,1,0,0);
glTranslatef(1,0,0);
gluCylinder(quad, 0.1f, 0.1f, 1.0f, 18, 8);
glPopMatrix();

gluDeleteQuadric(quad);
glPopMatrix();
glEndList();
//END OF GATE LIST

//START OF START/END STICK LIST CREATE
stickList = glGenLists(1);
glNewList(stickList,GL_COMPILE);
glPushMatrix();
quad = gluNewQuadric();

//Make an upwards cylinder
glPushMatrix();
glRotatef(-90,1,0,0);
gluCylinder(quad, 0.1f, 0.1f, 1.0f, 18, 8);
glPopMatrix();

//Make a cone at the top
glPushMatrix();
glTranslatef(0,1,0);
glRotatef(-90,1,0,0);
gluCylinder(quad, 0.1f, 0, 0.2f, 18, 8);
glPopMatrix();

gluDeleteQuadric(quad);
glPopMatrix();
glEndList();
//END OF START/END STICK LIST

//START OF CROQUET BALL LIST CREATE
ballList = glGenLists(1);
glNewList(ballList,GL_COMPILE);
glPushMatrix();
//Translate ball up so the bottom is in contact with playing surface
glTranslatef(0,0.25,0);

//Generate sphere
glutSolidSphere(0.25f, 20, 20);
glPopMatrix();
glEndList();
//END OF CROQUET BALL LIST
}



//Included all my display method as its reasonably short, assuming the call lists is all that may be applicable, but just in case..!
void display(void)
{
//clear window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
//Movement
glRotatef(angle,0,1,0);
glTranslatef(xPos,-2,zPos);

//START OF PLAYING FIELD
glBegin(GL_QUADS);

glColor3f(0.0f,0.5f,0.0f); // Set The Color To Green
glVertex3f( 25.0f,0.0f, 50.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-25.0f,0.0f, 50.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-25.0f,0.0f,-50.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 25.0f,0.0f,-50.0f); // Bottom Right Of The Quad (Bottom)

glEnd();
//END OF PLAYING FIELD

//SET UP GATES AND POSTS
//Start Post
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(0,0,44);
glCallList(stickList);
glPopMatrix();
//GATE 1
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(0,0,38);
glCallList(gateList);
glPopMatrix();

glPopMatrix();

//flush buffers
glFlush();

//Swap buffers
glutSwapBuffers();

//Updating done here
if (buildPower)
{
distToTravel = distToTravel + 0.001;
}
}


If theres anything else you need to know just ask, and thanks in advance!

Jack

Kelvin
04-15-2011, 04:11 PM
I don't see anything wrong in the code you have posted. Since neither of us see it, perhaps the error is elsewhere.

Try calling glGetError() at the bottom of your drawing frame.

glPushMatrix and glPopMatrix operate on whatever matrix stack was last specified by matrix mode. Maybe you are getting a matrix stack overflow because the the pushes and pops are modifying the projection matrix stack (i.e. last call to glMatrixMode was glMatrixMode(GL_PROJECTION), which is only required to have a stack depth of two (2). Since the top entry of the stack is always used by the current matrix, you can only push the projection matrix stack one level deep without overflowing the stack.

The OpenGL design anticipates that you will be using the modelview matrix to scale/rotate/translate objects. The modelview matrix stack (selected with glMatrixMode(GL_MODELVIEW) is guaranteed to have have at least 32 entries.

Note that glRotate, glTranslate, etc also modify the top entry of the current matrix stack.

FYI, proper fixed-function lighting support requires using the projection matrix and modelview matrices "as intended". E.g., set the projection matrix with glOrtho/glFrustum call, then leave it alone, and doing object manipulation with the modelview matrix, because there are some computations done with only one of the matrices applied.

One other thing -- I suggest start off every frame with a known matrix value by calling glLoadIdentity(), at least for the modelview matrix. Yes, it is possible to avoid this, but it's nice to have a known starting place without needing to wonder if you matched all your pushes and pops (and didn't overflow the stack) while you are debugging.

Jack2606
04-15-2011, 05:34 PM
Thanks very much for the response, I will try all of your suggestions.

Yeah, i found it a bit strange as I don't think my code was very complex and i've never had a problem with pushing/popping before. Its just strange how it works perfectly fine without using those push/pops around the callList() functions (but obviously i then have to either call glLoadIdentity() or do my rendering relative to the previous transformations.. which isnt really doable for this project).

If anyone else can see anything i'm doing stupidly please feel free to point it out ;) and thanks again for your help Kelvin.

Jack

Jack2606
05-05-2011, 08:08 AM
I don't see anything wrong in the code you have posted. Since neither of us see it, perhaps the error is elsewhere.

Try calling glGetError() at the bottom of your drawing frame.

glPushMatrix and glPopMatrix operate on whatever matrix stack was last specified by matrix mode. Maybe you are getting a matrix stack overflow because the the pushes and pops are modifying the projection matrix stack (i.e. last call to glMatrixMode was glMatrixMode(GL_PROJECTION), which is only required to have a stack depth of two (2). Since the top entry of the stack is always used by the current matrix, you can only push the projection matrix stack one level deep without overflowing the stack.

The OpenGL design anticipates that you will be using the modelview matrix to scale/rotate/translate objects. The modelview matrix stack (selected with glMatrixMode(GL_MODELVIEW) is guaranteed to have have at least 32 entries.

Note that glRotate, glTranslate, etc also modify the top entry of the current matrix stack.

FYI, proper fixed-function lighting support requires using the projection matrix and modelview matrices "as intended". E.g., set the projection matrix with glOrtho/glFrustum call, then leave it alone, and doing object manipulation with the modelview matrix, because there are some computations done with only one of the matrices applied.

One other thing -- I suggest start off every frame with a known matrix value by calling glLoadIdentity(), at least for the modelview matrix. Yes, it is possible to avoid this, but it's nice to have a known starting place without needing to wonder if you matched all your pushes and pops (and didn't overflow the stack) while you are debugging.

Apologies for another late reply on this topic. Would just like to thank you for your help, I'm not sure exactly what the problem was as I'm only starting out really with OpenGL, but I set the matrixMode to GL_Modelview just before i generated the call lists and everything seems perfect.

Thanks again, appreciate it!