How to update glut window continuously?

前端 未结 3 2075
生来不讨喜
生来不讨喜 2020-12-17 15:13

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 ne

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 15:59

    GLUT offers you a idle callback (void (*)(void) signature), set through glutIdleFunc. Retrieve the robot input data in the idle handler. Or use a separate thread polling the data, filling data structures; use a semaphore to unlock idle after new data arrived, use a locking with timeout so that your program remains interactive. Pseudocode:

    Semaphore robot_data_semaphore;
    
    void wait_for_data(void)
    {
        SemaphoreLockStatus lock_status = 
            semaphore_raise_timeout(robot_data_semaphore, RobotDataTimeout);
        if( lock_status == SEMAPHORE_RAISED ) {
            update_scene_with_robot_data();
            semaphore_lower(robot_data_semaphore);
            glutPostRedisplay();
        }
    }
    
    void main(int argc, char *argv[])
    {
    /* ... */
        semaphore_init(robot_data_semaphore);
        Thread thread_robot_data_poller = thread_create(robot_data_poller);
        glutIdleFunc(wait_for_data);
    
    /* ... */
        thread_start(thread_robot_data_poller);
        glutMainLoop();
    }
    

提交回复
热议问题