Can't get flat lighting in java and openGL to work

Random Coder :

I have been trying to get flat lighting to work in my openGL shader for about two weeks now, i have tried lot's of tutorials but still cant get it to work thats why i decided to post somthing on this website i have also searched on, mabey you can help me.

Vertex shader:

#version 330 core

// vertex shader input \\
in vec3 position;
in vec2 textureCoords;
in vec3 normal;

// vertex shader output \\
out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toCameraVector;
out float visibility;

// uniform variables \\
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;

// fog settings \\
const float density = 0.0012;
const float gradient = 5.0;

void main(void){

    vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
    vec4 positionRelativeToCam = viewMatrix * worldPosition;
    gl_Position = projectionMatrix * positionRelativeToCam;
    pass_textureCoords = textureCoords * 40.0;

    surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
    toLightVector = lightPosition - worldPosition.xyz;
    toCameraVector = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz - worldPosition.xyz;

    // fog stuff \\
    float distance = length(positionRelativeToCam.xyz);
    visibility = exp(-pow((distance * density), gradient));
    visibility = clamp(visibility, 0.0, 1.0);

}

Fragment shader:

#version 330 core

// fragment shader input \\
in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCameraVector;
in float visibility;

// colour output \\
out vec4 out_Colour;

// uniform variables \\
uniform sampler2D textureSampler;
uniform vec3 lightColour;
uniform float shineDamper;
uniform float reflectivity;
uniform vec3 skyColour;

void main(void){

    vec3 unitNormal = normalize(surfaceNormal);
    vec3 unitLightVector = normalize(toLightVector);

    float nDot1 = dot(unitNormal, unitLightVector);
    float brightness = max(nDot1, 0.25) + 0.05;
    vec3 diffuse = brightness * lightColour;

    vec3 unitVectorToCamera = normalize(toCameraVector);
    vec3 lightDirection = -unitLightVector;
    vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);

    float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
    specularFactor = max(specularFactor, 0.0);
    float dampedFactor = pow(specularFactor, shineDamper);
    vec3 finalSpecular = dampedFactor * lightColour;

    out_Colour =  vec4(diffuse, 1.0) * texture(textureSampler, pass_textureCoords) + vec4(finalSpecular, 1.0);
    out_Colour = mix(vec4(skyColour,1.0), out_Colour, visibility);

}

If you know how i could apply flat shading to my project please let me know.

Thanks in advance

Rabbid76 :

If you know how i could apply flat shading to my project please let me know.

Use the flat interpolation qualifier, for the vertex shader outputs, which are used for the clculation of the light:

Vertex shader:

flat out vec3 surfaceNormal;
flat out vec3 toLightVector;
flat out vec3 toCameraVector;

Fragment shader:

flat in vec3 surfaceNormal;
flat in vec3 toLightVector;
flat in vec3 toCameraVector;

See OpenGL Shading Language 3.30 Specification; 4.3.9 Interpolation; page 41

A variable qualified as flat will not be interpolated. Instead, it will have the same value for every fragment within a triangle. This value will come from a single provoking vertex, [...]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=101672&siteId=1