move a polygon

hello…pls help me in moving polygon along x axis slowly…

#include<stdio.h>
#include<GL/glut.h>
#include<windows.h>
void Init()
{
glClearColor(0,0,0,0);
gluOrtho2D(0,50,0,50);
}

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex2f(10,10);
glColor3f(0.0f,1.0f,0.0f);
glVertex2f(40,10);
glColor3f(0.0,0.0,1.0);
glVertex2f(25,30);

glEnd();

glFlush();
glutSwapBuffers();

}

int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow(" hai ");
glutDisplayFunc(display);
Init();
glutMainLoop();
}

First, use

 tag when post any code, it helps a lot.
All you need is play with [glTranslate](http://www.opengl.org/sdk/docs/man/xhtml/glTranslate.xml)

Keep the actual translation position into a variable and with that one as an argument to [b]glTranslate[/b] function.

i tried it dis way…bt its not working…
for(int i=0;i<30;i++)
{
glTranslatef(i,0,0);
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex2f(10,10);
glColor3f(0.0f,1.0f,0.0f);
glVertex2f(40,10);
glColor3f(0.0,0.0,1.0);
glVertex2f(25,30);

glEnd();
}

You’re assuming that the scene will be redrawn each time through your loop. That’s not how GL works. What would happen with this code is that the loop would run all the way through, then the scene would be drawn with i = 29. Your program has to be restructured a bit to do animation. See NeHe Tutorial #4 and read about GlutIdleFunc.

Again, use the code tag.

Remember, every call to display func will be rendered on screen. The for that you have will execute 30 times and only then the result will be available.

an example (using the code tag)


GLfloat tx = 0;

void display() {
        glTranslatef( tx, 0, 0 );
        
        [your draw code]

        dx += 0.01;
}

So, every time the display func is called, you draw your polygon and dx will be incremented.

Remember, glut keeps your program in a loop.

ok vl try…thanku:)