Compare commits

...

2 Commits

Author SHA1 Message Date
alistair 18cb4f522b edge detect 12 months ago
alistair ae92911efc fix ubo typename 12 months ago
  1. 52
      assets/shaders/deferred_lighting.frag
  2. 9
      assets/shaders/g_frag.gl
  3. 2
      assets/shaders/screen.frag
  4. 4
      assets/shaders/write/material_properties.gl
  5. 5
      assets/shaders/write/ubo_info.gl
  6. 22
      source/uniform-buffer.hpp

52
assets/shaders/deferred_lighting.frag

@ -51,6 +51,36 @@ float attenuation_val(float distance) { @@ -51,6 +51,36 @@ float attenuation_val(float distance) {
attenuation_quadratic * (distance * distance));
}
vec3 apply_kernel(float kernel[9], sampler2D tex) {
const float offset = 1.0 / 300.0;
vec2 offsets[9] = vec2[](
vec2(-offset, offset), // top-left
vec2( 0.0f, offset), // top-center
vec2( offset, offset), // top-right
vec2(-offset, 0.0f), // center-left
vec2( 0.0f, 0.0f), // center-center
vec2( offset, 0.0f), // center-right
vec2(-offset, -offset), // bottom-left
vec2( 0.0f, -offset), // bottom-center
vec2( offset, -offset) // bottom-right
);
vec3 sampleTex[9];
for(int i = 0; i < 9; i++)
{
sampleTex[i] = vec3(texture(tex, TexCoords.st + offsets[i]).rgb);
}
vec3 col = vec3(0.0);
for(int i = 0; i < 9; i++)
col += sampleTex[i] * kernel[i];
return col;
}
void main() {
vec3 FragPos = texture(gPosition, TexCoords).rgb;
vec3 Normal = texture(gNormal, TexCoords).rgb;
@ -61,6 +91,21 @@ void main() { @@ -61,6 +91,21 @@ void main() {
float attenuation_linear = 0.09;
float attenuation_quadratic = 0.032;
float sharpen_kernel[9] = float[](
-1, -1, -1,
-1, 9, -1,
-1, -1, -1
);
float edge_detect[9] = float[](
1, 1, 1,
1, -8, 1,
1, 1, 1
);
vec3 edge = apply_kernel(edge_detect, gAlbedoSpec);
if (debug_shader == 1) {
FragColor = vec4(Normal,1.0);
return;
@ -124,6 +169,13 @@ if (enable_fog) { @@ -124,6 +169,13 @@ if (enable_fog) {
lighting = clamp(lighting, vec3(0), vec3(1));
}
if (length(edge) > 0.1) {
edge = vec3(1);
} else {
edge = vec3(0);
}
FragColor = vec4(lighting, 1.0);
}

9
assets/shaders/g_frag.gl

@ -78,6 +78,7 @@ uniform Material material; @@ -78,6 +78,7 @@ uniform Material material;
// https://gamedev.stackexchange.com/questions/92015/optimized-linear-to-srgb-glsl
// Converts a color from sRGB gamma to linear light gamma
// should really preprocess so textures are loaded in linear space
vec3 toLinear(vec3 sRGB)
{
bvec3 cutoff = lessThan(sRGB, vec3(0.04045));
@ -100,7 +101,6 @@ main() { @@ -100,7 +101,6 @@ main() {
} else {
gNormal = normalize(TBN * normal);
}
gNormal = Normal;
// and the diffuse per-fragment color
vec4 tex = texture(material.texture_diffuse1, theTexCoord);
@ -109,5 +109,10 @@ main() { @@ -109,5 +109,10 @@ main() {
discard;
gAlbedoSpec.rgb = toLinear(tex.rgb); // store specular intensity in gAlbedoSpec's alpha component
gAlbedoSpec.a = texture(material.texture_specular1, theTexCoord).r;
// linear depth
float near = 0.1;
float far = 1000;
float ndc = gl_FragCoord.z * 2.0 - 1.0;
float linearDepth = (2.0 * near * far) / (far + near - ndc * (far - near));
gAlbedoSpec.a = linearDepth / far; // texture(material.texture_specular1, theTexCoord).r;
}

2
assets/shaders/screen.frag

@ -26,6 +26,8 @@ layout (std140) uniform material_properties @@ -26,6 +26,8 @@ layout (std140) uniform material_properties
void main()
{
vec3 hdrcol = texture(screenTexture, TexCoords).rgb;
if (use_hdr) {
vec3 mapped = vec3(1.0) - exp((-hdrcol) * exposure);

4
assets/shaders/write/material_properties.gl

@ -7,6 +7,6 @@ layout (std140) uniform ubo_info @@ -7,6 +7,6 @@ layout (std140) uniform ubo_info
mat4 projection; // 16 144
vec4 offset; // 16 208
vec4 colour; // 16 224
signed int debug_shader; // 4 240
signed int instancing_enabled; // 4 244
int debug_shader; // 4 240
int instancing_enabled; // 4 244
};

5
assets/shaders/write/ubo_info.gl

@ -7,7 +7,8 @@ layout (std140) uniform ubo_info @@ -7,7 +7,8 @@ layout (std140) uniform ubo_info
mat4 projection; // 16 144
vec4 offset; // 16 208
vec4 colour; // 16 224
signed int debug_shader; // 4 240
signed int instancing_enabled; // 4 244
int debug_shader; // 4 240
int instancing_enabled; // 4 244
}; 4 244
};244
};

22
source/uniform-buffer.hpp

@ -121,7 +121,7 @@ class ubo_proxy : public refl::runtime::proxy<ubo_proxy<T>, T> { @@ -121,7 +121,7 @@ class ubo_proxy : public refl::runtime::proxy<ubo_proxy<T>, T> {
struct gl_type_hint {
size_t alignment;
std::string name;
const std::string name;
bool is_array = false;
size_t n_elems;
size_t elem_size;
@ -214,6 +214,26 @@ private: @@ -214,6 +214,26 @@ private:
return type_hint;
}
template <> static constexpr gl_type_hint get_alignment<GLuint>() {
return {4, "uint"};
}
template <> static constexpr gl_type_hint get_alignment<GLint>() {
return {4, "int"};
}
template <> static constexpr gl_type_hint get_alignment<int>() {
return {4, "int"};
}
template <> static constexpr gl_type_hint get_alignment<unsigned int>() {
return {4, "uint"};
}
template <> static constexpr gl_type_hint get_alignment<GLboolean>() {
return {4, "bool"};
}
template <> static constexpr gl_type_hint get_alignment<glm::vec4>() {
return {4 * 4, "vec4"};
}

Loading…
Cancel
Save