ray picking for opengl es

       In OpenGL, there are encapsulated selection modes, name stacks, hit records, and pickup functions for picking, which are relatively easy. But it is unfortunate when it comes to opengl es, because opengl es is a simplified version of opengl, and many functions have been removed. No way, if you want to achieve the function of picking up, you have to do it yourself. At present, a relatively mature and relatively simple picking solution is: ray intersection picking.

      Let's first introduce the principle of ray intersection picking:

      (1): Convert the coordinates (X, Y) that the user clicks on the screen into normalized coordinates (Xn, Yn)

      (2): Considering that the viewing cone of the perspective projection has a near plane and a far plane, the normalized coordinates just now can be converted into two normalized coordinates, one with the z value on the near plane, and the z value on the near plane. is -1; the other has a z value of 1 on the far plane

      (3): Calculate the inverse matrix of the perspective projection, multiply the two normalized coordinates by this inverse matrix, and divide the values ​​obtained after multiplication by w respectively, that is, do perspective division, so that the virtual space is obtained. two points in .

      (4): Use the two points in the virtual space calculated in the previous step to generate a ray.

      (5): Use this ray to do intersection detection with other objects. In order to facilitate detection, the object to be detected can be regarded as a sphere, so that it is easier to detect whether the ray and the sphere intersect.

 

Originally, I wanted to explain all the steps in this blog in detail, but I found that my level was not enough when I really started. In this case, I recommend a good source to everyone. Chapter 9 of "Opengl ES Application Development Practice Guide Android Volume" is very exciting.

 

     

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325458881&siteId=291194637