machine.html
author Eugen Sawin <sawine@me73.com>
Wed, 27 Apr 2011 15:10:42 +0200
changeset 33 2af408534f28
parent 31 744b427d1379
child 36 a828c4cde5b3
permissions -rwxr-xr-x
Added specular lighting.
     1 <html>
     2 <head>
     3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     4 <title>Machine Alpha</title>
     5 <style type="text/css">
     6  body {margin: 0px;}
     7 </style>
     8 <script language="javascript" src="scripts/boot.js"></script>
     9 <script id="fragment-shader" type="x-shader/x-fragment"> 
    10     #ifdef GL_ES
    11     precision highp float;
    12     #endif
    13 
    14     varying vec4 vColour;
    15     varying vec3 vTransformedNormal;
    16     varying vec4 vPosition;
    17 
    18     uniform float uMaterialShininess;
    19 
    20     uniform bool uUseLighting;
    21    
    22     uniform vec3 uAmbientColour;
    23     uniform vec3 uPointLightingLocation;
    24     uniform vec3 uPointLightingSpecularColour;
    25     uniform vec3 uPointLightingDiffuseColour;
    26  
    27     void main(void)
    28     {
    29         vec3 lightWeighting;
    30         if (!uUseLighting) 
    31         {
    32             lightWeighting = vec3(1.0, 1.0, 1.0);
    33         }
    34         else
    35         {
    36             vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
    37             vec3 normal = normalize(vTransformedNormal);
    38             vec3 eyeDirection = normalize(-vPosition.xyz);
    39             vec3 reflectionDirection = reflect(-lightDirection, normal);
    40             float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);
    41             float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
    42             lightWeighting = uAmbientColour + uPointLightingSpecularColour * specularLightWeighting
    43                              + uPointLightingDiffuseColour * diffuseLightWeighting;
    44         }     
    45         gl_FragColor = vec4(vColour.rgb * lightWeighting, vColour.a);
    46     }
    47 </script> 
    48  
    49 <script id="vertex-shader" type="x-shader/x-vertex"> 
    50     attribute vec3 aVertexPosition;
    51     attribute vec3 aVertexNormal;
    52     attribute vec4 aVertexColour;    
    53 
    54     uniform mat4 uMVMatrix;
    55     uniform mat4 uPMatrix;
    56     uniform mat3 uNMatrix;
    57 
    58     varying vec4 vColour;
    59     varying vec3 vTransformedNormal;
    60     varying vec4 vPosition;
    61  
    62     void main(void) 
    63     {
    64         vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
    65         gl_Position = uPMatrix * vPosition;
    66         vColour = aVertexColour;
    67         vTransformedNormal = uNMatrix * aVertexNormal;
    68     }
    69 </script>
    70 </head>
    71  
    72 <body onload="boot();">
    73 <canvas id="machine">
    74   Download a browser with WebGL support, like Chrome or Firefox.
    75 </canvas>
    76 </body>
    77  
    78 </html>