使用 x 和 y 坐标与 glTranslate

Using x and y coordinates with glTranslate

本文关键字:glTranslate 坐标 使用      更新时间:2023-10-16

我有一个从我的电脑上运行的 c++ 程序,它连接到我手机上的 Android 应用程序,同时在我的电脑上打开 OpenGL 中的图像。目前,openGL中的图像使用键盘按钮进行翻译和放大。 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

解决了。我添加了此代码..

// 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 被划分为缩放到屏幕 im 使用。