Heightfield light shows triangle edges

  Kiến thức lập trình

I’m rendering an heightfield, single directional light, but the triangle edges stand out, even though the normals seem ok. I guess it’s a problem with the shader, then, but I can’t seem to find it.

This is the snippet of the fragment shader to do lighting calculations:

#version 460
out vec4 FragOutput;
uniform vec3 u_ViewPosition;

in vec3 v_VertexNormal;
in vec3 v_FragPosition;

struct AmbientLightData
    {
    vec3 Color;
    float Intensity;
    };
struct DirectionalLightData
    {
    vec4 Direction;
    vec4 Diffuse;
    vec4 Specular;
    float Intensity;
    };
layout ( std140 ) uniform u_LightData
    {
    AmbientLightData AmbientLight;
    DirectionalLightData DirectionalLights[8];
    int DirectionalLightCount;
    } LightData;
    
vec3 CalculateDirectionalLightContribution ( int LightIndex, vec3 ViewPosToFrag, vec3 Normal, vec3 MaterialDiffuse, vec3 MaterialSpecular, float MaterialShininess )
    {
    // diffuse
    vec3 NormalizedLightDir = normalize ( -LightData.DirectionalLights[LightIndex].Direction.xyz );
    float DiffuseComponent = max ( dot ( Normal, NormalizedLightDir ), 0.0 ); // Calculate dot product on the angle between the two vectors, and make it positive

    // add it all together
    vec3 DiffuseContribution = DiffuseComponent * LightData.DirectionalLights[LightIndex].Diffuse.xyz * MaterialDiffuse * LightData.DirectionalLights[LightIndex].Intensity;
    return DiffuseContribution;
    }

vec4 ApplyLighting( vec4 MaterialDiffuse, vec4 MaterialSpecular, float MaterialShininess, vec3 Normal )
    {
    vec3 Output = vec3 ( 0.0f, 0.0f, 0.0f );
    vec3 TotalDirectionalLightContribution = vec3 ( 0.0f, 0.0f, 0.0f );
    vec3 ViewToFrag = u_ViewPosition - v_FragPosition;
    
    for ( int DirectionalLightIndex = 0; DirectionalLightIndex < LightData.DirectionalLightCount; ++DirectionalLightIndex )
        {
        TotalDirectionalLightContribution += CalculateDirectionalLightContribution ( DirectionalLightIndex, ViewToFrag, Normal, MaterialDiffuse.xyz, MaterialSpecular.xyz, MaterialShininess );
        }
        
    Output += TotalDirectionalLightContribution;

    // Ambient light is calculated here, so it only adds once to the fragment, instead of once every light
    vec4 AmbientLightContribution = vec4 ( LightData.AmbientLight.Color, 1.0 ) * LightData.AmbientLight.Intensity * MaterialDiffuse;
    Output += AmbientLightContribution.xyz;
    return vec4 ( Output, MaterialDiffuse.a );
    }

void main()
{
FragOutput = vec4(1.0);
vec4 MaterialDiffuse = vec4(1,1,1,1);
vec4 MaterialSpecular = MaterialDiffuse;
float MaterialShininess = 0.8;
vec3 Normal = normalize(v_VertexNormal);
vec4 LightResult = ApplyLighting(MaterialDiffuse, MaterialSpecular, MaterialShininess, Normal );
FragOutput = LightResult;
}

What is the fault here?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT