PDA

View Full Version : update the glut window (online)



bahare
07-23-2011, 05:45 AM
hi there,
I came across a problem, i have a real robot that is ordering my virtual robot in open gl, i want show every movement of my master robot(real robot) in slave (virtual one in open gl) online, so i need to update my glut window continuously, actually as long as real robot moves my virtual one moves too, and all these movement should be online!
i get data from master always with get data function, but i dont know how i should update the window!

here is my codes!

while(1)
{
getData();

printf("\n");
for(int i=0;i<6; i++)
{

printf("%d = %.3f\n", i,drecvbuf[i]);
}

counter=counter+0.1;

Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi,count er);
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA |GLUT_DEPTH);
glutInitWindowSize(900,500);
int u=glutCreateWindow("3DOF robot");
myinit();
createMenu();
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutKeyboardFunc(KeyDown);

System::Timers::Timer^ aTimer = gcnew System::Timers::Timer( 100 );

// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler( OnTimedEvent );

// Set the Interval to 2 seconds (2000 milliseconds).
aTimer->Enabled = true;
glutMainLoop();
return 0;
}


I 'm really sorry for just copy & paste my codes, i searched but i couldn't find how to do it.

any help would be appreciate so much!

mobeen
07-23-2011, 06:15 AM
Hook an idle callback like this,


glutIdleFunc(OnIdle);

and then simply read the data in it like this


void OnIdle(void) {
getData();
printf("\n");
for(int i=0;i<6; i++)
{
printf("%d = %.3f\n", i,drecvbuf[i]);
}
counter=counter+0.1;
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi,count er);
///...add whatever

//finally call redisplay to refresh screen
glutPostRedisplay();
}


See if this helps.

bahare
07-23-2011, 08:03 AM
deer mobeen;
i tried it, but it get errors, is there any related header too include?
error: 'OnIdle' : undeclared identifier
there is some other errors but i think that one is the original one!

_arts_
07-23-2011, 09:28 AM
http://www.learn-programming.za.net/

bahare
07-23-2011, 10:22 PM
you know the problem is that, I have the getdata in main, here is the main part:
int main(int argc, char **argv)
{
initSocket();

printf("\n Defining Step Time Parameters and Initial Conditions for solving Dynamic equations\n");

xi=0;
xf=0.1;
printf("\n end value x : %f ",xf);
i=0; yi[i]=0;
i++;yi[i]=-1.570796;
i++;yi[i]=-1.570796;
i++;yi[i]=0;
i++;yi[i]=0;
i++;yi[i]=0;
ndata=2; fi=1;

double counter=0.1;


Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);

void OnIdle(void){
for(int i=0;i<50;i++)
//while(1)
{

getData();

printf("\n");
for(int i=0;i<6; i++)
{

printf("%d = %.3f\n", i,drecvbuf[i]);
}
printf("\n");

yi[0]=v1[ndata];
yi[1]=v2[ndata];
yi[2]=v3[ndata];
yi[3]=v4[ndata];
yi[4]=v5[ndata];
yi[5]=v6[ndata];
printf("my nadata %f\n",v1[ndata]);
counter=counter+0.1;

Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);
glutPostRedisplay();
}
}
/////////////////////////////////////////////////////
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(900,500);
int u=glutCreateWindow("3DOF robot");
myinit();
createMenu();
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutKeyboardFunc(KeyDown);
glutIdleFunc (OnIdle);
glutMainLoop();

System::Timers::Timer^ aTimer = gcnew System::Timers::Timer( 100 );

// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler( OnTimedEvent );

// Set the Interval to 2 seconds (2000 milliseconds).
aTimer->Enabled = true;
return 0;

}
and its obvious , i cant define another function in main(void OnIdle(void)).
do you have any suggestion?
excuseme if i ask these kind of questions, Im new in opengl and c++.

mobeen
07-24-2011, 02:31 AM
Define the function outside main. Like this,


void OnIdle(void){
//put stuff here
}

int main(int argc, char **argv)
{
initSocket();
printf("\n Defining Step Time Parameters and Initial Conditions for solving Dynamic equations\n");
xi=0;
xf=0.1;
printf("\n end value x : %f ",xf);
i=0; yi[i]=0;
i++;yi[i]=-1.570796;
i++;yi[i]=-1.570796;
i++;yi[i]=0;
i++;yi[i]=0;
i++;yi[i]=0;
ndata=2; fi=1;

double counter=0.1;
Eqdifp(v1,v2,v3,v4,v5,v6,xi,xf,yi,ndata,p,fi);

/////////////////////////////////////////////////////
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(900,500);
int u=glutCreateWindow("3DOF robot");
myinit();
createMenu();
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutKeyboardFunc(KeyDown);
glutIdleFunc (OnIdle);
glutMainLoop();

//No need for these
/*
System::Timers::Timer^ aTimer = gcnew System::Timers::Timer( 100);
// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler( OnTimedEvent );

// Set the Interval to 2 seconds (2000 milliseconds).
aTimer->Enabled = true;
*/
return 0;
}

bahare
07-24-2011, 07:29 AM
tnx alot, but it didnt work again!
you know what? I think it's all because of infinite loop that glutmainloop makes,
its seems as soon as compiler reachs here stucks there and cant exit.
is there any way to exit this loop?

mobeen
07-24-2011, 08:26 AM
I think it's all because of infinite loop that glutmainloop makes

this is so that ur program runs until you exit it by pressing the close button or Alt-X key.
WHat exactly do u want to do. the idle function will continuously be called do ur processing in it like reading from the socket. What's the problem with that?

bahare
07-24-2011, 12:42 PM
youre right, let me put it this way.i have master robot that order to slave to exactly move like itself. whole these should be done simultaneously, i mean i should get data from master then refresh the glut window and again get data and so on! but fortunately it just draws the first point of master robot and then stops.why?
i taught if i use gluidlefunc it would do it which didnt, i got confused i dont know what i should do anymore!:((