Skip to content

Instantly share code, notes, and snippets.

@jes
Created December 10, 2011 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jes/1456301 to your computer and use it in GitHub Desktop.
Save jes/1456301 to your computer and use it in GitHub Desktop.
/* lighting vertex shader (not quite phong)
* http://www.lighthouse3d.com/opengl/glsl/index.php/index.php?ogldir1
* James Stanley 2011
*/
#version 120
void main() {
vec3 normal, lightDir;
vec4 diffuse, ambient;
float NdotL;
/* first transform the normal into eye space and normalize the result */
normal = gl_Normal;
/* transform light position */
vec4 lightpos = gl_LightSource[0].position;
/* TODO: get another shader to deal with the real lighting which is
* directional.
*/
lightDir = normalize(vec3(gl_Vertex - lightpos));
/* compute the cos of the angle between the normal and lights direction.
The light is directional so the direction is constant for every vertex.
Since these two are normalized the cosine is the dot product. We also
need to clamp the result to the [0,1] range. */
NdotL = max(dot(normal, lightDir), 0.0);
/* compute the ambient term */
ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
/* Compute the diffuse term */
diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
gl_FrontColor = ambient + NdotL * diffuse;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment