Created
August 2, 2013 21:49
-
-
Save croxis/6143733 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
uniform sampler2D p3d_Texture0; | |
varying vec2 texture_coordinate; | |
varying vec4 texCoords; | |
// | |
varying vec4 position; | |
// position of the vertex (and fragment) in view space | |
varying vec3 varyingNormalDirection; | |
// surface normal vector in view space | |
// | |
//varying out vec4 FragColor; | |
void main(void) | |
{ | |
vec3 normalDirection = normalize(varyingNormalDirection); | |
//vec3 normalDirection = varyingNormalDirection; | |
vec3 viewDirection = -normalize(vec3(position)); | |
vec3 lightDirection; | |
//We will precompute sun intensity cpu side so we will not worry | |
//About attenuation | |
float attenuation = 1.0; | |
if (0.0 == gl_LightSource[0].position.w) | |
// directional light? | |
{ | |
lightDirection = | |
normalize(vec3(gl_LightSource[0].position)); | |
} | |
else // point light or spotlight (or other kind of light) | |
{ | |
vec3 positionToLightSource = | |
vec3(gl_LightSource[0].position - texCoords); | |
float distance = length(positionToLightSource); | |
lightDirection = normalize(positionToLightSource); | |
if (gl_LightSource[0].spotCutoff <= 90.0) // spotlight? | |
{ | |
float clampedCosine = max(0.0, dot(-lightDirection, | |
gl_LightSource[0].spotDirection)); | |
} | |
} | |
vec3 ambientLighting = vec3(gl_LightModel.ambient) | |
* vec3(gl_FrontMaterial.emission); | |
vec3 diffuseReflection = attenuation | |
* vec3(gl_LightSource[0].diffuse) | |
* vec3(gl_FrontMaterial.emission) | |
* max(0.0, dot(normalDirection, lightDirection)); | |
vec3 specularReflection; | |
if (dot(normalDirection, lightDirection) < 0.0) | |
// light source on the wrong side? | |
{ | |
//specularReflection = vec3(0.0, 0.0, 0.0); | |
// no specular reflection | |
} | |
else // light source on the right side | |
{ | |
specularReflection = attenuation | |
* vec3(gl_LightSource[0].specular) | |
* vec3(gl_FrontMaterial.specular) | |
* pow(max(0.0, dot(reflect(-lightDirection, | |
normalDirection), viewDirection)), | |
gl_FrontMaterial.shininess); | |
} | |
//vec4 tex0 = texture2D(p3d_Texture0, gl_TexCoord[0].st); | |
vec4 tex0 = texture2D(p3d_Texture0, texCoords.xy); | |
//FragColor = gl_Color; | |
//FragColor = tex0 * gl_Color; | |
//FragColor = vec4(diffuseColor * vec3(tex0) + specularColor, 1.0) * gl_Color; | |
gl_FragColor = vec4((ambientLighting | |
+ diffuseReflection) * vec3(tex0) + specularReflection, 1.0) * gl_Color; | |
//FragColor = vec4(diffuseColor * vec3(tex0), 1.0) * gl_Color; | |
//gl_FragColor = vec4(ambientLighting | |
// + diffuseReflection + specularReflection, 1.0) * gl_Color; | |
//gl_FragColor = vec4(normalDirection.xyz, 1); | |
} |
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
uniform mat4 p3d_ModelViewProjectionMatrix; | |
varying vec4 texCoords; | |
// Per pix lighting | |
varying vec3 varyingNormalDirection; | |
// surface normal vector in view space | |
varying vec4 position; | |
// position of the vertex (and fragment) in view space | |
///Per pix lighting | |
void main(void) { | |
vec4 vertex = gl_Vertex; | |
//By adding 1 to z we move our grid to make a cube. | |
//We do 1 instead of 0.5 to make all the math easy. | |
vertex.z = vertex.z + 1.0; | |
//While this simple normalizing works it causes some distortion near the edges | |
//While less distoration than a standard sphere there are more ideal solutions | |
//Some cubemapping software will compensate for said distortions however | |
gl_Position = p3d_ModelViewProjectionMatrix * vec4(normalize(vertex.xyz), vertex.w); | |
//The normalization creates almost no edge distortion | |
//Proof: http://mathproofs.blogspot.com/2005/07/mapping-cube-to-sphere.html | |
//float x = test.x; | |
//float y = test.y; | |
//float z = test.z; | |
//test.x *= sqrt(1.0 - y * y * 0.5 - z * z * 0.5 + y * y * z * z / 3.0); | |
//test.y *= sqrt(1.0 - z * z * 0.5 - x * x * 0.5 + z * z * x * x / 3.0); | |
//test.z *= sqrt(1.0 - x * x * 0.5 - y * y * 0.5 + x * x * y * y / 3.0); | |
//gl_Position = p3d_ModelViewProjectionMatrix * test; | |
//varyingNormalDirection = normalize(gl_NormalMatrix * gl_Normal); | |
position = gl_ModelViewMatrix * vertex; | |
varyingNormalDirection = (p3d_ModelViewProjectionMatrix * vec4(normalize(vertex.xyz), vertex.w)).xyz; | |
gl_FrontColor = gl_Color; | |
texCoords = gl_MultiTexCoord0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment