Hey! Thanks for the reply.
Your point actually makes alot of sense. I am using a very old laptop that only supports OpenGL 3.1 on Windows and 3.3 on Linux.
I also have a zip with all the source code but I can’t post links on the forum
Here are both frag and vert shaders:
FRAGMENT:
#version 330 core
#define MAX_POINT_LIGHTS 4
#define MAX_SPOT_LIGHTS 4
struct Material
{
float specular_power;
};
struct DirectionalLight
{ //Base Aligned
vec4 direction; // 16 0
vec4 ambient; // 16 16
vec4 diffuse; // 16 32
vec4 specular; // 16 48
}; // Total = 48 + 16 = 64
// Elems. = 64
struct PointLight
{ //Size
vec4 position; // 16
vec4 ambient; // 16
vec4 diffuse; // 16
vec4 specular; // 16
float constant; // 4
float linear; // 4
float quadratic;// 4
}; //Total = 76
struct SpotLight
{ //Base(size)
vec4 position; // 16
vec4 direction;// 16
vec4 ambient; // 16
vec4 diffuse; // 16 48
vec4 specular; // 16 64
float constant;// 4 80
float linear; // 4 84
float quadratic;// 4 88
float cutOff; // 4 92
float outerCutOff;// 4 96
}; //Total = 100
//Protos
vec3 directionalLightInfluence(DirectionalLight dL,vec3 normal,vec3 viewDir);
vec3 pointLightInfluence(PointLight pL,vec3 normal,vec3 fragPos,vec3 viewDir);
vec3 spotLightInfluence(SpotLight sL,vec3 normal,vec3 fragPos,vec3 viewDir);
out vec4 FragColor;
in vec3 Normals;
in vec3 fragPos;
in vec2 TexCoords;
in vec3 viewPos;
uniform sampler2D diffuseMap;
uniform sampler2D specularMap;
uniform samplerCube envMap;
uniform Material material;
layout(std140) uniform LightingData
{
// Size //Starts at //Additional bytes to align (padding)
DirectionalLight dLight; // 64 0 = (+64) +0
PointLight pLights[MAX_POINT_LIGHTS]; // 76 64 = (+76) +4
// 76 144 = (+76) +4
// 76 224 = (+76) +4
// 76 304 = (+76) +4
SpotLight sLights[MAX_SPOT_LIGHTS]; // 100 384 = (+100) +12
// 100 496 = (+100) +12
// 100 608 = (+100) +12
// 100 720 = (+100) +12
//No need to add extra bytes
int scenePointLights; // 4 832 = (+4) +0
int sceneSpotLights; // 4 836 = (+4) +0
//Total 840 + 64 initial bytes = 904 bytes
};
void main()
{
//Props
vec3 norm=normalize(Normals);
vec3 viewDir=normalize(viewPos - fragPos);
//Vector with our lighting color
// 1 :: Directional Light
vec3 result = directionalLightInfluence(dLight,norm,viewDir);
//Pointlights cause visual glitches. change to "scenePointLights" or 4 to see if your card is presumably affected
for(int i = 0; i < 1; i++)
{
result += pointLightInfluence(pLights[i], norm, fragPos, viewDir);
}
//The following code will however work fine but won't scale well
//result += pointLightInfluence(pLights[0], norm, fragPos, viewDir);
//result += pointLightInfluence(pLights[1], norm, fragPos, viewDir);
//result += pointLightInfluence(pLights[2], norm, fragPos, viewDir);
//result += pointLightInfluence(pLights[3], norm, fragPos, viewDir);
//3 :: Spotlight
for(int i = 0; i < sceneSpotLights; i++)
{
result+=spotLightInfluence(sLights[i],norm,fragPos,viewDir);
}
//Results
//Refelctions;
//vec3 I = normalize(fragPos - viewPos);
//vec3 R = reflect(I , normalize(Normals));
//vec3 relection = texture(envMap, R).rgb;
FragColor = vec4(result,1.0);
}
vec3 directionalLightInfluence(DirectionalLight dL,vec3 normal,vec3 viewDir)
{
vec3 lightDir = normalize(-dL.direction.xyz);
//Diffuse
float diff=max(dot(normal,lightDir),0.0);
//Specular
vec3 reflectDir=reflect(-lightDir,normal);
float spec=pow(max(dot(viewDir,reflectDir),0.0),material.specular_power);
//Add all lighting
vec3 ambient = dL.ambient.xyz * vec3(texture(diffuseMap,TexCoords));
vec3 diffuse = dL.diffuse.xyz * diff * vec3(texture(diffuseMap,TexCoords));
vec3 specular = dL.specular.xyz * spec * vec3(texture(specularMap,TexCoords));
return (ambient+diffuse+specular);
}
vec3 pointLightInfluence(PointLight pL,vec3 normal,vec3 fragPos,vec3 viewDir)
{
vec3 lightDir=normalize(pL.position.xyz - fragPos);
//Diffuse
float diff=max(dot(normal,lightDir),0.0);
//Specular
vec3 reflectDir=reflect(-lightDir,normal);
float spec= pow(max(dot(viewDir,reflectDir),0.0),material.specular_power);
//Attenuation
float distance=length(pL.position.xyz - fragPos);
float attenuation=1.0 / (pL.constant + pL.linear * distance +
pL.quadratic * (distance * distance));
//Add all light
vec3 ambient = pL.ambient.xyz * vec3(texture(diffuseMap,TexCoords));
vec3 diffuse = pL.diffuse.xyz * diff * vec3(texture(diffuseMap,TexCoords));
vec3 specular = pL.specular.xyz * spec * vec3(texture(specularMap,TexCoords));
return attenuation*(ambient+diffuse+specular);
}
vec3 spotLightInfluence(SpotLight sL,vec3 normal,vec3 fragPos,vec3 viewDir)
{
//Ambient
vec3 ambient = sL.ambient.xyz * vec3(texture(diffuseMap, TexCoords));
//Diffuse
vec3 norm=normalize(normal);
vec3 lightDir = normalize(sL.position.xyz - fragPos);
float diff = max(dot(norm,lightDir),0.0);
vec3 diffuse=sL.diffuse.xyz * diff * vec3(texture(diffuseMap, TexCoords));
//Specular
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir,reflectDir),0.0),material.specular_power);
vec3 specular=sL.specular.xyz * spec * vec3(texture(specularMap,TexCoords));
//Final frag
float theta=dot(lightDir,normalize(-sL.direction.xyz));
float epsilon=sL.cutOff-sL.outerCutOff;
float intensity=clamp((theta-sL.outerCutOff)/epsilon,0.0,1.0);
diffuse*=intensity;
specular*=intensity;
//Attenuation
float distance = length(sL.position.xyz - fragPos);
float attenuation = 1.0 / (sL.constant + sL.linear * distance +
sL.quadratic * (distance * distance));
return attenuation * (ambient+diffuse+specular);
}
VERTEX:
#version 330 core
layout (location=0) in vec3 aPos;
layout (location=1) in vec3 aNormals;
layout (location=2) in vec2 aTexCoords;
layout(std140) uniform PVMatrices
{
mat4 projection;
mat4 view;
vec4 cam_pos;
};
out vec3 Normals;
out vec3 fragPos;
out vec2 TexCoords;
out vec3 viewPos;
uniform mat4 model;
void main()
{
gl_Position = projection * view * model * vec4(aPos,1.0);
fragPos = vec3(model * vec4(aPos, 1.0));
//Can use transpose but is expensive + its uniform scale.
Normals= mat3( transpose( inverse(model) ) )* aNormals;
//Tho we need accurate lighting .... soooo we transpose
//Normals=aNormals;
TexCoords=aTexCoords;
viewPos = cam_pos.xyz;
}