problem CSG in OpenGL using MFC ( SDI) ?

Dear everybody,

I got a problem that I don’t know where to fix, I use MFC (SDI) for rendering. I execute CSG successfully, but the image looks like no depth. If I don’t execute CSG, it’s look like normal.

here is the picture that has no CSG:
http://upload.tinhco.net/thoai/csg3.JPG

here is the picture that has CSG:
http://upload.tinhco.net/thoai/csg4.JPG

The code that has no CSG, it render like normal,(there’s depth)

void CMaSimView:Render()
{
glEnable(GL_DEPTH_TEST);
RenderCutter();
RenderObject();
glDisable(GL_DEPTH_TEST);
}

If I replace the codes above with the CSG code below, then it looks like no depth


void CMaSimView:Render()
{
  glEnable(GL_DEPTH_TEST);
  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  glCullFace(GL_FRONT);     /* controls which face of a to use */
  RenderCutter();                  /* draw a face of a into depth buffer */

  /* use stencil plane to find parts of a in b */
  glDepthMask(GL_FALSE);
  glEnable(GL_STENCIL_TEST);
  glStencilFunc(GL_ALWAYS, 0, 0);
  glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
  glCullFace(GL_BACK);
  RenderObject();             /* increment the stencil where the front face of b is 
                           drawn */
  glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
  glCullFace(GL_FRONT);
  RenderObject();             /* decrement the stencil buffer where the back face of b is drawn */

  glDepthMask(GL_TRUE);
  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

  glStencilFunc(GL_NOTEQUAL, 0, 1);
  glDisable(GL_DEPTH_TEST);

  glCullFace(GL_FRONT);
 RenderCutter();

  ////////////////////
  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  glEnable(GL_DEPTH_TEST);
  glDisable(GL_STENCIL_TEST);
  glDepthFunc(GL_ALWAYS);
  RenderObject();                  /* draw the front face of a, fixing the depth buffer */
  glDepthFunc(GL_LESS);
  /////////////////

  glEnable(GL_DEPTH_TEST);
  glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  glCullFace(GL_BACK);     /* controls which face of a to use */
  RenderObject();                  /* draw a face of a into depth buffer */

  /* use stencil plane to find parts of a in b */
  glDepthMask(GL_FALSE);
  glEnable(GL_STENCIL_TEST);
  glStencilFunc(GL_ALWAYS, 0, 0);
  glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
  glCullFace(GL_BACK);
  RenderCutter();                  /* increment the stencil where the front face of b is 
                           drawn */
  glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
  glCullFace(GL_FRONT);
  RenderCutter();                  /* decrement the stencil buffer where the back face
                           of b is drawn */
  glDepthMask(GL_TRUE);
  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

  glStencilFunc(GL_EQUAL, 0, 1);
  glDisable(GL_DEPTH_TEST);

  glCullFace(GL_BACK);
 RenderObject();

  glDisable(GL_STENCIL_TEST);
}

Is there any wrong with my function SetupPixelFormat in MFC

bool CMaSimView::SetupPixelFormat()
{
	static PIXELFORMATDESCRIPTOR pfd =
	{
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		24,	         // 24-bit color depth
		0, 0, 0, 0, 0, 0,	// color bits ignored
		0,		// no alpha buffer
		0,		// shift bit ignored
		0,		// no accumulation buffer
		0, 0, 0, 0,	// accumulation bits ignored
		16,		// 16-bit z-buffer
		1,		// 1-bit stencil buffer
		0,		// no auxiliary buffer
		PFD_MAIN_PLANE,	// main layer
		0,		// reserved
		0, 0, 0		// layer masks ignored
	};
....

    return true;
}

here is my InitializeOpenGL()

bool CMaSimView::InitializeOpenGL()
{
    //Get a DC for the Client Area
    m_pDC = new CClientDC(this)

    //Failure to Get DC
    if(m_pDC == NULL)
    {
        MessageBox("Error Obtaining DC");
        return FALSE;
    }
    
    //Failure to set the pixel format
    if(!SetupPixelFormat())
    {
        return FALSE;
    }

    //Create Rendering Context
    m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());

    //Failure to Create Rendering Context
    if(m_hRC == 0)
    {
        MessageBox("Error Creating RC");
        return FALSE;
    }

    //Make the RC Current
    if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE)
    {
        MessageBox("Error making RC Current");
        return FALSE;
    }


    //Specify Black as the clear color
    ::glClearColor(0.0f,0.0f,0.0f,0.0f);

    ::glEnable(GL_CULL_FACE);

    //Specify the back of the buffer as clear depth
    ::glClearDepth(1.0f);

    //Enable Depth Testing
    ::glEnable(GL_DEPTH_TEST);

	return true;
}

I have to do what to fix this problem, please help me
http://upload.tinhco.net/thoai/csg4.JPG