2012-04-19 130 views
-1

我得到了一個從我的電腦上運行的C++程序,它連接到我手機上的Android應用程序,並同時在我的電腦上的OpenGL中打開一個圖像。目前,openGL中的圖像被翻譯並使用鍵盤按鈕放大。 X和Y座標從我的android設備流式傳輸到終端。在glTranslate中使用x和y座標

有沒有辦法讓這些原始的x和y值,並使用它們在OpenGL中翻譯我的圖像?因此,用戶從Android設備控制PC上的圖像。我猜測這些值需要以某種方式進行縮放和歸一化?

我可以提供我的代碼或它的一部分,如果它使得它更清晰。

using namespace std; 
#define USE_TOMS_OGL 


class device_info { 
    public: 
    char   *name; 

    vrpn_Analog_Remote *ana; 


}; 
const unsigned MAX_DEVICES = 2; 

float translateX = 0.0; 
float translateY = 0.0; 
float translateZ = 1.0; 
float imgScale = 0.8; 
float panX = 0.125; 
float panY = 0.125; 
float zoomFactor = 0.01; 



using namespace std; 

struct Image {     // stores image data 
    unsigned long sizeX; 
    unsigned long sizeY; 
    char *data; 
}; 

void display(void) 
{ 


    // Translation and Scaling 
    glTranslatef(translateX, translateY, translateZ); 
    glScalef(imgScale, imgScale, 0.0); 

    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, texture); 


void keyboard(unsigned char key, int x, int y) 
{ 
    switch (key) { 
     case 27: 
     exit(0); 
     break; 
     case 'x': 
     imgScale += zoomFactor; 
     glutPostRedisplay(); 
     break; 
     case 'z': 
      imgScale -= zoomFactor; 
      glutPostRedisplay(); 
     break; 
     case 'w': 
     translateY += panY; 
     glutPostRedisplay(); 
     break; 
     case 's': 
     translateY -= panY; 
     glutPostRedisplay(); 
     break; 
     case 'a': 
     translateX -= panX; 
     glutPostRedisplay(); 
     break; 
     case 'd': 
     translateX += panX; 
     glutPostRedisplay(); 
     break; 
     default: 
     break; 
    } 
} 

void IdleFunc(void) { 
    if (accepted != 1) 
    { 

     unsigned i; 

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

     device_list[i].ana->mainloop(); 
     //cerr << i; 
     } 

    // printf("Idle function test "); 

    } 
    glutPostRedisplay(); 
} 


//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"); 
} 













// main interactive loop 

    printf("Press ^C to exit.\n"); 
    while (! done) { 
#ifdef USE_TOMS_OGL 
     glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); 
    glutInitWindowSize(400,300); 
    glutInitWindowPosition(200,100); 
    glutCreateWindow("ImageViewer"); 


    init(); 
    glutDisplayFunc(display); 

    glutKeyboardFunc(keyboard); 
    glutIdleFunc(IdleFunc); 
    glutMainLoop(); 
#else 
     unsigned i; 

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

     device_list[i].ana->mainloop(); 
     //cerr << i; 
     } 
     //cerr << endl; 
#endif 

    } 
return 0; 
} 


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

請告訴我們代碼 – 2012-04-19 09:35:01

+0

根據要求已添加代碼。 – 2012-04-19 10:14:16

+0

a.channel [0] = x a.channel [1] = y a.channel [2] =縮放 – 2012-04-19 10:14:47

回答

1

解決了它。我已添加此代碼..

// Translate image on screen using data from device  

    yVal = a.channel[1]; 
    yVal = yVal/10000; 
    translateY -= yVal; 

    xVal = a.channel[0]; 

if (xVal >0 & xVal<250){ 

    xVal = xVal - 300; 

}   

    xVal = xVal/15000; 
    translateX += xVal; 

xVal和Yval被分成比例縮放到屏幕即時通訊使用。

+0

如果你解決了它,那麼你應該接受這個答案,即使它是你自己的。 – slayton 2012-04-23 18:50:30