roc/data/shaders/shadow_pass2.vert
2011-06-16 11:34:10 -06:00

49 lines
No EOL
1.6 KiB
GLSL

uniform mat3 WorldToShadow;
uniform vec3 SphereOrigin;
uniform vec3 CameraPos;
uniform vec3 CameraDir;
uniform float DepthNear;
uniform float DepthFar;
varying vec2 DepthTexCoord;
varying vec3 ShadowNear;
varying vec3 ShadowDir;
void main()
{
vec4 tmp1 = ftransform();
gl_Position = tmp1;
// Predivide out w to avoid perspective-correct interpolation.
// The quantities being interpolated are screen-space texture
// coordinates and vectors to the near and far shadow plane,
// all of which have to be bilinearly interpolated.
// This could potentially be done by setting glHint,
// but it wouldn't be guaranteed to work on all hardware.
gl_Position.xyz /= gl_Position.w;
gl_Position.w = 1.0;
// Grab the transformed vertex's XY components as a texcoord
// for sampling from the depth texture from pass 1.
// Normalize them from [0,0] to [1,1]
DepthTexCoord = gl_Position.xy * 0.5 + 0.5;
// offset = vector to vertex from camera's position
vec3 offset = (gl_Vertex.xyz / gl_Vertex.w) - CameraPos;
// z = distance from vertex to camera plane
float z = -dot(offset, CameraDir);
vec3 shadowOffsetNear = offset * DepthNear / z;
vec3 shadowOffsetFar = offset * DepthFar / z;
vec3 worldPositionNear = CameraPos + shadowOffsetNear;
vec3 worldPositionFar = CameraPos + shadowOffsetFar;
vec3 shadowFar = WorldToShadow * (worldPositionFar - SphereOrigin);
ShadowNear = WorldToShadow * (worldPositionNear - SphereOrigin);
ShadowDir = shadowFar - ShadowNear;
}