Problem with objects inside/outside an enclosed volume

Hi all!!

I’m trying to write codes to show objects(sphere,cylinder…etc) inside a volume defined by a Bezier surface and a polygon (looks like a hemisphere), objects not inside this volume should be hidden.

The way I’m currently using is:

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glDisable(GL_DEPTH_TEST);

DRAW SURFACES

glEnable(GL_DEPTH_TEST);
glColorMask(1,1,1,1);
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

DRAW OBJECTS

glDisable(GL_STENCIL_TEST);

DRAW SURFACES

if I look far away from it, it “looks” ok.
It shows a hemisphere with spheres, cylinders inside it.

Now, the problem comes. When I try to slice it(by changing the difference of the last two parameters of gluPerspective to a really small number), objects inside won’t show up.
In other words, what I’m expecting is something like a “D” shape boundary with circles, and lines inside it.

Any suggestion is appreciated.