Stencil Buffer

I am trying to use the stencil buffer to mask out part of the viewport so that when I clear the color buffer with black it does not clear out the masked parts. I have the mask in a 2D array and I am trying to use glDrawPixels to put the mask in the stencil buffer, but it doesn’t seem to work. Help!!!

If you have an array of unsigned bytes to put into the stencil use something like this:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glDisable(GL_STENCIL_TEST);
glDrawPixels(nWidth, nHeight, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, puBytes);

If you just have a true/false, 0/1, monochrome mask better use GL_BITMAP instead of GL_UNSIGNED_BYTE. Should be faster.

In any case, of you can draw the same thing with some geometric primitives, use those instead of glDrawPixels for better performance.