Created
June 22, 2011 15:56
-
-
Save netshade/1040417 to your computer and use it in GitHub Desktop.
Vertex and Fragment shaders
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
// VERTEX SHADER | |
// | |
// Lighting.vsh | |
const int MaxLights = 3; | |
struct Light { | |
highp vec3 position; | |
lowp vec4 diffuse; | |
lowp vec4 ambient; | |
lowp vec4 specular; | |
lowp vec3 attenuation; | |
}; | |
struct LightArray { | |
lowp int activeLights; | |
Light allLights[MaxLights]; | |
}; | |
struct Material { | |
lowp vec4 diffuse; | |
lowp vec4 specular; | |
lowp vec4 ambient; | |
lowp float shininess; | |
lowp float hasTexture; | |
}; | |
attribute vec3 normalAttr; | |
attribute vec3 position; | |
attribute vec2 texture0; | |
varying lowp vec2 textureVarying0; | |
varying mediump vec3 normal; | |
varying mediump vec3 eyeDirection; | |
varying mediump vec3 lightDirections[MaxLights]; | |
varying mediump float attenuation[MaxLights]; | |
varying mediump vec3 halfVectors[MaxLights]; | |
uniform LightArray lights; | |
uniform Material material0; | |
uniform lowp vec4 sceneAmbient; | |
uniform highp mat4 matProjection; | |
uniform highp mat4 matModelView; | |
uniform highp mat4 matModel; | |
uniform highp mat4 matModelViewProjection; | |
uniform highp mat4 matNormal; | |
void main() | |
{ | |
normal = vec3(matNormal * vec4(normalAttr, 1.0)); | |
highp vec4 worldPosition = matModel * vec4(position, 1.0); | |
eyeDirection = vec3( (matModelView * vec4(position, 1.0)).xyz); | |
lowp int i; | |
mediump float dist; | |
for (i = 0; i < lights.activeLights; i++) { | |
lightDirections[i] = (lights.allLights[i].position.xyz - worldPosition.xyz); | |
dist = length(lightDirections[i]); | |
halfVectors[i] = normalize(lights.allLights[i].position + lightDirections[i]); | |
attenuation[i] = 1.0 / ( lights.allLights[i].attenuation.x + lights.allLights[i].attenuation.y * dist + lights.allLights[i].attenuation.z * dist * dist); | |
} | |
gl_Position = matModelViewProjection * vec4(position, 1.0); | |
textureVarying0 = texture0; | |
} | |
// FRAGMENT SHADER | |
uniform lowp vec4 sceneAmbient; | |
uniform LightArray lights; | |
uniform Material material0; | |
uniform sampler2D tex0; | |
void main() | |
{ | |
highp vec4 final_color = sceneAmbient; | |
highp vec4 frontDiffuse; | |
if(material0.hasTexture > 1.0){ | |
frontDiffuse = texture2D(tex0, textureVarying0); | |
} else { | |
frontDiffuse = material0.diffuse; | |
} | |
mediump vec3 N = normalize(normal); | |
lowp int i; | |
mediump vec3 halfV; | |
mediump float ndothv; | |
mediump vec3 E = normalize(eyeDirection); | |
for (i=0; i<lights.activeLights; ++i) | |
{ | |
mediump vec3 L = normalize(lightDirections[i]); | |
mediump float lambertTerm = dot(N,L); | |
if (lambertTerm > 0.0) | |
{ | |
final_color += (lights.allLights[i].ambient * material0.ambient) * attenuation[i]; | |
final_color += lights.allLights[i].diffuse * frontDiffuse * lambertTerm * attenuation[i]; | |
halfV = normalize(halfVectors[i]); | |
ndothv = max(dot(N, halfV), 0.0); | |
final_color += attenuation[i] * material0.specular * lights.allLights[i].specular * pow(ndothv, material0.shininess); | |
} | |
} | |
gl_FragColor = final_color; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment