2012-04-14 86 views
1

我目前正在研究一個項目,在該項目中,我在PC上的OpenGL窗口中編寫了控件和對象。我得到了OpenGL窗口來做我想做的事情,並從我的android設備獲取數據流到終端。不過,我需要將流式傳輸到終端的數據用於OpenGL對象。當我嘗試在同一個腳本中運行它們時,它會卡在'glutMainLoop'中,並且永遠無法到達與我的設備建立連接的點。我知道這是glutMainLoop的comman問題。我正在尋找任何建議。我是否以錯誤的方式去做?有更好的方法嗎?我已附上我的代碼如下:在glutMainLoop上執行剩餘的程序?

#include <stdlib.h> 
#include <stdio.h> 
#include <signal.h> 
#include <string.h> 
#include <strings.h> 
#include <vrpn_Shared.h> 
#include <vrpn_Analog.h> 
#include <vector> 

#include <imageviewer.h> 

using namespace std; 

int done = 0;   // Signals that the program should exit 
unsigned tracker_stride = 1; // Every nth report will be printed 


//------------------------------------- 
// This section contains the data structure that holds information on 
// the devices that are created. For each named device, a remote of each 
// type analog is created. 

class device_info { 
    public: 
    char   *name; 

    vrpn_Analog_Remote *ana; 

}; 
const unsigned MAX_DEVICES = 2; 


//------------------------------------- 
// This section contains the data structure that is used to determine how 
// often to print a report for each sensor of each tracker. Each element 
// contains a counter that is used by the callback routine to keep track 
// of how many it has skipped. There is an element for each possible sensor. 
// A new array of elements is created for each new tracker object, and a 
// pointer to it is passed as the userdata pointer to the callback handlers. 


class t_user_callback { 
    public: 
    char   t_name[vrpn_MAX_TEXT_LEN]; 
     vector<unsigned> t_counts ; 
}; 

//Callback handlers 

void VRPN_CALLBACK handle_analog (void *userdata, const vrpn_ANALOGCB a) 
{ 
    int i; 
    const char *name = (const char *)userdata; 

    printf("Input from %s:\n \n  %5.0f", name, a.channel[0]); 
    for (i = 1; i < a.num_channel; i++) { 
    printf(" %5.0f \n", a.channel[1]); 
    } 
    printf(" \n"); 
} 


int main (int argc, char * argv []) 
{ 

    int print_for_tracker = 1; // Print tracker reports? 
    int print_for_button = 1; // Print button reports? 
    int print_for_analog = 1; // Print analog reports? 
    int print_for_dial = 1; // Print dial reports? 
    int print_for_text = 1; // Print warning/error messages? 

    device_info device_list[MAX_DEVICES]; 
    unsigned num_devices = 0; 

    int i; 

    // Parse arguments, creating objects as we go. Arguments that 
    // change the way a device is treated affect all devices that 
    // follow on the command line. 
    for (i = 1; i < argc; i++) { 
    if (!strcmp(argv[i], "-notracker")) { 
     print_for_tracker = 0; 
    } else if (!strcmp(argv[i], "-nobutton")) { 
     print_for_button = 0; 
    } else if (!strcmp(argv[i], "-noanalog")) { 
     print_for_analog = 0; 
    } else if (!strcmp(argv[i], "-nodial")) { 
     print_for_dial = 0; 
    } else if (!strcmp(argv[i], "-notext")) { 
     print_for_text = 0; 
    } else if (!strcmp(argv[i], "-trackerstride")) { 
     if (tracker_stride <= 0) { 
     fprintf(stderr, "-trackerstride argument must be 1 or greater\n"); 
     return -1; 
     } 
    } else { // Create a device and connect to it. 
    device_info *dev; 

    // Make sure we have enough room for the new device 
    if (num_devices == MAX_DEVICES) { 
     fprintf(stderr,"Too many devices!\n"); 
     exit(-1); 
    } 

    // Name the device and open it as everything 
    dev = &device_list[num_devices]; 
    dev->name = argv[i]; 

    dev->ana = new vrpn_Analog_Remote(dev->name); 

    if ((dev->ana == NULL)){   
     fprintf(stderr,"Error opening %s\n", dev->name); 
     return -1; 
    } else { 
     printf("Opened %s as:", dev->name); 
    } 
    if (print_for_analog) { 
     printf(" Analog"); 
     dev->ana->register_change_handler(dev->name, handle_analog); 
    } 
    printf(".\n"); 
    num_devices++; 
    } 

    } 


// main interactive loop 

    printf("Press ^C to exit.\n"); 
    while (! done) { 
     unsigned i; 

     // Let all the devices do their things 
     for (i = 0; i < num_devices; i++) { 

     device_list[i].ana->mainloop(); 

     glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); 
    glutInitWindowSize(400,300); 
    glutInitWindowPosition(200,100); 
    glutCreateWindow("ImageViewer"); 


    init(); 
    glutDisplayFunc(display); 
    glutMotionFunc(drag); 
    glutMouseFunc(mouse); 
    glutMainLoop(); 


     } 
    } 
return 0; 
} 



// a.channel[0] = x 
// a.channel[1] = y 
// a.channel[2] = Zoom? 

回答

2

您可以停止使用GLUT。 GLFW可讓您更好地控制循環,以便進行其他處理。

如果您堅持使用GLUT,並且您使用的是FreeGLUT,那麼您可以使用glutMainLoopEvent。該函數處理主循環的一次迭代。所以你可以把它放在一個無限循環中並重復地調用它。作爲該循環的一部分,您可以執行其他操作,例如從終端或其他任何地方提取數據。

+0

謝謝你的回覆!與機器不幸的是,應用程序將用於我唯一的選擇是GLUT。我會嘗試你的解決方案並回復你。再次感謝! – user1194366 2012-04-14 17:17:45

+0

@ user1194366:FreeGLUT和GLFW不支持GLUT支持的平臺。你所要做的就是用你的.exe發運適當的DLL,或者靜態鏈接到它們。 – 2012-04-14 17:33:48

+0

是的,我明白這一點,但應用程序將運行在強力牆上,其中Glut命令由Chromium捕獲並呈現在許多高分辨率顯示器上。 Chromium只能使用標準庫。 – user1194366 2012-04-14 19:13:11

0

freeglut優惠glutMainLoopEvent,所以你可以選擇你的運行模式。

+0

感謝您的建議,但這並不適合我。我已經去了glutIdleFuc。完美的作品 – user1194366 2012-04-17 12:14:14