machine.html
author Eugen Sawin <sawine@me73.com>
Sat, 30 Apr 2011 04:16:57 +0200
changeset 37 97af0c060479
parent 36 a828c4cde5b3
permissions -rwxr-xr-x
Added mipmapping.
     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 vec2 vTextureCoord;
    16     varying vec3 vTransformedNormal;
    17     varying vec4 vPosition;
    18 
    19     uniform float uMaterialShininess;
    20 
    21     uniform bool uUseLighting;
    22     uniform bool uUseTexture;
    23 
    24     uniform sampler2D uSampler;
    25    
    26     uniform vec3 uAmbientColour;
    27     uniform vec3 uPointLightingLocation;
    28     uniform vec3 uPointLightingSpecularColour;
    29     uniform vec3 uPointLightingDiffuseColour;
    30  
    31     void main(void)
    32     {
    33         vec3 lightWeighting;
    34         if (!uUseLighting) 
    35         {
    36             lightWeighting = vec3(1.0, 1.0, 1.0);
    37         }
    38         else
    39         {
    40             vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
    41             vec3 normal = normalize(vTransformedNormal);
    42             vec3 eyeDirection = normalize(-vPosition.xyz);
    43             vec3 reflectionDirection = reflect(-lightDirection, normal);
    44             float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);
    45             float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
    46             lightWeighting = uAmbientColour + uPointLightingSpecularColour * specularLightWeighting
    47                              + uPointLightingDiffuseColour * diffuseLightWeighting;
    48         }  
    49         vec4 colour;
    50         if (uUseTexture)
    51         {
    52             colour = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
    53         }
    54         else
    55         {
    56             colour  = vColour;
    57         }
    58         gl_FragColor = vec4(colour.rgb * lightWeighting, colour.a);
    59     }
    60 </script> 
    61  
    62 <script id="vertex-shader" type="x-shader/x-vertex"> 
    63     attribute vec3 aVertexPosition;
    64     attribute vec3 aVertexNormal;
    65     attribute vec4 aVertexColour;    
    66     attribute vec2 aTextureCoord;
    67 
    68     uniform mat4 uMVMatrix;
    69     uniform mat4 uPMatrix;
    70     uniform mat3 uNMatrix;
    71 
    72     varying vec4 vColour;
    73     varying vec2 vTextureCoord;
    74     varying vec3 vTransformedNormal;
    75     varying vec4 vPosition;
    76  
    77     void main(void) 
    78     {
    79         vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
    80         gl_Position = uPMatrix * vPosition;
    81         vColour = aVertexColour;
    82         vTextureCoord = aTextureCoord;
    83         vTransformedNormal = uNMatrix * aVertexNormal;
    84     }
    85 </script>
    86 </head>
    87  
    88 <body onload="boot();">
    89 <canvas id="machine">
    90   Download a browser with WebGL support, like Chrome or Firefox.
    91 </canvas>
    92 </body>
    93  
    94 </html>