depth buffer problem?

Hi,
when I’m executing following code, I suppose
to have the red cube (the depth function is
by default value GL_LESS), but I get the green one - the same result when I’m using GL_EQUAL depth function. What is wrong in my
code? Is it depth buffer?

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_BUFFER);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &red);
drawCube();
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, &green);
drawCube();

Thanks

Originally posted by dimka:
glEnable(GL_DEPTH_BUFFER);

Maybe you should use GL_DEPTH_TEST instead. I’m surprised it even compiles the way you have it, cause I can’t find GL_DEPTH_BUFFER being defined anywhere in my headers.

Sorry i have mistyped, certainly it
is glEnable(GL_DEPTH_TEST).
But it still doesn’t work as it seems to be!!

Do you even have a depth buffer, that is did you request one with the window?

As Bob has kindly pointed out, I have no idea what I am talking about, sorry

[This message has been edited by chowe6685 (edited 02-23-2004).]

Then the next obvious question would be; do you even have a depth buffer?

Check what glGetIntegerv with GL_DEPTH_BITS returns.

Two, you are drawing the cubes right on top of one another, at least it looks that way. No matter what you set the depth function to you are going to get some strange results, you need to separate your polygons at least slightly for depth testing to work

What he’s doing is perfectly correct (assuming there’s no extra tarnsformations hidden inside the call to drawCube). Fragments from the two cubes will be identical depth-wise, so the depth function GL_LESS should completely reject the green cube, and there should be no “stragne results”. Multipass techniques more or less relies on this fact that depth values are invariant over two passes.

Originally posted by Bob:
Fragments from the two cubes will be identical depth-wise, so the depth function GL_LESS should completely reject the green cube, and there should be no “stragne results”. Multipass techniques more or less relies on this fact that depth values are invariant over two passes.[/b]

That is the point, I have also assumed that the
red cube should be left without change…
The GL_DEPTH_BITS is 32, it seems also that
I have depth buffer

Given the information in this thread, I see no reasons why it shouldn’t work. Post the smallest possible code for a complete program that demonstrates the problem so we can try it.

I have suspection that the clue lies in OGL initialization-stuff in my program. I do MFC and paste some procedures from View-Class of my project. It’s a bit too much, sorry, but I really don’t know where lies the error.
thanks one more time!!

