# Stencils to control drawing only to specific parts of screen

Here is a demonstration of how to use stencils using these these functions:

mglOpen;
mglScreenCoordinates;
%Draw an oval stencil
mglStencilCreateBegin(1);
mglFillOval(300,400,[100 100]);
mglStencilCreateEnd;
mglClearScreen;
% now draw some dots, masked by the oval stencil
mglStencilSelect(1);
mglPoints2(rand(1,5000)*500,rand(1,5000)*500);
mglFlush;
mglStencilSelect(0);

## mglStencilCreateBegin: Start drawing a stencil

purpose: Begin drawing to stencil. Until mglStencilCreateEnd is called, all drawing operations will also draw to the stencil. Check MGL.stencilBits to see how many stencil planes there are. If invert is set to one, then the inverse stencil is made
usage: mglStencilCreateBegin(stencilNumber,invert)

argument value
stencilNumber stencil number, usualy 1-8 but look at the global variable MGL.stencilBits to see how many stencil planes there are.
invert 1 or 0 to invert the stencil that is made

see example above.

## mglStencilCreateEnd: End drawing a stencil

purpose: Ends drawing to stencil usage: mglStencilCreateEnd

see example above.

## mglStencilSelect: Select a stencil

purpose: Sets which stencil to use, 0 for no stencil usage: mglStencilSelect(stencilNumber)

argument value
stencilNumber number of stencil to use

See example above.