Created
November 27, 2012 19:36
-
-
Save finlaybob/4156484 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////////////////////////////////////////////////////// | |
// OpenGL4 Vertex Shader | |
#version 400 | |
layout (location = 0) in vec3 a_pos; | |
layout (location = 1) in vec3 a_nor; | |
layout (location = 2) in vec3 a_uvs; | |
layout (location = 3) in vec3 a_tan; | |
layout (location = 4) in vec3 a_bin; | |
uniform mat4 Projection; | |
uniform mat4 View; | |
uniform mat4 Model; | |
uniform mat3 normalMatrix; | |
out vec3 vVertex; | |
out vec3 vVec; | |
out vec3 vNormal; | |
out vec3 vTangent; | |
out vec3 vBinormal; | |
out mat4 mvm; | |
out vec2 vUvs; | |
uniform float lineWidth; | |
void main() | |
{ | |
vUvs = a_uvs.st ; | |
mvm = View * Model; | |
vVertex = (mvm *vec4(a_pos,1.0)).xyz; | |
//vNormal = normalize(mvm * vec4(a_nor,0.0)).xyz; | |
vNormal = normalize(normalMatrix * a_nor); | |
vTangent = normalize(normalMatrix * a_tan); | |
vBinormal = normalize(normalMatrix * a_bin); | |
vVec = a_pos; | |
gl_Position = (vec4(a_pos,1.0)); | |
} | |
///////////////////////////////////////////////////////////////////////////////////////// | |
// OpenGL4.x Tessellation Control Shader | |
#version 400 | |
layout( vertices = 16 ) out; | |
uniform float TessLevelInner; | |
uniform float TessLevelOuter; | |
in vec3 vVertex[]; | |
in vec3 vNormal[]; | |
in vec3 vTangent[]; | |
in vec3 vBinormal[]; | |
in vec2 vUvs[]; | |
in vec3 vVec[]; | |
out vec3 tcVertex[]; | |
out vec3 tcNormal[]; | |
out vec3 tcTangent[]; | |
out vec3 tcBinormal[]; | |
out vec2 tcUvs[]; | |
out vec3 tcVec[]; | |
void main( ) | |
{ | |
tcVertex[gl_InvocationID] =vVertex[gl_InvocationID]; | |
tcNormal[gl_InvocationID] =vNormal[gl_InvocationID]; | |
tcTangent[gl_InvocationID] =vTangent[gl_InvocationID]; | |
tcBinormal[gl_InvocationID] =vBinormal[gl_InvocationID]; | |
tcUvs[gl_InvocationID] = vUvs[gl_InvocationID]; | |
//gl_out[ gl_InvocationID ].gl_Position = gl_in[ gl_InvocationID ].gl_Position; | |
tcVec[gl_InvocationID] = vVec[gl_InvocationID]; | |
gl_TessLevelOuter[0] = TessLevelOuter; | |
gl_TessLevelOuter[1] = TessLevelOuter; | |
gl_TessLevelOuter[2] = TessLevelOuter; | |
gl_TessLevelOuter[3] = TessLevelOuter; | |
gl_TessLevelInner[0] = TessLevelOuter; | |
gl_TessLevelInner[1] = TessLevelOuter; | |
} | |
///////////////////////////////////////////////////////////////////////////////////////// | |
//OpenGL4.x Tessellation Evaluation Shader | |
#version 400 | |
layout( quads, fractional_odd_spacing,ccw) in; | |
//fractional_odd_spacing | |
//equal_spacing | |
out vec2 texcoord; | |
out float depth; | |
in vec3 tcVertex[]; | |
in vec3 tcNormal[]; | |
in vec3 tcTangent[]; | |
in vec3 tcBinormal[]; | |
in vec2 tcUvs[]; | |
in vec3 tcVec[]; | |
out vec3 teVertex; | |
out vec3 teNormal; | |
out vec3 teTangent; | |
out vec3 teBinormal; | |
out vec2 teUvs; | |
out vec3 teVec; | |
uniform sampler2D normalMap; | |
uniform mat4 mvp; | |
uniform mat4 Projection; | |
uniform mat4 View; | |
uniform mat4 Model; | |
uniform mat3 normalMatrix; | |
uniform mat4 B; | |
uniform mat4 BT; | |
void oldMain(){ | |
float u = gl_TessCoord.x; | |
float v = gl_TessCoord.y; | |
vec3 a = mix(tcVec[0],tcVec[3],u); | |
vec3 b = mix(tcVec[12],tcVec[15],u); | |
vec4 position = vec4(mix(a,b,v),1.0); | |
//teNormal = | |
vec3 at = mix(tcNormal[0],tcNormal[3],u); | |
vec3 bt = mix(tcNormal[12],tcNormal[15],u); | |
teNormal = mix(at,bt,v); | |
vec2 auv = mix(tcUvs[0],tcUvs[3],u); | |
vec2 buv = mix(tcUvs[12],tcUvs[15],u); | |
vec2 tc = mix(auv,buv,v); | |
texcoord = tc; | |
float height = texture(normalMap, texcoord).a; | |
//height = 0.0; | |
//position.y += height; | |
gl_Position = Projection * View * Model * position; | |
} | |
void main( ) | |
{ | |
float u = gl_TessCoord.x; | |
float v = gl_TessCoord.y; | |
for(int i=0;i<16;i++){ | |
teNormal = tcNormal[i]; | |
teBinormal = tcBinormal[i]; | |
teTangent = tcTangent[i]; | |
} | |
//vec3 a = mix(tcVec[0],tcVec[3],u); | |
//vec3 b = mix(tcVec[12],tcVec[15],u); | |
//vec4 position = vec4(mix(a,b,v),1.0); | |
mat4 Px = mat4( | |
tcVec[0].x, tcVec[1].x, tcVec[2].x, tcVec[3].x, | |
tcVec[4].x, tcVec[5].x, tcVec[6].x, tcVec[7].x, | |
tcVec[8].x, tcVec[9].x, tcVec[10].x, tcVec[11].x, | |
tcVec[12].x, tcVec[13].x, tcVec[14].x, tcVec[15].x ); | |
mat4 Py = mat4( | |
tcVec[0].y, tcVec[1].y, tcVec[2].y, tcVec[3].y, | |
tcVec[4].y, tcVec[5].y, tcVec[6].y, tcVec[7].y, | |
tcVec[8].y, tcVec[9].y, tcVec[10].y, tcVec[11].y, | |
tcVec[12].y, tcVec[13].y, tcVec[14].y, tcVec[15].y ); | |
mat4 Pz = mat4( | |
tcVec[0].z, tcVec[1].z, tcVec[2].z, tcVec[3].z, | |
tcVec[4].z, tcVec[5].z, tcVec[6].z, tcVec[7].z, | |
tcVec[8].z, tcVec[9].z, tcVec[10].z, tcVec[11].z, | |
tcVec[12].z, tcVec[13].z, tcVec[14].z, tcVec[15].z ); | |
mat4 cx = B * Px * BT; | |
mat4 cy = B * Py * BT; | |
mat4 cz = B * Pz * BT; | |
vec4 U = vec4(u*u*u, u*u, u, 1); | |
vec4 V = vec4(v*v*v, v*v, v, 1); | |
float height = texture(normalMap, texcoord).a; | |
float x = dot(cx * V, U); | |
float y = dot(cy * V, U); | |
float z = dot(cz * V, U); | |
vec4 position = vec4(x,y,z,1.0); | |
texcoord = gl_TessCoord.xy; | |
//height = 0.0; | |
//position.y += height; | |
gl_Position = Projection * View * Model * position; | |
} | |
///////////////////////////////////////////////////////////////////////////////////////// | |
// OpenGL4.x Geometry Shader | |
#version 400 | |
layout(triangles) in; | |
//layout(line_strip, max_vertices = 4) out; | |
layout(triangle_strip, max_vertices = 4) out; | |
in vec3 vVertex[]; | |
in vec3 vNormal[]; | |
in vec3 vTangent[]; | |
in vec3 vBinormal[]; | |
in vec2 teUvs[]; | |
in vec3 teVertex[]; | |
in vec3 teTangent[]; | |
in vec3 teNormal[]; | |
in vec3 teBinormal[]; | |
out vec3 gVertex; | |
out vec3 gNormal; | |
out vec3 gTangent; | |
out vec3 gBinormal; | |
out vec2 gUvs; | |
out vec3 gEdgeDist; | |
in vec2 texcoord[]; | |
uniform mat3 vpMat; | |
uniform float shrinkAmount; | |
uniform sampler2D normalMap; | |
uniform mat3 normalMatrix; | |
void applyOthersOnly(int i){ | |
gVertex = vVertex[i]; | |
gNormal = vNormal[i]; | |
gTangent = vTangent[i]; | |
gBinormal = vBinormal[i]; | |
gUvs = teUvs[i]; | |
} | |
void passThrough(){ | |
for(int i = 0; i < gl_in.length(); i++) { | |
gVertex = teVertex[i]; | |
gNormal = teNormal[i]; | |
gTangent = teTangent[i]; | |
gBinormal = teBinormal[i]; | |
gUvs = texcoord[i]; | |
vec4 mPos = gl_in[i].gl_Position; | |
gVertex = mPos.xyz; | |
gl_Position = mPos; | |
EmitVertex(); | |
} | |
EndPrimitive(); | |
} | |
void applyOthersAndEmit(int i){ | |
gl_Position = gl_in[i].gl_Position; | |
gVertex = gl_in[i].gl_Position.xyz; | |
//gNormal = teNormal[i]; | |
//gTangent = teTangent[i]; | |
//gBinormal = vBinormal[i]; | |
//gUvs = teUvs[i]; | |
gUvs = texcoord[i]; | |
EmitVertex(); | |
} | |
void solidWire(){ | |
mat3 vpTrans = transpose(vpMat); | |
vec3 p0 = vec3(vpTrans * (gl_in[0].gl_Position).xyz); | |
vec3 p1 = vec3(vpTrans * (gl_in[1].gl_Position).xyz); | |
vec3 p2 = vec3(vpTrans * (gl_in[2].gl_Position).xyz); | |
float a = length(p1 - p2); | |
float b = length(p2 - p0); | |
float c = length(p1 - p0); | |
float alpha = acos((b*b + c*c - a*a)/(2.0*b*c)); | |
float beta = acos((a*a + c*c - b*b)/(2.0*a*c)); | |
float ha = abs(c* sin(beta)); | |
float hb = abs(c* sin(alpha)); | |
float hc = abs(b* sin(alpha)); | |
vec3 A = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz; | |
vec3 B = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz; | |
gNormal = normalMatrix * normalize(cross(A,B)); | |
//gNormal = texture(normalMap,texcoord[0].xy).xyz; | |
vec3 fv1 = B; | |
vec3 fv2 = A; | |
vec2 uv1 = texcoord[0]; | |
vec2 uv2 = texcoord[1]; | |
vec2 uv3 = texcoord[2]; | |
vec2 fuv1 = uv2 - uv1; | |
vec2 fuv2 = uv3 - uv1; | |
float r = 1.0 / (fuv1.x * fuv2.y - fuv1.y * fuv2.x); | |
vec3 sdir,tdir; | |
gTangent = vec3( | |
(fuv2.t * fv1.x - fuv1.t * fv2.x) * r, | |
(fuv2.t * fv1.y - fuv1.t * fv2.y) * r, | |
(fuv2.t * fv1.z - fuv1.t * fv2.z) * r | |
); | |
gBinormal = vec3( | |
(fuv1.s * fv2.x - fuv2.s * fv1.x)*r, | |
(fuv1.s * fv2.y - fuv2.s * fv1.y)*r, | |
(fuv1.s * fv2.z - fuv2.s * fv1.z)*r | |
); | |
vec3 ta,tb; | |
gEdgeDist = vec3(ha,0,0); | |
applyOthersAndEmit(0); | |
gEdgeDist = vec3(0,hb,0); | |
applyOthersAndEmit(1); | |
gEdgeDist = vec3(0,0,hc); | |
applyOthersAndEmit(2); | |
EndPrimitive(); | |
} | |
void drawNormals(){ | |
vec3 A = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz; | |
vec3 B = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz; | |
vec3 crs = normalize(cross(A,B)); | |
//crs = texture(normalMap,texcoord[0].xy).xyz; | |
vec4 pos = gl_in[0].gl_Position + gl_in[1].gl_Position + gl_in[2].gl_Position; | |
gl_Position = pos; | |
EmitVertex(); | |
gl_Position = vec4(pos.xyz + (crs),0.0); | |
EmitVertex(); | |
} | |
float rand(vec2 co){ | |
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); | |
} | |
void shrinkPoly(){ | |
vec4 v[3]; | |
v[0] = gl_in[0].gl_Position; | |
v[1] = gl_in[1].gl_Position; | |
v[2] = gl_in[2].gl_Position; | |
vec4 CG = (v[0] + v[1] + v[2]) / 3.0; | |
for(int i = 0; i < gl_in.length(); i++) { | |
gVertex = vVertex[i]; | |
gNormal = vNormal[i]; | |
gTangent = vTangent[i]; | |
gBinormal = vBinormal[i]; | |
gUvs = teUvs[i]; | |
vec4 mPos = vec4( CG + shrinkAmount * ( v[i] - CG )); | |
gl_Position = mPos;//vec4(v[i],1);//vec4(CG + 0.11*(v[i] -CG),1.0); | |
EmitVertex(); | |
} | |
EndPrimitive(); | |
} | |
void main() { | |
solidWire(); | |
//drawNormals(); | |
//passThrough(); | |
} | |
///////////////////////////////////////////////////////////////////////////////////////// | |
// OpenGL4.x Fragment Shader | |
#version 400 | |
layout (location=0) out vec4 frag_main; | |
uniform vec3 lightPos; | |
uniform vec3 lightCol; | |
uniform sampler2D colorMap; | |
uniform sampler2D normalMap; | |
uniform float lineWidth; | |
uniform vec3 lineColor; | |
in vec3 gVertex; | |
in vec3 gNormal; | |
in vec3 gTangent; | |
in vec3 gBinormal; | |
in vec2 gUvs; | |
in vec3 gCol; | |
in vec3 gEdgeDist; | |
void solidWire(){ | |
vec3 mapNormal = (texture(normalMap,gUvs).rgb * 2)-1; | |
vec3 matColor = texture(colorMap,gUvs).rgb; | |
vec3 pertNormal = gNormal; | |
pertNormal = normalize(gNormal + (mapNormal)); | |
//vec3 vLightDir = normalize(lightPos - vVertex); | |
//vec3 gLightDir = normalize(lightPos - gVertex); | |
//vec3 vLightDir = normalize(lightPos - vVertex); | |
vec3 tmp = lightPos - gVertex; | |
vec3 gLightDir; | |
gLightDir.x = dot(tmp,gTangent); | |
gLightDir.y = dot(tmp,gBinormal); | |
gLightDir.z = dot(tmp,gNormal); | |
gLightDir = normalize(gLightDir); | |
tmp = (- gVertex); | |
vec3 gEyeDir; | |
gEyeDir.x = dot(tmp,gTangent); | |
gEyeDir.y = dot(tmp,gBinormal); | |
gEyeDir.z = dot(tmp,gNormal); | |
gEyeDir = normalize(gEyeDir); | |
//gEyeDir = normalize(-gVertex); | |
vec3 gReflect = normalize(-reflect(gLightDir,mapNormal)); | |
vec3 gHalfVec = normalize( gEyeDir + gLightDir); | |
float d = min(gEdgeDist.x,gEdgeDist.y); | |
d = min(d,gEdgeDist.z); | |
//float mixVal = smoothstep(lineWidth-1,lineWidth+1,d); | |
float mixVal = smoothstep(0,3,d); | |
vec3 LightColor = lightCol; | |
vec3 A = matColor * LightColor.xyz * 0.3; | |
vec3 D = matColor * LightColor.xyz * 0.9 * max(dot(mapNormal,gLightDir),0.0); | |
vec3 S = vec3(1.0) * pow(max(dot(gReflect,gEyeDir),0.0),64); | |
//vec3 S = vec3(1.0) * pow(max(dot(gHalfVec,mapNormal),0.0),512); | |
vec3 final = mix(vec3(0.0), A+D+S, mixVal); | |
//final = gNormal; | |
//final = A+S+D; | |
final.rgb = final.rgb; | |
frag_main = vec4(final,1.0); | |
} | |
void tbnBump(){ | |
vec3 mapNormal = texture(normalMap,gUvs).rgb * 2; | |
mapNormal -= 1; | |
vec3 matColor = texture(colorMap,gUvs).rgb; | |
vec3 pertNormal = normalize(gNormal + (mapNormal)); | |
//vec3 vLightDir = normalize(lightPos - vVertex); | |
vec3 tmp = lightPos - gVertex; | |
vec3 gLightDir; | |
gLightDir.x = dot(tmp,gTangent); | |
gLightDir.y = dot(tmp,gBinormal); | |
gLightDir.z = dot(tmp,gNormal); | |
gLightDir = normalize(gLightDir); | |
tmp = (- gVertex); | |
vec3 gEyeDir; | |
gEyeDir.x = dot(tmp,gTangent); | |
gEyeDir.y = dot(tmp,gBinormal); | |
gEyeDir.z = dot(tmp,gNormal); | |
gEyeDir = normalize(gEyeDir); | |
//gEyeDir = normalize(-gVertex); | |
vec3 gReflect = normalize(-reflect(gLightDir,pertNormal)); | |
vec3 LightColor = lightCol; | |
vec3 A = matColor * LightColor.xyz * 0.3; | |
vec3 D = matColor * LightColor.xyz * 0.9 * max(dot(pertNormal,gLightDir),0.0); | |
vec3 S = vec3(1.0) * pow(max(dot(gReflect,gEyeDir),0.0),256); | |
vec3 final = A+D+S; | |
//final = gEdgeDist; | |
//final = vVertex; | |
//final = gCol; | |
frag_main = vec4(final,1.0); | |
} | |
void main() | |
{ | |
solidWire(); | |
//tbnBump(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment