Shadow Volume - Code Sample

//The shadow volume shader
#if defined(shv_shader)
    attribute vec3 tangent;
    
    void main(void)
    {
        vec3 light_dir;
        vec3 eye_dir;
        
        /* Transform the vertex into eye-space.
         */
        vec3 vert_pos = vec3(gl_ModelViewMatrix * gl_Vertex);
        
        /* gl_LightSource[0].position is the eye-space position of the light.  By
         * subtracting the eye-space position of the vertex, we obtain a vector
         * from the vertex to the light.
         */
        vec3 light = gl_LightSource[0].position.xyz - vert_pos;
        /* Calculate an orthonormal basis for the vertex in surface-space.
         */
        
        /* vec3(0.0, 0.0, 0.0) is the eye-space position of the eye.  By
         * subtracting the eye-space position of the vertex, we obtain a vector
         * from the vertex to the eye.
         */
        vec3 ep = -vert_pos;
        
        vec3 t = normalize(gl_NormalMatrix * tangent);
        vec3 n = normalize(gl_NormalMatrix * gl_Normal);
        vec3 b = cross(n, t);
        
        /* Transform the vertex-light vector to surface-space.
         */
        vec3 lv = vec3(dot(light, t), dot(light, b), dot(light, n));
        light_dir = normalize(lv);
        
        
        /* Transform the vertex-eye vector to surface-space.
         */
        vec3 ev = vec3(dot(ep, t), dot(ep, b), dot(ep, n));
        eye_dir = normalize(ev);
        
        //Project Vertex Away from Light if not lit
        if (light_dir.z < 0.0) {
            vert_pos -= 100.0 * light;
        }
        
        gl_Position = gl_ProjectionMatrix * vec4(vert_pos, 1.0);
        gl_FrontColor = gl_Color;
    }
#endif //shv_shader