Comparison between cube painter's blanking algorithm and Z-buffer Algorithms

Source: Internet
Author: User

Painter algorithm, also known as deep sorting.
Let's first look at its algorithm:
(1) set the screen to the background color,

(2) sort the objects to be drawn (polygon) from the distance from the viewpoint to the near Order (# Add) Are you sure you can sort them? Don't there be cross faces? Which point does this distance mean? It is learned from the following that the painter's algorithm is not applicable to crossover and can only be used to enclose the body. This distance is also the minimum distance ). This constitutes a deep priority table. Then draw an object (polygon) from far to near, and the near will overwrite the far polygon because of its high priority. This eliminates the need for hiding.

Z-Buffer Algorithm)
First, open a frame buffer area to record the brightness value of each pixel, and then open a Z buffer area to store the depth value of each pixel. For each pixel, there are two parameter values and two arrays need to be defined. Therefore, for each pixel, because of the Depth array, It is three-dimensional in the memory, so the entire image in the memory is three-dimensional.
Then, scan row by row to measure the depth of each row. In this way, the effect of hiding is achieved.
From this, we can see the differences between the painter algorithm and the Z-buffer algorithm 1.

It should be said that the most essential difference between them is that the painter algorithm sorts objects (polygon) in depth, which is easier to implement. The Z-buffer algorithm sorts images by each pixel, it is more flexible and simple than general sorting.

The cube painter's blanking algorithm is similar to the Z-buffer blanking Algorithm in other aspects. in <computer graphics practice tutorial VC ++ Kong lingde>, the cubes used by the two algorithms are orthogonal projection, instead of perspective projection, the original size of the displayed cube remains unchanged when the cube is far from the viewpoint. the two algorithms have different core ideas:

 

I. cube Z-buffer hiding is a pixel. You do not have to sort the planes that are close to or away from the viewpoint. You only need to traverse each plane and find the point with the largest depth of the pixel (Z is the largest, that is, the point closest to the viewpoint) (first, find the point with a large depth setpixel (). If there is a greater depth in the future, set a new color setpixel () to overwrite the previous point, note that when Yi is used as the scanning line, the scanning line may not be parallel to the screen, but may be skewed, just like rotating the X axis of OpenGL around the Y axis, such as vector (1, -). Z will change accordingly as X increases each time-A/C, where (a, B, c) is the normal vector of this surface ), it can be said that it makes a deep judgment on pixels one by one. the main code is as follows:

For (T1 = heade; T1! = NULL; T1 = T1-> next) // fill in the intersection interval between the scanning line and the Polygon

{

If (in = false)

{

XB = T1-> X;

Curdeep =-(XB * A + currentb-> scanline * B + d)/C; // Z =-(AX + by + d)/C

In = true; // each time a node is accessed, the in value is reversed once.

}

Else // If the in value is true, it is filled with the range from the X value of the current node to the end of the X value of the next node.

{

Xe = T1-> X;

For (Double X = XB; x <= Xe; X ++)

{

If (curdeep> = ZB [round (x) + 200] [currentb-> scanline + 200]) // If the depth of the new sampling point is greater than that of the original one, this function is called to draw each plane, so if there is a more in-depth point in the face, it will overwrite the color of the previous point. note that we need to traverse each integer point on [XB, Xe] and draw continuous pixels to form a line.

{

ZB [round (x) + 200] [currentb-> scanline + 200] = curdeep; // the XY coordinates are consistent with the array subscript, plus 200

MDC-> setpixel (round (x), currentb-> scanline, RGB [face]);

}

Curdeep + = deepstep;

}

In = false;

}

}

 

 

 

II. the cube painter's blanking algorithm is to find the mindeep value with the smallest Z value for each surface, and then sort the mindeep values of the six surfaces from small to large (that is, the surface painting near the viewpoint, to cover the surface far from the viewpoint), and then draw six faces from the distance to the nearest. however, this algorithm is still limited. It is only for the surrounding body (such as cubes, if the unclosed body (such as two intersecting and infinitely extending planes (like the plane xoy and plane yoz In the OpenGL Coordinate System) occurs, the problem may occur. the main code is as follows:

For (T1 = heade; T1! = NULL; T1 = T1-> next)

{

XB = T1-> X; curdeep =-(XB * A + currentb-> scanline * B + d)/C; // Z =-(AX + by-d) /C

If (curdeep <= f [face]. mindeep) // find the point with the smallest depth (that is, the smallest Z, the least away from the viewpoint) on the surface. Here, unlike the Z-buffer, we find two points XB, XE, and you need to traverse each X point, it only needs to find the minimum value, and this minimum value must be on the two endpoints of the online segment (either XB or Xe ), therefore, you can access one endpoint at a time to find the minimum value of Z, and it does not draw pixels for the moment, instead, sort the six faces in the order of first, last, and then draw the six faces.

{

F [face]. mindeep = curdeep; // record the same row

}

Curdeep + = deepstep;

}

 

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.