Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D...

22
3D Graphics for Game Programming Chapter IV Fragment Processing and Output Merging

Transcript of Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D...

Page 1: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Chapter IVFragment Processing and Output Merging

Page 2: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Fragment Processing§ The per-fragment attributes may include a normal vector, a set of texture

coordinates, a set of color values, a depth, etc.

§ Using these data, the fragment processing stage determines the final color of each fragment.

§ Two most important things to do in the fragment processing stage § Per-fragment lighting (presented in Chapter 5)§ Texturing (presented in Chapters 8, 9, and 10).

§ Let’s see what texturing is.

4-2

Page 3: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Texture Coordinates

§ An image texture is a 2D array of texels (texture elements). Each texel has a unique address, i.e., 2D array index.

§ Let’s map the texture into a square in the parameter space and use normalized texture coordinates (u,v) instead of the texel address. Then, multiple images of different resolutions can be glued to a surface without changing the texture coordinates.

4-3

Page 4: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Surface Parameterization§ The texture coordinates are assigned to the vertices of the polygon mesh. This

process is called surface parameterization or simply parameterization. § In general, parameterization requires unfolding a 3D surface onto a 2D planar

domain.

4-4

Page 5: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Chart and Atlas§ The surface to be textured is often subdivided into a (hopefully small) number

of patches.§ Each patch is unfolded and parameterized. Then, the artist draws an image for

each patch. An image for a patch is often called a chart. § Multiple charts are usually packed and arranged in a texture, which is often

called an atlas.

4-5

Page 6: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Texture Coordinates to Texel Address§ Scan conversion is done with the texture coordinates.

§ D3D performs the following computation to convert the texture coordinates to the texel address.

4-6

