#version 460

layout ( packed, binding = 0 ) uniform events
{

	vec2 mouse;

	vec2 window;

};

layout ( packed, binding = 1 ) uniform orientation
{

	vec2 quadPosition;

};

struct quad{

	vec2 position;

	vec2 size;

	vec3 color;

	float zIndex;

	float opacity;

	int textureIndex;

	int features;

	int elementIndex;

	//int characters[];

};


layout( std430, binding = 2 ) readonly buffer meshes
{

	quad meshArray[100];
	
};


layout(location = 0) in vec3 position;

layout(location = 1) in vec2 textureCoordinates;

layout(location = 2) in int meshIndex;



out 	vec2 	vertex_textureCoordinates;

out 	float 	vertex_meshIndex;


out 	vec3 	quad_color;

out 	vec2 	quad_position;

out 	vec2 	quad_size;

flat out int 	quad_textureIndex;

flat out int 	quad_useTexture;

flat out float 	quad_opacity;


int checkFeature( int features, int currentFeature ) {
	
	if ( ( features & currentFeature ) > 0 ) {

		return 1;

	} else {

		return -1;

	}

}

void main() {

	quad currentMesh	= meshArray[ meshIndex ];


	vec2 quadPosition	= ( position.xy ); 

	// vec2( -1.0, -1.0 ) -> vec2( 1.0, 1.0 )


	quadPosition		+= vec2( 1.0 );

	quadPosition		/= 2.0;

	// vec2( 0, 0 ) -> vec2( 1.0, 1.0 )


	vec2 bias			= vec2(0, 0); // ??????



	quadPosition 		*= currentMesh.size;

	quadPosition		+= currentMesh.position.xy;

	quadPosition		+= bias;

	quadPosition		/= window;




	quadPosition		*= 2.0;

	quadPosition		-= vec2( 1.0 );


	//quadPosition.y		+= .02;

	quadPosition.y		*= -1;
 
	quad_color 					= currentMesh.color;

	quad_size					= currentMesh.size;

	quad_position				= currentMesh.position.xy;

	quad_opacity				= currentMesh.opacity;



	
	
	gl_Position.xy				= quadPosition;



	const int useTexture		= 1;
	

	quad_useTexture				= checkFeature( currentMesh.features, useTexture );
	
	quad_textureIndex			= currentMesh.textureIndex;

	vertex_textureCoordinates 	= textureCoordinates;

	vertex_meshIndex 			= float( meshIndex );

	gl_Position.w = 1.0;


	gl_Position.z				= -currentMesh.zIndex / 10000;

}

