"Reprint" screen coordinates converted to three-dimensional space coordinates

Source: Internet
Author: User

Original: http://www.cnblogs.com/graphics/archive/2009/11/28/1612832.html

If you are learning Arcball technology or if you are vague about the conversion of screen coordinates to three-dimensional coordinates, you must not miss this article. Screentovector function is a function of the Acrball class in Microsoft Dxut Framework, its function is to complete the conversion of two-dimensional screen coordinates to three-dimensional spherical coordinates, first look at the function definition

Code1 D3dxvector3 cd3darcball::screentovector (float fscreenptx, float fscreenpty)
2 {
3//scale-to-screen
4 FLOAT x =-(FSCREENPTX-M_OFFSET.X-M_NWIDTH/2)/(M_fradius * M_NWIDTH/2);
5 FLOAT y = (FSCREENPTY-M_OFFSET.Y-M_NHEIGHT/2)/(M_fradius * M_NHEIGHT/2);
6
7 FLOAT z = 0.0f;
8 FLOAT Mag = x * x + y * y;
9
if (Mag > 1.0f)
11 {
FLOAT scale = 1.0F/SQRTF (MAG);
x *= Scale;
Y *= scale;
15}
+ Else
+ z = sqrtf (1.0f-mag);
18
//Return Vector
return D3dxvector3 (x, y, z);
21}

The input to the function is screen coordinates, and the output is a three-dimensional vector-this vector is located on the Arcball sphere. Why do you want to analyze this function? The first is that it is very important, second it is more difficult to understand, its first line of code so many people just touch it do not understand why the value of x to reverse it? I have been thinking for a long time, and hard search for a long time, but always find a satisfactory answer, so I have to bite the bullet analysis. After several weeks of bitter struggle, finally a bit of a vision, below I will share their own views to everyone. I do not guarantee that my analysis is correct, only hope to give you a little inspiration, if you find something wrong, or you have a simpler way, please tell me, thank you first!

In order to make it easier for everyone to understand, we must first emphasize a few knowledge points

1. Windows screen/window coordinate features: the upper left corner of the window is the origin, the X value is incremented to the right, and the Y value is incremented downward

2. DirectX uses the left-hand line: X-axis pointing right, y-axis pointing up, z-axis vertical screen pointing inside

3. quaternion represents the rotation of the right-hand system-counterclockwise rotation around the rotating axis (looking at the rotation axis to the origin)

All right, in the last picture, compare the screen coordinates to the DirectX coordinates.

The following begins to analyze the code, the two lines of code, their role is to convert screen coordinates to a value in the [0-1] range, usually the window offset is set to 0, and the radius of the Arcball is set to 1, so the two lines of code can be converted to the following form

Code1 FLOAT x =-(FSCREENPTX-M_NWIDTH/2)/(M_NWIDTH/2);
2 FLOAT y = (FSCREENPTY-M_NHEIGHT/2)/(M_NHEIGHT/2);
3

The following code calculates the value of z, if x, Y constitute a two-dimensional vector of the modulo >1, then directly to the z=0, otherwise calculate the z-value, and the three-dimensional vector to maintain the modulus of 1, which is to facilitate the calculation of rotation angle and rotation axis in the back

Why does the value of x take the inverse?

First: By the z calculation process, Z's value is always >=0, and because DirectX uses the left-hand system, so, actually here is the inside of the screen of the hemisphere to rotate ! This requires that the direction of rotation of the arcball is opposite to that of the mouse, which in turn requires that the X, y value of the screen coordinates be the opposite of the X, Y value of the Arcball , because the screen coordinates have no z-values, so Z-values are not considered.

Because the x-axis of the screen coordinates is the same as the x-axis of the DirectX coordinate system, the X-values are reversed and, for the y-axis, they are the opposite, so no processing is necessary. This actually contains the following procedure

Note: The ball in this figure is the hemisphere inside the screen, because the picture is not good, the effect is not very obvious.

From the above figure, the X-value reversed, in fact, the upper-left area of the screen is mapped to the right lower hemisphere of the arcball, the upper-right area is mapped to the lower left hemisphere, the lower-left area is mapped to the upper-right hemisphere, the lower-right area is mapped to the upper-left hemisphere, haha, around confused, but The direction of the map is represented by a rectangle of the same color to a circle.

So let's see how the rotation is achieved, and then look at the figure

This figure indicates that when the user drags the mouse to the right in the upper left area of the window, the starting and ending points are P1 and P2 respectively, then two vectors will be generated in the lower right area of the Arcball, V1 and V2 respectively, while the Arcball rotation direction is from V1 to V2, and the rotation axis is determined by the V1 and V2 cross product.

Sometimes in order to pursue the simple, will often make the problem more complex, like this function, it may be that the brother of the function to write the value of x, so that we understand that it is so difficult, if he can write a negative sign, then the problem is much simpler, why so?

Because using the screen in the hemisphere causes the mouse to drag the direction and Arcball rotation direction opposite, if we use the outside of the screen side of the hemisphere is not easy ! yeah! You got it!

Keep the X value constant, the value of y,z is reversed, and the code becomes the following

Code1
2 FLOAT x = (FSCREENPTX-M_OFFSET.X-M_NWIDTH/2)/(M_fradius * M_NWIDTH/2);
3 FLOAT y = -(FSCREENPTY-M_OFFSET.Y-M_NHEIGHT/2)/(M_fradius * M_NHEIGHT/2  );
4
5 FLOAT z = 0.0f;
6 FLOAT Mag = x * x + y * y;
7
8 if (Mag > 1.0f)
9 {
Ten FLOAT scale = 1.0F/SQRTF (MAG);
x *= Scale;
Y *= scale;
13}
+ Else
z = -sqrtf (1.0f- mag);
16

This allows the x, Y axis of the window coordinates to coincide with the X, Y axis of the DirectX coordinate system. The z-value is always <=0 to ensure the use of the outer hemisphere, so that the mouse drag direction and arcball the direction of the rotation of the same! The above analysis is for DirectX, if you are using OpenGL, the opposite is true, OpenGL uses the right-hand system, so you can directly reverse the Y value. Do you get it?

Happy Coding!!!

= = the END = =

"Reprint" screen coordinates converted to three-dimensional space coordinates

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.