#version 410 core

uniform sampler2D diffuse;
uniform sampler2D specular;
uniform sampler2D bump;

in vec2 pos_zw;
in vec3 normal;
in vec3 ec_pos;
in vec3 uv_shade;

#include <deferred/gbuffer_write_include.frag>

#include <deferred/tangentspace_include.frag>

void main()
{
	vec3 tangent, binormal;
	per_fragment_tangent_space(ec_pos, uv_shade.xy, tangent, binormal);
	
	vec3 baseNormal = normalize(normal);
	vec3 b = texture(bump, uv_shade.xy).xyz*2.0 - vec3(1.0);
	mat3 mr = mat3(tangent, binormal, baseNormal);
	vec3 n = normalize(mr*b);

	diffuse_specular.rgb = texture(diffuse, uv_shade.xy).rgb * uv_shade.z;
	diffuse_specular.a = texture(specular, uv_shade.xy).r;

	normal_depth.xyz = n;
	normal_depth.w = pos_zw.x/pos_zw.y;
}
