Basic alpha blending works but I have no z ordering yet.

This commit is contained in:
Zed A. Shaw 2026-08-29 16:08:33 -04:00
parent 128f522089
commit 11b7f4a3ed
14 changed files with 272 additions and 30 deletions

View file

@ -0,0 +1,168 @@
#version 330 core
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};
out vec4 FragColor;
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
uniform vec3 viewPos;
uniform Material material;
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
float quadratic;
};
struct SpotLight {
vec3 direction;
vec3 position;
vec3 diffuse;
vec3 specular;
vec3 ambient;
float constant;
float linear;
float quadratic;
float cut_off;
float outer_cut_off;
};
#define MAX_LIGHTS 4
uniform int pointLightCount = 0;
uniform PointLight pointLights[MAX_LIGHTS];
uniform int dirLightCount = 0;
uniform DirLight dirLights[MAX_LIGHTS];
uniform int spotLightCount = 0;
uniform SpotLight spotLights[MAX_LIGHTS];
float near = 0.1;
float far = 100.0;
vec4 CalcDirLight(vec4 mat_tex, vec4 spec_tex, DirLight 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);
vec4 ambient = vec4(light.ambient, 1.0) * mat_tex;
vec4 diffuse = vec4(light.diffuse, 1.0) * diff * mat_tex;
vec4 specular = vec4(light.specular, 1.0) * spec * spec_tex;
return ambient + diffuse + specular;
}
vec4 CalcPointLight(vec4 mat_tex, vec4 spec_tex, PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
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));
vec4 ambient = vec4(light.ambient, 1.0) * mat_tex;
vec4 diffuse = vec4(light.diffuse, 1.0) * diff * mat_tex;
vec4 specular = vec4(light.specular, 1.0) * spec * spec_tex;
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return ambient + diffuse + specular;
}
vec4 CalcSpotLight(vec4 mat_tex, vec4 spec_tex, SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
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));
vec4 ambient = vec4(light.ambient, 1.0) * mat_tex;
vec4 diffuse = vec4(light.diffuse, 1.0) * diff * mat_tex;
vec4 specular = vec4(light.specular, 1.0) * spec * spec_tex;
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.cut_off - light.outer_cut_off;
float intensity = clamp((theta - light.outer_cut_off) / epsilon, 0.0, 1.0);
diffuse *= intensity;
specular *= intensity;
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return ambient + diffuse + specular;
}
float LinearizeDepth(float depth)
{
float ndc = depth * 2.0 - 1.0;
return (2.0 * near * far) / (far + near - ndc * (far - near));
}
void main()
{
vec4 mat_tex = texture(material.diffuse, TexCoords);
vec4 spec_tex = texture(material.specular, TexCoords);
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);
// kick it off with the first dirlight
vec4 result = CalcDirLight(mat_tex, spec_tex, dirLights[0], norm, viewDir);
// then augment with more (off by one)
for(int i = 1; i < dirLightCount; i++) {
result += CalcDirLight(mat_tex, spec_tex, dirLights[i], norm, viewDir);
}
for(int i = 0; i < pointLightCount; i++) {
result += CalcPointLight(mat_tex, spec_tex, pointLights[i], norm, FragPos, viewDir);
}
for(int i = 0; i < spotLightCount; i++) {
result += CalcSpotLight(mat_tex, spec_tex, spotLights[i], norm, FragPos, viewDir);
}
FragColor = result;
}

View file

@ -0,0 +1,9 @@
#version 330 core
out vec4 FragColor;
uniform vec3 diffuse;
void main()
{
FragColor = vec4(diffuse, 1.0); // set all 4 vector values to 1.0
}

View file

@ -0,0 +1,20 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0f);
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;
TexCoords = aTexCoords;
}