opengl.shaders: Use unified lighting and texturing 'snippets' for fragment shaders.

This commit is contained in:
Rod Kay
2024-02-23 22:35:12 +11:00
parent d1f702aab5
commit 68c1ff4764
17 changed files with 439 additions and 568 deletions

View File

@@ -1,4 +1,5 @@
#version 140
struct light
{
@@ -11,21 +12,13 @@ struct light
};
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
uniform vec3 specular_Color; // The materials specular color.
uniform sampler2D Texture;
uniform int light_Count;
uniform light Lights [10];
uniform light Lights [50];
uniform vec3 specular_Color; // The materials specular color.
in float frag_Shine;
in vec3 frag_Site;
in vec3 frag_Normal;
in vec2 frag_Coords;
in float frag_Shine;
out vec4 final_Color;
vec3
@@ -77,12 +70,15 @@ apply_Light (light Light,
vec3 Diffuse = diffuse_Coefficient * lit_surface_Color;
float specular_Coefficient = 0.0;
if (diffuse_Coefficient > 0.0)
{
specular_Coefficient = pow (max (0.0,
dot (Surface_to_Camera,
reflect (-Surface_to_Light,
Normal))),
frag_Shine);
}
vec3 Specular = specular_Coefficient * specular_Color * Light.Color;
@@ -91,34 +87,3 @@ apply_Light (light Light,
void
main()
{
vec3 surface_Site = vec3 ( model_Transform
* vec4 (frag_Site, 1));
vec4 surface_Color = texture (Texture, frag_Coords);
vec3 Surface_to_Camera = normalize (camera_Site - surface_Site);
vec3 Normal = normalize ( frag_Normal
* inverse_model_Rotation);
// Combine color from all the lights.
//
vec3 linear_Color = vec3 (0);
for (int i = 0; i < light_Count; ++i)
{
linear_Color += apply_Light (Lights [i],
surface_Color.rgb,
Normal,
surface_Site,
Surface_to_Camera);
}
vec3 Gamma = vec3 (1.0 / 2.2);
final_Color = vec4 (pow (linear_Color, // Final color (after gamma correction).
Gamma),
surface_Color.a);
}

View File

@@ -1,92 +1,17 @@
#version 140
struct light
{
vec4 Site;
vec3 Color;
float Attenuation;
float ambient_Coefficient;
float cone_Angle;
vec3 cone_Direction;
};
// Include 'version.header'.
// Include 'lighting-frag.snippet'
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
uniform vec3 specular_Color; // The materials specular color.
uniform int light_Count;
uniform light Lights [50];
in vec3 frag_Site;
in vec3 frag_Normal;
in vec4 frag_Color;
in vec3 frag_Site;
in vec3 frag_Normal;
in vec4 frag_Color;
in float frag_Shine;
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
out vec4 final_Color;
vec3
apply_Light (light Light,
vec3 surface_Color,
vec3 Normal,
vec3 surface_Site,
vec3 Surface_to_Camera)
{
vec3 Surface_to_Light;
float Attenuation = 1.0;
if (Light.Site.w == 0.0)
{
// Directional light.
//
Surface_to_Light = normalize (-Light.Site.xyz);
Attenuation = 1.0; // No attenuation for directional lights.
}
else
{
// Point light.
//
vec3 Surface_to_Light_vector = Light.Site.xyz - surface_Site;
float Distance_to_Light = length (Surface_to_Light_vector);
Surface_to_Light = normalize (Surface_to_Light_vector);
Attenuation = 1.0
/ ( 1.0
+ Light.Attenuation
* pow (Distance_to_Light, 2));
// Cone restrictions which affects attenuation.
//
float Light_to_Surface_Angle = degrees (acos (dot (-Surface_to_Light,
normalize (Light.cone_Direction))));
if (Light_to_Surface_Angle > Light.cone_Angle)
{
Attenuation = 0.0;
}
}
vec3 lit_surface_Color = surface_Color * Light.Color;
vec3 Ambient = Light.ambient_Coefficient * lit_surface_Color;
float diffuse_Coefficient = max (0.0,
dot (Normal,
Surface_to_Light));
vec3 Diffuse = diffuse_Coefficient * lit_surface_Color;
float specular_Coefficient = 0.0;
if (diffuse_Coefficient > 0.0)
specular_Coefficient = pow (max (0.0,
dot (Surface_to_Camera,
reflect (-Surface_to_Light,
Normal))),
frag_Shine);
vec3 Specular = specular_Coefficient * specular_Color * Light.Color;
return Ambient + Attenuation * (Diffuse + Specular); // Linear color (before gamma correction).
}
out vec4 final_Color;
@@ -120,4 +45,7 @@ main()
final_Color = vec4 (pow (linear_Color, // Final color (after gamma correction).
Gamma),
surface_Color.a);
final_Color = min (final_Color, // Prevent light saturation.
surface_Color);
}

View File

@@ -1,106 +1,26 @@
#version 140
struct light
{
vec4 Site;
vec3 Color;
float Attenuation;
float ambient_Coefficient;
float cone_Angle;
vec3 cone_Direction;
};
// Include 'version.header'.
// Include 'lighting-frag.snippet'
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
uniform vec3 specular_Color; // The materials specular color.
uniform sampler2D Texture;
uniform int light_Count;
uniform light Lights [50];
in vec3 frag_Site;
in vec3 frag_Normal;
in vec4 frag_Color;
in vec3 frag_Site;
in vec3 frag_Normal;
in vec4 frag_Color;
in vec2 frag_Coords;
in float frag_Shine;
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
out vec4 final_Color;
vec3
apply_Light (light Light,
vec3 surface_Color,
vec3 Normal,
vec3 surface_Site,
vec3 Surface_to_Camera)
{
vec3 Surface_to_Light;
float Attenuation = 1.0;
if (Light.Site.w == 0.0)
{
// Directional light.
//
Surface_to_Light = normalize (-Light.Site.xyz);
Attenuation = 1.0; // No attenuation for directional lights.
}
else
{
// Point light.
//
vec3 Surface_to_Light_vector = Light.Site.xyz - surface_Site;
float Distance_to_Light = length (Surface_to_Light_vector);
Surface_to_Light = normalize (Surface_to_Light_vector);
Attenuation = 1.0
/ ( 1.0
+ Light.Attenuation
* pow (Distance_to_Light, 2));
// Cone restrictions which affects attenuation.
//
float Light_to_Surface_Angle = degrees (acos (dot (-Surface_to_Light,
normalize (Light.cone_Direction))));
if (Light_to_Surface_Angle > Light.cone_Angle)
{
Attenuation = 0.0;
}
}
vec3 lit_surface_Color = surface_Color * Light.Color;
vec3 Ambient = Light.ambient_Coefficient * lit_surface_Color;
float diffuse_Coefficient = max (0.0,
dot (Normal,
Surface_to_Light));
vec3 Diffuse = diffuse_Coefficient * lit_surface_Color;
float specular_Coefficient = 0.0;
if (diffuse_Coefficient > 0.0)
specular_Coefficient = pow (max (0.0,
dot (Surface_to_Camera,
reflect (-Surface_to_Light,
Normal))),
frag_Shine);
vec3 Specular = specular_Coefficient * specular_Color * Light.Color;
return Ambient + Attenuation * (Diffuse + Specular); // Linear color (before gamma correction).
}
out vec4 final_Color;
void
main()
{
vec3 surface_Site = vec3 ( model_Transform
* vec4 (frag_Site, 1));
vec4 surface_Color = ( texture (Texture, frag_Coords)
+ frag_Color)
/ 2.0;
vec3 surface_Site = vec3 ( model_Transform
* vec4 (frag_Site, 1));
vec4 surface_Color = frag_Color;
vec3 Surface_to_Camera = normalize (camera_Site - surface_Site);
vec3 Normal = normalize ( frag_Normal
@@ -124,4 +44,7 @@ main()
final_Color = vec4 (pow (linear_Color, // Final color (after gamma correction).
Gamma),
surface_Color.a);
final_Color = min (final_Color, // Prevent light saturation.
surface_Color);
}

View File

@@ -1,124 +1,19 @@
#version 140
// Texturing snippet.
//
uniform int texture_Count;
uniform sampler2D Textures [32];
uniform float Fade [32];
vec4
apply_Texturing (vec2 Coords)
{
vec4 Color = vec4 (0);
for (int i = 0; i < texture_Count; ++i)
{
Color.rgb += texture (Textures [i], Coords).rgb
* texture (Textures [i], Coords).a
* (1.0 - Fade [i]);
Color.a = max (Color.a, texture (Textures [i],
Coords).a);
}
return Color;
}
// Include 'version.header'.
// Include 'texturing-frag.snippet'.
// Include 'lighting-frag.snippet'
struct light
{
vec4 Site;
vec3 Color;
float Attenuation;
float ambient_Coefficient;
float cone_Angle;
vec3 cone_Direction;
};
in vec3 frag_Site;
in vec3 frag_Normal;
in vec4 frag_Color;
in vec2 frag_Coords;
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
uniform vec3 specular_Color; // The materials specular color.
//uniform sampler2D Texture;
uniform int light_Count;
uniform light Lights [50];
in vec3 frag_Site;
in vec3 frag_Normal;
in vec4 frag_Color;
in vec2 frag_Coords;
in float frag_Shine;
out vec4 final_Color;
vec3
apply_Light (light Light,
vec3 surface_Color,
vec3 Normal,
vec3 surface_Site,
vec3 Surface_to_Camera)
{
vec3 Surface_to_Light;
float Attenuation = 1.0;
if (Light.Site.w == 0.0)
{
// Directional light.
//
Surface_to_Light = normalize (-Light.Site.xyz);
Attenuation = 1.0; // No attenuation for directional lights.
}
else
{
// Point light.
//
vec3 Surface_to_Light_vector = Light.Site.xyz - surface_Site;
float Distance_to_Light = length (Surface_to_Light_vector);
Surface_to_Light = normalize (Surface_to_Light_vector);
Attenuation = 1.0
/ ( 1.0
+ Light.Attenuation
* pow (Distance_to_Light, 2));
// Cone restrictions which affects attenuation.
//
float Light_to_Surface_Angle = degrees (acos (dot (-Surface_to_Light,
normalize (Light.cone_Direction))));
if (Light_to_Surface_Angle > Light.cone_Angle)
{
Attenuation = 0.0;
}
}
vec3 lit_surface_Color = surface_Color * Light.Color;
vec3 Ambient = Light.ambient_Coefficient * lit_surface_Color;
float diffuse_Coefficient = max (0.0,
dot (Normal,
Surface_to_Light));
vec3 Diffuse = diffuse_Coefficient * lit_surface_Color;
float specular_Coefficient = 0.0;
if (diffuse_Coefficient > 0.0)
specular_Coefficient = pow (max (0.0,
dot (Surface_to_Camera,
reflect (-Surface_to_Light,
Normal))),
frag_Shine);
vec3 Specular = specular_Coefficient * specular_Color * Light.Color;
return Ambient + Attenuation * (Diffuse + Specular); // Linear color (before gamma correction).
}
out vec4 final_Color;
@@ -150,4 +45,7 @@ main()
final_Color = vec4 (pow (linear_Color, // Final color (after gamma correction).
Gamma),
surface_Color.a);
final_Color = min (final_Color, // Prevent light saturation.
surface_Color);
}

View File

@@ -1,127 +0,0 @@
#version 140
struct light
{
vec4 Site;
vec3 Color;
float Attenuation;
float ambient_Coefficient;
float cone_Angle;
vec3 cone_Direction;
};
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
uniform vec3 specular_Color; // The materials specular color.
uniform sampler2D Texture;
uniform int light_Count;
uniform light Lights [50];
in vec3 frag_Site;
in vec3 frag_Normal;
in vec4 frag_Color;
in vec2 frag_Coords;
in float frag_Shine;
out vec4 final_Color;
vec3
apply_Light (light Light,
vec3 surface_Color,
vec3 Normal,
vec3 surface_Site,
vec3 Surface_to_Camera)
{
vec3 Surface_to_Light;
float Attenuation = 1.0;
if (Light.Site.w == 0.0)
{
// Directional light.
//
Surface_to_Light = normalize (-Light.Site.xyz);
Attenuation = 1.0; // No attenuation for directional lights.
}
else
{
// Point light.
//
vec3 Surface_to_Light_vector = Light.Site.xyz - surface_Site;
float Distance_to_Light = length (Surface_to_Light_vector);
Surface_to_Light = normalize (Surface_to_Light_vector);
Attenuation = 1.0
/ ( 1.0
+ Light.Attenuation
* pow (Distance_to_Light, 2));
// Cone restrictions which affects attenuation.
//
float Light_to_Surface_Angle = degrees (acos (dot (-Surface_to_Light,
normalize (Light.cone_Direction))));
if (Light_to_Surface_Angle > Light.cone_Angle)
{
Attenuation = 0.0;
}
}
vec3 lit_surface_Color = surface_Color * Light.Color;
vec3 Ambient = Light.ambient_Coefficient * lit_surface_Color;
float diffuse_Coefficient = max (0.0,
dot (Normal,
Surface_to_Light));
vec3 Diffuse = diffuse_Coefficient * lit_surface_Color;
float specular_Coefficient = 0.0;
if (diffuse_Coefficient > 0.0)
specular_Coefficient = pow (max (0.0,
dot (Surface_to_Camera,
reflect (-Surface_to_Light,
Normal))),
frag_Shine);
vec3 Specular = specular_Coefficient * specular_Color * Light.Color;
return Ambient + Attenuation * (Diffuse + Specular); // Linear color (before gamma correction).
}
void
main()
{
vec3 surface_Site = vec3 ( model_Transform
* vec4 (frag_Site, 1));
vec4 surface_Color = mix (texture (Texture, frag_Coords),
frag_Color,
0.5);
vec3 Surface_to_Camera = normalize (camera_Site - surface_Site);
vec3 Normal = normalize ( frag_Normal
* inverse_model_Rotation);
// Combine color from all the lights.
//
vec3 linear_Color = vec3 (0);
for (int i = 0; i < light_Count; ++i)
{
linear_Color += apply_Light (Lights [i],
surface_Color.rgb,
Normal,
surface_Site,
Surface_to_Camera);
}
vec3 Gamma = vec3 (1.0 / 2.2);
final_Color = vec4 (pow (linear_Color, // Final color (after gamma correction).
Gamma),
surface_Color.a);
}

View File

@@ -1,98 +1,18 @@
// Include 'version.header'.
// Include 'texturing.frag'.
struct light
{
vec4 Site;
vec3 Color;
float Attenuation;
float ambient_Coefficient;
float cone_Angle;
vec3 cone_Direction;
};
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
uniform vec3 specular_Color; // The materials specular color.
uniform int light_Count;
uniform light Lights [50];
in vec3 frag_Site;
in vec3 frag_Normal;
in vec2 frag_Coords;
in float frag_Shine;
out vec4 final_Color;
// Include 'texturing-frag.snippet'.
// Include 'lighting-frag.snippet'
in vec3 frag_Site;
in vec3 frag_Normal;
in vec2 frag_Coords;
vec3
apply_Light (light Light,
vec3 surface_Color,
vec3 Normal,
vec3 surface_Site,
vec3 Surface_to_Camera)
{
vec3 Surface_to_Light;
float Attenuation = 1.0;
if (Light.Site.w == 0.0)
{
// Directional light.
//
Surface_to_Light = normalize (-Light.Site.xyz);
Attenuation = 1.0; // No attenuation for directional lights.
}
else
{
// Point light.
//
vec3 Surface_to_Light_vector = Light.Site.xyz - surface_Site;
float Distance_to_Light = length (Surface_to_Light_vector);
Surface_to_Light = normalize (Surface_to_Light_vector);
Attenuation = 1.0
/ ( 1.0
+ Light.Attenuation
* pow (Distance_to_Light, 2));
// Cone restrictions which affects attenuation.
//
float Light_to_Surface_Angle = degrees (acos (dot (-Surface_to_Light,
normalize (Light.cone_Direction))));
if (Light_to_Surface_Angle > Light.cone_Angle)
{
Attenuation = 0.0;
}
}
vec3 lit_surface_Color = surface_Color * Light.Color;
vec3 Ambient = Light.ambient_Coefficient * lit_surface_Color;
float diffuse_Coefficient = max (0.0,
dot (Normal,
Surface_to_Light));
vec3 Diffuse = diffuse_Coefficient * lit_surface_Color;
float specular_Coefficient = 0.0;
if (diffuse_Coefficient > 0.0)
specular_Coefficient = pow (max (0.0,
dot (Surface_to_Camera,
reflect (-Surface_to_Light,
Normal))),
frag_Shine);
vec3 Specular = specular_Coefficient * specular_Color * Light.Color;
return Ambient + Attenuation * (Diffuse + Specular); // Linear color (before gamma correction).
}
uniform mat4 model_Transform;
uniform mat3 inverse_model_Rotation;
uniform vec3 camera_Site;
out vec4 final_Color;
@@ -118,8 +38,12 @@ main()
Surface_to_Camera);
}
vec3 Gamma = vec3 (1.0 / 2.2);
vec3 Gamma = vec3 (1.0 / 2.2);
final_Color = vec4 (pow (linear_Color, // Final color (after gamma correction).
Gamma),
surface_Color.a);
final_Color = min (final_Color, // Prevent light saturation.
surface_Color);
}

View File

@@ -0,0 +1,27 @@
uniform int texture_Count;
uniform sampler2D Textures [32];
uniform float Fade [32];
vec4
apply_Texturing (vec2 Coords)
{
vec4 Color = vec4 (0);
for (int i = 0; i < texture_Count; ++i)
{
Color.rgb += texture (Textures [i], Coords).rgb
* texture (Textures [i], Coords).a
* (1.0 - Fade [i]);
// Color.a += texture (Textures [i], Coords).a * (1.0 - Fade[1]);
Color.a = max (Color.a,
texture (Textures [i],Coords).a * (1.0 - Fade[i]));
// Color.a = max (Color.a,
// texture (Textures [i],Coords).a);
}
return Color;
}