<vertex>
uniform mat4 matView;
uniform mat4 matTexture;
varying float depthDistance;

void main(void) {
	gl_Position = ftransform();
	gl_TexCoord[0].xyz = gl_Normal;

	vec4 pointEye = gl_ModelViewMatrix * gl_Vertex;

	vec4 pointTexture = matTexture * pointEye;
	vec4 planeTexture = matTexture * vec4(pointEye.xy, 0.0, pointEye.w);

	depthDistance = distance(pointTexture, planeTexture);
}
</vertex>

<fragment>
varying float depthDistance;
uniform float depthDivisor;
	<option name="first" value="1">
	void main(void) {
		gl_FragColor = vec4(gl_TexCoord[0].xyz, 1.0);
		//Why does it crash when setting it to depthDistance only!?
		gl_FragDepth = depthDistance / depthDivisor;
	}
	</option>

	<option name="first" value="0">	
	uniform sampler2DRectShadow prevDepthTexture;
	void main(void) {
		//Why does it crash when setting it to depthDistance only!?
		float depth = depthDistance / depthDivisor;

		vec3 coord = vec3(gl_FragCoord.st, (depth)-0.0001); //Change to unitstep * steplength? 
		float prevDepth = shadow2DRect(prevDepthTexture, coord).x;

		if (prevDepth - 0.5 > 0.0) {
			gl_FragColor = vec4(normalize(gl_TexCoord[0].xyz), 1.0);
			gl_FragDepth = depth;
		}
		else discard;
	}
	</option>
</fragment>
