0

If we are going to generate fog using the vertex shader and the fragment shader, one of the common ways would be to compute the distance of a vertex in the vertex shader and apply the fog-mixed color in the fragment shader.

My question is, since we only computed the distance of the triangle vertices, how does OpenGL decide the colors of points inside the triangle? If it is automatically computed, is there a way to control the computation?

peter
  • 1,034
  • 1
  • 9
  • 23

1 Answers1

2

In OpenGL ES 2, the value of all attributes passed from the vertex shader to the fragment shader are linearly interpolated, perspective-correct, over the surface of the triangle being shaded.

In some other versions of OpenGL you can change this behavior.

So for example for fog, as long as your fog formula is a linear function over the world space, you can blend the interpolated fog value from the vertex shader into gl_FragColor in the fragment shader and it should look correct.

However if your fog is not linear, to get correct results you will need to calculate the fog value for each fragment individually in the fragment shader.

Michael Slade
  • 13,802
  • 2
  • 39
  • 44