[SOLVED] Cut out and carve an object by a screen shape

In my program I have objects that can move around the screen and must to be cut out of certain screen territories. Like if such an object comes half on such a territory, half of it is cut out, and exactly in the shape of the territory’s border. How can I acheive that?

My initial solution was this: take an image of the size of the screen that has the area visible for object being white, and the invisible one - black. So this mask is set inside the shader as MaskMap and use the gl_FragCoord.xy to determine which pixel currently we’re on and to either discard it or draw. However, this does not seem to work…

Solved - I was miscalulating the coordinates.