OpenGL-ES: How to set an object at the coordinates

2019-08-06 19:25发布

问题:

I'm looking for a way to have an opengl object(triangles etc) at the location on the screen where you touch the screen. I now have an 3D pyramid on the screen, wich moves over the screen, based on the difference between the start of your touch, and where you dragged it to.
In normal surfaceViews I could just do something like element.x = event.getX(), but this will not work in openGL. So anyone has an idea?
ps: i'm using java/Android.

回答1:

OpenGL is nor like a surface view, where each element is persistent. It's the 3D equivalent to pens and brushes. You need to keep track of all data yourself.

So in your touch event handler you use the input coordinate to project a ray so some plane perpendicular to the view. You've to implement this projection yourself, but it's easy enough: Search for "ray plane intersection". This gives you the coordinates on the plane, that corresponds with that points projection on the screen where the user touched. You use this coordinates to transform the object and redraw the scene.