Skip to content

Instantly share code, notes, and snippets.

@feliwir
Created July 5, 2016 11:04
Show Gist options
  • Save feliwir/560136ec508d20dea177b492ee403238 to your computer and use it in GitHub Desktop.
Save feliwir/560136ec508d20dea177b492ee403238 to your computer and use it in GitHub Desktop.
#version 400 core
in vec3 position;
in vec2 uv;
in mat3 TBN;
in vec4 shadowCoord;
uniform int use_shadows;
uniform sampler2D albedoTex;
uniform sampler2D normalTex;
uniform sampler2D specularTex;
uniform sampler2D ambientTex;
uniform sampler2DShadow shadowMap;
uniform vec2 texOffset;
layout (std140) uniform light_block
{
vec4 cameraPos;
vec4 lightDir;
vec4 diffuse;
vec4 ambient;
vec4 spec;
};
vec2 poissonDisk[16] = vec2[](
vec2( -0.94201624, -0.39906216 ),
vec2( 0.94558609, -0.76890725 ),
vec2( -0.094184101, -0.92938870 ),
vec2( 0.34495938, 0.29387760 ),
vec2( -0.91588581, 0.45771432 ),
vec2( -0.81544232, -0.87912464 ),
vec2( -0.38277543, 0.27676845 ),
vec2( 0.97484398, 0.75648379 ),
vec2( 0.44323325, -0.97511554 ),
vec2( 0.53742981, -0.47373420 ),
vec2( -0.26496911, -0.41893023 ),
vec2( 0.79197514, 0.19090188 ),
vec2( -0.24188840, 0.99706507 ),
vec2( -0.81409955, 0.91437590 ),
vec2( 0.19984126, 0.78641367 ),
vec2( 0.14383161, -0.14100790 )
);
out vec4 color;
void main(void)
{
vec4 albedo = vec4(0.0, 0.0, 0.0, 1.0) + texture(albedoTex, uv).rgba;
float ambiFac = texture(ambientTex, uv).r;
float specFac = texture(specularTex, uv).r;
vec3 texNormal = texture(normalTex, uv).rgb;
texNormal = normalize(texNormal*2.0-1.0);
vec4 viewDir = normalize(cameraPos - vec4(position,1.0));
//Pixelnormal
vec4 PN = normalize(vec4(texNormal*TBN,0.0));
//normalized Light directory
vec4 LD = normalize(vec4(vec3(lightDir)*TBN,0.0));
//normalized view dir
vec4 VD = normalize(viewDir);
//half direct
vec4 HD = normalize(LD + VD);
//specular intensity
float SI = specFac*pow(max(dot(PN, HD), 0.0), 16);
//diffuse intensitry
float DI = max(0.0, dot(PN,LD));
//ambient intensity
float AI = ambiFac*0.5;
color += AI*albedo;
color += DI*albedo;
color += SI*albedo;
//color += SI;
float visibility=1.0;
float bias = 0.0007;
for (int i=0;i<4;i++){
visibility -= 0.2*(1.0-texture( shadowMap, vec3(shadowCoord.xy + poissonDisk[i]/700.0, (shadowCoord.z-bias)/shadowCoord.w) ));
}
if (use_shadows > 0)
color *= visibility;
color.w = 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[];
layout (std140) uniform matrix_block
{
mat4 vp;
mat4 v;
};
layout (std140) uniform depth_matrix_block
{
mat4 depth_vp;
mat4 depth_bias_vp;
};
uniform mat4 m;
out vec3 position;
out vec2 uv;
out vec3 normal;
out mat3 TBN;
out vec4 shadowCoord;
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));
vec4 vertexTangent_cameraspace = v * vec4(vertexTangent_modelspace,1.0);
vec4 vertexBitangent_cameraspace = v * vec4(vertexBitangent_modelspace,1.0);
vec4 vertexNormal_cameraspace = v * vec4(e_normal[i],1.0);
TBN = transpose(mat3(
vertexTangent_cameraspace,
vertexBitangent_cameraspace,
vertexNormal_cameraspace
));
}
void createPt(int i)
{
uv = e_uv[i];
normal = e_normal[i];
calculateLightingData(i);
gl_Position = vp * (m * vec4(e_position[i], 1.0));
shadowCoord = depth_bias_vp * (m* vec4(e_position[i], 1.0));
EmitVertex();
}
void main()
{
int i;
for(i=0; i < gl_in.length(); i++)
{
createPt(i);
}
EndPrimitive();
}
#version 400 core
layout(vertices = 3) out;
in vec3 v_position[];
in vec2 v_uv[];
in vec3 v_normal[];
layout (std140) uniform matrix_block
{
mat4 vp;
mat4 v;
};
layout (std140) uniform tessellation_block
{
int tess_factor;
int max_tess_factor;
};
uniform mat4 m;
out vec3 tc_position[];
out vec2 tc_uv[];
out vec3 tc_normal[];
vec2 ss(vec3 pos)
{
vec4 tmp = (vp * m * vec4(pos, 1.0));
return tmp.xy / tmp.w;
}
float liness(vec3 p1, vec3 p2){
return distance(ss(p1), ss(p2));
}
void main()
{
#define id gl_InvocationID
tc_position[id] = v_position[id];
tc_uv[id] = v_uv[id];
tc_normal[id] = v_normal[id];
gl_TessLevelOuter[2] = clamp(liness(v_position[0], v_position[1]) * tess_factor, 1, max_tess_factor);
gl_TessLevelOuter[0] = clamp(liness(v_position[1], v_position[2]) * tess_factor, 1, max_tess_factor);
gl_TessLevelOuter[1] = clamp(liness(v_position[2], v_position[0]) * tess_factor, 1, max_tess_factor);
gl_TessLevelInner[0] = gl_TessLevelOuter[0];
gl_TessLevelInner[1] = gl_TessLevelOuter[1];
}
#version 400 core
layout(triangles, equal_spacing, ccw) in;
in vec3 tc_position[];
in vec2 tc_uv[];
in vec3 tc_normal[];
uniform sampler2D displacementTex;
uniform vec2 texOffset;
out vec3 e_position;
out vec2 e_uv;
out vec3 e_normal;
void main()
{
e_position = gl_TessCoord.x * tc_position[0] + gl_TessCoord.y * tc_position[1] + gl_TessCoord.z * tc_position[2];
e_uv = gl_TessCoord.x * tc_uv[0] + gl_TessCoord.y * tc_uv[1] + gl_TessCoord.z * tc_uv[2];
e_normal = normalize(gl_TessCoord.x * tc_normal[0] + gl_TessCoord.y * tc_normal[1] + gl_TessCoord.z * tc_normal[2]);
float height = texture(displacementTex, e_uv + texOffset).x;
e_position += e_normal * (height * 0.01f);
}
#version 400 core
layout(location = 0) in vec3 in_position;
layout(location = 1) in vec2 in_uv;
layout(location = 2) in vec3 in_normal;
layout(location = 3) in vec2 in_influences;
uniform bool useSkeleton;
uniform int meshType;
uniform int parentPivot;
uniform mat4 pivots[128];
out vec3 v_position;
out vec2 v_uv;
out vec3 v_normal;
void main(void)
{
v_uv = in_uv;
vec4 n = vec4(in_normal, 1.0f);
vec4 v = vec4(in_position, 1.0f);
if(useSkeleton)
{
int parent = parentPivot;
if(meshType == 128)
{
parent = int(in_influences[0]);
}
v = pivots[parent] * v;
n = pivots[parent] * n;
}
v_normal = n.xyz;
v_position = v.xyz;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment