16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-19 13:29:18 +02:00

shader changes

This commit is contained in:
milek7
2018-07-19 18:13:07 +02:00
parent 8cac3d4cc9
commit a729c5fa1d
12 changed files with 258 additions and 298 deletions

View File

@@ -5,6 +5,7 @@ namespace gl
const std::string glsl_common =
"const uint MAX_LIGHTS = " + std::to_string(MAX_LIGHTS) + "U;\n" +
"const uint MAX_PARAMS = " + std::to_string(MAX_PARAMS) + "U;\n" +
"const uint ENVMAP_SIZE = " + std::to_string(ENVMAP_SIZE) + "U;\n" +
R"STRING(
const uint LIGHT_SPOT = 0U;
const uint LIGHT_POINT = 1U;

View File

@@ -65,7 +65,7 @@ std::unordered_map<std::string, gl::shader::defaultparam_e> gl::shader::defaultp
{
{ "required", defaultparam_e::required },
{ "nan", defaultparam_e::nan },
{ "none", defaultparam_e::zero },
{ "zero", defaultparam_e::zero },
{ "one", defaultparam_e::one },
{ "ambient", defaultparam_e::ambient },
{ "diffuse", defaultparam_e::diffuse },

View File

@@ -32,6 +32,7 @@ namespace gl
#pragma pack(push, 1)
const size_t MAX_TEXTURES = 8;
const size_t ENVMAP_SIZE = 1024;
struct scene_ubs
{

View File

@@ -25,7 +25,6 @@ opengl_renderer GfxRenderer;
extern TWorld World;
int const EU07_PICKBUFFERSIZE{1024}; // size of (square) textures bound with the pick framebuffer
int const EU07_ENVIRONMENTBUFFERSIZE{256}; // size of (square) environmental cube map texture
void opengl_light::apply_intensity(float const Factor)
{
@@ -222,9 +221,9 @@ bool opengl_renderer::Init(GLFWwindow *Window)
return false;
m_env_rb = std::make_unique<gl::renderbuffer>();
m_env_rb->alloc(GL_DEPTH_COMPONENT24, EU07_ENVIRONMENTBUFFERSIZE, EU07_ENVIRONMENTBUFFERSIZE);
m_env_rb->alloc(GL_DEPTH_COMPONENT24, gl::ENVMAP_SIZE, gl::ENVMAP_SIZE);
m_env_tex = std::make_unique<gl::cubemap>();
m_env_tex->alloc(GL_RGB16F, EU07_ENVIRONMENTBUFFERSIZE, EU07_ENVIRONMENTBUFFERSIZE, GL_RGB, GL_FLOAT);
m_env_tex->alloc(GL_RGB16F, gl::ENVMAP_SIZE, gl::ENVMAP_SIZE, GL_RGB, GL_FLOAT);
m_empty_cubemap = std::make_unique<gl::cubemap>();
m_empty_cubemap->alloc(GL_RGB16F, 16, 16, GL_RGB, GL_FLOAT);
@@ -451,9 +450,8 @@ void opengl_renderer::Render_pass(rendermode const Mode)
glDebug("rendermode::shadows");
// bias (TBD: here or in glsl?)
// glEnable(GL_POLYGON_OFFSET_FILL);
// glPolygonOffset(1.0f, 1.0f);
glPolygonOffset(1.0f, 1.0f);
glEnable(GL_POLYGON_OFFSET_FILL);
glEnable(GL_DEPTH_TEST);
@@ -474,7 +472,7 @@ void opengl_renderer::Render_pass(rendermode const Mode)
m_shadowpass = m_renderpass;
// glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_POLYGON_OFFSET_FILL);
m_shadow_fb->unbind();
@@ -487,9 +485,8 @@ void opengl_renderer::Render_pass(rendermode const Mode)
{
glDebug("rendermode::cabshadows");
// bias (TBD: here or in glsl?)
// glEnable(GL_POLYGON_OFFSET_FILL);
// glPolygonOffset(1.0f, 1.0f);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
@@ -507,7 +504,7 @@ void opengl_renderer::Render_pass(rendermode const Mode)
Render_cab(World.Train->Dynamic(), true);
m_cabshadowpass = m_renderpass;
// glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_POLYGON_OFFSET_FILL);
m_cabshadows_fb->unbind();
@@ -623,7 +620,7 @@ bool opengl_renderer::Render_reflections()
m_environmentupdatetime = timestamp;
m_environmentupdatelocation = m_renderpass.camera.position();
glViewport(0, 0, EU07_ENVIRONMENTBUFFERSIZE, EU07_ENVIRONMENTBUFFERSIZE);
glViewport(0, 0, gl::ENVMAP_SIZE, gl::ENVMAP_SIZE);
for (m_environmentcubetextureface = 0; m_environmentcubetextureface < 6; ++m_environmentcubetextureface)
{
m_env_fb->attach(*m_env_tex, m_environmentcubetextureface, GL_COLOR_ATTACHMENT0);

79
shaders/light_common.glsl Normal file
View File

@@ -0,0 +1,79 @@
float calc_shadow()
{
vec3 coords = f_light_pos.xyz / f_light_pos.w;
if (coords.z > 1.0f)
return 1.0f;
float bias = 0.0f;
//sampler PCF
//float shadow = texture(shadowmap, vec3(coords.xy, coords.z - bias));
//sampler PCF + PCF
float shadow = 0.0;
vec2 texel = 1.0 / textureSize(shadowmap, 0);
for (float y = -1.5; y <= 1.5; y += 1.0)
for (float x = -1.5; x <= 1.5; x += 1.0)
shadow += texture(shadowmap, coords.xyz + vec3(vec2(x, y) * texel, -bias));
shadow /= 16.0;
return shadow;
}
vec3 apply_fog(vec3 color)
{
float sun_amount = 0.0;
if (lights_count >= 1U && lights[0].type == LIGHT_DIR)
sun_amount = max(dot(normalize(f_pos), normalize(-lights[0].dir)), 0.0);
vec3 fog_color_v = mix(fog_color, lights[0].color, pow(sun_amount, 30.0));
float fog_amount_v = 1.0 - min(1.0, exp(-length(f_pos) * fog_density));
return mix(color, fog_color_v, fog_amount_v);
}
// [0] - diffuse, [1] - specular
// do magic here
vec2 calc_light(vec3 light_dir)
{
#ifdef NORMALMAP
vec3 normal = f_tbn * normalize(texture(normalmap, f_coord).rgb * 2.0 - 1.0);
#else
vec3 normal = normalize(f_normal);
#endif
vec3 view_dir = normalize(vec3(0.0f, 0.0f, 0.0f) - f_pos);
vec3 halfway_dir = normalize(light_dir + view_dir);
float diffuse_v = max(dot(normal, light_dir), 0.0);
float specular_v = pow(max(dot(normal, halfway_dir), 0.0), 15.0);
return vec2(diffuse_v, specular_v);
}
vec2 calc_point_light(light_s light)
{
vec3 light_dir = normalize(light.pos - f_pos);
vec2 val = calc_light(light_dir);
float distance = length(light.pos - f_pos);
float atten = 1.0f / (1.0f + light.linear * distance + light.quadratic * (distance * distance));
return val * atten;
}
vec2 calc_spot_light(light_s light)
{
vec3 light_dir = normalize(light.pos - f_pos);
float theta = dot(light_dir, normalize(-light.dir));
float epsilon = light.in_cutoff - light.out_cutoff;
float intensity = clamp((theta - light.out_cutoff) / epsilon, 0.0, 1.0);
vec2 point = calc_point_light(light);
return point * intensity;
}
vec2 calc_dir_light(light_s light)
{
vec3 light_dir = normalize(-light.dir);
return calc_light(light_dir);
}

View File

@@ -8,103 +8,36 @@ in vec4 f_light_pos;
#include <common>
#param (color, 0, 0, 4, diffuse)
#param (diffuse, 1, 0, 1, one)
#param (diffuse, 1, 0, 1, diffuse)
#param (specular, 1, 1, 1, specular)
#param (reflection, 1, 2, 1, zero)
uniform sampler2DShadow shadowmap;
uniform samplerCube envmap;
float calc_shadow()
{
vec3 coords = f_light_pos.xyz / f_light_pos.w;
// do something better
float bias = 0.0001f;
//sampler PCF
//float shadow = texture(shadowmap, vec3(coords.xy, coords.z - bias));
//sampler PCF + PCF
float shadow = 0.0;
vec2 texel = 1.0 / textureSize(shadowmap, 0);
for (float y = -1.5; y <= 1.5; y += 1.0)
for (float x = -1.5; x <= 1.5; x += 1.0)
shadow += texture(shadowmap, coords.xyz + vec3(vec2(x, y) * texel, -bias));
shadow /= 16.0;
if (coords.z > 1.0f)
shadow = 1.0f;
return shadow;
}
vec3 apply_fog(vec3 color)
{
float sun_amount = 0.0;
if (lights_count >= 1U && lights[0].type == LIGHT_DIR)
sun_amount = max(dot(normalize(f_pos), normalize(-lights[0].dir)), 0.0);
vec3 fog_color_v = mix(fog_color, lights[0].color, pow(sun_amount, 30.0));
float fog_amount_v = 1.0 - min(1.0, exp(-length(f_pos) * fog_density));
return mix(color, fog_color_v, fog_amount_v);
}
float calc_light(vec3 light_dir)
{
vec3 normal = normalize(f_normal);
vec3 view_dir = normalize(vec3(0.0f, 0.0f, 0.0f) - f_pos);
vec3 halfway_dir = normalize(light_dir + view_dir);
float diffuse_v = max(dot(normal, light_dir), 0.0);
float specular_v = pow(max(dot(normal, halfway_dir), 0.0), 15.0);
//return specular_v * param[1].y + diffuse_v * param[1].x;
return specular_v + diffuse_v;
}
float calc_point_light(light_s light)
{
vec3 light_dir = normalize(light.pos - f_pos);
float val = calc_light(light_dir);
float distance = length(light.pos - f_pos);
float atten = 1.0f / (1.0f + light.linear * distance + light.quadratic * (distance * distance));
return val * atten;
}
float calc_spot_light(light_s light)
{
vec3 light_dir = normalize(light.pos - f_pos);
float theta = dot(light_dir, normalize(-light.dir));
float epsilon = light.in_cutoff - light.out_cutoff;
float intensity = clamp((theta - light.out_cutoff) / epsilon, 0.0, 1.0);
float point = calc_point_light(light);
return point * intensity;
}
float calc_dir_light(light_s light)
{
vec3 light_dir = normalize(-light.dir);
return calc_light(light_dir);
}
#include <light_common.glsl>
void main()
{
vec4 tex_color = vec4(param[0].rgb, 1.0f);
if (tex_color.a < opacity)
discard;
vec3 normal = normalize(f_normal);
vec3 refvec = reflect(f_pos, normal);
vec3 envcolor = texture(envmap, refvec).rgb;
vec3 envcolor = texture(envmap, reflect(f_pos, normalize(f_normal))).rgb;
vec3 result = ambient * 0.5 + param[0].rgb * emission;
float shadow = calc_shadow();
vec3 result = ambient * 0.3 + vec3(1.0) * emission;
for (uint i = 0U; i < lights_count; i++)
if (lights_count > 0U)
{
vec2 part = calc_dir_light(lights[0]);
vec3 c = (part.x * param[1].x + part.y * param[1].y) * calc_shadow() * lights[0].color;
result += mix(c, envcolor, param[1].z);
}
for (uint i = 1U; i < lights_count; i++)
{
light_s light = lights[i];
float part = 0.0;
vec2 part = vec2(0.0);
if (light.type == LIGHT_SPOT)
part = calc_spot_light(light);
@@ -113,11 +46,8 @@ void main()
else if (light.type == LIGHT_DIR)
part = calc_dir_light(light);
if (i == 0U)
part *= shadow;
result += light.color * part;// * envcolor;
result += light.color * (part.x * param[1].x + part.y * param[1].y);
}
vec3 c = apply_fog(result * tex_color.xyz);
gl_FragColor = vec4(c, tex_color.w);
gl_FragColor = vec4(apply_fog(result * tex_color.rgb), tex_color.a);
}

View File

@@ -8,106 +8,42 @@ in vec4 f_light_pos;
#include <common>
#param (color, 0, 0, 4, diffuse)
#param (diffuse, 1, 0, 1, one)
#param (diffuse, 1, 0, 1, diffuse)
#param (specular, 1, 1, 1, specular)
#param (reflection, 1, 2, 1, zero)
#texture (diffuse, 0, sRGB_A)
uniform sampler2D tex1;
uniform sampler2D diffuse;
uniform sampler2DShadow shadowmap;
uniform samplerCube envmap;
float calc_shadow()
{
vec3 coords = f_light_pos.xyz / f_light_pos.w;
// do something better
float bias = 0.0001f;
//sampler PCF
//float shadow = texture(shadowmap, vec3(coords.xy, coords.z - bias));
//sampler PCF + PCF
float shadow = 0.0;
vec2 texel = 1.0 / textureSize(shadowmap, 0);
for (float y = -1.5; y <= 1.5; y += 1.0)
for (float x = -1.5; x <= 1.5; x += 1.0)
shadow += texture(shadowmap, coords.xyz + vec3(vec2(x, y) * texel, -bias));
shadow /= 16.0;
if (coords.z > 1.0f)
shadow = 1.0f;
return shadow;
}
vec3 apply_fog(vec3 color)
{
float sun_amount = 0.0;
if (lights_count >= 1U && lights[0].type == LIGHT_DIR)
sun_amount = max(dot(normalize(f_pos), normalize(-lights[0].dir)), 0.0);
vec3 fog_color_v = mix(fog_color, lights[0].color, pow(sun_amount, 30.0));
float fog_amount_v = 1.0 - min(1.0, exp(-length(f_pos) * fog_density));
return mix(color, fog_color_v, fog_amount_v);
}
float calc_light(vec3 light_dir)
{
vec3 normal = normalize(f_normal);
vec3 view_dir = normalize(vec3(0.0f, 0.0f, 0.0f) - f_pos);
vec3 halfway_dir = normalize(light_dir + view_dir);
float diffuse_v = max(dot(normal, light_dir), 0.0);
float specular_v = pow(max(dot(normal, halfway_dir), 0.0), 15.0);
//return specular_v * param[1].y + diffuse_v * param[1].x;
return specular_v + diffuse_v;
}
float calc_point_light(light_s light)
{
vec3 light_dir = normalize(light.pos - f_pos);
float val = calc_light(light_dir);
float distance = length(light.pos - f_pos);
float atten = 1.0f / (1.0f + light.linear * distance + light.quadratic * (distance * distance));
return val * atten;
}
float calc_spot_light(light_s light)
{
vec3 light_dir = normalize(light.pos - f_pos);
float theta = dot(light_dir, normalize(-light.dir));
float epsilon = light.in_cutoff - light.out_cutoff;
float intensity = clamp((theta - light.out_cutoff) / epsilon, 0.0, 1.0);
float point = calc_point_light(light);
return point * intensity;
}
float calc_dir_light(light_s light)
{
vec3 light_dir = normalize(-light.dir);
return calc_light(light_dir);
}
#include <light_common.glsl>
void main()
{
vec4 tex_color = texture(tex1, f_coord);
vec4 tex_color = texture(diffuse, f_coord);
if (tex_color.a < opacity)
discard;
vec3 envcolor = texture(envmap, reflect(f_pos, normalize(f_normal))).rgb;
vec3 normal = normalize(f_normal);
vec3 refvec = reflect(f_pos, normal);
vec3 envcolor = texture(envmap, refvec).rgb;
float shadow = calc_shadow();
vec3 result = ambient * 0.3 + vec3(1.0) * emission;
for (uint i = 0U; i < lights_count; i++)
vec3 result = ambient * 0.5 + param[0].rgb * emission;
if (lights_count > 0U)
{
vec2 part = calc_dir_light(lights[0]);
vec3 c = (part.x * param[1].x + part.y * param[1].y) * calc_shadow() * lights[0].color;
result += mix(c, envcolor, param[1].z);
}
for (uint i = 1U; i < lights_count; i++)
{
light_s light = lights[i];
float part = 0.0;
vec2 part = vec2(0.0);
if (light.type == LIGHT_SPOT)
part = calc_spot_light(light);
@@ -116,11 +52,8 @@ void main()
else if (light.type == LIGHT_DIR)
part = calc_dir_light(light);
if (i == 0U)
part *= shadow;
result += light.color * part;// * envcolor;
result += light.color * (part.x * param[1].x + part.y * param[1].y);
}
vec3 c = apply_fog(result * tex_color.xyz);
gl_FragColor = vec4(c, tex_color.w);
gl_FragColor = vec4(apply_fog(result * tex_color.rgb), tex_color.a);
}

View File

@@ -1 +1 @@
#include <mat_default.frag>
#include <mat_reflmap.frag>

View File

@@ -1,98 +0,0 @@
#version 330
in vec3 f_normal;
in vec2 f_coord;
in vec3 f_pos;
in mat3 f_tbn;
in vec4 f_tangent;
#texture (tex1, 0, sRGB_A)
uniform sampler2D tex1;
#texture (tex2, 1, RGB)
uniform sampler2D tex2;
#param(color, 0, 0, 1, one)
#param(color2, 0, 1, 1, zero)
#param(color3, 0, 2, 1, one)
#include <common>
vec3 apply_fog(vec3 color)
{
float sun_amount = 0.0;
if (lights_count >= 1U && lights[0].type == LIGHT_DIR)
sun_amount = max(dot(normalize(f_pos), normalize(-lights[0].dir)), 0.0);
vec3 fog_color_v = mix(fog_color, lights[0].color, pow(sun_amount, 30.0));
float fog_amount_v = 1.0 - min(1.0, exp(-length(f_pos) * fog_density));
return mix(color, fog_color_v, fog_amount_v);
}
float calc_light(vec3 light_dir)
{
//vec3 normal = normalize(f_normal);
vec3 normal = f_tbn * normalize(texture(tex2, f_coord).rgb * 2.0 - 1.0);
vec3 view_dir = normalize(vec3(0.0f, 0.0f, 0.0f) - f_pos);
vec3 halfway_dir = normalize(light_dir + view_dir);
float diffuse_v = max(dot(normal, light_dir), 0.0);
float specular_v = pow(max(dot(normal, halfway_dir), 0.0), 15.0) * 0.2;
return specular_v + diffuse_v;
}
float calc_point_light(light_s light)
{
vec3 light_dir = normalize(light.pos - f_pos);
float val = calc_light(light_dir);
float distance = length(light.pos - f_pos);
float atten = 1.0f / (1.0f + light.linear * distance + light.quadratic * (distance * distance));
return val * atten;
}
float calc_spot_light(light_s light)
{
vec3 light_dir = normalize(light.pos - f_pos);
float theta = dot(light_dir, normalize(-light.dir));
float epsilon = light.in_cutoff - light.out_cutoff;
float intensity = clamp((theta - light.out_cutoff) / epsilon, 0.0, 1.0);
float point = calc_point_light(light);
return point * intensity;
}
float calc_dir_light(light_s light)
{
vec3 light_dir = normalize(-light.dir);
return calc_light(light_dir);
}
void main()
{
vec3 result = ambient * 0.3 + param[0].rgb * emission;
for (uint i = 0U; i < lights_count; i++)
{
light_s light = lights[i];
float part = 0.0;
if (light.type == LIGHT_SPOT)
part = calc_spot_light(light);
else if (light.type == LIGHT_POINT)
part = calc_point_light(light);
else if (light.type == LIGHT_DIR)
part = calc_dir_light(light);
result += light.color * part;
}
vec4 tex_color = texture(tex1, f_coord);
vec3 c = apply_fog(result * tex_color.xyz);
gl_FragColor = vec4(c, tex_color.w);
// gl_FragColor = vec4(f_tangent.xyz, 1.0f);
}

View File

@@ -0,0 +1,64 @@
#version 330
in vec3 f_normal;
in vec2 f_coord;
in vec3 f_pos;
in mat3 f_tbn;
in vec4 f_light_pos;
#include <common>
#param (color, 0, 0, 4, diffuse)
#param (diffuse, 1, 0, 1, diffuse)
#param (specular, 1, 1, 1, specular)
#param (reflection, 1, 2, 1, zero)
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;
#texture (normalmap, 1, RGB)
uniform sampler2D normalmap;
uniform sampler2DShadow shadowmap;
uniform samplerCube envmap;
#define NORMALMAP
#include <light_common.glsl>
void main()
{
vec4 tex_color = texture(diffuse, f_coord);
if (tex_color.a < opacity)
discard;
vec3 normal = normalize(f_normal);
vec3 refvec = reflect(f_pos, normal);
vec3 envcolor = texture(envmap, refvec).rgb;
vec3 result = ambient * 0.5 + param[0].rgb * emission;
if (lights_count > 0U)
{
vec2 part = calc_dir_light(lights[0]);
vec3 c = (part.x * param[1].x + part.y * param[1].y) * calc_shadow() * lights[0].color;
result += mix(c, envcolor, param[1].z);
}
for (uint i = 1U; i < lights_count; i++)
{
light_s light = lights[i];
vec2 part = vec2(0.0);
if (light.type == LIGHT_SPOT)
part = calc_spot_light(light);
else if (light.type == LIGHT_POINT)
part = calc_point_light(light);
else if (light.type == LIGHT_DIR)
part = calc_dir_light(light);
result += light.color * (part.x * param[1].x + part.y * param[1].y);
}
gl_FragColor = vec4(apply_fog(result * tex_color.rgb), tex_color.a);
}

62
shaders/mat_reflmap.frag Normal file
View File

@@ -0,0 +1,62 @@
#version 330
in vec3 f_normal;
in vec2 f_coord;
in vec3 f_pos;
in vec4 f_light_pos;
#include <common>
#param (color, 0, 0, 4, diffuse)
#param (diffuse, 1, 0, 1, diffuse)
#param (specular, 1, 1, 1, specular)
#param (reflection, 1, 2, 1, one)
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;
#texture (reflmap, 1, R)
uniform sampler2D reflmap;
uniform sampler2DShadow shadowmap;
uniform samplerCube envmap;
#include <light_common.glsl>
void main()
{
vec4 tex_color = texture(diffuse, f_coord);
if (tex_color.a < opacity)
discard;
vec3 normal = normalize(f_normal);
vec3 refvec = reflect(f_pos, normal);
vec3 envcolor = texture(envmap, refvec).rgb;
vec3 result = ambient * 0.5 + param[0].rgb * emission;
if (lights_count > 0U)
{
vec2 part = calc_dir_light(lights[0]);
vec3 c = (part.x * param[1].x + part.y * param[1].y) * calc_shadow() * lights[0].color;
result += mix(c, envcolor, param[1].z * texture(reflmap, f_coord).r);
}
for (uint i = 1U; i < lights_count; i++)
{
light_s light = lights[i];
vec2 part = vec2(0.0);
if (light.type == LIGHT_SPOT)
part = calc_spot_light(light);
else if (light.type == LIGHT_POINT)
part = calc_point_light(light);
else if (light.type == LIGHT_DIR)
part = calc_dir_light(light);
result += light.color * (part.x * param[1].x + part.y * param[1].y);
}
gl_FragColor = vec4(apply_fog(result * tex_color.rgb), tex_color.a);
}

View File

@@ -42,17 +42,8 @@ vec3 filmic(vec3 x)
void main()
{
vec2 texcoord = f_coords;
// float x = texcoord.x;
// texcoord.x += sin(texcoord.y * 4*2*3.14159 + 0) / 100;
// texcoord.y += sin(x * 4*2*3.14159 + 0) / 100;
vec3 hdr_color = texture(tex1, texcoord).xyz;
vec3 mapped;
//if (texcoord.x < 0.33f)
// mapped = reinhard(hdr_color);
//else if (texcoord.x < 0.66f)
// mapped = filmic(hdr_color);
//else
mapped = ACESFilm(hdr_color);
vec3 mapped= ACESFilm(hdr_color);
gl_FragColor = vec4(mapped, 1.0);
}