#version 120

/**
 * Lit Sphere (spherically mapped captured light) shader.
 *
 **Project Name:**      MakeHuman
 *
 **Product Home Page:** http://www.makehuman.org/
 *
 **Code Home Page:**    https://bitbucket.org/MakeHuman/makehuman/
 *
 **Authors:**           Jonas Hauquier
 *
 **Copyright(c):**      MakeHuman Team 2001-2017
 *
 **Licensing:**         AGPL3
 *
 *
 * Abstract
 * --------
 *
 * Has the effect of a specular material, shaded relative to the camera without
 * lights.
 * The normal is used as UV coordinates of the litsphere texture, which is a
 * spherically shaped mapping of colors for all angles that normals can face in
 * the half sphere facing the camera.
 * Litsphere textures express lighting for normal angles in view space.
**/


#ifdef NORMALMAP
    // Inputs
    attribute vec4 tangent;

    // Output
    varying mat3 tbnMat;    // Tangent to view space conversion matrix
#else
    // Output
    varying vec3 vNormal;   // Vertex normal in view space
#endif


void main() {
    gl_Position = ftransform();
    gl_TexCoord[0] = gl_MultiTexCoord0;
    #ifdef NORMALMAP
        vec3 vNormal = vec3(0, 0, 0);
    #endif
    vNormal = normalize(gl_NormalMatrix * gl_Normal);  // Vertex normal in view space

    #ifdef DIFFUSE
        gl_TexCoord[0] = gl_MultiTexCoord0;
    #endif

    #ifdef NORMALMAP
        // Tangent and binormal in view space
        vec3 tang = normalize(gl_NormalMatrix * tangent.xyz);
        //normal = normalize(gl_NormalMatrix * gl_Normal);
        vec3 binormal = normalize(cross(vNormal, tang));

        tbnMat = mat3(tang, binormal, vNormal);   // TBN matrix, converts from Tangent space in view space
    #endif

    #ifdef VERTEX_COLOR
        gl_FrontColor = gl_Color;
    #endif
}
