Problem with stencilling

I’m trying to use stencilling to control where my reflected object is visible like I’ve seen in several tutorials, but I can’t seem to prevent the reflection from showing up at all. Here’s the relevant code snippet:

  glClear(GL_STENCIL_BUFFER_BIT);
  
  //disable drawing
  glDisable(GL_DEPTH_TEST);
  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  
  //enable stencilling
  glEnable(GL_STENCIL_TEST);
  glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
  glStencilFunc(GL_ALWAYS, 1, 1);

  //would draw stencil for reflective surface here

  //reenable drawing
  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  glEnable(GL_DEPTH_TEST);
  
  //set up stencil
  glStencilFunc(GL_EQUAL, 1, 1);
  glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

  draw_reflection();

  //disable stencilling
  glDisable(GL_STENCIL_TEST);

From what I understand of stencilling, since I haven’t specified any area to have a stencil value of 1, the object from draw_reflection shouldn’t be drawn at all. When I run the code, the reflected object shows up unchanged. What am I not understanding here?

Try glClearStencil(0) before glClear(GL_STENCIL_BUFFER_BIT).

Thanks for the tip, but that didn’t change anything.

Do you actually have a stencil buffer ?
You should ask for it at context creation (like double buffering, depth buffer, etc) otherwise the driver may or may not create one for you.
Example for glut :
glutInitDisplayMode(GLUT_DOUBLE|GLUT_STENCIL|GLUT_DEPTH|GLUT_ALPHA);

Then once the gl window is created, you can verify how many stencil bits you actually got :

int stencilBits=-1;
glGetIntegerv(GL_STENCIL_BITS, &stencilBits);

If stencilBits is 0, then you effectively have no stencil buffer available.

Yep, that did it. Thanks for the help. Embarrassing to have missed something so simple. >.<