我有一个从我的电脑上运行的c ++程序,它连接到我手机上的Android应用程序,同时在我的电脑上打开OpenGL中的图像。目前,openGL中的图像被翻译并使用键盘按钮放大。 X和y坐标从我的Android设备流式传输到终端。
有没有办法获取这些原始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 :(得分: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按比例划分为屏幕即时。