#version 330 core
#define MAX_JOINTS 128

uniform sampler2D iTexture0; // Albedo
uniform sampler2D iTexture1; // Specular
uniform sampler2D iTexture2; // Smoothness
uniform sampler2D iTexture3; // Normal
uniform sampler2D iTexture4; // Height
uniform sampler2D iTexture5; // Occulusion
uniform sampler2D iTexture6; // Emission
uniform sampler2D iTexture7; // Detail Mask
uniform sampler2D iTexture8; // Detail Albedo
uniform sampler2D iTexture9; // Detail Normal
uniform samplerCube iCubeTexture0; // Skybox
uniform samplerCube iCubeTexture1; // Skybox Radiance
uniform samplerCube iCubeTexture2; // Skybox Irradiance

uniform vec4 iLeftEye;
uniform vec4 iMouse;
uniform vec4 iResolution;
uniform vec4 iGlobalTime;

uniform mat4 iMiscRoomData;
uniform vec4 iPlayerPosition;
uniform vec4 iUseClipPlane;
uniform vec4 iClipPlane;
uniform vec4 iFogEnabled;
uniform vec4 iFogMode;
uniform vec4 iFogDensity;
uniform vec4 iFogStart;
uniform vec4 iFogEnd;
uniform vec4 iFogCol;
uniform mat4 iRoomMatrix;

uniform mat4 iMiscObjectData;
uniform vec4 iConstColour;
uniform vec4 iChromaKeyColour;
uniform vec4 iUseLighting;
uniform vec4 iObjectPickID;
uniform mat4 iModelMatrix;
uniform mat4 iViewMatrix;
uniform mat4 iProjectionMatrix;
uniform mat4 iInverseViewMatrix;
uniform mat4 iModelViewMatrix;
uniform mat4 iModelViewProjectionMatrix;
uniform mat4 iTransposeInverseModelMatrix;
uniform mat4 iTransposeInverseModelViewMatrix;
uniform vec4 iBlend;
uniform vec4 iUseSkelAnim;
uniform mat4 iSkelAnimJoints[MAX_JOINTS];

uniform vec4 iAmbient;
uniform vec4 iDiffuse;
uniform vec4 iSpecular;
uniform vec4 iShininess;
uniform vec4 iUseTexture[4];


// Vertex Shader Inputs/Attributes
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aTexCoord0;
layout(location = 3) in vec2 aTexCoord1;
layout(location = 4) in vec4 aColor;
layout(location = 5) in vec3 aBlendV0;
layout(location = 6) in vec3 aBlendV1;
layout(location = 7) in vec3 aBlendV2;
layout(location = 8) in vec3 aBlendN0;
layout(location = 9) in vec3 aBlendN1;
layout(location = 10) in vec3 aBlendN2;
layout(location = 11) in vec4 aSkelAnimIndexes;
layout(location = 12) in vec4 aSkelAnimWeights;

// Vertex Shader Ouputs
smooth out vec3 iPosition;
smooth out vec3 iPositionWorld;
smooth out vec3 iPositionCamera;
smooth out vec3 iNormal;
smooth out vec3 iNormalWorld;
smooth out vec3 iNormalCamera;
smooth out vec2 iTexCoord0;
smooth out vec2 iTexCoord1;
smooth out vec4 iColor;

void main(void) 
{
	vec4 position = vec4(aPosition, 1.0);
	vec3 blend_pos = position.xyz;
	vec3 blend_norm = aNormal;

	//skeletal animation

	if (iUseSkelAnim.x == 1)
	{
		int index = int(aSkelAnimIndexes.x);     
		if (index >= 0)
		{
			blend_pos = (iSkelAnimJoints[index] * position).xyz * aSkelAnimWeights.x;
			blend_norm = (iSkelAnimJoints[index] * vec4(aNormal, 0.0)).xyz * aSkelAnimWeights.x;
		}
       
		index = int(aSkelAnimIndexes.y);        
		if (index >= 0) {
			blend_pos += (iSkelAnimJoints[index] * position).xyz * aSkelAnimWeights.y;
			blend_norm += (iSkelAnimJoints[index] * vec4(aNormal, 0.0)).xyz * aSkelAnimWeights.y;
		}
		
		index = int(aSkelAnimIndexes.z);        
		if (index >= 0) {
			blend_pos += (iSkelAnimJoints[index] * position).xyz * aSkelAnimWeights.z;
			blend_norm += (iSkelAnimJoints[index] * vec4(aNormal, 0.0)).xyz * aSkelAnimWeights.z;
		}
    
		index = int(aSkelAnimIndexes.w);        
		if (index >= 0) {
			blend_pos += (iSkelAnimJoints[index] * position).xyz * aSkelAnimWeights.w;
			blend_norm += (iSkelAnimJoints[index] * vec4(aNormal, 0.0)).xyz * aSkelAnimWeights.w;
		}
	}
	else
	{    
		blend_pos += (aBlendV0 - position.xyz) * iBlend.x;
		blend_norm += (aBlendN0 - aNormal.xyz) * iBlend.x;
		blend_pos += (aBlendV1 - position.xyz) * iBlend.y;
		blend_norm += (aBlendN1 - aNormal.xyz) * iBlend.y;
		blend_pos += (aBlendV2 - position.xyz) * iBlend.z;
		blend_norm += (aBlendN2 - aNormal.xyz) * iBlend.z;
	}
	iPosition = blend_pos.xyz;

  float intensity=3.0*abs(sin(iGlobalTime.x));
	float miny=-0.4;
  float maxy=0.4;
  
  float angle = (aPosition.y-miny)/(maxy-miny);
  if (angle <0.0) angle =0.0;
  if (angle >1.0) angle=1.0;
  angle*=intensity;
  
  float s = sin(angle);
  float c = cos(angle);
  vec4 defpos = vec4(c*aPosition.x-s*aPosition.z,aPosition.y,s*aPosition.x+c*aPosition.z,1.0); 
  iPosition = defpos.xyz;
 
	iNormal = normalize(blend_norm);

	vec4 defnormal=vec4(c*iNormal.x-s*iNormal.z,iNormal.y,s*iNormal.x+c*iNormal.z,0.0); 
  iNormal=defnormal.xyz;

	iNormal = normalize(iNormal);

	iPositionWorld = (iModelMatrix * defpos).xyz;  
	iPositionCamera = (iModelViewMatrix * defpos).xyz;   
	iNormalWorld = (iTransposeInverseModelMatrix * vec4(iNormal, 0.0)).xyz;  
	iNormalCamera = (iTransposeInverseModelViewMatrix * vec4(iNormal, 0.0)).xyz; 
	iTexCoord0 = vec2(aTexCoord0.x, 1.0 - aTexCoord0.y);
	iTexCoord1 = vec2(aTexCoord1.x, 1.0 - aTexCoord1.y);
	iColor = aColor;
	gl_Position = iModelViewProjectionMatrix * defpos;
}
