machine.html
author Eugen Sawin <sawine@me73.com>
Wed, 27 Apr 2011 14:14:07 +0200
changeset 30 95688249c40c
parent 26 84a998ac11c0
child 31 744b427d1379
permissions -rwxr-xr-x
Added per-vertex positional 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 vLightWeighting;
    16  
    17     void main(void) 
    18     {
    19         gl_FragColor = vec4(vColour.rgb * vLightWeighting, vColour.a);
    20     }
    21 </script> 
    22  
    23 <script id="vertex-shader" type="x-shader/x-vertex"> 
    24     attribute vec3 aVertexPosition;
    25     attribute vec3 aVertexNormal;
    26     attribute vec4 aVertexColour;    
    27 
    28     uniform mat4 uMVMatrix;
    29     uniform mat4 uPMatrix;
    30     uniform mat3 uNMatrix;
    31 
    32     uniform vec3 uAmbientColour;
    33     uniform vec3 uLightingDirection;
    34     uniform vec3 uDirectionalColour;
    35     uniform vec3 uPointLightingLocation;
    36     uniform vec3 uPointLightingColour;
    37 
    38     uniform bool uUseLighting;
    39 
    40     varying vec4 vColour;
    41     varying vec3 vLightWeighting;
    42  
    43     void main(void) 
    44     {
    45         vec4 mvPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
    46         gl_Position = uPMatrix * mvPosition;
    47         vColour = aVertexColour;
    48 
    49         if (!uUseLighting)
    50         {
    51             vLightWeighting = vec3(1.0, 1.0, 1.0);
    52         }
    53         else
    54         {
    55             //vec3 transformedNormal = uNMatrix * aVertexNormal;
    56             //float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0);
    57             //vLightWeighting = uAmbientColour + uDirectionalColour * directionalLightWeighting;
    58 
    59             vec3 lightDirection = normalize(uPointLightingLocation - mvPosition.xyz);
    60             vec3 transformedNormal = uNMatrix * aVertexNormal;
    61             float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0);
    62             vLightWeighting = uAmbientColour + uPointLightingColour * directionalLightWeighting;
    63         }
    64     }
    65 </script>
    66 </head>
    67  
    68 <body onload="boot();">
    69 <canvas id="machine">
    70   Download a browser with WebGL support, like Chrome or Firefox.
    71 </canvas>
    72 </body>
    73  
    74 </html>