distant object renders over near object

Started by
9 comments, last by slowmike 15 years, 12 months ago
I've got an object located at about 10.0f on the Z axis and another object at about 1.0f on the Z axis. For some reason, the object that's farther away is being rendered in front of the closer object. Any thoughts? When I move my camera (point of view), I can see that both objects are located at their correct 3D positions. But no matter which side of the world I'm looking in from (front or back), the same object is always displayed in front of the other. I'm thinking it has something to do with my World, View, or Projection matrix. Or maybe it's a render_state problem. I know that I'm rendering the object that's always displayed in front last, but this isn't 2D, so it shouldn't matter the order in which the objects are being rendered. Correct? Thanks.
Advertisement
Quote:I know that I'm rendering the object that's always displayed in front last, but this isn't 2D, so it shouldn't matter the order in which the objects are being rendered. Correct?


Only if you're using the depth buffer. Can you post your code?
Sounds like you need to enable z-buffering support. Something along the lines below would do it.

In OpenGL:
glEnable(GL_DEPTHTEST);

In Direct3D:
pDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
I'm setting the render state, D3DRS_ZENABLE to true. (??)

Any code snippet in particular that you'd like to see?
Are you by any chance clearing the depth buffer between drawing the 2 objects? I cant think of any other reason besides your depth buffering not being enabled.
Setting a render state is not enough, you also need to set the appropriate parameters in D3DPRESENT_PARAMETERS. The Debug Runtimes can often point out such errors.

If this doesn't fix it, we'll need to see the code (in source tags. See the FAQ if you don't know what that means).
D3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));if(fullscreen){	d3dpp.Windowed = false;	d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;}else{	d3dpp.Windowed = true;	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;}d3dpp.BackBufferFormat = d3ddm.Format;d3dpp.BackBufferCount = 1;d3dpp.BackBufferWidth = winWidth;d3dpp.BackBufferHeight = winHeight;d3dpp.hDeviceWindow = hwnd;d3dpp.EnableAutoDepthStencil = true;d3dpp.AutoDepthStencilFormat = D3DFMT_D16;if(FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,		D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3dDevice)))		return false;


For the debug runtimes.. I selected "Use Debug" in the control panel, but I couldn't find where to complete this step: "Under Managed Projects, you need to select "Enable unmanaged debugging" in the Debug section of the project properties."

I'm going to trace back some pointers to see if I've got some garbage data that's not triggering an error. Thanks for the help.
Quote:I couldn't find where to complete this step: "Under Managed Projects, you need to select "Enable unmanaged debugging" in the Debug section of the project properties."


That's only for Visual C# users.

Also, don't forget to clear the depth buffer.
Do you have a zbuffer setup, with write enabled and ztest less than equal?
Quote:Also, don't forget to clear the depth buffer.


d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clearColor, 1.0f, 0);

I make this call once per frame, right before BeginScene. I'm still looking into the problem myself. It's just taking some time, because I'm a bit busy right now.

This topic is closed to new replies.

Advertisement