Draw a rotating cube with GDI

Source: Internet
Author: User

Address: http://simplesource.blog.163.com/blog/static/10341406200911269325863/

 

 

If you want to create a cube with a 3D perspective effect, you do not need to use a large 3D tool such as OpenGL or d3d, because the height matrix is not required because the masking effect of each face of the cube is relatively simple, you only need simple computing.

We can use the normal idea of the 3D rendering engine to determine the plane to be drawn. In the 3D world described by OpenGL and d3d, each surface has a normal. The normal determines the shadow coloring and whether to draw the surface. D3d allows you to set parameters to draw only the planes whose front faces the user. If you use this method to plot the plane of the cube, you can simply solve the hidden relationship between each plane. We only need to draw the plane whose zcoordinate of the normal is less than 0 to draw the rectangular box.

Taking the shadow plane as an example, its normal vector (f) is the vector 0-> 4 (a) and the vector 0-> 3 (B) the cross product (F = A × B) of, we do not pay attention to the X and Y coordinate values of this vector, just the zcoordinate value, because we only need to determine whether the normal is directed to the user. Formula for Calculating the zcoordinate of the normal:

FZ = AX * by-ay * BX

The method for implementing 3D pivoting is simpler. To put it bluntly, the 3D pivoting effect is far and near small. Why is this effect? This is because the light is transmitted in a straight line. If we use the user's eyes as the origin, the world we see is a cone,

What we need to calculate is the ing of the AB point of a three-dimensional object in the screen a 'B', which can be solved by the properties of similar triangles. The calculation formula is as follows:

A' x = AX * f/(F + az)

A' y = Ay * f/(F + az)

Program:

The first cube uses the triangle texture function mentioned in the previous blog. The second cube simply uses the polygon function of the GDI polygon rendering tool.

Download programs and source code

Rotatebox.rar

PS: triangle maps are directly used in 3D rendering, which may cause distortion. This is because a single Triangle Plane in the 3D world still has a 3D perspective effect, my Paster function does not consider this issue.

PPS: We are considering applying the triangle texture function to a previously written small program and trying to improve the program running speed.

 

 

 

 

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.