Skip to content

Instantly share code, notes, and snippets.

@feliwir
Created June 23, 2016 09:08
Show Gist options
  • Save feliwir/aa538736d8cecd69cc910df2f5ac06f2 to your computer and use it in GitHub Desktop.
Save feliwir/aa538736d8cecd69cc910df2f5ac06f2 to your computer and use it in GitHub Desktop.
#version 400 core
in vec3 position;
in vec2 uv;
in vec3 normal;
in vec3 material;
in mat3 TBN;
uniform vec3 cameraPos;
uniform vec3 lightDir;
uniform sampler2DArray albedoTex;
uniform sampler2DArray normalTex;
uniform sampler2DArray specularTex;
uniform sampler2DArray ambientTex;
out vec4 color;
void main()
{
vec3 albedo = texture(albedoTex, vec3(uv, material[0])).rgb;
float ambient = texture(ambientTex, vec3(uv, material[0])).r;
float spec = texture(specularTex, vec3(uv, material[0])).r;
vec3 texNormal = texture(normalTex, vec3(uv, material[0])).rgb;
albedo = mix(albedo, texture(albedoTex, vec3(uv, material[1])).rgb, material[2]);
ambient = mix(ambient,texture(ambientTex, vec3(uv, material[1])).r, material[2]);
spec = mix(spec, texture(specularTex, vec3(uv, material[1])).r , material[2]);
texNormal = mix(texNormal, texture(normalTex, vec3(uv, material[1])).rgb, material[2]);
texNormal = normalize(texNormal*2.0-1.0);
texNormal = texNormal*TBN;
vec3 viewDir = normalize(cameraPos - position);
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(texNormal, halfwayDir), 0.0), spec);
color = vec4(ambient*albedo+spec,1.0);
}
#version 400 core
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in vec3 e_position[];
in vec2 e_uv[];
in vec3 e_normal[];
in vec3 e_material[];
layout (std140) uniform matrix_block
{
mat4 vp;
mat3 v3x3;
};
out vec3 position;
out vec2 uv;
out vec3 normal;
out vec3 material;
out mat3 TBN;
void calculateLightingData(int i)
{
//calculate tangents and binormals
vec3 vertexTangent_modelspace;
vec3 vertexBitangent_modelspace;
vec3 c1 = cross(normal, vec3(0.0, 0.0, 1.0));
vec3 c2 = cross(normal, vec3(0.0, 1.0, 0.0));
if (length(c1) > length(c2))
vertexTangent_modelspace = c1;
else
vertexTangent_modelspace = c2;
vertexTangent_modelspace = normalize(vertexTangent_modelspace);
vertexBitangent_modelspace = normalize(cross(e_normal[i], vertexTangent_modelspace));
vec3 vertexTangent_cameraspace = v3x3 * vec3(vertexTangent_modelspace);
vec3 vertexBitangent_cameraspace = v3x3 * vec3(vertexBitangent_modelspace);
vec3 vertexNormal_cameraspace = v3x3 * vec3(e_normal[i]);
TBN = transpose(mat3(
vertexTangent_cameraspace,
vertexBitangent_cameraspace,
vertexNormal_cameraspace
));
}
void createPt(int i)
{
material = e_material[i];
normal = normalize(e_normal[i]);
uv = e_uv[i];
calculateLightingData(i);
gl_Position = vp * vec4(e_position[i], 1.0);
EmitVertex();
}
void main()
{
int i;
for(i=0; i < gl_in.length(); i++)
{
createPt(i);
}
EndPrimitive();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment