# OpenGL # 3D point picking

Source: Internet
Author: User

A long time ago, it was still a freshman year. After learning OpenGL, I entered the graphics field. Now my work in the Chinese Emy of Sciences is also related to graphics, everything happens by accident and I like graphics too. I still know the basic knowledge about this path and I am familiar with OpenGL. However, in the future, my work will not be connected with graphics at all. I really wanted to give up on wasting time in this field, but I don't know why I can't bear it anymore. So I also secretly said, in any case, stick to learning graphics a little bit. As Steve Jobs said, it seems that this is not the case if you learn anything or change your identity. According to the project requirements, I studied how to pick up 3D points. In fact, I wanted to write a similar articleArticleNo, but I am too lazy and don't want to post a very watery article.

Kissing

In a game full of 3D objects, how to accurately pick up the objects you specify is very important to the game experience, so today we will introduce how to pick up 3D points in 3D scenarios. And attachedCode. You can also understand the three-dimensional point. If it is mapped to a two-dimensional screen, this question is especially suitable for beginners.

Foreplay

We know that the so-called 3D is actually displayed on a two-dimensional screen, but the rendering of the image is very realistic. How can we convert a three-dimensional point to a two-dimensional coordinate on the screen?
Here, the OpenGL flowchart is used as an example. Other graphics libraries, such as DirectX, are similar.
First, we can understand several concepts in several graphics. The so-called world coordinate system is the global coordinate system, and the point coordinate of an object is given based on this coordinate system. Point, object rotation, scaling, and translation are all carried out in this coordinate system. The observing coordinate system is also called the camera coordinate system. When we take a picture, we actually build a coordinate system based on the camera, then, the landscape is projected onto the film. Therefore, the coordinate system determines how the points and objects are mapped to the observation plane. We generally have two projection modes: forward projection and Perspective Projection. The observation plane can be imagined as the film of an old-fashioned camera or your screen.

With the previous knowledge, we can understand some advanced items in OpenGL. In OpenGL, OpenGL maintains two sets of matrices: Model View matrix and projection matrix. We can't see it. For example, gltranslate, glrotatef, glulookat, and glloadidentity are all operating these two sets of matrices. The glmatrixmode determines what matrices are in OpenGL's current state, the corresponding parameters include gl_modelview and gl_projection.

The model view matrix m0 is used to convert the P0 point to the observed coordinate system. These actions include rotation, scaling, and moving. The P0 coordinate after conversion is the coordinate P1 created from the camera as the origin.
P1 = M0 * P0

The role of the projection matrix M1 is to coordinate the P1 to the screen coordinate P2. Note that the screen coordinates here have been normalized, that is, the P2 coordinate range is already in [-1, 1. I don't know why it should be normalized, but it seems that a lot of mathematical knowledge should be normalized. we can imagine that the two numbers 0, 1 are magical. Haha, I am confused.
P2 = m1 * P1

Let's give a two-dimensional example.

P0 in the black coordinate system. If it is translated in the X and Y axes, it will reach P1. Remember that P1 is relative to the Black coordinate system. After it is converted to the red coordinate system, at this moment, P1 has coordinates in the red coordinate system. All the above actions are equivalent to the model view matrix, that is, P1 = M0 * P0.

Next, observe the coordinate p1 in the coordinate system and perform projection based on the projection (taking Perspective Projection as an example)

The observation plane in OpenGL is the near plane. Therefore, points within the field of view are projected onto the near plane. The field of view is between the near plane and the far plane, and objects in other spaces are cropped.

Climax

In computer systems, we do far more work than we have mentioned above, such as cropping, normalization, and grating. However, from the programming perspective alone, the above knowledge is sufficient.

View plaincopy to clipboardprint?
  1. VoidDisplay (Void)
  2. {
  3. M3dmatrix44f mat_proj, mat_modelview;
  4. IntWidth = fig, Height = fig (FIG );
  5. Glviewport (0, 0, (glsizei) width, (glsizei) height );
  6. Glgetintegerv (gl_viewport, viewport );
  7. Glclear (gl_color_buffer_bit );
  8. Glpushattrib (gl_polygon_bit );
  9. Glpolygonmode (gl_front_and_back, gl_line );
  10. Glmatrixmode (gl_projection );
  11. Glloadidentity ();
  12. Gluperspective (65.0, (glfloat) width/(glfloat) height, 1.0, 300 );
  13. // Obtain the Projection Matrix
  14. Glgetfloatv (gl_projection_matrix, mat_proj );
  15. Glmatrixmode (gl_modelview );
  16. Glloadidentity ();
  17. Glulookat (0, 0, 70, 0, 0, 0, 0, 1, 0 );
  18. Glcolor4f (0.1, 0.4, 0.6, 0.7 );
  19. Glpushmatrix ();
  20. // Obtain the Model View Matrix
  21. Glgetfloatv (gl_modelview_matrix, mat_modelview );
  22. /*** Draw the model you want. For details, refer to the code and click ***/
  23. Glpopmatrix ();
  24. Glpopattrib ();
  25. // Configure
  26. Selection. set_config (corners, 8, left_bottom, right_top, mat_modelview, mat_proj, viewport );
  27. /*************************************** *********************************/
  28. /* Construct a new environment */
  29. /*************************************** *********************************/
  30. If(Bool_select_area ){// If it is being picked up, it will be displayed
  31. Selection. draw_area ();
  32. Selection. highlight_selected_pts ();
  33. }
  34. }

Note that the above work is mainly to obtain the current OpenGL status, that is, to obtain the Model View matrix, projection matrix, and view parameters.
As we know, the normalized coordinate points are as follows:
P2 = m1 * M0 * P0, and then convert it to pixel coordinates by using the viewport parameter. when determining whether point P2 falls in the selected area, you can determine whether to select a three-dimensional point.
For specific code, see the m3dprojectxy function in math3d. cpp in the later connection.

Sleep

All the specific code is put on my GitHub. Click here to connect.
Https://github.com/baiyang/opengl

In the figure above,

 

 

----------------- Create high-quality articles. pay more attention to the wine ---------------

To create high-quality articles, pleaseRecommendationCome on .... Thank you. Please pay attention to my subsequent articles, which will be even better.

Sina Weibo: http://weibo.com/baiyang26

The wine ghost official blog: http://www.ibaiyang.org [recommended to use Google Reader subscription]

Pour wine into the official Douban: http://www.douban.com/people/baiyang26/

If you want to repost this blog,Please specify the source

If you have any comments or suggestions on this article,Leave a message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.