opengl.shaders: Use unified lighting and texturing 'snippets' for fragment shaders.
This commit is contained in:
@@ -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 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);
|
||||
}
|
||||
@@ -1,94 +1,19 @@
|
||||
#version 140
|
||||
// Include 'version.header'.
|
||||
// Include 'lighting-frag.snippet'
|
||||
|
||||
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 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).
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
main()
|
||||
@@ -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);
|
||||
}
|
||||
@@ -1,106 +1,26 @@
|
||||
#version 140
|
||||
// Include 'version.header'.
|
||||
// Include 'lighting-frag.snippet'
|
||||
|
||||
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;
|
||||
|
||||
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).
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
vec3 surface_Site = vec3 ( model_Transform
|
||||
* vec4 (frag_Site, 1));
|
||||
|
||||
vec4 surface_Color = ( texture (Texture, frag_Coords)
|
||||
+ frag_Color)
|
||||
/ 2.0;
|
||||
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);
|
||||
}
|
||||
@@ -1,127 +1,22 @@
|
||||
#version 140
|
||||
// Include 'version.header'.
|
||||
// Include 'texturing-frag.snippet'.
|
||||
// Include 'lighting-frag.snippet'
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
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).
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -1,101 +1,21 @@
|
||||
// Include 'version.header'.
|
||||
// Include 'texturing.frag'.
|
||||
// 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;
|
||||
};
|
||||
|
||||
|
||||
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;
|
||||
|
||||
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).
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
@@ -119,7 +39,11 @@ main()
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
27
3-mid/opengl/assets/shader/texturing-frag.snippet
Normal file
27
3-mid/opengl/assets/shader/texturing-frag.snippet
Normal 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;
|
||||
}
|
||||
@@ -109,7 +109,8 @@ is
|
||||
the_Light : openGL.Light.item := Demo.Renderer.new_Light;
|
||||
begin
|
||||
the_Light. Site_is ([5_000.0, 2_000.0, 5_000.0]);
|
||||
the_Light.Color_is (White);
|
||||
the_Light.Color_is (Grey);
|
||||
-- the_Light.Color_is (Black);
|
||||
|
||||
Demo.Renderer.set (the_Light);
|
||||
end;
|
||||
@@ -271,7 +272,7 @@ is
|
||||
heights_File : constant asset_Name := to_Asset ("assets/opengl/terrain/kidwelly-terrain.png");
|
||||
texture_File : constant asset_Name := to_Asset ("assets/opengl/terrain/kidwelly-terrain-texture.png");
|
||||
|
||||
the_Region : constant IO.height_Map_view := IO.to_height_Map (heights_File, 10.0);
|
||||
the_Region : constant IO.height_Map_view := IO.to_height_Map (heights_File, Scale => 10.0);
|
||||
Tiling : constant texture_Transform_2d := (S => (0.0, 1.0),
|
||||
T => (0.0, 1.0));
|
||||
the_ground_Model : constant Model.terrain.view
|
||||
|
||||
@@ -78,7 +78,9 @@ is
|
||||
the_Program.Program := new openGL.Program.lit.item;
|
||||
|
||||
the_Program. vertex_Shader.define (Shader.Vertex, "assets/opengl/shader/lit_colored.vert");
|
||||
the_Program.fragment_Shader.define (Shader.Fragment, "assets/opengl/shader/lit_colored.frag");
|
||||
the_Program.fragment_Shader.define (Shader.Fragment, (asset_Names' (1 => to_Asset ("assets/opengl/shader/version.header"),
|
||||
2 => to_Asset ("assets/opengl/shader/lighting-frag.snippet"),
|
||||
3 => to_Asset ("assets/opengl/shader/lit_colored.frag"))));
|
||||
|
||||
the_Program.Program.define (the_Program. vertex_Shader'Access,
|
||||
the_Program.fragment_Shader'Access);
|
||||
|
||||
@@ -102,7 +102,7 @@ is
|
||||
|
||||
if is_Defined
|
||||
then
|
||||
raise Error with "The lit_colored_textured_skinned program has already been defined.";
|
||||
raise Error with "The 'lit_colored_textured_skinned' program has already been defined.";
|
||||
end if;
|
||||
|
||||
is_Defined := True;
|
||||
@@ -110,8 +110,9 @@ is
|
||||
-- Define the shaders and program.
|
||||
--
|
||||
vertex_Shader .define (Shader.Vertex, "assets/opengl/shader/lit_colored_skinned.vert");
|
||||
fragment_Shader.define (Shader.Fragment, "assets/opengl/shader/lit_colored_skinned.frag");
|
||||
|
||||
fragment_Shader.define (Shader.Fragment, (asset_Names' (1 => to_Asset ("assets/opengl/shader/version.header"),
|
||||
2 => to_Asset ("assets/opengl/shader/lighting-frag.snippet"),
|
||||
3 => to_Asset ("assets/opengl/shader/lit_colored_skinned.frag"))));
|
||||
the_Program.define ( vertex_Shader'Access,
|
||||
fragment_Shader'Access);
|
||||
the_Program.enable;
|
||||
|
||||
@@ -99,9 +99,25 @@ is
|
||||
white_Texture := openGL.Texture.Forge.to_Texture (white_Image);
|
||||
the_Program.Program := new openGL.Program.lit.item;
|
||||
|
||||
the_Program. vertex_Shader.define (Shader.Vertex, "assets/opengl/shader/lit_colored_textured.vert");
|
||||
the_Program.vertex_Shader.define (Shader.Vertex, "assets/opengl/shader/lit_colored_textured.vert");
|
||||
|
||||
if use_fragment_Shader = "assets/opengl/shader/lit_colored_text.frag"
|
||||
then
|
||||
the_Program.fragment_Shader.define (Shader.Fragment, use_fragment_Shader);
|
||||
|
||||
-- TODO: The below code produces ugly text. Investigate and fix.
|
||||
--
|
||||
-- the_Program.fragment_Shader.define (Shader.Fragment, (asset_Names' (1 => to_Asset ("assets/opengl/shader/version.header"),
|
||||
-- 2 => to_Asset ("assets/opengl/shader/texturing-frag.snippet"),
|
||||
-- 3 => to_Asset ("assets/opengl/shader/lighting-frag.snippet"),
|
||||
-- 4 => to_Asset ("assets/opengl/shader/lit_colored_textured.frag"))));
|
||||
else
|
||||
the_Program.fragment_Shader.define (Shader.Fragment, (asset_Names' (1 => to_Asset ("assets/opengl/shader/version.header"),
|
||||
2 => to_Asset ("assets/opengl/shader/texturing-frag.snippet"),
|
||||
3 => to_Asset ("assets/opengl/shader/lighting-frag.snippet"),
|
||||
4 => to_Asset ("assets/opengl/shader/lit_colored_textured.frag"))));
|
||||
end if;
|
||||
|
||||
the_Program.Program.define (the_Program. vertex_Shader'Access,
|
||||
the_Program.fragment_Shader'Access);
|
||||
the_Program.Program.enable;
|
||||
|
||||
@@ -126,7 +126,11 @@ is
|
||||
white_Texture := openGL.Texture.Forge.to_Texture (white_Image);
|
||||
|
||||
vertex_Shader .define (Shader.Vertex, "assets/opengl/shader/lit_colored_textured_skinned.vert");
|
||||
fragment_Shader.define (Shader.Fragment, "assets/opengl/shader/lit_colored_textured_skinned.frag");
|
||||
fragment_Shader.define (Shader.Fragment, (asset_Names' (1 => to_Asset ("assets/opengl/shader/version.header"),
|
||||
2 => to_Asset ("assets/opengl/shader/lighting-frag.snippet"),
|
||||
3 => to_Asset ("assets/opengl/shader/texturing-frag.snippet"),
|
||||
4 => to_Asset ("assets/opengl/shader/lit_colored_textured.frag"))));
|
||||
-- fragment_Shader.define (Shader.Fragment, "assets/opengl/shader/lit_colored_textured_skinned.frag");
|
||||
|
||||
the_Program.define ( vertex_Shader'Access,
|
||||
fragment_Shader'Access);
|
||||
|
||||
@@ -82,8 +82,9 @@ is
|
||||
|
||||
vertex_Shader .define (Shader.Vertex, "assets/opengl/shader/lit_textured.vert");
|
||||
fragment_Shader.define (Shader.Fragment, (asset_Names' (1 => to_Asset ("assets/opengl/shader/version.header"),
|
||||
2 => to_Asset ("assets/opengl/shader/texturing.frag"),
|
||||
3 => to_Asset ("assets/opengl/shader/lit_textured.frag"))));
|
||||
2 => to_Asset ("assets/opengl/shader/texturing-frag.snippet"),
|
||||
3 => to_Asset ("assets/opengl/shader/lighting-frag.snippet"),
|
||||
4 => to_Asset ("assets/opengl/shader/lit_textured.frag"))));
|
||||
the_Program := new openGL.Program.lit.item;
|
||||
the_Program.define ( vertex_Shader'Access,
|
||||
fragment_Shader'Access);
|
||||
|
||||
@@ -115,8 +115,11 @@ is
|
||||
white_Texture := openGL.Texture.Forge.to_Texture (white_Image);
|
||||
|
||||
vertex_Shader .define (Shader.Vertex, "assets/opengl/shader/lit_textured_skinned.vert");
|
||||
fragment_Shader.define (Shader.Fragment, "assets/opengl/shader/lit_textured_skinned.frag");
|
||||
|
||||
-- fragment_Shader.define (Shader.Fragment, "assets/opengl/shader/lit_textured_skinned.frag");
|
||||
fragment_Shader.define (Shader.Fragment, (asset_Names' (1 => to_Asset ("assets/opengl/shader/version.header"),
|
||||
2 => to_Asset ("assets/opengl/shader/lighting-frag.snippet"),
|
||||
3 => to_Asset ("assets/opengl/shader/texturing-frag.snippet"),
|
||||
4 => to_Asset ("assets/opengl/shader/lit_textured.frag"))));
|
||||
the_Program.define ( vertex_Shader'Access,
|
||||
fragment_Shader'Access);
|
||||
the_Program.enable;
|
||||
|
||||
@@ -76,7 +76,7 @@ is
|
||||
|
||||
vertex_Shader .define (Shader.vertex, "assets/opengl/shader/textured.vert");
|
||||
fragment_Shader.define (Shader.Fragment, (asset_Names' (1 => to_Asset ("assets/opengl/shader/version.header"),
|
||||
2 => to_Asset ("assets/opengl/shader/texturing.frag"),
|
||||
2 => to_Asset ("assets/opengl/shader/texturing-frag.snippet"),
|
||||
3 => to_Asset ("assets/opengl/shader/textured.frag"))));
|
||||
the_Program := new openGL.Program.item;
|
||||
|
||||
|
||||
202
3-mid/opengl/source/lean/model/opengl-model-texturing.adb
Normal file
202
3-mid/opengl/source/lean/model/opengl-model-texturing.adb
Normal file
@@ -0,0 +1,202 @@
|
||||
with
|
||||
openGL.Model,
|
||||
GL.lean,
|
||||
GL.Binding,
|
||||
ada.Strings.fixed;
|
||||
|
||||
with ada.Text_IO;
|
||||
|
||||
|
||||
package body openGL.Model.texturing
|
||||
is
|
||||
use GL;
|
||||
|
||||
|
||||
type texture_Units is array (texture_Set.texture_Id) of GLenum;
|
||||
|
||||
all_texture_Units : constant texture_Units := (GL_TEXTURE0,
|
||||
GL_TEXTURE1,
|
||||
GL_TEXTURE2,
|
||||
GL_TEXTURE3,
|
||||
GL_TEXTURE4,
|
||||
GL_TEXTURE5,
|
||||
GL_TEXTURE6,
|
||||
GL_TEXTURE7,
|
||||
GL_TEXTURE8,
|
||||
GL_TEXTURE9,
|
||||
GL_TEXTURE10,
|
||||
GL_TEXTURE11,
|
||||
GL_TEXTURE12,
|
||||
GL_TEXTURE13,
|
||||
GL_TEXTURE14,
|
||||
GL_TEXTURE15,
|
||||
GL_TEXTURE16,
|
||||
GL_TEXTURE17,
|
||||
GL_TEXTURE18,
|
||||
GL_TEXTURE19,
|
||||
GL_TEXTURE20,
|
||||
GL_TEXTURE21,
|
||||
GL_TEXTURE22,
|
||||
GL_TEXTURE23,
|
||||
GL_TEXTURE24,
|
||||
GL_TEXTURE25,
|
||||
GL_TEXTURE26,
|
||||
GL_TEXTURE27,
|
||||
GL_TEXTURE28,
|
||||
GL_TEXTURE29,
|
||||
GL_TEXTURE30,
|
||||
GL_TEXTURE31);
|
||||
|
||||
|
||||
|
||||
|
||||
procedure enable (for_Model : in openGL.Model.view;
|
||||
Uniforms : in texturing.Uniforms;
|
||||
texture_Set : in openGL.texture_Set.Item)
|
||||
is
|
||||
use GL.Binding,
|
||||
GL.lean;
|
||||
|
||||
use type GLint;
|
||||
|
||||
begin
|
||||
for i in 1 .. openGL.texture_Set.texture_Id (for_Model.texture_Count)
|
||||
loop
|
||||
Uniforms.Textures (i).fade_Uniform.Value_is (Real (for_Model.Fade (i)));
|
||||
|
||||
glUniform1i (Uniforms.Textures (i).texture_Uniform.gl_Variable,
|
||||
GLint (i) - 1);
|
||||
glActiveTexture (all_texture_Units (i));
|
||||
glBindTexture (GL_TEXTURE_2D,
|
||||
texture_Set.Textures (i).Object.Name);
|
||||
end loop;
|
||||
|
||||
Uniforms.Count.Value_is (for_Model.texture_Count);
|
||||
end enable;
|
||||
|
||||
|
||||
|
||||
|
||||
procedure create (Uniforms : out texturing.Uniforms;
|
||||
for_Program : in openGL.Program.view)
|
||||
is
|
||||
begin
|
||||
for Id in texture_Set.texture_Id'Range
|
||||
loop
|
||||
declare
|
||||
use ada.Strings,
|
||||
ada.Strings.fixed;
|
||||
i : constant Positive := Positive (Id);
|
||||
texture_uniform_Name : constant String := "Textures[" & trim (Natural'Image (i - 1), Left) & "]";
|
||||
fade_uniform_Name : constant String := "Fade[" & trim (Natural'Image (i - 1), Left) & "]";
|
||||
begin
|
||||
Uniforms.Textures (Id).texture_Uniform := for_Program.uniform_Variable (named => texture_uniform_Name);
|
||||
Uniforms.Textures (Id). fade_Uniform := for_Program.uniform_Variable (named => fade_uniform_Name);
|
||||
end;
|
||||
end loop;
|
||||
|
||||
|
||||
Uniforms.Count := for_Program.uniform_Variable ("texture_Count");
|
||||
end create;
|
||||
|
||||
|
||||
|
||||
|
||||
-------------
|
||||
--- Mixin ---
|
||||
-------------
|
||||
|
||||
-- generic
|
||||
package body Mixin
|
||||
is
|
||||
use openGL.texture_Set;
|
||||
|
||||
|
||||
texture_Uniforms : texturing.Uniforms;
|
||||
|
||||
procedure create_Uniforms (for_Program : in openGL.Program.view)
|
||||
is
|
||||
begin
|
||||
create (texture_Uniforms, for_Program);
|
||||
end create_Uniforms;
|
||||
|
||||
|
||||
|
||||
overriding
|
||||
procedure Fade_is (Self : in out Item; Now : in texture_Set.fade_Level;
|
||||
Which : in texture_Set.texture_ID := 1)
|
||||
is
|
||||
begin
|
||||
Self.texture_Set.Textures (which).Fade := Now;
|
||||
end Fade_is;
|
||||
|
||||
|
||||
|
||||
overriding
|
||||
function Fade (Self : in Item; Which : in texture_Set.texture_ID := 1) return texture_Set.fade_Level
|
||||
is
|
||||
begin
|
||||
return Self.texture_Set.Textures (which).Fade;
|
||||
end Fade;
|
||||
|
||||
|
||||
|
||||
overriding
|
||||
procedure Texture_is (Self : in out Item; Now : in openGL.Texture.Object;
|
||||
Which : in texture_Set.texture_ID := 1)
|
||||
is
|
||||
begin
|
||||
Texture_is (in_Set => Self.texture_Set,
|
||||
Which => Which,
|
||||
Now => Now);
|
||||
end Texture_is;
|
||||
|
||||
|
||||
|
||||
overriding
|
||||
function Texture (Self : in Item; Which : texture_Set.texture_ID := 1) return openGL.Texture.Object
|
||||
is
|
||||
begin
|
||||
return openGL.texture_Set.Texture (in_Set => Self.texture_Set,
|
||||
Which => Which);
|
||||
end Texture;
|
||||
|
||||
|
||||
|
||||
-- overriding
|
||||
-- procedure Texture_is (Self : in out Item; Now : in openGL.Texture.Object)
|
||||
-- is
|
||||
-- begin
|
||||
-- Texture_is (in_Set => Self.texture_Set,
|
||||
-- Now => Now);
|
||||
-- end Texture_is;
|
||||
--
|
||||
--
|
||||
--
|
||||
-- overriding
|
||||
-- function Texture (Self : in Item) return openGL.Texture.Object
|
||||
-- is
|
||||
-- begin
|
||||
-- return texture_Set.Texture (in_Set => Self.texture_Set,
|
||||
-- Which => 1);
|
||||
-- end Texture;
|
||||
|
||||
|
||||
|
||||
overriding
|
||||
procedure enable_Textures (Self : in out Item)
|
||||
is
|
||||
begin
|
||||
-- ada.Text_IO.put_Line (Self.Model'Image);
|
||||
|
||||
texturing.enable (for_Model => Self.Model.all'Access,
|
||||
Uniforms => texture_Uniforms,
|
||||
texture_Set => Self.texture_Set);
|
||||
end enable_Textures;
|
||||
|
||||
|
||||
end Mixin;
|
||||
|
||||
|
||||
|
||||
end openGL.Model.texturing;
|
||||
103
3-mid/opengl/source/lean/model/opengl-model-texturing.ads
Normal file
103
3-mid/opengl/source/lean/model/opengl-model-texturing.ads
Normal file
@@ -0,0 +1,103 @@
|
||||
with
|
||||
openGL.Variable.uniform,
|
||||
openGL.texture_Set,
|
||||
openGL.Program;
|
||||
|
||||
limited
|
||||
with
|
||||
openGL.Model;
|
||||
|
||||
|
||||
private
|
||||
package openGL.Model.texturing
|
||||
--
|
||||
-- Provides texturing support for models.
|
||||
--
|
||||
is
|
||||
|
||||
--- Uniforms
|
||||
--
|
||||
|
||||
type texture_fade_Uniform_pair is
|
||||
record
|
||||
texture_Uniform : openGL.Variable.uniform.sampler2D;
|
||||
fade_Uniform : openGL.Variable.uniform.float;
|
||||
end record;
|
||||
|
||||
|
||||
type texture_fade_Uniform_pairs is array (openGL.texture_Set.texture_Id
|
||||
range 1 .. openGL.texture_Set.max_Textures) of texture_fade_Uniform_pair;
|
||||
|
||||
type Uniforms is
|
||||
record
|
||||
Textures : texture_fade_Uniform_pairs;
|
||||
Count : openGL.Variable.uniform.int;
|
||||
end record;
|
||||
|
||||
|
||||
|
||||
--- Operations
|
||||
--
|
||||
|
||||
procedure enable (for_Model : in openGL.Model.view;
|
||||
Uniforms : in texturing.Uniforms;
|
||||
texture_Set : in openGL.texture_Set.Item);
|
||||
|
||||
|
||||
|
||||
procedure create (Uniforms : out texturing.Uniforms;
|
||||
for_Program : in openGL.Program.view);
|
||||
|
||||
|
||||
|
||||
|
||||
-------------
|
||||
--- Mixin ---
|
||||
-------------
|
||||
|
||||
generic
|
||||
package Mixin
|
||||
is
|
||||
type Item is new Geometry.item with private;
|
||||
|
||||
|
||||
procedure create_Uniforms (for_Program : in openGL.Program.view);
|
||||
|
||||
|
||||
|
||||
overriding
|
||||
procedure Fade_is (Self : in out Item; Now : in texture_Set.fade_Level;
|
||||
Which : in texture_Set.texture_ID := 1);
|
||||
overriding
|
||||
function Fade (Self : in Item; Which : texture_Set.texture_ID := 1) return texture_Set.fade_Level;
|
||||
|
||||
|
||||
overriding
|
||||
procedure Texture_is (Self : in out Item; Now : in openGL.Texture.Object;
|
||||
Which : in texture_Set.texture_ID := 1);
|
||||
overriding
|
||||
function Texture (Self : in Item; Which : in texture_Set.texture_ID := 1) return openGL.Texture.Object;
|
||||
|
||||
-- overriding
|
||||
-- procedure Texture_is (Self : in out Item; Now : in openGL.Texture.Object);
|
||||
--
|
||||
-- overriding
|
||||
-- function Texture (Self : in Item) return openGL.Texture.Object;
|
||||
|
||||
|
||||
overriding
|
||||
procedure enable_Textures (Self : in out Item);
|
||||
|
||||
|
||||
private
|
||||
|
||||
type Item is new Geometry.item with
|
||||
record
|
||||
texture_Set : openGL.texture_Set.item;
|
||||
end record;
|
||||
|
||||
end Mixin;
|
||||
|
||||
|
||||
|
||||
end openGL.Model.texturing;
|
||||
Reference in New Issue
Block a user