Planar Shadows - Code Sample

//By: Chris Kaynor

//The light position.
uniform vec3 light_pos;
uniform mat4 light_matrix;

//The tangent for the vertex.
attribute vec3 tangent;

void main(void)
{
    vec3 eye_pos = -vec3(gl_ModelViewMatrix * gl_Vertex);
    
//  gl_Position = ftransform();
    
    //Generate the T, N, and B vectors in world-space
    vec3 t = normalize(gl_NormalMatrix * tangent);
    vec3 n = normalize(gl_NormalMatrix * gl_Normal);
    vec3 b = cross(n, t);
    
    //Generate the light position in world-space
    vec3 Light = vec3(light_matrix * vec4(light_pos, 1.0));
    Light += eye_pos;
    
    //Generate the light direction
    vec3 v;
    
    v.x = dot(Light, t);
    v.y = dot(Light, b);
    v.z = dot(Light, n);
    vec3 light_dir = normalize(v);
    
    //Calculate Projection of Object from Light onto Plane
    mat4 ProjectionMatrix;
    
    vec4 Vert = gl_Vertex * gl_ModelViewMatrix;
    
    vec3 p = vec3(0.0,  0.0, 0.0);
    vec3 pNormal = vec3(0.0,  1.0, 0.0);
    vec3 Px = light_pos;
    
    float NDotP = dot(pNormal, p);      //Dot product of plane normal and light position
    float d = dot(pNormal, p);          //Dot product of plane Normal and point on Plane
    float NDotPplusD = NDotP - d;
    
    //Generate the projection matrix from the values we have.
    //Two versions of this code depending...one is merely the transposed version of the matrix.
//*
    ProjectionMatrix[0].x = NDotPplusD - (Px.x * pNormal.x);
    ProjectionMatrix[0].y = -(Px.x * pNormal.y);
    ProjectionMatrix[0].z = -(Px.x * pNormal.z);
    ProjectionMatrix[0].w = -(Px.x * d);
    
    ProjectionMatrix[1].x = -(Px.y * pNormal.x);
    ProjectionMatrix[1].y = NDotPplusD - (Px.y * pNormal.y);
    ProjectionMatrix[1].z = -(Px.y * pNormal.z);
    ProjectionMatrix[1].w = -(Px.y * d);
    
    ProjectionMatrix[2].x = -(Px.z * pNormal.x);
    ProjectionMatrix[2].y = -(Px.z * pNormal.y);
    ProjectionMatrix[2].z = NDotPplusD - (Px.z * pNormal.z);
    ProjectionMatrix[2].w = -(Px.z * d);
    
    ProjectionMatrix[3].x = -pNormal.x;
    ProjectionMatrix[3].y = -pNormal.y;
    ProjectionMatrix[3].z = -pNormal.z;
    ProjectionMatrix[3].w = NDotP;
/*/
    ProjectionMatrix[0].x = NDotPplusD - (Px.x * pNormal.x);
    ProjectionMatrix[1].x = -(Px.x * pNormal.y);
    ProjectionMatrix[2].x = -(Px.x * pNormal.z);
    ProjectionMatrix[3].x = -(Px.x * d);
    
    ProjectionMatrix[0].y = -(Px.y * pNormal.x);
    ProjectionMatrix[1].y = NDotPplusD - (Px.y * pNormal.y);
    ProjectionMatrix[2].y = -(Px.y * pNormal.y);
    ProjectionMatrix[3].y = -(Px.y * d);
    
    ProjectionMatrix[0].z = -(Px.z * pNormal.x);
    ProjectionMatrix[1].z = -(Px.z * pNormal.y);
    ProjectionMatrix[2].z = NDotPplusD - (Px.z * pNormal.z);
    ProjectionMatrix[3].z = -(Px.z * d);
    
    ProjectionMatrix[0].w = -pNormal.x;
    ProjectionMatrix[1].w = -pNormal.y;
    ProjectionMatrix[2].w = -pNormal.z;
    ProjectionMatrix[3].w = NDotP;
//*/
    
    //Transform the vertex by the projection matries.
    gl_Position = Vert * ProjectionMatrix * gl_ProjectionMatrix;
    gl_FrontColor = vec4(1.0);
}