#ifdef _VERSION_CHECK_
	#ifndef MSET

		#ifndef _NOT_INSTANCED_
			#version 120
			#extension EXT_gpu_shader4 : enable          
		#endif
		
	#endif
#endif

varying vec3	w0,
				w1,
				w2;
//attribute vec3 	normal;
attribute vec4	binormal; 
varying vec3	viewDir;
uniform mat4	WTM;
uniform vec3	campos;

varying vec2 	texcoord;
uniform vec2	tscale0;

#ifndef _NOT_INSTANCED_
	uniform samplerBuffer	WorldPoses;
#endif
//uniform vec2	tofs;		// cambiare in uniform
	
void main()
{  
	vec4 normal4   	= vec4( gl_Normal.xyz, 0.0 );
	vec4 binormal4  = vec4( binormal.xyz, 0.0 );
	vec4 tangent4   = vec4(cross(binormal4.xyz,normal4.xyz),0.0);
	binormal4.xyz*=binormal.w;

	vec4 	outTangent,
			outNormal,
			outBinormal;
				
	#ifndef _NOT_INSTANCED_
		mat4 MTX;
		int index=gl_InstanceID*4;
		MTX[0]=texelFetchBuffer(WorldPoses,index);
		MTX[1]=texelFetchBuffer(WorldPoses,index+1);
		MTX[2]=texelFetchBuffer(WorldPoses,index+2);
		MTX[3]=texelFetchBuffer(WorldPoses,index+3);

		outNormal=normalize(MTX*normal4);
		outTangent=normalize(MTX*tangent4);
		outBinormal=normalize(MTX*binormal4);
					
		w0=vec3(outTangent.x,outBinormal.x,outNormal.x);
		w1=vec3(outTangent.y,outBinormal.y,outNormal.y);
		w2=vec3(outTangent.z,outBinormal.z,outNormal.z);

		// transform to world space
		vec4 vpos=MTX*gl_Vertex;

		// compute view direction
		viewDir=campos-vpos.xyz;
		
		gl_Position = (gl_ModelViewProjectionMatrix*MTX)*gl_Vertex;//ftransform();
		texcoord = gl_MultiTexCoord0.st*tscale0;// + tofs;
		
	#else
	
		outNormal=normalize(WTM*normal4);
		outTangent=normalize(WTM*tangent4);
		outBinormal=normalize(WTM*binormal4);
					
		w0=vec3(outTangent.x,outBinormal.x,outNormal.x);
		w1=vec3(outTangent.y,outBinormal.y,outNormal.y);
		w2=vec3(outTangent.z,outBinormal.z,outNormal.z);

		// transform to world space
		vec4 vpos=WTM*gl_Vertex;

		// compute view direction
		viewDir=campos-vpos.xyz;
		
		gl_Position = ftransform();
		texcoord = gl_MultiTexCoord0.st*tscale0;// + tofs;
	
	#endif
}