SparrowRenderer/shaders/gbuffer.frag.glsl
2016-06-24 18:33:53 +02:00

84 lines
1.8 KiB
GLSL

// - Normal buffer
layout (location = 0) out vec3 outNormal;
// - Color + objectId buffer
layout (location = 1) out vec4 outColor;
// - Specular color + Specular exponent buffer
layout (location = 2) out vec4 outSpecular;
// - Position in view space
layout (location = 3) out vec4 outPosition;
uniform float materialNs;
// variables used for picking
uniform unsigned int object_identifier;
#ifdef INSTANCED
flat in int instanceId;
#endif
#ifdef ALPHA_MASK
uniform sampler2D alphaMask;
#endif
#ifdef DIFFUSE_TEXTURE
uniform sampler2D diffuseTexture;
#else
uniform vec3 materialKd;
#endif
#ifdef SPECULAR_TEXTURE
uniform sampler2D specularTexture;
#else
uniform vec3 materialKs;
#endif
in vec4 posInView;
#ifdef NORMAL_MAP
uniform sampler2D normalMap;
in vec3 varTangent;
in vec3 varBinormal;
#endif
in vec3 varNormal;
in vec2 varTexCoord;
void main()
{
#ifdef ALPHA_MASK
if(texture(alphaMask, varTexCoord).r < 0.5)
discard;
#endif
#ifdef NORMAL_MAP
vec3 normalTexel = normalize(texture(normalMap, varTexCoord).xyz -0.5);
vec3 tangent = normalize(varTangent);
vec3 binormal = normalize(varBinormal);
vec3 normal = normalize(varNormal);
mat3 tangentSpace = mat3(vec3(tangent.x, binormal.x, normal.x),
vec3(tangent.y, binormal.y, normal.y),
vec3(tangent.z, binormal.z, normal.z));
outNormal = normalize(normalTexel * tangentSpace);
#else
outNormal = normalize(varNormal);
#endif
#ifdef DIFFUSE_TEXTURE
outColor.rgb = texture(diffuseTexture, varTexCoord).rgb;
#else
outColor.rgb = materialKd;
#endif
outColor.a = float(object_identifier)/255;
#ifdef SPECULAR_TEXTURE
outSpecular.rgb = texture(specularTexture, varTexCoord).rgb;
#else
outSpecular.rgb = materialKs;
#endif
outSpecular.w = float(materialNs)/255;
outPosition = posInView;
}