BOOL CShadowVolumesView::PreCreateWindow(CREATESTRUCT& cs)
{
TRACE0("PreCreateWindow
") ;
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN ; //added for OpenGL Window
return CView::PreCreateWindow(cs);
}

int CShadowVolumesView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

//Describing the Pixel Format
CClientDC dc(this) ;

 // Fill in the pixel format descriptor
PIXELFORMATDESCRIPTOR pfd ;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize      = sizeof(PIXELFORMATDESCRIPTOR); 
pfd.nVersion = 1 ;                           // Version number
pfd.dwFlags =  PFD_DOUBLEBUFFER |            // Use double buffer
               PFD_SUPPORT_OPENGL |          // Use OpenGL
               PFD_DRAW_TO_WINDOW;// |			 // Pixel format is for a window
			  
pfd.iPixelType = PFD_TYPE_RGBA ;
	pfd.cColorBits = 32;                         // 32-bit color
pfd.cDepthBits = 32 ;					   	 // 32-bit depth buffer
	pfd.iLayerType = PFD_MAIN_PLANE ;            // Layer type

//Choosing and Setting the Pixel Format
int nPixelFormat = ChoosePixelFormat(dc.m_hDC, &pfd);
if (nPixelFormat == 0)
{
	TRACE("ChoosePixelFormat Failed %d

",GetLastError()) ;
return -1 ;
}

BOOL bResult = SetPixelFormat (dc.m_hDC, nPixelFormat, &pfd);
if (!bResult)
{
   TRACE("SetPixelFormat Failed %d

",GetLastError()) ;
return -1 ;
}

//Creating a Rendering Context
m_hrc = wglCreateContext(dc.m_hDC);
if (!m_hrc)
{
   TRACE("wglCreateContext Failed %x

", GetLastError()) ;
return -1;
}

return 0;

}

void CShadowVolumesView::OnInitialUpdate()
{
// Attach the window dc to OpenGL
CClientDC dc(this) ;
BOOL bResult = wglMakeCurrent(dc.m_hDC, m_hrc);
if (!bResult)
{
TRACE("wglMakeCurrent Failed %x
", GetLastError() ) ;
}
PrepareScene() ;
CView::OnInitialUpdate();
wglMakeCurrent(NULL, NULL) ;
}

void CShadowVolumesView::PrepareScene(void)
{
CShadowVolumesDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

pDoc->initGeometry(); //Creating display lists	

m_fDistance = 130.0;

glClearColor(0, 0, 0, 0);
glShadeModel(GL_FLAT);
// Enable depth calculations.
glEnable(GL_DEPTH_TEST);  			

}

void CShadowVolumesView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

if ( (cx <= 0) | | (cy <= 0) ) return ;

CClientDC dc(this) ;

//Make the rendering context m_hrc current
BOOL bResult = wglMakeCurrent (dc.m_hDC, m_hrc);
if (!bResult)
{
   TRACE("wglMakeCurrent Failed %x

", GetLastError() ) ;
return ;
}
// Set up the mapping of 3-space to screen space
gldAspect = (GLdouble) cx/ (GLdouble) cy;
gldFovy = 60.0;
gldNear = 8.0;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(gldFovy, gldAspect, gldNear, 200.0);
glViewport(0, 0, cx, cy);
glMatrixMode(GL_MODELVIEW);

// No rendering context will be current
wglMakeCurrent(NULL, NULL);

}

void CShadowVolumesView::OnDraw(CDC* pDC)
{
CShadowVolumesDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

// Make the HGLRC current.
BOOL bResult = wglMakeCurrent (pDC->m_hDC, m_hrc);
if (!bResult)
{
   TRACE("wglMakeCurrent Failed %x

", GetLastError() ) ;
}

// Draw.
RenderAll();

// Swap buffers.
SwapBuffers(pDC->m_hDC) ;

wglMakeCurrent(NULL, NULL) ;

}

void CShadowVolumesView::RenderAll(void)
{
CShadowVolumesDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(gldFovy, gldAspect, gldNear, 200.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (20, 50.0, m_fDistance, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotated(m_fRotateX, 1.0, 0.0, 0.0);
glRotated(m_fRotateY, 0.0, 1.0, 0.0);

glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_LIGHTING);
glDisable(GL_LIGHT0);
GLfloat redAmbient[] = {1.0, 0.5, 0.5, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, redAmbient);

DrawScene();
	
GLfloat greenAmbient[] = {0.2, 1.0, 0.2, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, greenAmbient);

glDepthFunc(GL_LESS);

DrawScene();

}

I don’t think there’s much more information there other than the pixel format setup, and I see nothing wrong there. The code for DrawScene would be nice too, and please, use the code-tag for proper formatting.

I don’t think you should use the command glLightModel to control the object’s color.
If you use the command once and then use the material or color for the object,you will see the great red cube!

I’ve tried with no light at all, only with glColor3f, then with glMaterial - the result is the same - I have big GREEN cube…
the code in drawGeometry is only glCallList(1);
and the display list looks as:

GLdouble size = 50. ;
static GLdouble plate_vertices = {size, -size/10, size, size, 0., size, size, -size/10, -size, size, 0, -size,
-size, -size/10, -size,
-size, 0, -size,
-size, -size/10, size,
-size, 0, size,
size, -size/10, size,
size, 0., size,
size, -size/10, -size,
size, 0, -size,
-size, -size/10, -size,
-size, 0, -size,
-size, -size/10, size,
-size, 0, size,
size, -size/10, size,
size, 0., size,
size, -size/10, -size,
size, 0, -size,
-size, -size/10, -size,
-size, 0, -size,
-size, -size/10, size,
-size, 0, size};
static GLdouble normals = {0, 0, 1, 0, 0, 1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, 1,
0, 0, 1,

  						 1, 0, 0,
  						 1, 0, 0,
  						 1, 0, 0,
  						 1, 0, 0,
  						 -1, 0, 0,
  						 -1, 0, 0,
  						 -1, 0, 0,
  						 -1, 0, 0,

  						 0, -1, 0,
  						 0, 1, 0,
  						 0, -1, 0,
  						 0, 1, 0,
  						 0, -1, 0,
  						 0, 1, 0,
  						 0, -1, 0,
  						 0, 1, 0};

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_DOUBLE, 0, plate_vertices);
glNormalPointer(GL_DOUBLE, 0, normals);
glNewList(1, GL_COMPILE) ;
glBegin(GL_TRIANGLES); //counterclockwise
glArrayElement(6);
glArrayElement(0);
glArrayElement(1);

  	glArrayElement(1);
  	glArrayElement(7);
  	glArrayElement(6);
  
      glArrayElement(5);
  	glArrayElement(3);
  	glArrayElement(2);
  
  	glArrayElement(2);
  	glArrayElement(4);
  	glArrayElement(5);
  
      glArrayElement(8);
  	glArrayElement(10);
  	glArrayElement(11);
  
  	glArrayElement(11);
  	glArrayElement(9);
  	glArrayElement(8);
  
      glArrayElement(15);
  	glArrayElement(13);
  	glArrayElement(12);
  
  	glArrayElement(12);
  	glArrayElement(14);
  	glArrayElement(15);
  
      glArrayElement(23);
  	glArrayElement(17);
  	glArrayElement(19);
  
  	glArrayElement(19);
  	glArrayElement(21);
  	glArrayElement(23);
  
      glArrayElement(20);
  	glArrayElement(18);
  	glArrayElement(16);
  
  	glArrayElement(16);
  	glArrayElement(22);
  	glArrayElement(20);
  glEnd();
  glDisableClientState(GL_NORMAL_ARRAY);
  glDisableClientState(GL_VERTEX_ARRAY);

glEndList() ;