SharpGL learning notes (3) Projection Transformation and viewpoint transformation, sharpgl learning notes

Source: Internet
Author: User

SharpGL learning notes (3) Projection Transformation and viewpoint transformation, sharpgl learning notes

 

Starting from this section, we use the VS2010 extension of SharpGL to directly generate the SharpGL project.

If you do not see the following SharpGL project when creating a project, find a directory named "SharpGL 2.0 Visual Studio Extension" in the source code of SharpGL and install the directory named SharpGL. vsix vs2010 extension, and then restart your vs2010.

 

Using the above SharpGL project, you can directly generate a complete SharpGL project with Opengl form controls. The basic code has been completed. You only need to modify the code of the 3D drawing part.

The generated project can also be directly run to see the effect, as shown in.

We can see that a three-cube has been drawn and is still rotating. The number of frames for rendering is also displayed dynamically below.

 

The triangle generated by the code we wrote in the first two articles is like 2D. Now, the effect is like this, and it is very stereoscopic. In fact, the two are different only because the project has implemented Projection Transformation and viewpoint transformation by default.

 

After a 3D body is placed in the world coordinate system, because the display can only display a 2D image with a 3D break, it is necessary to rely on projection to reduce the dimension of the 3D body.

The purpose of projection transformation is to define a visual object so that excess parts of the visual object are not displayed.

Projection includes perspective projection and orthographic projection.

It is the effect of Perspective Projection. It is in line with human observation experience, that is, the object closest to the viewpoint is large, the object farther away from the viewpoint is small, and disappears from the far to the Pole, and becomes the extinction point. It looks like a dot at the end of a straight road.

 

Let's take a look at the code on the projection part of the code and explain the parameter Meanings of the function.

 1   private void openGLControl_Resized(object sender, EventArgs e) 2         { 3             //  TODO: Set the projection matrix here. 4             OpenGL gl = openGLControl.OpenGL; 5             gl.MatrixMode(OpenGL.GL_PROJECTION); 6             gl.LoadIdentity(); 7  8             gl.Perspective(20.0f, (double)Width / (double)Height, 0.01, 100.0); 9             gl.LookAt(-5, 5, -5, 0, 0, 0, 0, 1, 0);10 11             gl.MatrixMode(OpenGL.GL_MODELVIEW);12         }

 

The function name and prototype for creating the perspective effect are:

Perspective (double fovy, double aspect, double zNerar, double zFar );

Aspect is the aspect ratio of the window

ZNerar and zFar are near and distant surface cutting positions, respectively.

Fovy is the angle that controls the field of view on the XY plane. The value range is 0--180 degrees. You can think of it as the field of view parameter corresponding to the camera lens in 3dsmax.

 

In the following Perspective function, the angle of view refers to the fovy parameter, aspect = w/h, zNerar is the near-plane distance, and zFar is the far-plane distance.

 

For fovy parameters that are hard to understand, if you are still not too clear, we will explain them in another way.

 

We use the camera parameters of 3dsmax as a metaphor (meaning similar). This is what we see with a 15mm lens (with a 100.389 degree view. A smaller camera has a larger field of view. In photography, a small lens is called a wide-angle lens.

 

Now we can use a 85mm lens (with a 23.913 degree view) to see only the part of the object. Its visible range is reduced.

The fovy parameter of the Perspective () function is equivalent to the field of view degree represented by the lens.

 


After the projection transformation is set, you need to set the viewpoint transformation.

Set the original form of the LookAt function of the viewpoint Conversion Function:

LookAt (double eyex, double eyey, double eyez, double centerx, double centery, double centerz, double upx, double upy, double upz );

This function is used for viewpoint conversion. You can think of it as a function for camera setting.

In opengl, by default, the viewpoint is located at the origin of the coordinate system, and the orientation is initially directed to the negative direction of the Z axis. As mentioned above, the default 3D model is placed on the depth of the Z axis 0 in the world coordinate system. Therefore, if you do not change the viewpoint, it is equivalent to sticking the camera to an object and seeing nothing, this is why the previous Code contains a code gl. translate () is used to move an object to a certain distance in the negative direction of the Z axis.

 

We still use the camera in 3dsmax as an analogy. In the following example, we use a camera called a "target camera". It is called a camera because it can be adjusted with a target point.

 

The first three parameters, eyex, eyey, and eyez, indicate the position of the camera itself in the world coordinate system.

In 3dsmax, the camera position can be freely translated.

We place the XY axis at the origin of the world coordinate system. The Z axis traveled far in the forward direction. Because the camera is far away from the object, you can see the object normally.

 

In, the XYZ of the camera is far away from the object transformation, which is equivalent to observing around the three-dimensional body.

 

When the three parameters are centerx, centery, and centerz, they are equivalent to adjusting the target point position of the 3dsmax target camera.

As shown in, changing the position of the target point has a similar effect around the observed object. In addition, the distance between the target point and the object on the Z axis is meaningless, and the main significance is the change of XY. You can test this on your own.

 

 

The last three parameters upx, upy, and upz are equivalent to rotating the 3dsmax target camera.

However, the value of this parameter is not an angle value, but a vector. The value size is meaningless and positive and negative values make sense.

 

In 3dsmax, the camera can rotate at any angle.

However, in the Lookat () function, the last three parameters, upx and upy, can only be changed in the upper, lower, left, and 45 degrees angles, and the upz value seems meaningless.

 

After explaining these two functions, we use a program to draw a triangle to test the effect of the above viewpoint transformation and projection transformation.

Is the effect of the field of view of 20, you can see that the triangle looks larger.

 

The field of view is changed to 60, and the triangle looks smaller.

The above results are consistent with the fovy parameter of the Perspective () function above.

 

Now we have changed the first three parameters of LookAt. The triangle is displayed to us.

 

Now we change the last three parameters of LookAt, And the Y vector is changed to-1, which is equivalent to 180 degrees of rotation of the camera, and the result is triangle down.

 

 

To save download space, I will delete all three dDLL files in SharpGL in the source code.

Copy the three dll files to the "Dependencies" and "bin \ debug \" directories. Open the project.

I will not go into details later.

 

Download the source code of this Article

 

Related Article

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.