HELP! DECALING...

Hi dudes, i’d like to understand a little bit more of stencil buffer and decaling.
I’m trying to create a decal on a 3d-world and i’ve read a bit on stencil to create some functi«ons for the decal.

 
void setStencil()
{
	glEnable(GL_STENCIL_TEST);
	glStencilFunc(GL_ALWAYS,1,1);
	glStencilOp(GL_KEEP, GL_ZERO, GL_REPLACE);
}

draw wall

void setDecal()
{
	glStencilFunc(GL_EQUAL, 1, 1);
	glStencilMask(GL_FALSE);
	glDisable(GL_DEPTH_TEST);
}

make decal(a simple square)

void disableStencil()
{

	glEnable(GL_DEPTH_TEST);
	glStencilMask(GL_TRUE);
	glStencilFunc(GL_ALWAYS,1,1);
	glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
	 glDisable(GL_STENCIL_TEST);
}

The question is: the decal is ALWAYS visible, even when i put a object in front of it…Can anyone help me on this one?

The decal stencil approach is really best suited to planar surfaces like a runway. Unless you change the stencil test value for each surface it will not behave well for a self occluding 3D surface scenario like terrain.

You should investigate glPolygonOffset instead of stencil or look at using the same polygons with a different texture that will typically give you z invariance.