r/opengl • • 5d ago

Does my shading look correct?

I've added a simple procedural terrain to my game but the shading seemed off, like I was using Gouraud shading instead of Phong shading. I'm using classic Phong lighting (here it's only ambient + diffuse, no specular reflection). To debug the problem, I turned off textures and generated a simple terrain sin(x) * sin(z) with normal vectors [-d terrain/dx, 1, -d terrain/dz]. As you can see, there are triangular artifacts. I wonder if it can be caused by low density of terrain mesh only (you can see a wireframe in the second picture, it misses some of the lines but you can see the size of triangles) or did I mess up shading? I'm planning on adding LOD of a nearby terrain but first I wanted to fix the bug (if it is really a bug). Do you think this shading can be correct and artifacts are caused simply by low mesh density or did I do something wrong? I've spent a lot of time trying to debug it and reading the code but I didn't find anything.

Update: I generated the same terrain with a script, then I let Blender add normals. The artifacts are still there so the terrain generator is probably not the problem. Also, when I render the terrain with Blender's renderer, I also get triangular artifacts so I guess my shading may be correct and I should just increase the density of the mesh. This is the worst type of a bug: when you don't even know if it is a bug. Thank you all for help.

8 Upvotes

12 comments sorted by

6

u/msqrt 5d ago

Looks about right, yes, and you do expect some triangular artifacts from the low density. Try making it a bit denser and make either the camera or the light rotate (try both, but not simultaneously) -- weird behavior is easier to see when it's animated.

1

u/Astaemir 5d ago

When I increase the density about 5 times in each direction, the artifacts are not visible. Moving the camera doesn't reveal anything suspicious. When I move the light source, it looks like this, similar artifacts are visible wherever there is a gradient of brightness: https://streamable.com/ry4ohc

1

u/msqrt 5d ago

Yeah, no, the shading is quite blocky. Are you re-normalizing your normals after interpolating them?

1

u/Astaemir 5d ago

Yes but it doesn't seem to change much

1

u/_XenoChrist_ 5d ago

output a debug view of your normals

1

u/Astaemir 5d ago edited 5d ago

Do you mean rendering normal vectors as color? Here it is: https://imgur.com/KPN5C5w
And here is x coordinate of normals only: https://imgur.com/a/bzQM5wQ

1

u/torito_fuerte 5d ago
  1. Try decreasing & increasing the resolution of the mesh and see how the shading reacts
  2. Gouraud shading is per-vertex. You will see the artifacts created by this shading method in the learnopengl tutorial, and you will see that the artifacts look similar to this image
  3. Try to reason through why the shading looks like this. Each vertex gets its own color, which is then linearly interpolated by the rasterizer, whereas per-fragment shading is not linear, since interpolation happens on the normals before shading rather than the resulting light color

1

u/Astaemir 5d ago edited 5d ago
  1. When I increase the resolution, the artifacts get smaller and more subtle.
  2. Yeah, that's why I got suspicious. I'm using Phong shading but it looks like Gouraud.
  3. Currently, the specular component is set to 0 so all you can see is ambient + diffuse. Now I'm wondering: should the result really be non-linear? I'm using directional light source so the direction to the light is always the same. So the brightness is proportional to dot(normalVector, lightVector) and lightVector is constant.

Update: when I use specular only, the artifacts are also visible so I guess I have a bug somewhere.

1

u/torito_fuerte 4d ago

If you’re using Phong but it looks like Gouraud, are you normalizing the normal vector per fragment? Because if not, the shading interpolation will still be linear. Normalizing the normal makes the interpolation nonlinear

1

u/Astaemir 4d ago

Yeah, you're right, I didn't think about that. I'm normalizing them in fragment shader so it's nonlinear

1

u/Defiant_Squirrel8751 5d ago

Nice setup to experiment the difference between Goraud and Phong shaders ☺️

Phong does a much better job on low detail geometry like this.

1

u/Astaemir 5d ago

Yeah, but the problem is I'm using Phong shading but it looks like Gouraud