如何持续更新 glut 窗口? [英] How to update glut window continuously?

查看:37
本文介绍了如何持续更新 glut 窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个真正的机器人,它在 open gl 中订购我的虚拟机器人.我想在线显示我的主机器人(真实机器人)在从属机器人(open gl 中的虚拟机器人)中的每一个动作,所以我需要不断更新我的 glut 窗口,实际上只要真实机器人也移动我的虚拟机器人,所有这些运动应该在线.

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.

我总是使用 get data 函数从 master 获取数据,但我不知道应该如何更新窗口.

I get data from master always with get data function, but I dont know how I should update the window.

这是我的代码:

********************************************强>/

********************************************/

  void OnIdle(void){  
    initSocket();

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

  xi=0;
  xf=0.1;
  printf("
    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);


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

      getData();

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

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

   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
",v1[ndata]);
    counter=counter+0.1;

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

    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;

  }

推荐答案

你可以在更新后调用 glutPostRedisplay,它会安排窗口在返回消息队列时立即重绘(当然使用 GLUT 的 display func), 我认为.

You can call glutPostRedisplay after the update, which schedules the window to be redrawn (using GLUT's display func, of course) as soon as it returns to the message queue, I think.

但是如果您在无限循环中不断轮询机器人数据,这将不起作用,因为这会不断阻塞程序.您应该做的是使用计时器在短时间内安排机器人更新,以便在这些更新之间程序可以返回主事件循环并重绘窗口.或者你可以调用一些函数,它告诉框架访问事件循环.您的代码示例目前并没有真正解释您的操作方式(或者我只是不熟悉您调用的函数).

But this won't work if you are continously polling the robot data in an infinite loop as this continously blocks the program. What you should do is use a timer to schedule the robot update in short intervals, so that between these updates the program can return to the main event loop and redraw the window. Or you can call some function, which tells the framework to visit the event loop. Your code sample doesn't really explain how you do it at the moment (or I'm just not familiar with the functions you call).

这篇关于如何持续更新 glut 窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