Easy StencilTest


#1

I wrote a simple code, but the result was different from what I thought.

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


	float sabun = -10;

	glColor3f(1, 0, 0);
	glStencilFunc(GL_ALWAYS, 1, 0);
	glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
	planecreate(ofVec3f(0,0, sabun));

	sabun =0;

	glColor3f(0, 1,0);
	glStencilFunc(GL_ALWAYS, 0, 0);
	glStencilOp(GL_KEEP, GL_INCR, GL_INCR);
	planecreate(ofVec3f(10, 10, sabun));

	sabun = 10; 
	
	glColor3f(0, 0, 1);
	glStencilFunc(GL_EQUAL,2, 0);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
	planecreate(ofVec3f(15, 15, sabun));


	glDisable(GL_STENCIL_TEST);

//

void  testApp::planecreate(ofVec3f v) {
	glBegin(GL_POLYGON);
	float x = 40;
	float y = 40;
	float z = v.z;

	glVertex3d(-x+ v.x,-y+v.y,z);
	glVertex3d(-x+ v.x, y + v.y,z);
	glVertex3d( x+ v.x, y + v.y, z);
	glVertex3d( x+ v.x,-y + v.y, z);
	glEnd();
}

I expected that the blue panel had been scraped off…


#2

the default OF window doesn’t have a stencil buffer so even if you enable it in opengl it won’t work. to create a window with a stencil buffer you need to use:

int main(){
    ofGLFWWindowSettings settings;
    settings.stencilBits = 8;
    ofCreateWindow(settings);
    return ofRunApp(new ofApp);
}

#3

Arturo, thanks to you, the problem has been solved!
Thank you