Computer Graphics | Interesting Testing and Merging – Fragment Operations

Computer Graphics | Interesting Testing and Merging – Fragment Operation

  • Computer Graphics | Interesting Testing and Merging – Fragment Operations
    • 10.1 Look at the fragment operation again
      • Fragment operations
      • several important buffers
    • 10.2 Who covered me?
      • The concept of blanking
      • face culling
      • depth test
        • Depth buffer algorithm (Z-buffer algorithm)
        • depth sorting method
      • Culling in OpenGL
        • face culling
        • depth test

Huazhong University of Science and Technology “Computer Graphics” course

MOOC Address: Computer Graphics (HUST)

Computer Graphics | Interesting Testing and Merging – Fragment Operations

10.1 Look at the fragment operation again

Fragment operations

Geometry stage:

Rasterization stage:

Several important buffers

  • Color Buffer (Color Buffer/Pixel Buffer): The color buffer stores the color value of each pixel.
  • Stencil Buffer: The stencil buffer stores a stencil, for example, it can be set that the pixel corresponding to 1 on the stencil will be displayed.
  • Depth Buffer: The depth buffer stores the depth information of each pixel, that is, the Z coordinate value. The depth value is normalized between 0.0 and 1.0.
  • Accumulation Buffer (AccumulationBuffer): Similar to the color buffer, it also stores the color value of the pixel.
    Purpose: Synthesize multiple images to achieve “multiple exposures” in the scene.
    Approach: Render the image multiple times, make small, incremental changes to the scene position (or selected objects), and accumulate the results.
    Effect: Improve the authenticity of the image, produce effects such as anti-aliasing and motion blur (synthesis of multiple images with small displacements).

What is color mixing?

When A (actually α coefficient, Alpha Coefficient) is not 1.0f, that is, when the color has a certain transparency, color blending (Color Blending) can be performed.

Application of color mixing:

  • transparent object
  • Handles certain special effects: such as motion blur
  • Handle some special effects: such as the blooming effect Bloom

GPU Double Buffering (Double Buffering):

10.2 Who covered me?

The concept of blanking

Concealment: Decide which surfaces of objects in the scene are visible and which are occluded from view.

Face culling

Definition of front and back:

If Vview?N>0, the polygon is the back face, and the back face is invisible.

When to do face culling?

Face culling is done before fragment shading.

Depth test

The depth buffer (Depth Buffer) stores the depth information of each fragment, that is, the Z coordinate value.

Deep buffer algorithm (Z-buffer algorithm)

Algorithm idea: For each pixel, find the fragment closest to the viewpoint.

In screen space: In fact, it is the fragment closest to the coordinate screen, that is, the fragment with the smallest z value. The color value of this fragment is the color value of this pixel.

Algorithm steps:

  1. Initialization: Initialize the depth buffer and all units (x, y) in the frame buffer:
    Each (x, y) unit in the depth buffer is set to the maximum value of z 1DepthBuffer (x, y) = 1
    Set the color value of each (x, y) unit in the frame buffer as the background color FrameBuffer (x, y) = BackgroundColor

  2. Process each polygon in the scene, one at a time:
    Calculate the depth value z of each point (x, y) on the polygon If z Get the color value surfColor (x, y) of the polygon surface;
    frameBuff(x,y) = surfColor(x,y)

depth sorting method

Depth sorting algorithm (depth sorting method), also known as painter’s algorithm (painter’s algorithm).

Idea: When a painter creates an oil painting, he always paints the background first, then the distant scene, then the nearer object, and finally the nearest scene.

data structure:

  1. Polygon Queue M: store all polygons
  2. Priority queue N: results obtained by depth sorting, store all polygons according to priority

Algorithm steps:

  1. Depth sorting: Sort the polygons according to the depth priority, and store the result in the queue N
    Those closer to the viewpoint have higher priority, and those farther from the viewpoint have lower priority.
  2. Scan conversion: take out polygons one by one from the queue N for drawing
    In fact, it starts from the polygon with low priority, and scans and converts the polygons one by one.

Deep sort

Step 1. Initialization
Store all polygons in the scene in a first-in-first-out queue in order of zmax from large to small, denoted as M;
At the same time, an empty first-in-first-out queue N is initialized.

Step 2. Only one polygon
If the number of polygons in M is 1, add the polygons in M directly to N, and delete A from M at the same time.

Step 3. There are multiple polygons
Take the polygon B from the current M, and distinguish between A and B:

Case 1: If zmax(B)

Case 2: Determine whether the bounding boxes projected by polygons A and B on the xoy plane overlap. If there is no overlap, the order of A and B in the queue does not matter. Add A to N according to the first-in-first-out principle.

Case 3: Add A to N according to the principle of first-in-first-out after judging that plane A is completely located on the overlapping plane of A and B on B.

Situation 4: Part of A is not behind this overlapping plane: judge whether the overlapping plane of planes A and B on B is completely in front of A. If so, add A to N according to the first-in-first-out principle.

When A and B are sorted, continue to process other polygons and take out polygon C from the current M: through discrimination, it is found that B covers part of C, so C has the lowest priority. The order of the last priority from low to high is C, B, and A.

Scan to Convert

Draw polygons from N in priority order.

Blanking in OpenGL

Face culling

Polygon culling can be enabled in OpenGL.

like:

glEnable(GL_CULL_FACE);
// mode can be GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
glCullFace(mode);

Depth test

Depth testing is disabled by default, so if we want to enable depth testing, we need the GL_DEPTH_TEST option to enable it.

like:

glEnable(GL_DEPTH_TEST);