// All content © 2010-2011 DigiPen (USA) Corporation, all rights reserved.

//Vertex shader for the lava effect

#version 150

// ---------------------------------------------------------------------------
// uniforms

uniform mat4		uMVP; //modelview-projection matrix
uniform mat4		uModelView; 
uniform mat3		uTextureMatrix0;	
uniform mat3		uTextureMatrix1;
uniform float		uHeightMultiplier; //height multiplier to scale the normals

// ---------------------------------------------------------------------------
// to fragment shader

out vec2			vTexCoord0;
out vec2			vTexCoord1;

out float			vDepth; //depth
out mat3			vTBNMatrix; //matrix to transform from view space to tangent

out vec3			vLightDir;

// ---------------------------------------------------------------------------
// attributes

//for normal mapping
in vec3				in_Tan;
in vec3				in_Bin;

//default variables
in vec3				in_Pos; 
in vec3				in_Normal;
in vec2				in_TexCoord;

// ---------------------------------------------------------------------------
// const

const vec4			LIGHT_DIR = vec4( 0.5, -1.0, 0.0, 0.0 );

void main()
{
	vec4 vert = vec4( in_Pos, 1.0 );
	gl_Position = uMVP * vert;

	//view space depth
	vDepth = (uModelView * vert).z;

	//view space normal
	vec3 normal = ( uModelView * vec4(in_Normal, 0.0) ).xyz;
	normal /= dot( normal, normal ); //take into account scaling

	//view space light direction
	vLightDir = normalize( (uModelView * LIGHT_DIR).rgb );

	//-------------------------------------------------------
	//calculate TBN matrix
	
	//transform tangent/binormal to view space
	//ignore translation during the transform
	vTBNMatrix = mat3( (uModelView * vec4( in_Tan, 0.0 ) ).xyz, (uModelView * vec4( in_Bin, 0.0 ) ).xyz, normal/uHeightMultiplier );

	//calculate texture coords for both layers
	vTexCoord0 = (uTextureMatrix0 * vec3( in_TexCoord, 1.0 )).st; 
	vTexCoord1 = (uTextureMatrix1 * vec3( in_TexCoord, 1.0 )).st;
}
