[Solved] A malfunctionning Shader

Everything related to 3D programming
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

[Solved] A malfunctionning Shader

Post by StarBootics »

Hello everyone,

I'm trying to add some lighting to a rendering system in OpenGL but it's not working properly and I don't understand why. So there is the vertex shader :

Code: Select all

#version 140

in vec3 Position;
in vec3 Normal;
in vec2 UVMap;
in vec3 Tangent;

out vec2 STMap;
out vec3 normal0;
out vec3 FragPos;

uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;

void main()
{
        vec4 WorldPosition = ModelMatrix * vec4(Position, 1.0);
        gl_Position = ProjectionMatrix * ViewMatrix * WorldPosition;

        normal0 = vec3(ModelMatrix * vec4(Normal, 0.0));
        STMap = UVMap;
        FragPos = WorldPosition.xyz;
}
And the corresponding Fragment shader :

Code: Select all

#version 140

#define NUMBER_OF_POINT_LIGHTS 4
#define NUMBER_OF_SPOT_LIGHTS 4

struct SMaterial
{
    sampler2D Diffuse;
    sampler2D Specular;
    float Shininess;
};

struct SDirectionalLight
{
    vec3 Direction;
    vec3 Ambient;
    vec3 Diffuse;
    vec3 Specular;
};

struct SPointLight
{
    vec3 Position;
    vec3 Ambient;
    vec3 Diffuse;
    vec3 Specular;
    float Constant;
    float Linear;
    float Quadratic;
};

struct SSpotLight
{
    vec3 Position;
    vec3 Direction;
    vec3 Ambient;
    vec3 Diffuse;
    vec3 Specular;
    float CutOff;
    float OuterCutOff;
    float Constant;
    float Linear;
    float Quadratic;
};



in vec2 STMap;
in vec3 normal0;
in vec3 FragPos;

uniform vec3 ViewPos;
uniform SDirectionalLight DirectionalLight;
uniform SPointLight PointLights[NUMBER_OF_POINT_LIGHTS];
uniform SSpotLight SpotLights[NUMBER_OF_SPOT_LIGHTS];
uniform SMaterial Material;

vec3 CalcDirectionalLight(SDirectionalLight Light, vec3 Normal, vec3 ViewDir);
vec3 CalcPointLight(SPointLight Light, vec3 Normal, vec3 FragPos, vec3 ViewDir);
vec3 CalcSpotLight(SSpotLight Light, vec3 Normal, vec3 FragPos, vec3 ViewDir);

void main()
{

	vec3 norm = normalize(normal0);
	vec3 ViewDirection = normalize(ViewPos - FragPos);

	vec3 Result = CalcDirectionalLight(DirectionalLight, norm, ViewDirection);
	
	for (int i = 0; i < NUMBER_OF_POINT_LIGHTS; i++)
	{
		Result += CalcPointLight(PointLights[i], norm, FragPos, ViewDirection);
	}

	for (int i = 0; i < NUMBER_OF_SPOT_LIGHTS; i++)
	{
		Result += CalcSpotLight(SpotLights[i], norm, FragPos, ViewDirection);
	}

	gl_FragColor = vec4(Result, 1.0);

}


// Calculates the color when using a directional light.
vec3 CalcDirectionalLight(SDirectionalLight Light, vec3 Normal, vec3 ViewDir)
{
    vec3 LightDir = normalize(-Light.Direction);
    
    // Diffuse shading
    float Diff = max(dot(Normal, LightDir), 0.0);
    
    // Specular shading
    vec3 ReflectDir = reflect( -LightDir, Normal);
    float Spec = pow( max( dot( ViewDir, ReflectDir ), 0.0 ), Material.Shininess);
    
    // Combine results
    vec3 Ambient = Light.Ambient * vec3( texture(Material.Diffuse, STMap));
    vec3 Diffuse = Light.Diffuse * Diff * vec3( texture(Material.Diffuse, STMap));
    vec3 Specular = Light.Specular * Spec * vec3( texture(Material.Specular, STMap));
    
    return (Ambient + Diffuse + Specular);
}

// Calculates the color when using a point light.
vec3 CalcPointLight(SPointLight Light, vec3 Normal, vec3 FragPosition, vec3 ViewDir)
{
    vec3 LightDir = normalize(Light.Position - FragPosition);
    
    // Diffuse shading
    float Diff = max(dot(Normal, LightDir), 0.0);
    
    // Specular shading
    vec3 ReflectDir = reflect( -LightDir, Normal);
    float Spec = pow( max( dot( ViewDir, ReflectDir ), 0.0 ), Material.Shininess);
    
    // Attenuation
    float Distance = length(Light.Position - FragPosition);
    float Attenuation = 1.0f / (Light.Constant + Light.Linear * Distance + Light.Quadratic * Distance * Distance);
    
    // Combine results
    vec3 Ambient = Light.Ambient * vec3( texture(Material.Diffuse, STMap));
    vec3 Diffuse = Light.Diffuse * Diff * vec3( texture(Material.Diffuse, STMap));
    vec3 Specular = Light.Specular * Spec * vec3( texture(Material.Specular, STMap));

    Ambient *= Attenuation;
    Diffuse *= Attenuation;
    Specular *= Attenuation;
    
    return (Ambient + Diffuse + Specular);
}

