<|start_filename|>assets/shaders/common.glsl<|end_filename|>
#ifndef COMMON_GLSL_INCLUDED
#define COMMON_GLSL_INCLUDED

precision highp float;

#ifdef VERTEX_SHADER
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
#endif
uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat3 uNormalMatrix;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform mat4 uModelViewProjectionMatrix;
uniform vec2 uViewportSize;

uniform sampler2D uTexture;

#endif


<|start_filename|>assets/shaders/fog.glsl<|end_filename|>
#ifndef FOG_GLSL_INCLUDED
#define FOG_GLSL_INCLUDED

/* Calculates the fog factor for a fog of the given density and the current
   vertex or fragment position.  This function behaves differently depending
   on if it's excuted from the vertex or fragment shader. */
float getFogFactor(float density)
{
    const float LOG2 = 1.442695;
    float z;
#ifdef VERTEX_SHADER
    z = length(vec3(uModelViewMatrix * vec4(aVertexPosition, 1.0)));
#else
    z = gl_FragCoord.z / gl_FragCoord.w;
#endif
    return clamp(exp2(-density * density * z * z * LOG2), 0.0, 1.0);
}

#endif


<|start_filename|>assets/style.css<|end_filename|>
body            { background: #222; font-family: 'Helvetica Neue', 'Helvetica',
                  sans-serif; margin: 40px 0; }
h1              { text-align: center; margin: 0; color: #ddd; font-weight: normal;
                  font-size: 30px; }
p               { font-weight: normal; font-size: 14px; text-align: center;
                  margin: 0; color: white; }
#viewport       { display: block; margin: 20px auto; border: 1px solid #111; }

img#throbber    { position: fixed; top: 20px; right: 20px; }

#debugpanel     { border: 1px solid #ccc; color: black;
                  background: #eee; margin: 20px auto; width: 960px;
                  border-collapse: collapse; }
#debugpanel tr  { height: 30px; }
#debugpanel th  { text-align: left; width: 0px; padding: 0 10px; white-space: pre;
                  width: 200px; }
#debugpanel td  { text-align: left; white-space: pre; }


<|start_filename|>Makefile<|end_filename|>
all: compile

watch-compile:
	coffee -cwo compiled src/*.coffee

compile:
	coffee -co compiled src/*.coffee

upload: compile
	rsync -a . pocoo.org:public_html/webglmc/


<|start_filename|>assets/shaders/block.glsl<|end_filename|>
#include "common.glsl"
#include "lighting.glsl"
#include "fog.glsl"
uniform vec3 uSunDirection;
uniform vec4 uFogColor;
uniform float uFogDensity;
uniform vec4 uSunColor;
varying vec3 vNormal;
varying vec2 vTextureCoord;
varying vec3 vHalfVec;
varying vec3 vSunDirection;
varying float vFogFactor;

#ifdef VERTEX_SHADER
void main(void)
{
    gl_Position = uModelViewProjectionMatrix * vec4(aVertexPosition, 1.0);
    vTextureCoord = aTextureCoord;
    vSunDirection = uSunDirection;
    vNormal = uNormalMatrix * aVertexNormal;
    vHalfVec = normalize(aVertexPosition + vSunDirection);
    vFogFactor = getFogFactor(uFogDensity);
}
#endif

#ifdef FRAGMENT_SHADER
void main(void)
{
    vec4 darkness = vec4(0.1, 0.1, 0.1, 1.0);
    vec4 ambient = vec4(0.4, 0.4, 0.4, 1.0);
    vec4 diffuse = vec4(0.0);
    vec4 specular = vec4(0.0);
    vec4 color = texture2D(uTexture, vec2(vTextureCoord.s, vTextureCoord.t));

    vec4 sunColor = uSunColor;
    sunColor.a = 0.3;
    directionalLight(vNormal, vSunDirection, vHalfVec,
                     30.0, sunColor, vec4(0.0), diffuse, specular);

    color = color * clamp(darkness + ambient + diffuse, 0.0, 1.0);
    color = mix(uFogColor, color, vFogFactor);
    gl_FragColor = clamp(color, 0.0, 1.0);
}
#endif


<|start_filename|>assets/shaders/lighting.glsl<|end_filename|>
#ifndef LIGHTING_GLSL_INCLUDED
#define LIGHTING_GLSL_INCLUDED

void directionalLight(in vec3 normal,
                      in vec3 lightDir,
                      in vec3 halfVec,
                      in float shininess,
                      in vec4 lightDiffuse,
                      in vec4 lightSpecular,
                      inout vec4 diffuse,
                      inout vec4 specular)
{
    float nDotVp;   /* normal . light dir */
    float nDotHv;   /* normal . half vec */
    float pf;       /* power factor */

    nDotVp = max(0.0, dot(normal, normalize(lightDir)));
    nDotHv = max(0.0, dot(normal, halfVec));
    pf = (nDotVp == 0.0) ? 0.0 : pow(nDotHv, shininess);

    diffuse += lightDiffuse * nDotVp;
    specular += lightSpecular * pf;
}

#endif


<|start_filename|>assets/shaders/postprocess.glsl<|end_filename|>
#include "common.glsl"
#include "fxaa.glsl"

#ifdef VERTEX_SHADER
void main(void)
{
    gl_Position = vec4(aVertexPosition, 1.0);
}
#endif

#ifdef FRAGMENT_SHADER
void main(void)
{
    gl_FragColor = applyFXAA(gl_FragCoord.xy, uTexture);
}
#endif
