Depth Test problem

Started by
0 comments, last by hallgeir 20 years, 2 months ago
Hey. I got a problem with the depth test in openGL. I got two squares, one red and one blue. The red one overlaps the blue one. I have enabled depth testing, and each frame, I call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);. But something is wrong. When the camera object is in a specific angle or position, the blue square shows THROUGH the red one, just as if I don't have depth testing enabled. It happens only when my camera is a specific distance away from the squares or when it is in a specific angle. Also, when I move the camera to the other side of the squares, so the blue one overlaps the red one, it is really messed up (the red one overlaps the blue instead, AND the blue one shows through the red one when the camera is in the right position and angle(like described above). EDIT: I have taken 2 screenshots that you may look at, that shows the problem pretty clearly: This is how it is supposed to be: buggy1.jpg When I move the camera a little, it gets like this: buggy2.jpg I have made the program available for download if someone wants to see the problem for theirselves: engine.zip, so that you may see exactly what the problem is. Here's the initialization code for the program:

//init GL
bool COpenGL::Initialize(HWND hWnd)
{
	m_hDC = GetDC(hWnd);

	//prepare the pixel format
	static PIXELFORMATDESCRIPTOR p_PixelFormat = {
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		24,
		0,0,0,0,0,0,
		0,0,
		0,0,0,0,0,
		32,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0,0,0 };

	int p_nPixelFormatID = ChoosePixelFormat(m_hDC,&p_PixelFormat);

	SetPixelFormat(m_hDC,p_nPixelFormatID,&p_PixelFormat);

	//create the render context
	m_hRC = wglCreateContext(m_hDC);

	wglMakeCurrent(m_hDC,m_hRC);

	//enable depth testing
	glEnable(GL_DEPTH_TEST);
	glDepthMask(GL_TRUE);
	
	glShadeModel(GL_SMOOTH);
	
	glFrontFace(GL_CCW);
	
	glEnable(GL_TEXTURE_2D);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

	m_fFov = 60.0f;

	glClearColor(0.0f,0.0f,0.0f,1.0f);
	glColor4f(1.0f,1.0f,1.0f,1.0f);

	return true;
}
     
and here's the rendering code:

//main engine loop
void CEngine::Run()
{
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//store the matrix
	glPushMatrix();

	//translate/rotate the matrix
	m_Camera.Update();

	//m_Mod.m_fnMainProc(VM_RUN_FRAME);
	glEnable(GL_DEPTH_TEST);

	glDisable(GL_TEXTURE_2D); //disabled texturing (debugging purposes)
	glColor4f(1,0,0,1);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f+1, 1.5f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5+5, -0.5f+1, 1.5f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5+5, 0.5f+5, 1.5f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f+5, 1.5f);
	glEnd();
	glColor4f(0,0,1,1);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f+1, 0.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5+5, -0.5f+1, 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5+5, 0.5f+5, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f+5, 0.0f);
	glEnd();

	if(GetAsyncKeyState(VK_LEFT))
	{
		m_Camera.m_vecRot.m_fY--;
	}
	if(GetAsyncKeyState(VK_RIGHT))
	{
		m_Camera.m_vecRot.m_fY++;
	}
	if(GetAsyncKeyState(VK_UP))
	{
		m_Camera.m_vecRot.m_fX--;
	}
	if(GetAsyncKeyState(VK_DOWN))
	{
		m_Camera.m_vecRot.m_fX++;
	}
	if(GetAsyncKeyState(VK_NUMPAD1))
	{
		m_Camera.m_vecRot.m_fZ++;
	}
	if(GetAsyncKeyState(VK_NUMPAD3))
	{
		m_Camera.m_vecRot.m_fZ--;
	}

	if (GetAsyncKeyState(VK_NUMPAD8))
	{
		m_Camera.m_vecPos.m_fZ-=0.1f;
	}
	if (GetAsyncKeyState(VK_NUMPAD2))
	{
		m_Camera.m_vecPos.m_fZ+=0.1f;
	}
	if (GetAsyncKeyState(VK_NUMPAD4))
	{
		m_Camera.m_vecPos.m_fX+=0.1f;
	}
	if (GetAsyncKeyState(VK_NUMPAD6))
	{
		m_Camera.m_vecPos.m_fX-=0.1f;
	}

	//restore the matrix
	glPopMatrix();

	//swap buffers
	m_OpenGL.Render();
}
     
COpenGL::Render():

void COpenGL::Render()
{
	int p_nErr;
	int i = 0;

	glFlush();

	do
	{
		p_nErr = glGetError();

		if (p_nErr != GL_NO_ERROR)
		{
			MessageBox(NULL,(const char*)gluErrorString(p_nErr),"GL Error",MB_OK);
		}
		i++;
	} while ((i < 6) && (p_nErr != GL_NO_ERROR));

	SwapBuffers(m_hDC);
}
     
I hope anyone can help me with this. I have tried and tried, but just couldn't figure out WHAT could be wrong. Sorry for making such a huge post btw EDIT: Oh, and the controls for the program: Arrow keys & numpad 1 and 3 - Rotates camera Numpad 8,4,6 and 2: move the camera Sorry for not including this in the first place "The mistakes are all waiting to be made. [edited by - ziggwarth on January 25, 2004 6:59:56 AM] [edited by - ziggwarth on January 25, 2004 9:34:39 AM] [edited by - ziggwarth on January 25, 2004 9:35:29 AM] [edited by - ziggwarth on January 25, 2004 9:36:41 AM]
Advertisement
DAMN!
I''m fucking stupid
When I resize the scene, I use
gluPerspective(m_fFov,m_fAspect,1.0,m_fClipRange);
but of course, I FORGOT to set the m_fClipRange member variable when initializing opengl!

Well, it works perfectly now

"The mistakes are all waiting to be made.

This topic is closed to new replies.

Advertisement