Hey guys,
I recently implemented a way of procedurally generating my Terrain by following this Tutorial: https://rendermeapangolin.wordpress.com/2015/05/26/screen-space-grid/
This works quite well. If you have a look a the screenshot at the very end of this Tutorial, you can see, that the shading is some sort of flat shading. I would like to achieve a smoother shading. I tried to compute per-vertex normals within the vertex shader by averaging the normals of all faces that contain the vertex. This creates a smoother shading, however either I still have mistakes in my computation or it doesn't work so well with the Screen Space grid, because The normals have no.. "smooth transition" as I move my camera, but rather change their values drastically.
Currently I do the following:
//----------------------------------------------------------------
// Sample the heightmap(2048^2)
float height[5];
height[0] = texture( myTextureSampler, finalPos.xz + vec2(-1.0/2048.0, 0.0)).r;
height[1] = texture( myTextureSampler, finalPos.xz + vec2(0.0, -1.0/2048.0)).r;
height[2] = texture( myTextureSampler, finalPos.xz + vec2(0.0, 0.0)).r;
height[3] = texture( myTextureSampler, finalPos.xz + vec2(1.0/2048.0, 0.0)).r;
height[4] = texture( myTextureSampler, finalPos.xz + vec2(0.0, 1.0/2048.0)).r;
vec3 n[4];
vec3 l0;
vec3 l1;
// Bottom left Triangle
l0 = vec3(finalPos.x - 1.0/2048.0, height[0], finalPos.z) - vec3(finalPos.x, height[2], finalPos.z);
l1 = vec3(finalPos.x, height[1], finalPos.z - 1.0/2048.0) - vec3(finalPos.x, height[2], finalPos.z);
n[0] = normalize(cross(l0,l1));
if(n[0].y <0)
n[0] = -n[0];
// Upper right Triangle
l0 = vec3(finalPos.x + 1.0/2048.0, height[3], finalPos.z) - vec3(finalPos.x, height[2], finalPos.z);
l1 = vec3(finalPos.x, height[4], finalPos.z + 1.0/2048.0) - vec3(finalPos.x, height[2], finalPos.z);
n[1] = normalize(cross(l0,l1));
if(n[1].y <0)
n[1] = -n[1];
// Upper left Triangle
l0 = vec3(finalPos.x - 1.0/2048.0, height[0], finalPos.z) - vec3(finalPos.x, height[2], finalPos.z);
l1 = vec3(finalPos.x, height[4], finalPos.z + 1.0/2048.0) - vec3(finalPos.x, height[2], finalPos.z);
n[2] = normalize(cross(l0,l1));
if(n[2].y <0)
n[2] = -n[2];
// Bottom right Triangle
l0 = vec3(finalPos.x + 1.0/2048.0, height[3], finalPos.z) - vec3(finalPos.x, height[2], finalPos.z);
l1 = vec3(finalPos.x, height[1], finalPos.z - 1.0/2048.0) - vec3(finalPos.x, height[2], finalPos.z);
n[3] = normalize(cross(l0,l1));
if(n[3].y <0)
n[3] = -n[3];
// Average normal
oNormal = normalize((n[0] + n[1] + n[2] + n[3])/4);
//---------------------------------------------------------------------------------
The shader is certainly smooth now, but the normals have no smooth transition to one another. I suppose, that happens, because the vertices are more dense near the camera than further away and have not that uniform 1.0/2048.0 distance? Any Ideas, how I can realize a completely smooth shading, while procedurally generating Terrain as explained within the Tutorial?
Thank you very much!
Xardes