0
votes

According to my understanding, the fragment shader takes the result from rasterization. However, according to Computer Graphics: Principles and Practice (3rd Edition), the rasterization will calculate the fragment color based on lighting in various directions:

for each pixel position (x, y):
closest[x, y] = ∞
for each triangle T:
   for each pixel position (x, y):
   let R be the ray through (x, y) from the eye
   let P be the intersection of R and T
   if P exists:
      sum = 0
      for each direction:
          sum += . . .
          if the distance to P is less than closest[x, y]:
             pixel[x, y] = sum
             closest[x, y] = |P − O|

so the fragment color pixel[x, y] is calculated base on lightings in the scene. So, after this, the fragment shader will apply colors, textures etc. to each fragement. How the colors in fragment shader or from the texture mix with the color calculated from rasterization?

2

2 Answers

1
votes

Fragment shader is your implementation of rasterization process, rasterization described is done by shader in fixed pipeline, flexible pipeline uses shader program you've supplied instead.

From opengl docs: A Fragment Shader is the Shader stage that will processes a Fragment generated by theRasterization into a set of colors and a single depth value.

The fragment shader is the OpenGL pipeline stage after a primitive is rasterized. For each sample of the pixels covered by a primitive, a "fragment" is generated. Each fragment has aWindow Space position, a few other values, and it contains all of the interpolated per-vertex output values from the last Vertex Processing stage.

The output of a fragment shader is a depth value, a possible stencil value (unmodified by the fragment shader), and zero or more color values to be potentially written to the buffers in the current framebuffers.

If fragment shader is not supplied color of pixels in fragment will stay unmodified

1
votes

Your understanding is correct, shading happens after rasterization.

It is the programmable step in-between rasterization and output merging (blending). Rasterization interpolates (non-flat) per-vertex attributes and generates one or more samples per-fragment using the values it computed.

The values computed during rasterization are passed onto the fragment shader stage, which decides what to do with them (e.g. sample a texture at a specific coordinate) to produce color and depth. After shading, fragments that pass a depth/stencil test are merged (blended) with whatever already exists in the framebuffer according to the setup blend equation to produce a pixel.