i am making a program in opengl to animate a solid-sphere over a curve
it goes like
//display function
void display()
{
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//set of condition
loop:j=1 to 5
loop:i=1 to 3
if(j==1)
{
animate(x,y,z)
glutSwapBuffers();
}
elseif(j==5)
{
animate(x,y,z)
glutSwapBuffers();
}
else //for all value j between 1 and 5
{
animate(x,y,z);
glutSwapBuffers();
}
}
//animate function
void animate(float x, float y, float z)
{
glLoadIdentity();
glTranslatef(0,0,-20);
glPushMatrix();
glTranslatef (x, y, z);
glutSolidSphere (0.3, 10, 10);
int i, j;
for(i = 0; i < 10000; i++) //for introducing delay
for(j = 0; j < 5000; j++);
glPopMatrix();
glutSwapBuffers();
}
Problem:the solid sphere is translating over the curve but for its each next position i am not able to remove its previous position...for instance if the sphere goes from sequence of position like P1,P2,P3,P4 AND THEN P5..after coming to position P5 its still visible at all others previous position(P1,P2,P3,P4) but i want it to display sphere only at current position while translation How can i do this?
You are not clearing the frame buffer, which means that you are drawing each frame on top of the previous frame. Try using
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
with the color you desire.OpenGL is not a scene graph. All it does is colouring pixels. After you sent some geometry to OpenGL and it processed it, its gone and forgotten and all whats left are its traces in the framebuffer. Note that the contents of a Vertex Buffer Objects are not geometry per se. Only the drawing calls (glDrawElements, glDrawArrays) turn values in a vertex buffer into geometry.
Also your program doesn't follow the typical animation loop. The way you're doing it right now does not allow user interaction, or any other kind of event processing during the animation. You should change your code into this:
EDIT Full working code example