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