Hi there. I am writing a program that simulates a wireframe pool ball bouncing around a pool table. It works with one exception: whenever I make a call to glutPostRedisplay(), the entire scene vanishes. I can’t figure out why. It’s definitely not a problem with my double buffering, as the error still exists when I am using single-buffering. The problem still occurs when I switch my animation from being in a timer function to an idle function. My animation function only changes two variables, neither of which are used for any type of dimensions, window size, or anything else that could impact that. If I comment out the glutPostRedisplay() call, the image appears and stays, but is static.
Anyway, here is some code. The rest of what I have is standard stuff: initialization, displaymode, mouse callback, keyboard callback, resize, etc. Nothing that would have an impact on this (most likely). Variables that are not declared in this code are all in the header. It compile, so I’m sure that that’s not the problem.
For the sake of academic honesty, this is homework, so please give me hints and not the entire answer.
//updates the location of the ball
void updateball(int unused)
{
if((Xloc-RADIUS)<=-7 || (Xloc+RADIUS)>=7) Xspd=-Xspd; //collisions
if((Yloc-RADIUS)<=-13 || (Yloc+RADIUS)>=13) Yspd=-Yspd;
Xspd=Xspd*FRCTN_COEF; //reduce speed with friction
Yspd=Yspd*FRCTN_COEF;
Yloc+=Yspd;
Xloc+=Xspd;
glutTimerFunc(5, updateball, 1);
glutPostRedisplay();
}
//================================================================================================================
// Drawing routine.
void drawScene(void)
{
//set up parameters for drawing
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,0,15,0,0,0,0,1,0);
double theta=0.0; //angle parameter of ball
double currspeed=0.0; //speed of the ball in whatever direction it is traveling
double rtx=0.0; //x coordinate of vector around which ball will be rotated
double rty=0.0; //y coordinate of vector around which ball will be rotated
//================================================================================================================
//Begin drawing pool table
glColor3f(49.0/255.0,185.0/255.0,77.0/255.0); //pool table green- divide by 255 because color came in terms of 255
//Draw base of pool table from (-4, -2) to (4,2)
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-7.0, -13.0, 0.0);
glVertex3f(-7.0, 13.0, 0.0);
glVertex3f(7.0, -13.0, 0.0);
glVertex3f(7.0, 13.0, 0.0);
glEnd();
glColor3f(199.0/255.0,97.0/255.0,20.0/255.0); //wood-looking brown
//Pool table side 1
glPushMatrix();
glTranslatef(-7.0,0.0,0.0);
glScalef(1.0,27.0,0.5);
glutSolidCube(1.0);
glPopMatrix();
//Pool table side 2
glPushMatrix();
glTranslatef(7.0,0.0,0.0);
glScalef(1.0,27.0,0.5);
glutSolidCube(1.0);
glPopMatrix();
glColor3f(66.0/255.0,12.0/255.0,7.0/255.0); //cherry brown- so that it is easier to see three dimensions in the walls
//Pool table side 3
glPushMatrix();
glTranslatef(0.0,13,0.0);
glScalef(13.0,1.0,0.5);
glutSolidCube(1.0);
glPopMatrix();
//Pool table side 4
glPushMatrix();
glTranslatef(0.0,-13,0.0);
glScalef(13.0,1.0,0.5);
glutSolidCube(1.0);
glPopMatrix();
//End Drawing pool table
//================================================================================================================
//Draw ball based on parameters in timer function
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glTranslatef(Xloc,Yloc,0.0);
//rotate ball and display
currspeed=sqrt(Xspd*Xspd+Yspd*Yspd);
theta=360.0*currspeed/(2.0*(double)RADIUS*PI);
rtx=Yspd;
rty=-Xspd;
glRotated(theta, rtx, rty, 0.0);
glutWireSphere(RADIUS,15,15);
glPopMatrix();
//End drawing ball
glutSwapBuffers();
}