everything
This commit is contained in:
parent
79fa59c3aa
commit
8998bc3936
19 changed files with 855 additions and 7504 deletions
|
|
@ -1,56 +1,204 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
|
||||
struct Material {
|
||||
sampler2D diffuse;
|
||||
sampler2D specular;
|
||||
float shininess;
|
||||
};
|
||||
|
||||
struct DirectionalLight {
|
||||
vec3 direction;
|
||||
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
struct PointLight {
|
||||
bool disabled;
|
||||
|
||||
vec3 position;
|
||||
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
struct SpotLight {
|
||||
bool disabled;
|
||||
|
||||
vec3 position;
|
||||
vec3 direction;
|
||||
float cutOff;
|
||||
float outerCutOff;
|
||||
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
struct Decal {
|
||||
sampler2D tex;
|
||||
float opacity;
|
||||
vec4 uvCoords;
|
||||
};
|
||||
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoord;
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform sampler2D ourTexture;
|
||||
uniform sampler2D decal;
|
||||
#define MAX_POINT_LIGHTS 4
|
||||
#define MAX_SPOT_LIGHTS 4
|
||||
#define MAX_DECALS 4
|
||||
|
||||
// Texture & Decals
|
||||
uniform Material material;
|
||||
uniform sampler2D mainTexture;
|
||||
uniform Decal decals[MAX_DECALS];
|
||||
uniform int numberOfDecals;
|
||||
|
||||
uniform vec3 lightPos;
|
||||
uniform vec3 lightColor;
|
||||
uniform vec3 viewPos;
|
||||
|
||||
vec4 calcTextures(){
|
||||
vec4 tex1 = texture(ourTexture, TexCoord);
|
||||
vec4 tex2 = texture(decal, TexCoord);
|
||||
vec4 textures = mix(tex1, tex2, 0.2f);
|
||||
// Lights
|
||||
uniform DirectionalLight dirLight;
|
||||
|
||||
return textures;
|
||||
}
|
||||
uniform PointLight pointLights[MAX_POINT_LIGHTS];
|
||||
uniform int numberOfPointLights;
|
||||
|
||||
vec4 calcDirectLight(norm, viewDir){
|
||||
// Ambient Lighting
|
||||
float ambientStrength = 0.1;
|
||||
vec3 ambient = ambientStrength * lightColor;
|
||||
uniform SpotLight spotLights[MAX_SPOT_LIGHTS];
|
||||
uniform int numberOfSpotLights;
|
||||
|
||||
// Diffuse Lighting
|
||||
vec3 lightDir = normalize(lightPos - FragPos);
|
||||
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * lightColor;
|
||||
|
||||
// Specular Lighting
|
||||
float specularStrength = 0.5;
|
||||
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
||||
vec3 specular = specularStrength * spec * lightColor;
|
||||
|
||||
vec4 result = vec4(ambient.rgb + diffuse.rgb + specular.rgb, 1.0f);
|
||||
|
||||
return result;
|
||||
}
|
||||
// func declarations
|
||||
vec3 calcTextures();
|
||||
vec4 calcDirectLight(DirectionalLight light, vec3 normal, vec3 viewDir, vec3 albedo);
|
||||
vec4 calcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 albedo);
|
||||
vec4 calcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 albedo);
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 result = vec4(0.0f);
|
||||
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
|
||||
vec4 result = calcTextures();
|
||||
result += calcDirectLight(norm, viewDir);
|
||||
vec3 albedo = calcTextures();
|
||||
albedo = vec3(1,1,1);
|
||||
|
||||
//result += calcDirectLight(dirLight, norm, viewDir, albedo);
|
||||
|
||||
for(int i = 0; i < numberOfPointLights; i++){
|
||||
if (pointLights[i].disabled) continue;
|
||||
result += calcPointLight(pointLights[i], norm, FragPos, viewDir, albedo);
|
||||
}
|
||||
|
||||
for(int i = 0; i < numberOfSpotLights; i++){
|
||||
if (spotLights[i].disabled) continue;
|
||||
result += calcSpotLight(spotLights[i], norm, FragPos, viewDir, albedo);
|
||||
}
|
||||
|
||||
FragColor = result;
|
||||
}
|
||||
|
||||
|
||||
vec3 calcTextures(){
|
||||
vec4 textureAsVec = texture(mainTexture, TexCoords);
|
||||
|
||||
vec4 decalMix;
|
||||
for(int i = 0; i < numberOfDecals; i++){
|
||||
// We invert the 2nd one to make uv scaling easier
|
||||
vec2 decalCoords = TexCoords * (1.0 / decals[i].uvCoords.xy) + decals[i].uvCoords.zw;;
|
||||
|
||||
vec4 decal = texture(decals[i].tex, decalCoords);
|
||||
|
||||
decalMix += mix(decalMix, decal, 0.2f);
|
||||
}
|
||||
|
||||
vec4 finalMix = mix(textureAsVec, decalMix, 0.2f);
|
||||
|
||||
return vec3(finalMix);
|
||||
}
|
||||
|
||||
// calculates the color when using a directional light.
|
||||
vec4 calcDirectLight(DirectionalLight light, vec3 norm, vec3 viewDir, vec3 albedo)
|
||||
{
|
||||
vec3 lightDir = normalize(-light.direction);
|
||||
|
||||
// diffuse shading
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
|
||||
// specular shading
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
|
||||
// combine results
|
||||
vec3 ambient = light.ambient * albedo;
|
||||
vec3 diffuse = light.diffuse * diff * albedo;
|
||||
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
|
||||
|
||||
return vec4(ambient + diffuse + specular, 1.0f);
|
||||
}
|
||||
|
||||
vec4 calcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 albedo)
|
||||
{
|
||||
vec3 lightDir = normalize(light.position - fragPos);
|
||||
|
||||
// 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 - fragPos);
|
||||
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
||||
|
||||
// combine results
|
||||
vec3 ambient = light.ambient * albedo;
|
||||
vec3 diffuse = light.diffuse * diff * albedo;
|
||||
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
|
||||
return vec4(ambient + diffuse + specular, 1.0f);
|
||||
}
|
||||
|
||||
vec4 calcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 albedo)
|
||||
{
|
||||
vec3 lightDir = normalize(light.position - fragPos);
|
||||
|
||||
// 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 - fragPos);
|
||||
float attenuation = 1.0 / (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 * albedo;
|
||||
vec3 diffuse = light.diffuse * diff * albedo;
|
||||
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
|
||||
ambient *= attenuation * intensity;
|
||||
diffuse *= attenuation * intensity;
|
||||
specular *= attenuation * intensity;
|
||||
|
||||
return vec4(ambient + diffuse + specular, 1.0f);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue