Hidden surface removal problem

Hi everyone:
I got a problem to remove hidden surface. When I draw a sealed surface, glEnable(GL_DEPTH_TEST) is working ok. When I draw a complex object for example streetlamp, the hidden surface can’t be removed. It always cover the surface that I want.
Could you please help me to solve this problem. Thanks in advance

try setting the following states:
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glFrontFace(GL_CCW or GL_CW);
(depending on polygon winding)
glDepthMask(GL_TRUE);

and post a screen shot if you can, that would help.

Thanks Aeluned! I tried your suggestion, but problem still there. Maybe I didn’t described it very clear. When I use left key to rotate picture, I can see front surface coverd by back surface that should be covered by front surface. It looks like front object is covered by back object. I have spent two days on it.

Are you allocating bits for a z-buffer in your Pixel Format Descriptor?

it seems that you don’t have a depth buffer.

Thanks again! This is my code:
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

int nPixelFormat;					// Pixel format index
m_hDC = ::GetDC(m_hWnd);			// Get the Device context

static PIXELFORMATDESCRIPTOR pfd = {
	sizeof(PIXELFORMATDESCRIPTOR),	// Size of this structure
	1,								// Version of this structure	
	PFD_DRAW_TO_WINDOW |			// Draw to Window (not to bitmap)
	PFD_SUPPORT_OPENGL,// |			// Support OpenGL calls in window

// PFD_DOUBLEBUFFER, // Double buffered mode
PFD_TYPE_RGBA, // RGBA Color mode
24, // Want 24bit color
0,0,0,0,0,0, // Not used to select mode
0,0, // Not used to select mode
0,0,0,0,0, // Not used to select mode
32, // Size of depth buffer
0, // Not used to select mode
0, // Not used to select mode
PFD_MAIN_PLANE, // Draw in main plane
0, // Not used to select mode
0,0,0 }; // Not used to select mode

// Choose a pixel format that best matches that described in pfd
nPixelFormat = ChoosePixelFormat(m_hDC, &pfd);

// Set the pixel format for the device context
VERIFY(SetPixelFormat(m_hDC, nPixelFormat, &pfd));

// Create the rendering context
m_hRC = wglCreateContext(m_hDC);

// Make the rendering context current, perform initialization, then
// deselect it
VERIFY(wglMakeCurrent(m_hDC,m_hRC));
GLSetupRC(m_hDC);
wglMakeCurrent(NULL,NULL);

// Create the palette if needed
InitializePalette();

// SetTimer(101,175,NULL); // 8 times a second…

return 0;

//////////////////////////////////////////////////
SetUpRC:
GLfloat ambientLight[] = { 0.35f, 0.35f, 0.35f, 1.0f };
GLfloat diffuseLight[] = { 0.75f, 0.75f, 0.75f, 0.3f };
GLfloat specular[] = { 0.5f, 0.5f, 0.5f, 1.0f};
GLfloat lightPos[] = { -230.0f, 0.0f, 200.0f, 1.0f };
GLfloat lightPos1[] = { 230.0f, 0.0f, -200.0f, 1.0f };
GLfloat specref[] = { 0.25f, 0.25f, 0.25f, 1.0f };

glEnable(GL_DEPTH_TEST);	// Hidden surface removal
glFrontFace(GL_CCW);// Counter clock-wise polygons face out 
glEnable(GL_CULL_FACE);		// Do not calculate inside of oject
glShadeModel(GL_SMOOTH);

// Enable lighting
glEnable(GL_LIGHTING);

// Setup and enable light 0
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);

glLightfv(GL_LIGHT1,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT1,GL_SPECULAR,specular);
glLightfv(GL_LIGHT1,GL_POSITION,lightPos1);
glEnable(GL_LIGHT1);

// Enable color tracking
glEnable(GL_COLOR_MATERIAL);

// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

// All materials hereafter have full specular reflectivity
// with a high shine
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,specref);

glMateriali(GL_FRONT,GL_SHININESS,128);

// Black blue background
glClearColor(0.0f, 0.40f, 0.0f, 1.0f );

/////////////////////////////////////////////////I have two view in my program. But doule buffer not working. I commented out. In my another view, it’s working. I don’t know why. This is another problem that bother me. Thanks for your help!

just an idea: remove all comments.
some of the // seem to be displaced, like

// PFD_DOUBLEBUFFER, // Double buffered mode

are you using an editor with syntax highlighting? is a great help, sometimes…

and, by the way-

do you have

glEnable(GL_DEPTH_TEST);

somewhere? and when redisplaying your scene, is there

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

?

Thanks RigidBody! This is one of problem. If I remove this comment, The picture can’t be displayed. The code screen is displayed and looks frozen. My another view that I didn’t comment this works good. It’s strange. I just try to rotate my object, If object is in Z<0 area, deep buffer works ok. When object is in z>0 area, the problem happens. Thanks for your help. This forum is so good. This is first time to come here. I’m still working on this.

Have a nice weekend!!!

Yes I have glEnable(GL_DEPTH_TEST) in GLSetUpRC and glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); before I redisplay.

I forgot to tell you I’m using MFC and OpenGl in my project.
Thanks

Hi

I had almost exactly the same problem.

By default you are looking from 0,0,0

Sooooo, if your model is created around the 0,0,0 point you are effectively inside it. This causes “wierd” effects like surfaces that should be in front to be viewed as behind…

As a quick check, try and add a global Z offset to your model so that it isn’t around the 0,0,0 position.

I guess you can alter the viewing position etc but I haven’t figured out how to do that yet.

HTH a little

Julian