# Stencil BUffer Functioning

Dear All,

I’d like to know how whether my code walkthrough interpretation is correct or not in the given code snippet below. What I expect to see based on the runing code is: First triangle(skewed one) to be used as mask and as a resulting of this stencil buffer to be filled with masquerading value of 1s.

Draw the 2nd triangle on screen but using the stencil buffer to block only the portion of screen where filled with 1s. I’m pretty much familiar with related stencil buffer calls and read many tutorials related to that, but seems that I never get any tangible solution up to now. I don’t want to succeed simply to get what I want on screen for this example, instead I really want to grasp the full idea behind of stenciling.

I also commented out the code and what I interpret from it. I really will appreciate your help if you point out where actually I’m making mistake, or my interpretation is wrong or correct.

``````glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT);

glStencilFunc(GL_NEVER,1,1); // Stencil func will always fail, so due to that any drawn image on color buffer will affect stencil buffer also
glStencilOp(GL_REPLACE, GL_KEEP,GL_KEEP); // Here we specify how to modify the stencil buffer, 0s will be replaced by 1s in stencil buffer where shape is drawn

glStencilMask(1); // Enable writing to stencil buffer
glPushMatrix();
glRotatef(30, 0.0f, 0.0f, 1.0f);
DrawTri();        // Draw the triangle so the stencil buffer will be overwritten with 1s where there is skewed triangle
glPopMatrix();

glStencilMask(0); // Turn off writing to stencil buffer
glStencilFunc(GL_EQUAL,1,1); // Instruct where to draw 2nd triangle clipped by 1st triangle (skewed).

DrawTri();  // Draw the 2nd triangle where there is 1s in stencil buffer
SwapBuffers(hDC);
``````

This says that the stencil “test” will always fail. What happens when it fails is dictated by the first argument to glStencilOp(). You state in the next line that it should replace the stencil buffer value with the ref value, 1.

glStencilMask(1); // Enable writing to stencil buffer

Not quite. This says to enable writing to the first bit of the stencil buffer only. For instance, if you’re ref value was 2 (or any value where the least significant bit is zero), then you’d be writing 0s to that stencil buffer bit instead of 1.

glStencilMask(0); // Turn off writing to stencil buffer
glStencilFunc(GL_EQUAL,1,1); // Instruct where to draw 2nd triangle clipped by 1st triangle (skewed).

Instead of StencilMask, I think you could instead use StencilOp() and set them all to KEEP. But this should work too.

Thank You,

glStencilMask(1); // Enable writing to stencil buffer
Not quite. This says to enable writing to the first bit of the stencil buffer only. For instance, if you’re ref value was 2 (or any value where the least significant bit is zero), then you’d be writing 0s to that stencil buffer bit instead of 1.

Your prognosis was completely correct the code is working, I created a project from scratch and its operating as I expected probably I mis-configured something undeliberately.

In order to fully grasp the idea behind of stenciling, I’d like to know the below code is exactly where writing to stencil buffer is performed right?

``````glStencilMask(1); //
glPushMatrix();
glRotatef(30, 0.0f, 0.0f, 1.0f);
DrawTri();        // Draw the triangle so the stencil buffer will be overwritten with 1s where there is skewed triangle
glPopMatrix();
``````

Thanks,

Yes, once the triangles dispatched in DrawTri() are rasterized on the GPU.