检测用户对 OpenGL 方块的触摸 [英] Detect user's touches over an OpenGL square

查看:15
本文介绍了检测用户对 OpenGL 方块的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
我如何确定什么是从屏幕触摸到 3D 空间?

我正在开发一个 Android 应用程序.我也在使用 OpenGL 图形.

I'm developing an Android application. I'm also working with OpenGL graphics.

我在屏幕上绘制了一个正方形,我想让用户在触摸它时移动它.换句话说,当用户将手指放在方块上时,他应该能够移动方块.如果他触及广场外,什么都不会发生.

I have a square drawn on the screen and I want to let user move it when he touches over it. In other words, user should be able to move square when he puts his finger on the square. If he touches outside the square nothing happens.

如何检测用户何时触摸正方形?

How can I detect when user touches over square?

谢谢.

推荐答案

其他答案提供的挑选教程在这里不适用;其中 glSelectBuffer 和 glRenderMode 不是 OpenGL ES 的一部分.

The picking and selection tutorials supplied in other answers aren't applicable here; amongst other glSelectBuffer and glRenderMode aren't part of OpenGL ES.

如其他地方所述,基本上有两种方法.您可以弄清楚触摸如何映射到您的场景中,并以数学方式测试您的对象.如果您的正方形是平的,就好像您在 2d 中绘制它一样,那么您可以很容易地做到这一点.

There are, as stated elsewhere, essentially two approaches. You can figure out how the touch maps into your scene and test your objects mathematically. If your square is flat on, as though you were drawing it in 2d, then you can achieve that quite easily.

或者,您可以执行一些基本上类似于拾取但适应 OpenGL ES 下可用功能的操作.例如,将您的场景清除为黑色,并将您的正方形绘制为纯蓝色.使用 glReadPixels 获取触摸位置的颜色值.如果它是蓝色的,那么触摸就在正方形上.这可能是一种更昂贵的测试方式,因为这意味着额外绘制场景,但这意味着您不必实现任何额外的数学运算或超出 GL 提供的其他代码.

Alternatively, you can do something essentially like picking but adapted for the functionality available under OpenGL ES. For example, clear your scene to black and draw your square in solid blue. Use glReadPixels to get the colour value at the touch location. If it's blue then the touch is on the square. This is probably a more expensive way to test because it means an additional draw of the scene, but it means that you don't have to implement any additional maths or other code beyond what GL supplies.

唯一需要记住的一点是,OpenGL 使用相同的方式来处理各处的像素——(0, 0) 是左下角,正 y 向上,正 x 向左.因此,无论您的触摸坐标相对于哪个轴,您都需要在执行 glReadPixels 之前将它们转换为相对于该坐标.

The only slight thing to keep in mind is that OpenGL uses the same way of addressing pixels everywhere — (0, 0) is the bottom left, positive y goes up and positive x goes left. So whatever axes you have your touch coordinates relative to, you need to transform them to be relative to that before doing the glReadPixels.

这篇关于检测用户对 OpenGL 方块的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