Now i'm wondering how i can fix it: have i to reduce the scale of the 3d universe? Or have i to manually sort the objects from far to near? The scale is big because i'm trying to represent the solar system, and i need to have meter resolution near the camera, but also display big and far planets. Any suggestion?
🔒 Locked
Problem with depth-buffer
Advertisement
Original Post
Hi again. First of all thank you for all yours answer to my previous questions. Now: i've some annoying problems using the depth-buffer. On my hardware i've setting to 24S8, (the pure 32 is not supported), and i've this result:
Now i'm wondering how i can fix it: have i to reduce the scale of the 3d universe? Or have i to manually sort the objects from far to near? The scale is big because i'm trying to represent the solar system, and i need to have meter resolution near the camera, but also display big and far planets. Any suggestion?
Now i'm wondering how i can fix it: have i to reduce the scale of the 3d universe? Or have i to manually sort the objects from far to near? The scale is big because i'm trying to represent the solar system, and i need to have meter resolution near the camera, but also display big and far planets. Any suggestion?
As Gage64 said; it depends on how your depth buffer is set up. In particular, make sure your near clip plane is as far away as possible.
Also remember that the depth range isn't linear, so objects further away will be less accurate.
Also remember that the depth range isn't linear, so objects further away will be less accurate.
You might want to search the forum, since I remember this subject coming up occasionally.
I think the easiest solution would be to do rough sorting, then use the Z buffer for groups of objects. For example, if you have some objects around a million km from you, and others at a range of 1 to 1000 metres, first render the far object with a near clip plane of around a million km (the nearest part of the nearest object), then clear the Z buffer and render the nearby objects over the already rendered one, with a far clip plane of 1000 metres.
I think the easiest solution would be to do rough sorting, then use the Z buffer for groups of objects. For example, if you have some objects around a million km from you, and others at a range of 1 to 1000 metres, first render the far object with a near clip plane of around a million km (the nearest part of the nearest object), then clear the Z buffer and render the nearby objects over the already rendered one, with a far clip plane of 1000 metres.
Quote:
Original post by Gage64
What are you setting your near/far clipping planes to? If the former is too small and/or the latter is too large, you will have precision problems.
The problem is this.
In solar system i.e. the distance are also 1.000.000.000 km (from jupiter i can see the sun), so i cannot set the far clip plane too near, or i'll see disappear the sun too early.
And if i'm into a ship, and i'm flying near another ship, i've distance about 100 meter, so i need at least ten digit of difference from far to near.
Quote:
Original post by ET3D
You might want to search the forum, since I remember this subject coming up occasionally.
I think the easiest solution would be to do rough sorting, then use the Z buffer for groups of objects. For example, if you have some objects around a million km from you, and others at a range of 1 to 1000 metres, first render the far object with a near clip plane of around a million km (the nearest part of the nearest object), then clear the Z buffer and render the nearby objects over the already rendered one, with a far clip plane of 1000 metres.
Thank you, this is interesting and should solve my problem.
A little question: what is the function to erase the z-buffer?
Excuse me, but i'm a bit noob..
Quote:A 24-bit depth buffer only has 16.7 million possibly unique values. So you can't pysically get 10 digits of precision out of it. You'll have to do as ET3D suggests and split the scene into chunks, and render those, or find a way to do without the depth buffer entirely.
Original post by PdG Quote:
Original post by Gage64
What are you setting your near/far clipping planes to? If the former is too small and/or the latter is too large, you will have precision problems.
The problem is this.
In solar system i.e. the distance are also 1.000.000.000 km (from jupiter i can see the sun), so i cannot set the far clip plane too near, or i'll see disappear the sun too early.
And if i'm into a ship, and i'm flying near another ship, i've distance about 100 meter, so i need at least ten digit of difference from far to near.
EDIT:
Quote:IDirect3DDevice9::Clear(). Although you might be better off rendering a screen sized quad near the far clip plane with z-tests disabled but z-writes still enabled.
Original post by PdG
Thank you, this is interesting and should solve my problem.
A little question: what is the function to erase the z-buffer?
Excuse me, but i'm a bit noob..
Quote:
Original post by Evil Steve
IDirect3DDevice9::Clear(). Although you might be better off rendering a screen sized quad near the far clip plane with z-tests disabled but z-writes still enabled.
Ok, i'm working on it and it seems to work... but i've still a problem.
Choosing a clip near-far area that evelopes jupiter, an hole appears when i move the camera. If the camera is in front, all works fine, but if i try to rotate.. this is the result:
It seems like the near clip is too far and it cuts the nearest part of the planet, generating that hole...
I've setted the near-far planes
near = distance_camera_planet-radius
far = distance_camera_planet+radius
and if i rotate the camera, the distance remains the same value..
Here the code
HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0)); HR(gd3dDevice->BeginScene());// bla bla bla // Setup the rendering FX HR(mFX->SetTechnique(mhTech)); UINT numPasses = 0; HR(mFX->Begin(&numPasses, 0)); for(UINT i = 0; i < numPasses; ++i) { HR(mFX->BeginPass(i)); // draw jupiter distance = sqrt(pow(star->coords.x-origin.x,2)+pow(star->coords.y-origin.y,2)+pow(star->coords.z-origin.z,2)); buildProjMtx(distance-star->radius,distance+star->radius); star->draw(mhWVP, mhWorldInvTrans, mhMtrl, mhWorld, mhTex, matWorld, mView, mProj, &origin);// clear the z-buffer only HR(gd3dDevice->Clear(0, 0, D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0));// now draw the sun distance= sqrt(pow(planet->coords.x-origin.x,2)+pow(planet->coords.y-origin.y,2)+pow(planet->coords.z-origin.z,2)); buildProjMtx(distance-planet->radius,distance +planet->radius); planet->draw(mhWVP, mhWorldInvTrans, mhMtrl, mhWorld, mhTex, matWorld, mView, mProj, &origin); HR(mFX->EndPass()); } HR(mFX->End()); HR(gd3dDevice->EndScene()); // Present the backbuffer. HR(gd3dDevice->Present(0, 0, 0, 0));And here the buildProjMtx function:
void Main::buildProjMtx(float n, float f){ float w = (float)md3dPP.BackBufferWidth; float h = (float)md3dPP.BackBufferHeight; D3DXMatrixPerspectiveFovLH(&mProj, D3DX_PI * 0.25f/4, w/h, n, f); HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj)); }any idea?
I've tried to multiply the radius by 2, and it works but it's an hack and i don't like it..
(yes, there's yet the sort, i draw first the star then the planet always, it's just a test to see if it's works..)
That's not how you're supposed to use the Z-buffer. Unless, of course, the only thing you plan on rendering is the planet, when it's in the exact right position. Or else you'll run into the problem you're experiencing.
Setting different z-near and z-far values for each object is likely to cause some odd things to happen. Since both your objects are rendered to the same z-buffer, it means that you're not going to get correct Z-testing.
For example, let's say that I set the Z-near to 1.0f and the Z-far to 3.0f. Then, I render something at a Z position of 2.0f. Then, let's say that I set my Z-near to 1000.0f, and my Z-far to 2000.0f, and render something else at 1500.0f.
My two objects will then occupy the same Z position in the Z-buffer! The result will be z-fighting and incorrect depth.
Setting different z-near and z-far values for each object is likely to cause some odd things to happen. Since both your objects are rendered to the same z-buffer, it means that you're not going to get correct Z-testing.
For example, let's say that I set the Z-near to 1.0f and the Z-far to 3.0f. Then, I render something at a Z position of 2.0f. Then, let's say that I set my Z-near to 1000.0f, and my Z-far to 2000.0f, and render something else at 1500.0f.
My two objects will then occupy the same Z position in the Z-buffer! The result will be z-fighting and incorrect depth.
Quote:
Original post by Sc4Freak
That's not how you're supposed to use the Z-buffer. Unless, of course, the only thing you plan on rendering is the planet, when it's in the exact right position. Or else you'll run into the problem you're experiencing.
Setting different z-near and z-far values for each object is likely to cause some odd things to happen. Since both your objects are rendered to the same z-buffer, it means that you're not going to get correct Z-testing.
For example, let's say that I set the Z-near to 1.0f and the Z-far to 3.0f. Then, I render something at a Z position of 2.0f. Then, let's say that I set my Z-near to 1000.0f, and my Z-far to 2000.0f, and render something else at 1500.0f.
My two objects will then occupy the same Z position in the Z-buffer! The result will be z-fighting and incorrect depth.
Ok, but between the first and second drawing i clear the z-buffer.
Indeed, for planets, i may also disable the depth-buffer at all: if i sort from far to near there's no need to use it because spheres are convex, and with the back culling enabled the mesh doesn't needs any z buffer. But, for other meshes, i.e. some starship, i've to use it.
The strange is that if i set near = center-radius*2 and far = center + radius * 2 all works fine. I've tried to sort and use this tecnique: result is that there's no more garbage due to the depht buffer and jupiter and sun are drawed well both if jupiter is in front or sun.
The question is: why radius*2? why simpling rotating the camera the near plane clips a piece of planet? If i rotate, the distance is alway the same, and the space occupied by the volume of the plane is center-radius,center+radius
Quote:Only for a perfect sphere - a sphere made up from triangles won't be like that, it has bits that stick out to a distance greater than the radius, and bits that are less than the radius. That also assumes that the Z-buffer is exactly accurate, which is may well not be.
Original post by PdG
The question is: why radius*2? why simpling rotating the camera the near plane clips a piece of planet? If i rotate, the distance is alway the same, and the space occupied by the volume of the plane is center-radius,center+radius
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement