0

I am struggling to understand as to why fundamentally triangles shaded through Gouraud shading and ones shaded through Phong shading look different.

From my understanding, Gouraud Shading, takes the vertices of a triangle. It calculates the color at each vertex with the help of vertex normals, and interpolates that color across the triangle per-pixel. Phong Shading calculates the normal at each vertex, and interpolates the normals across the triangle per-pixel and shades the said pixels using interpolated normals

Both involve interpolation. Why is it that interpolating normals produces a more realistic results, while interpolating the colors directly does not? What is it about the interpolation of colors that makes it less accurate?

Under the hood, isn't interpolating just a color similar to interpolating the normal and finding the color at every point? If not, intuitively how are they both different then?

Hash
  • 157
  • 5
  • Hint: Consider specular highlights. – Simon F Jul 21 '21 at 10:31
  • @SimonF Yes I did see several clips where the specular highlights in Gouraud appear to be...concentrated near the vertexes for a lack of better words. I am assuming this happens because closer to the vertexes the colors would obviously be closer to that of the vertex's colors

    But why doesn't a similar thing happen for Phong shading? Why won't the normals be closer to the vertex normals the closer we are to the vertex? Why wont a similar effect happen in Phong shading then?

    – Hash Jul 21 '21 at 10:36

2 Answers2

4

View angle changes all the time. In goraud shading the color sampling is done once per vertex.

But in phong shading the angle between view angle and normal is calculated for every pixel.

Why would that be different. Well simply because angle changes, and its cosine, is not a linearly changing quantity. (Ill draw a picture later when i get to a computer)

Imagine a large plane diffuse plane. Then the diffuse shading you see is based on the dot product of the normal and the normalized vector to light. Now imagine the light is in the middle of the plane. If you only have verices at the corners of the plane you get a uniform intensity. If you subdivide once you do not. If you subdivide by pixel you get phong shading and due to the dot prduct not being linear a different answer.

enter image description here

TL;DR

A noninear function does not approximate well with a linear function.

joojaa
  • 8,437
  • 1
  • 24
  • 47
  • If the large plane is lit uniformly as you said, such that every point on the plane is hit by the same 'intensity' of light, why would there be a specular highlight on a particular point on the plane? – Hash Jul 22 '21 at 18:18
  • @Hash not uniformly, symmetrically. In real life planes dont become uniformly lit just as long as corners are lit the same way. – joojaa Jul 22 '21 at 18:30
  • What do you mean exactly by 'symmetrically' in 'symmetrically lit' here? Sorry I am a bit confused – Hash Jul 22 '21 at 19:11
  • 1
    Any light source that lights the object so that the color at corners is the same. Like say a point light smack in the middle of a square, whike your camera is pointing down smack in the middle of the square. In real life you get this nice radial falloff. As you do with phong, with goraud you get a flat color. – joojaa Jul 22 '21 at 19:22
  • Could it be that @Hash is unaware of radiometric concepts? If this is the case please read Chapter 5 of PBRT as an introduction. Otherwise, I apologize.

    In the rendering equation, there is a cosine in the irradiance term. Hence, even if the incoming light radiance and the brdf are constant everywhere, the reflected radiance will be non-linear, which cannot be expressed as a linear combination such as the one carried out by Gouraud shading.

    This is exactly what is being depicted in this answer.

    – vgs Jul 23 '21 at 19:28
  • @vgs probably, hard to say. Some missing mathematical basis maybe. – joojaa Jul 23 '21 at 19:39
  • @vgs So since cosine which I assume is involved in lighting equations since we do a dot product of vertex normal and normal of the light ray is not linear, a linear interpolation of color would thus be inaccurate? – Hash Jul 24 '21 at 10:45
  • @Hash yes. I am showing just one source since one source us enough to explain difference. Also if the vertex bormal is turning along with camera angle, angle change is also not linear. As is the reflection direction. (Note wjen you have rotation of any kind you can bet the solution is nonlinear as rotation is sin and cos functions) But basically all of this is non linear. Note though nobody is really doing goraud shading anymore, except maybe for baking vertex coloring which results in pretty much the same issue. – joojaa Jul 24 '21 at 10:48
  • Also protip if you havent got conclusive proof to the contrary always assume things are nonlinear. Just because you have easier time to think linear does not mean thats a good assumption; Motivate linearity first. – joojaa Jul 24 '21 at 10:56
  • @joojaa Why would the normal of a vertex change if the camera angle changes? Isnt the vertex normal inherent to the vertex itself and where it is positioned in 3d space? Why does it change with a change in camera angle? – Hash Jul 24 '21 at 17:02
  • @Hash because you are looking at the normal from a camera. The perspective tranformation makes it so that: 1. Paralell lines are nolonger parallel 2. The distribution of linear data has changed. As i said stop thinking about how things are linear they are not. – joojaa Jul 24 '21 at 17:10
  • @joojaa By perspective transformation do you mean when all the meshes in the scene are perspective corrected and projected on the screen? – Hash Jul 26 '21 at 20:23
  • @Hash when your camera has perspective yes. – joojaa Jul 26 '21 at 20:30
1

RGB "Colors" contain chromaticity(color) and luminance(brightness), normal vectors contain direction and magnitude.

Gouraud shading approximates color gradients.

Phong shading approximates surface gradients.

Imagine a large triangle with a small specular highlight at the center and all 3 vertex positions have normal vectors pointing away from the highlight so are only dimly lit.

Interpolating the color will completely miss the highlight and only give values between those at the 3 vertex positions.

Interpolating the normal vectors will force a change of direction in the vector so that the highlight is visible.

Also, interpolation of normal vectors changes their length which can be recalculated in the fragment shader to help improve the approximation. But there is very little we can do to help correct interpolated colors.

Edit: Why interpolating between two vectors changes length. To interpolate vectors we use the formula $N_c = N_a\alpha+N_b(1-\alpha)$ where $N_a$ and $N_b$ are the two vectors being interpolated. This includes the sum of two vectors. Adding vectors can be thought of visually as placing them end to end, so the sum of two vectors results in a third vector with its own length resulting from the angle between the two vectors.

enter image description here

As simon f pointed out in comments my description of mach banding is poorly worded and misleading. See the comment below. Here is an image of the effect in action. enter image description here

pmw1234
  • 3,209
  • 1
  • 8
  • 16
  • Why would interpolation of normal vectors change their length? Aren't normal vectors by definition have a length of 1? What causes their length to change from 1 to something else when they are interpolated? – Hash Jul 22 '21 at 12:42
  • I added a brief explanation of vector sums and their length. – pmw1234 Jul 22 '21 at 13:40
  • @pmw1234 re "Additionally, interpolated colors tend have a nice perfect change from one color to another across the surface of the triangle, this causes an effect called "mach banding" where it looks like there are color bands when the transition is actually smooth." The Mach bands occur at the boundaries where triangles meet because the eye picks up the discontinuity in the rates of change from triangle to triangle – Simon F Jul 23 '21 at 12:06
  • 1
    I agree, I edited the answer and linked an example. – pmw1234 Jul 23 '21 at 12:24
  • @SimonF what is referred by rate of change of triangle to triangle? Is it how the lighting values calculated in gouraud shading is calculated per vertex hence moving from triangle to triangle we see "changes" and discontinuities in color? – Hash Jul 23 '21 at 19:47
  • @pmw1234 Also in the formula you provided for interpolation, what is the symbol alpha referring to? And am I right in assuming Nc is a vector in the "middle" of the line connecting vector Na and vector Nb? – Hash Jul 23 '21 at 19:48
  • @hash "what is referred by rate of change of triangle to triangle" I mean dRGB/dPixel. As you step across a triangle pixel by pixel in given direction (eg x), with Gouraud shading you have a fixed increment in colour values (ie constant dRGB/dPixel). In the neighbouring triangle, that rate will (probably) be different. Even though the RGBs themselves will be "continuous" as you go from triangle to triangle, the rates of change will not be. The human visual system is very good at identifying these 1st derivative discontinuities ( probably to help avoid being eaten by lions/wolves etc). – Simon F Jul 28 '21 at 09:39
  • @SimonF Im assuming the rate in a specific triangle is constant due to the fact that colors are linearly interpolated from the vertices right? But why is it that another triangle would have a different rate of change? Every triangle uses linear interpolation of colors from the vertices so why would the rate of color change be diffeerent? Is it because dRGB might be different because different triangles will have different colors calculated at the vertices? – Hash Jul 28 '21 at 19:43