5.0)(5.0)(

−×=

−×=

yy

xx

SvtSut

Page 7: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Texturing Examples

4-7

§ Observe that multiple images of different resolutions can be glued to a surface.

Page 8: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Texture Coordinates (revisited)

4-8

§ Direct3D and OpenGL adopt different parameter spaces for texture coordinates.

§ When a textured model is exported between Direct3D and OpenGL, the v-coordinates should be converted into (1-v).

Page 9: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Texture Addressing Mode

8-9

§ The texture coordinates (u,v) are not necessarily in the range of [0,1]. The texture addressing mode handles (u,v)s outside the range.§ Border: The out-of-range coordinates are rendered with a separately defined

border color.§ Wrap, repeat, or tile: The texture is tiled at every integer junction. § Mirror: The texture is mirrored or reflected at every integer junction. We have

smooth transition at the boundaries.

Page 10: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Texture Filtering – Magnification & Minification

8-10

§ Consider a quad. For each fragment located at (x,y) in the screen, its texture coordinates (u,v) are mapped to the texel address (tx,ty). We say that the fragment at (x,y) is projected onto (tx,ty).

§ Note that (tx,ty) are floating-point values in almost all cases.§ Consequently, texels around (tx,ty) are sampled. This sampling process is

called texture filtering.§ The screen-space quad may appear larger than the image texture, and

therefore the texture is magnified so as to fit to the quad. There are more pixels than texels.

Page 11: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Texture Filtering – Magnification & Minification (cont’d)

8-11

§ In contrast, the screen-space quad may appear smaller than the image texture, and the texture is minified. The pixels are sparsely projected onto the texture space.

§ Summary§ Magnification: more pixels than texels§ Minification : less pixels than texels

Page 12: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Filtering for Magnification

8-12

§ Option 1: Nearest point sampling§ A block of pixels can be mapped to a single texel.§ Consequently, adjacent pixel blocks can change abruptly from one texel to the

next texel, and a blocky image is often produced.

Page 13: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Filtering for Magnification (cont’d)

8-13

§ Option 2: Bilinear interpolation§ It is preferred to nearest point sampling not only because the final result suffers

much less from the blocky image problem but also because the graphics hardware is usually optimized for bilinear interpolation.

Page 14: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Filtering for Minification

8-14

§ Consider the following checker-board image texture.

§ If all pixels are surrounded by dark-gray texels, the textured primitive appears dark gray. If all pixels are surrounded by light-gray texels, the textured primitive appears light gray.

Page 15: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Z-buffering

4-15

§ The output of the fragment program is often called the RGBAZ fragment.§ A or alpha for representing the fragment's opacity§ Z or depth used for z-buffering

§ Using alpha and depth values, the fragment competes or is merged with the pixel of the color buffer.

§ The z-buffer has the same resolution as the color buffer, and records the z-values of the pixels currently stored in the color buffer. § When a fragment at (x,y) is passed from the fragment program, its z-value is

compared with the z-buffer value at (x,y). § If the fragment has a smaller z-value, its color and z-value are used to update

the color buffer and z-buffer at (x,y), respectively.§ Otherwise, the fragment is discarded.

Page 16: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Z-buffering (cont’d)

4-16

§ Assume MaxZ is 1.0, the red triangle’s depth is 0.8, and the blue triangle’s is 0.5.

Page 17: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Z-buffering (cont’d)

4-17

§ Rendering-order independence!!

Page 18: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Ed Catmull

4-18

§ Founder of Pixar and now president of Walt Disney and Pixar Animation Studios

§ Academy awards§ 1993 Academy Scientific and Technical Award "for the development of

PhotoRealistic RenderMan software which produces images used in motion pictures from 3D computer descriptions of shape and appearance"

§ 1996 Academy Scientific and Technical Award "for pioneering inventions in Digital Image Compositing"

§ 2001 Oscar "for significant advancements to the field of motion picture rendering as exemplified in Pixar's RenderMan"

§ 2008 Gordon E. Sawyer Award “for an individual in the motion picture industry whose technological contributions have brought credit to the industry“

§ Ed Catmull’s work§ texture mapping§ z-buffering § bicubic patches and subdivision surfaces

Page 19: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Alpha Blending

4-19

§ The alpha channel in the range of [0,1]§ 0 denotes “fully transparent.”§ 1 denotes “fully opaque.”

§ A typical blending equation is described as follows:

§ For alpha blending, the primitives cannot be rendered in an arbitrary order. They must be rendered after all opaque primitives, and in back-to-front order. Therefore, the partially transparent objects should be sorted.

Page 20: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Z-culling

4-20

§ The z-buffering test is done after the fragment processing stage. When the z-value of the fragment is greater than the buffered value, the fragment is determined to be occluded and thus discarded. It’s inefficient!!!

§ Let’s kill the fragments “before the fragment processing stage” if possible.§ A tile is composed of nxn pixels, and records the maximum among their z-values.§ Let’s compare it with the minimum z-coordinate of the three vertices of a triangle.

Page 21: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Z-culling (cont’d)

4-21

§ Z-culling is powerful, and is enabled by default.§ Consider the scene, where the first object occludes almost of triangles of the

other objects.

§ In a test, the average frame rate with the front-to-back ordered objects is 12.75 fps whereas that with the back-to-front ordered objects is 2.71.

§ This shows that the triangles may need to be sorted in front-to-back order in order to maximize the performance increase brought by z-culling.

Page 22: Chapter IV Fragment Processing and Output Merging · Fragment Processing and Output Merging. 3D Graphics for Game Programming Fragment Processing § The per-fragment attributes may

3D Graphics for Game Programming

Pre-Z Pass

4-22

§ Two-pass algorithm§ 1st pass: The scene is rendered with no shading, e.g., no lighting and no

texturing. Then, the color buffer is not filled, but the z-buffer is filled with the depths of the visible surfaces of the scene.

§ 2nd pass: The scene is rendered with full shading. Then, z-culling may cull out all fragments that are occluded by the visible surfaces. No occluded fragment enters the fragment processing stage.

§ The pre-Z pass algorithm even with back-to-front ordered objects outperforms the single-pass front-to-back rendering.