#ifdef _VERSION_CHECK_
	#ifndef MSET

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

#ifdef SKINNING

	//uniform mat4 	boneMatrices[100];
			
		uniform samplerBuffer 	boneMatrices;
	
	attribute vec4  weights;
	attribute vec4  matrixIndices;

#endif

#ifndef _NOT_INSTANCED_
	uniform samplerBuffer 			WorldPoses;
#endif

varying vec2 			texcoord;
uniform vec2			tscale;
uniform vec2			tofs;
varying vec2			alphacoords;

vec2 ComputeFrame(float dt, vec2 nf)
{
	vec2 ofs=vec2(1.0,1.0)/nf;
	float frame=floor(dt*nf.x*nf.y);
	float row=floor(frame/nf.x);
	float col=floor(frame-nf.x*row);
	
	return vec2(ofs.x*col, 1.0-(ofs.y*row)-ofs.y);
}

void main()
{
#ifdef SKINNING

	vec4 index  		= matrixIndices;
	vec4 weight 		= weights;
	vec4 position   	= vec4( 0.0, 0.0, 0.0, 0.0 );
	mat4 TM;

	int j;
  	for( int i = 0; i < 4; i++ )
 	{
	
				j=int(index[i])*4;
				TM[0]=texelFetchBuffer(boneMatrices,j);
				TM[1]=texelFetchBuffer(boneMatrices,j+1);
				TM[2]=texelFetchBuffer(boneMatrices,j+2);
				TM[3]=texelFetchBuffer(boneMatrices,j+3);
				
				position = position + (TM * gl_Vertex) *weight[i];
			
			
 	    // Apply influence of bone i
	//	TM=boneMatrices[int(index[i])];
    	//position = position + (TM * gl_Vertex) *weight[i];
	}
	gl_Position = gl_ModelViewProjectionMatrix * position;

#else

	#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);
		gl_Position = (gl_ModelViewProjectionMatrix*MTX)*gl_Vertex;//ftransform();
	#else
		gl_Position = ftransform();
	#endif

#endif
			
	texcoord = gl_MultiTexCoord0.st*tscale+tofs;	
	alphacoords = gl_MultiTexCoord0.st;
}