uniform int poly_size;
uniform float[256] poly_x;
uniform float[256] poly_y;
uniform sampler2D u_texture;

varying vec2 v_texCoord;
varying vec4 v_fragmentColor;

bool pointInPolygon(vec2 point) {

  int i = 0;
  int j = 0;
  int nvert = poly_size;
  bool c = false;

  for(i = 0, j = nvert - 1; i < nvert; j = i++) {
    if( ( (poly_y[i]) >= point.y != (poly_y[j] >= point.y) ) &&
        (point.x <= (poly_x[j] - poly_x[i]) * (point.y - poly_y[i]) / (poly_y[j] - poly_y[i]) + poly_x[i])
      )
      c = !c;
  }

  return c;
}

void main()
{
	vec4 color = texture2D(u_texture, v_texCoord);

	if(pointInPolygon(v_texCoord) == false) color = vec4(0,0,0,0);
	
	gl_FragColor = color;
}
