Created
March 10, 2013 00:37
-
-
Save finlaybob/5126530 to your computer and use it in GitHub Desktop.
Normal Map Fragment Shader
This file contains hidden or 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
| #version 400 | |
| layout( location = 0 ) out vec4 frag_main; | |
| struct PointLight{ | |
| vec3 position; | |
| vec3 ambient; | |
| vec3 diffuse; | |
| vec3 specular; | |
| float shininess; | |
| float intensity; | |
| }; | |
| uniform PointLight light[2]; | |
| uniform sampler2D colorTexture; | |
| uniform sampler2D normalTexture; | |
| uniform mat4 mvm; | |
| // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| // FLAGS - all flags start false; | |
| // [0][0] = colour map | |
| // [0][1] = normal map | |
| // [0][2] = unused | |
| // [0][3] = show normal map in colour channel | |
| // [1][0] = use tangent space | |
| // [1][1] = unused | |
| // [1][2] = unused | |
| // [1][3] = unused | |
| // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| uniform bvec4 flags[2]; | |
| in vec3 vertexPos_Eye; | |
| in vec3 normalDir_World; | |
| in vec3 normalDir_Eye; | |
| in vec2 vUvs; | |
| in mat3 tbnMatrix; | |
| void main(){ | |
| uint numLights=2; | |
| vec3 colorFragment; | |
| if(flags[0][0]){ | |
| colorFragment = texture(colorTexture,vUvs).xyz; | |
| }else{ | |
| colorFragment = vec3(0.5); | |
| } | |
| vec3 normalMapNormal = (texture(normalTexture,vUvs).rgb * 2) - 1; | |
| vec3 normalDir; | |
| if(flags[0][1]){ | |
| normalDir = normalize(normalDir_Eye); | |
| }else{ | |
| normalDir = normalize(normalMapNormal.rgb); | |
| } | |
| vec4 outColor = vec4(1); | |
| outColor.xyz = vec3(0); | |
| float duller = 1/numLights; | |
| for(int i = 0;i< numLights;i++){ | |
| vec3 lightPos_Eye = (mvm * vec4(light[i].position,1.0)).xyz; | |
| vec3 lightDir_Eye = normalize(lightPos_Eye - vertexPos_Eye); | |
| vec3 lightDir_Tangent = lightDir_Eye * tbnMatrix; | |
| vec3 vertDir_Eye = normalize(-vertexPos_Eye); | |
| vec3 vertDir_Tangent = vertDir_Eye * tbnMatrix; | |
| vec3 halfDir_Eye = normalize(vertDir_Eye + lightDir_Eye); | |
| vec3 halfDir_Tangent = halfDir_Eye * tbnMatrix; | |
| vec3 Ia = colorFragment.xyz * light[i].ambient; | |
| vec3 Id; | |
| vec3 Is; | |
| // tangent space | |
| Id = (colorFragment.xyz * light[i].diffuse ) * max(dot(lightDir_Tangent, normalDir), 0); | |
| Is = light[i].specular * pow(max(dot(halfDir_Eye, normalDir), 0), light[i].shininess*2); | |
| // eye space | |
| //Id = (colorFragment.xyz * light[i].diffuse ) * max(dot(lightDir_Eye, normalDir), 0); | |
| //Is = light[i].specular * pow(max(dot(halfDir_Eye, normalDir), 0), light[i].shininess*2); | |
| outColor.xyz += (Ia + Id + Is); | |
| } | |
| if(flags[0][3]){ | |
| outColor.xyz = normalDir; | |
| } | |
| frag_main = (outColor); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment