Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How to give time delay for animation ....

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    23

    How to give time delay for animation ....

    Hello guys....
    it will be a great help if someone can tell me how to give a pause or delay in the display function. if possible provide a link for an example -like a simple example of a point moving in 3d space .

  2. #2
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    OpenGL doesn't have a "display function". Also, being able to do what you ask has nothing to do with animating a point moving in space.

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    23
    actually i was talking about display function using glut.the callback function of Glutdisplayfunc . can i add a delay there?

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    If you want to pause an animation, simply don't update it. With GLUT, you'd normally implement animation by registering a callback function with glutTimerFunc(). The callback updates the animation state then calls glutPostRedisplay(). To pause, you'd just have the callback return immediately without either updating the state or requesting redisplay.

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    23
    Quote Originally Posted by GClements View Post
    If you want to pause an animation, simply don't update it. With GLUT, you'd normally implement animation by registering a callback function with glutTimerFunc(). The callback updates the animation state then calls glutPostRedisplay(). To pause, you'd just have the callback return immediately without either updating the state or requesting redisplay.
    Thanx for the help.
    I have tried to build a very simple code in which a vertical line moves along the x axis.
    here is the code:


    #include<Myheader.txt>

    float i=0;
    void Draw() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINES);
    glVertex3f(10*i,1, 0.0);
    glVertex3f(10*i,50,0.0);
    glEnd();
    glutSwapBuffers();
    i=i+0.1;
    if(i>10)
    {i=1;}
    //glutPostRedisplay();
    }
    void makedelay(int)
    {
    Draw();
    glutTimerFunc(40,makedelay,1);
    }

    void Initialize() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);
    }

    int main(int iArgc, char** cppArgv) {
    glutInit(&iArgc, cppArgv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(50, 50);
    glutCreateWindow("XoaX.net");
    Initialize();
    //glutDisplayFunc(Draw);
    glutTimerFunc(40,makedelay,1);
    glutMainLoop();
    return 0;
    }


    note that i have not registered a callback for glutDisplayfunc is it ok?

  6. #6
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    Quote Originally Posted by qawded View Post
    note that i have not registered a callback for glutDisplayfunc is it ok?
    No.

    You should not use rendering functions outside of the display function. Other functions (e.g. timer and idle functions) should simply call glutPostRedisplay() if they want the display to be redrawn.

    You should always register a display function which redraws the window. This will be called whenever redraw is required, either because the application requested it with glutPostRedisplay() or because the OS requested it (e.g. because the window was obscured then exposed).

  7. #7
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    23
    Quote Originally Posted by GClements View Post
    No.

    You should not use rendering functions outside of the display function. Other functions (e.g. timer and idle functions) should simply call glutPostRedisplay() if they want the display to be redrawn.

    You should always register a display function which redraws the window. This will be called whenever redraw is required, either because the application requested it with glutPostRedisplay() or because the OS requested it (e.g. because the window was obscured then exposed).
    ok thanx..

    But if my display function is like this
    display()
    {

    draw1();
    draw2();
    draw();

    };

    and i only want to delay drawing the draw2 function.
    how to do that. Please help. I might me asking very silly questions. please ignore.

  8. #8
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    Quote Originally Posted by qawded View Post
    and i only want to delay drawing the draw2 function.
    When your display function is called, the contents of buffers are undefined, so you have to draw everything. You cannot rely upon the OS preserving the contents of the front buffer,, and the contents of the back buffer are undefined after calling glutSwapBuffers() (it may copy the back buffer to the front buffer, it may swap the front and back buffers, it may do something else entirely).

  9. #9
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by qawded View Post
    Hello guys....
    it will be a great help if someone can tell me how to give a pause or delay in the display function. if possible provide a link for an example -like a simple example of a point moving in 3d space .
    if you really want to delay just do this:
    Code :
    #include <time.h>
    void delay(float secs)
    {
    	float end = clock()/CLOCKS_PER_SEC + secs;
    	while((clock()/CLOCKS_PER_SEC) < end);
    }

  10. #10
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by sandbucket View Post
    if you really want to delay just do this:
    Code :
    #include <time.h>
    void delay(float secs)
    {
    	float end = clock()/CLOCKS_PER_SEC + secs;
    	while((clock()/CLOCKS_PER_SEC) < end);
    }
    actually this wont work. the variables are not accurate enough. it will work for large delays but thats all, not very useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •