PDA

View Full Version : How to give time delay for animation ....



qawded
06-14-2013, 09:46 AM
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 .

Alfonse Reinheart
06-14-2013, 09:52 AM
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.

qawded
06-14-2013, 10:15 AM
actually i was talking about display function using glut.the callback function of Glutdisplayfunc . can i add a delay there?

GClements
06-14-2013, 11:24 AM
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.

qawded
06-16-2013, 10:15 AM
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?

GClements
06-16-2013, 04:44 PM
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).

qawded
06-18-2013, 08:26 AM
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.

GClements
06-18-2013, 10:26 AM
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).

sandbucket
06-30-2013, 07:53 AM
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:

#include <time.h>
void delay(float secs)
{
float end = clock()/CLOCKS_PER_SEC + secs;
while((clock()/CLOCKS_PER_SEC) < end);
}

sandbucket
06-30-2013, 08:01 AM
if you really want to delay just do this:

#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.

Carmine
07-01-2013, 10:38 AM
But if my display function is like this


display()
{
draw1();
draw2();
draw();
};

and i only want to delay drawing the draw2 function.

Try putting a Sleep function in draw2(). Like this --> Sleep (5000);
This will pause execution of draw2 for 5 seconds.
Sleep (100) would pause execution for 0.1 seconds.