qtmobility/examples/sensors/cubehouse/lighting.vsh
changeset 4 90517678cc4f
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     1 // Algorithm from section 2.14.1 of OpenGL 2.1 specification.
       
     2 
       
     3 attribute highp vec4 vertex;
       
     4 attribute highp vec3 normal;
       
     5 attribute highp vec4 texcoord;
       
     6 uniform mediump mat4 matrix;
       
     7 uniform mediump mat4 modelView;
       
     8 uniform mediump mat3 normalMatrix;
       
     9 varying highp vec4 qTexCoord;
       
    10 
       
    11 uniform mediump vec4 scli;      // Specular intensity of the light
       
    12 uniform mediump vec3 pli;       // Position of the light
       
    13 uniform mediump float pliw;     // 0 for directional, 1 for positional.
       
    14 uniform mediump vec4 acm;       // Ambient color of the material
       
    15 uniform mediump vec4 dcm;       // Diffuse color of the material
       
    16 uniform mediump vec4 scm;       // Specular color of the material
       
    17 uniform mediump float srm;      // Specular exponent of the material
       
    18 uniform mediump vec4 acs;       // Light model's ambient color of the scene
       
    19 uniform bool viewerAtInfinity;  // Light model indicates viewer at infinity
       
    20 
       
    21 varying mediump vec4 qColor;
       
    22 varying mediump vec4 qSecondaryColor;
       
    23 
       
    24 void qLightVertex(vec4 vertex, vec3 normal)
       
    25 {
       
    26     int i, material;
       
    27     vec3 toEye, toLight, h;
       
    28     float angle, spot, attenuation;
       
    29     vec4 color, scolor;
       
    30     vec4 adcomponent, scomponent;
       
    31 
       
    32     // Start with the material's emissive color and the ambient scene color.
       
    33     // ecm is assumed to be black.
       
    34     color = acm * acs;
       
    35     scolor = vec4(0, 0, 0, 0);
       
    36 
       
    37     // Vector from the vertex to the eye position (i.e. the origin).
       
    38     if (viewerAtInfinity)
       
    39         toEye = vec3(0, 0, 1);
       
    40     else
       
    41         toEye = normalize(-vertex.xyz);
       
    42 
       
    43     // Determine the cosine of the angle between the normal and the
       
    44     // vector from the vertex to the light.
       
    45     if (pliw == 0.0)
       
    46         toLight = normalize(pli);
       
    47     else
       
    48         toLight = normalize(pli - vertex.xyz);
       
    49     angle = max(dot(normal, toLight), 0.0);
       
    50 
       
    51     // Calculate the ambient and diffuse light components.
       
    52     // Assumptions: acli = (0, 0, 0, 1), dcli = (1, 1, 1, 1).
       
    53     adcomponent = acm * vec4(0, 0, 0, 1) + angle * dcm;
       
    54 
       
    55     // Calculate the specular light components.
       
    56     if (angle != 0.0) {
       
    57         h = normalize(toLight + toEye);
       
    58         angle = max(dot(normal, h), 0.0);
       
    59         scomponent = pow(angle, srm) * scm; // scli = (1, 1, 1, 1).
       
    60     } else {
       
    61         scomponent = vec4(0, 0, 0, 0);
       
    62     }
       
    63 
       
    64     // Add up the color components we computed.
       
    65     color += adcomponent;
       
    66     scolor += scomponent;
       
    67 
       
    68     // Generate the final output colors.
       
    69     float alpha = dcm.a;
       
    70     qColor = vec4(clamp(color.rgb, 0.0, 1.0), alpha);
       
    71     qSecondaryColor = clamp(scolor, 0.0, 1.0);
       
    72 }
       
    73 
       
    74 void main(void)
       
    75 {
       
    76     gl_Position = matrix * vertex;
       
    77     highp vec4 vertex = modelView * vertex;
       
    78     highp vec3 norm = normalize(normalMatrix * normal);
       
    79     qLightVertex(vertex, norm);
       
    80     qTexCoord = texcoord;
       
    81 }