// Calculates the color when using a spot light.
vec3 CalcSpotLight(SSpotLight Light, vec3 Normal, vec3 FragPosition, vec3 ViewDir )
{
    vec3 LightDir = normalize(Light.Position - FragPosition );
    
    // Diffuse shading
    float Diff = max( dot(Normal, LightDir ), 0.0 );
    
    // Specular shading
    vec3 ReflectDir = reflect(-LightDir, Normal );
    float Spec = pow( max( dot( ViewDir, ReflectDir ), 0.0 ), Material.Shininess );
    
    // Attenuation
    float Distance = length(Light.Position - FragPosition );
    float Attenuation = 1.0f / (Light.Constant + Light.Linear * Distance + Light.Quadratic * Distance * Distance);
    
    // Spotlight intensity
    float Theta = dot(LightDir, normalize(-Light.Direction));
    float Epsilon = Light.CutOff - Light.OuterCutOff;
    float Intensity = clamp((Theta - Light.OuterCutOff ) / Epsilon, 0.0, 1.0);
    
    // Combine results
    vec3 Ambient = Light.Ambient * vec3( texture(Material.Diffuse, STMap));
    vec3 Diffuse = Light.Diffuse * Diff * vec3( texture(Material.Diffuse, STMap));
    vec3 Specular = Light.Specular * Spec * vec3( texture(Material.Specular, STMap));

    Ambient *= Attenuation * Intensity;
    Diffuse *= Attenuation * Intensity;
    Specular *= Attenuation * Intensity;
    
    return (Ambient + Diffuse + Specular);
}
Based on the original code available here : https://github.com/SonarSystems/Modern-OpenGL-Tutorials
The proof the the original code is working can be seen here : https://youtu.be/vnUQOIODAKc

The problem I have is simple the 3D model is solid black instead of a textured one and I don't understand why. Any ideas ...

Thanks.
StarBootics
Last edited by StarBootics on Fri Feb 08, 2019 10:18 pm, edited 1 time in total.
The Stone Age did not end due to a shortage of stones !
DarkDragon
Addict
Addict
Posts: 2228
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: A malfunctionning Shader

Post by DarkDragon »

Usual issues when a model is only black:
* Are the uniforms set properly?
* Does the model contain normals?
bye,
Daniel
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: A malfunctionning Shader

Post by StarBootics »

DarkDragon wrote:* Are the uniforms set properly?
This is the rendering code

Code: Select all

              ShaderPrograms::SendUniformMatrix4fv(Instances()\Mesh\SubMeshs()\ShaderProgramName, "ProjectionMatrix", Camera::GetProjection())
              ShaderPrograms::SendUniformMatrix4fv(Instances()\Mesh\SubMeshs()\ShaderProgramName, "ViewMatrix", Camera::GetView())
              ShaderPrograms::SendUniformMatrix4fv(Instances()\Mesh\SubMeshs()\ShaderProgramName, "ModelMatrix", *Model)
              ShaderPrograms::SendUniformVector3fv(Instances()\Mesh\SubMeshs()\ShaderProgramName, "ViewPos", Camera::GetPosition())
              
              ShaderPrograms::SendUniform1i(Instances()\Mesh\SubMeshs()\ShaderProgramName, "Material.Diffuse", 0)
              ShaderPrograms::SendUniform1i(Instances()\Mesh\SubMeshs()\ShaderProgramName, "Material.Specular", 1)
              ShaderPrograms::SendUniform1f(Instances()\Mesh\SubMeshs()\ShaderProgramName, "Material.Shininess", Instances()\Mesh\SubMeshs()\Shininess)
              
              Lights::Load_To_Shader(Instances()\Mesh\SubMeshs()\ShaderProgramName); To be optimized
              
              OGL::glActiveTexture(OGL::#GL_TEXTURE0)
              OGL::glBindTexture(#GL_TEXTURE_2D, Instances()\Mesh\SubMeshs()\DiffuseTextureID)
              
              OGL::glActiveTexture(OGL::#GL_TEXTURE1)
              
              If Instances()\Mesh\SubMeshs()\SpecularTextureID <> 0
                OGL::glBindTexture(#GL_TEXTURE_2D, Instances()\Mesh\SubMeshs()\SpecularTextureID)
              Else
                OGL::glBindTexture(#GL_TEXTURE_2D, Secondary\DefaultSpecularMapID)
              EndIf
              
              OGL::glBindVertexArray(Instances()\Mesh\SubMeshs()\VertexArrayObjectID)
              OGL::glDrawArrays(#GL_TRIANGLES, 0, Instances()\Mesh\SubMeshs()\IndiceCount * 3)
              OGL::glBindVertexArray(0)
              OGL::glBindTexture(#GL_TEXTURE_2D, 0)
DarkDragon wrote:* Does the model contain normals?
The model contain normal, yes.

I'm running out of ideas.

Thanks for you response.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: A malfunctionning Shader

Post by Samuel »

Try testing your fragment shader with just a basic color like gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);. If the model is green then move on to the next test. Send a 3f color value from purebasic to the fragment shader gl_FragColor = vec4(Uniform3fRed, 1.0);. Then start adding basic lighting. Keep adding to the fragment shader piece by piece until it fails.
This is the method I use for shader debugging.
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: A malfunctionning Shader

Post by StarBootics »

OK the directional light is working as expected mostly, I can see the texture on the model. Piece by piece like Samuel suggest I was able to pin point that the Specular Lighting is the problem. That being said I don't understand why it didn't work.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: A malfunctionning Shader

Post by StarBootics »

Hello everyone,

Directional Light --> OK
Point Light --> OK
Spot Light --> KO

The big part of the problem was the Shininess set to 0.0. The smallest value should be 1.0. Now I'm trying to figure out the Spot light problem.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: A malfunctionning Shader

Post by StarBootics »

Hello everyone,

Problem solved by using Forward Rendering. 3 videos to watch for more details :

https://www.youtube.com/watch?v=rdjD0YPmLPo
https://www.youtube.com/watch?v=TAv8UsEO0j0
https://www.youtube.com/watch?v=dTQxnRbZ9hM

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
Post Reply