开发者

identify wheteher the user touched sphere or not

开发者 https://www.devze.com 2023-02-12 07:05 出处:网络
I\'m developing an Android application. I\'m also working with OpenGL graphics. I have a sphere drawn on the screen and I开发者_运维技巧 want to let user move it when he touches over it.If he touches

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

I have a sphere drawn on the screen and I开发者_运维技巧 want to let user move it when he touches over it. If he touches outside the sphere nothing happens.

How can I detect when user touches over sphere?


The easiest way in this case is to use gluUnproject twice, once with Z set to near and once with Z set to far, that will give you a ray going "into" the screen. If the ray intersects the sphere (kind of trivial), then the user has clicked it. Otherwise, not.

Alternatively, you could read back the z buffer value at that pixel (this will be a pipeline stall, but you could use the previous frame, and unless the animation is totally wild nobody will probably notice). That will give you a single point in 3D with gluUnproject. Which is even more trivial to test, because if it is on the sphere, then sqrt(dx*dx+dy*dy+dz*dz) must be the radius. To account for rounding errors, you might want to compare <= 1.0001*radius or such.

And lastly, you could do an occlusion query with a 1x1 pixel viewport. This is not terribly useful with a sphere, since the collision checking is so much easier, but it may be something you want to do with a much more complex object that maybe has holes and where you need pixel precise detection.

0

精彩评论

暂无评论...
验证码 换一张
取 消