// Copyright (C) 2011 by The Alexandra Institute Ltd.
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#version 330

precision mediump float;

// Attributes
in vec3 POSITION;
in vec3 NORMAL;
in vec2 TEXCOORD0;

out vec3 kWSPos;	// World-space position
out vec3 kWSNormal;	// World-space normal
out float fDepth;	// View-space depth
out vec4 kCSPos;	// Clip-space position
out vec2 kTexcoord;	// Texcoord

// Uniforms
uniform mat4 kWorld;				// World matrix
uniform mat4 kITWorld;				// Inverse-transposed world matrix
uniform mat4 kWorldView;			// World * View
uniform mat4 kWorldViewProjection;	// World * View * Projection

// Shader code
void main()
{
	// Transform to clip-space
	vec4 kHPos = vec4(POSITION, 1.0);
	kCSPos = kWorldViewProjection * kHPos;
	gl_Position = kCSPos;

	// Transform to world-space
	kWSPos = (kWorld * kHPos).xyz;

	// Transform normal to world-space
	kWSNormal =  (kITWorld * vec4(NORMAL, 1.0)).xyz;

	// View-space linear depth
	fDepth = (kWorldView * kHPos).z;

	// Pass-through texcoord
	kTexcoord = TEXCOORD0;
}
