Hi,
I am creating a procedural terrain and I have some issues with lighting as you can see there https://imgur.com/a/fadvHt9.
I have a grid with heights and a gradient function for each point based on interpolation function and smooth functions. I also can render normals and they seem to be good. The problem is when normals are interpolated for each triangle, it should be bilinear interpolation in my case. So the interpolated normal is not correct but not so far from the correct one if there are enough triangles (~16000 in this case).
Here is my code for fragment shader :
#version 430 core
out vec4 out_color;
in vec2 uv_frag; // the input variable from the vertex shader (same name and same type)
in vec3 frag_pos;
in vec3 frag_norm;
in vec3 frag_color;
uniform vec3 frag_eye;
void main()
{
vec3 normal = normalize(frag_norm);
vec3 light_color = vec3(1.0f, 1.0f, 0.0f);//vec3(0.1f, 0.35f, 0.1f);
vec3 light_pos = vec3(0.0f, 100.0f, 0.0f);
vec3 light_dir = normalize(light_pos - frag_pos);
vec3 view_dir = normalize(frag_eye - frag_pos);
vec3 reflect_dir = reflect(-light_dir, normal);
float light_strength = 100.0f;
float ambient_weight = 100.0f;
float diffuse_weight = 1.0f;
float specular_weight = 0.5f;
float ambient = ambient_weight/distance(light_pos, frag_pos);
float diffuse = diffuse_weight * max(dot(normal, light_dir), 0.0f);
float spec = specular_weight * pow(clamp(dot(view_dir, reflect_dir), 0.0, 1.0), 2);
float color_normal_ratio = atan(normal.y/sqrt(normal.x * normal.x + normal.z + normal.z));
color_normal_ratio = abs(color_normal_ratio);
color_normal_ratio /= 1.57079632679;
vec3 colorB = color_normal_ratio > 0.5 ? vec3(0.0,0.33,0.1) : vec3(0.36,0.30,0.16);
vec3 color = light_strength*((ambient+diffuse+spec)/(ambient_weight + diffuse_weight + specular_weight))*light_color*colorB;
out_color = vec4(color, 1.0);
}
The 3 lightings can be seen separately there https://imgur.com/a/JiDre2A
Both specular and diffuse participate to the problem.
My model matrix is always equal to Identity so there shouldn’t be a problem with the transpose of inverse upper 3x3 model.
Thank you !
PS : In the pictures, values are exaggerated compared to reality because of missing weights.