Can't get stenciling to work

All I’m trying to do at the moment is to get one ofRectangle to stencil the other, but it’s not working. I’ve set the stencilbits to 8. I’ve searched the forum and other people have always managed to get it working, I’ve tried their solutions but it just doesn’t work for me.

glEnable(GL_STENCIL_TEST|GL_DEPTH_TEST);
glClearStencil(0);
glClearDepth(0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

glStencilFunc(GL_ALWAYS, 1, 0);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

ofSetColor(0, 255, 0);
ofDrawRectangle(ofRectangle(20,380,100,100));

glStencilFunc(GL_EQUAL, 1, 0);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

ofSetColor(255, 0, 0);
ofDrawRectangle(ofRectangle(40,400,100,100));

glDisable(GL_STENCIL_TEST);

edit: never mind, got it working, just needed to play with the functions parameters.

Can you post the correct function parameters?

Call Draw(), then draw whatever you want, then call CompleteDraw().

void InterfaceAreaBorder::Draw()
{
    DrawBackground();
    DrawBoundary();
    
    glStencilFunc(GL_EQUAL, 1, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
}

void InterfaceAreaBorder::DrawBackground()
{
    glEnable(GL_STENCIL_TEST);
    glDisable(GL_DEPTH_TEST);
    glClearStencil(0);
    glClearDepth(0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    
    glStencilFunc(GL_ALWAYS, 1, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
    
    ofSetColor(_backgroundColour);
    ofFill();
    ofSetLineWidth(_boundaryWidth);
    DrawRect(0);
}

void InterfaceAreaBorder::DrawBoundary()
{
    ofSetColor(_boundaryColour);
    ofNoFill();
    
    glStencilFunc(GL_ALWAYS, 0, 0);
    glStencilOp(GL_NEVER, GL_NEVER, GL_NEVER);
    
    DrawRect((int)_boundaryWidth/2);
}

void InterfaceAreaBorder::CompleteDraw()
{
    //disable stencil
    glDisable(GL_STENCIL_TEST);
}