PDA

View Full Version : simple animation



02-19-2001, 07:40 PM
Hi,
I'm new to OpenGL. Eventually I would like to get a bouncing ball to animate, but to start off I'm just trying to get the sphere to translate right/left. It doesn't want to move. Can anyone take a look at my code to see if there's something I'm doing wrong?
Thanks...
#include <GL/glut.h>
static GLfloat xt=0.0;

void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidSphere(0.2,50,50);
glTranslatef(-xt,0,0);
glPopMatrix();
glFlush();
glutSwapBuffers();
}

void moveright(void)
{
xt = xt + 10.0;
if (xt > 500)
xt = xt-500;
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();

}

void mouse(int button, int state, int x, int y) {
exit(0);
}

void main(int argc, char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("GLUT ball");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutIdleFunc(moveright);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glRotatef(-140.0, 1.0, 1.0, 0.0);
glutMainLoop();
}

DLink
02-19-2001, 07:52 PM
Hi, unless I'm totally mistaken and just being an idiot (Which is quite possible) http://www.opengl.org/discussion_boards/ubb/smile.gif You need to call the glTranslatef(-xt,0,0); before you call glutSolidSphere(0.2,50,50);

I think that's the only problem, Good Luck!

03-01-2001, 08:42 PM
Yes DLink you are absolutely right.