How do you select an individual triangle in OpenGL ES 1.1?
What I want to do is to select a point on the screen, cast a ray through that point straight down the z-axis and return the 开发者_C百科x y z coordinates and vertex normals of the first triangle that my ray intersects. Problem is, I'm not familiar enough with OpenGL to figure this out. I've done picking on windows OpenGL using different colors on the back buffer, but nothing that involved individual triangles.
I have done this on the iPhone, but as mentioned, I had to roll my own picking. Inevitably, it has a flavor unique to my particular application, but at a high level, this is how it goes.
You'll need a function to un-project your screen coordinate into world coordinates. On the desktop, there is gluUnProject(), but on the iPhone there is no such utility. Personally, I use an iPhone port of the very same function available from Mesa here. You may need to tweak it.
You can use the above function to get two points in world space, which represent where the infinite line behind your pixel intersects the near and far clipping plane. Using this vector, you'll have to iterate the triangles in your scene, taking into account any local transformations, and test for collisions using a ray-plane intersection algorithm, which could be a question in and of itself.
There are plenty of ways to intersect a ray with a triangle. I recommend a trip to Wikipedia. I will say that I use something like the following: First, simply intersect the ray with the plane containing the triangle. Then, test to see if the point of intersection is actually within the triangle in question. If it is, you're done. If not, keep going.
OpenGL ES 1.1 has no dedicated picking functionality. You have to construct your own system to do it.
Just thinking off the top of my head you need to start, by whatever method, of getting the point you want to use for the picking. If this point is in a different coordinate system you will need to convert it making sure to be aware of any current transformations you have on your scene. After this you can cast an array down the z axis and using any triangle to ray intersection algorithm to get the first triangle it goes through. You could keep track of your scene using a scene graph or something like that. Hope this helps.
精彩评论