Skip to main content
GameDev.net gamedev.net
🔒 Locked

Far away objects

Started by Dark_Light Nov 13, 2004 at 5:02 PM 20 replies 2.2k views
Original Post
Dark_Light
Dark_Light
Can anyone tell me how to force a very distant object to draw even though its way out size the zbuffer. I am turning the zbuffer of temporarily but this is not working! Sounds like such an easy thing, I can’t believe I am stuck on it.
Geoffrey
Geoffrey
Are you sure you're turning off z-buffer comparison? Because merely turning off z-write won't help.

Alternatively, it's possible you need to disable clipping or circumvent it somehow. To be honest I don't know if this is necessary - in my game the zfar distance is infinity so it's not a problem!
Sr_Guapo
Sr_Guapo
Do you have your projection matrix set up with a really big distance? What is the value for you far clipping frustrum?
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
rjackets
rjackets
You will want to set the render state to

Compare.Always

This will force the object to always pass the Z-buffer test. After you draw your stuff you will likely want to set it back to LessEqual (ie. Default setting).
You can see the specifics of this on the MSDN link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/directx/ref/ns/microsoft.directx.direct3d/e/compare/compare.asp

Edit: Forgot to mention, you want to be setting the ZBufferFunction to Compare.Always (as opposed to StencilFunction, AlphaFunction, CounterClockwiseStencilFunction)
Dark_Light
Dark_Light
Hmm done all that and not having any success.
in fact i am getting really wearied results.

I have a 3D world and i am trying to make a sky.

The shy is FAR away and way beyond the limits of the Z-buffer. But i want to force it to draw then i will draw anything else over the top.

however i cant extend my Zbuffer to that size else i will Over stretch the z-buffer.

Have any of you done this type of thing before?. No matter what i do my sky only draws when i am close and stop drawing when i get VERY close.. its odd indeed.
Dark_Light
Dark_Light
Some of the code i am using while testing

code is from diferent parts of the porject..

myDevice.Transform.Projection = Matrix.PerspectiveFovLH ((float)(Math.PI / 4),this.Width / this.Height ,1f,5000);

myDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer,Color.SkyBlue,1f,0);

DirectXDevice.RenderState.ZBufferFunction = Compare.Always;
SkyDome = Mesh.Sphere(DirectXDevice,8000,12,12);
DirectXDevice.RenderState.CullMode = Cull.None;
SkyDome.DrawSubset(0);
DirectXDevice.RenderState.CullMode = Cull.CounterClockwise;
DirectXDevice.RenderState.ZBufferFunction = Compare.LessEqual;
snisarenko
snisarenko
Just turn of writing to z buffer , and draw the sky (skybox i assume) close. It doesn't have to be far like real sky, it just has to look right. And then turn on writing to z buffer, and draw your scene.
rjackets
rjackets
Ah, well thats a simple solution if your just drawing a backdrop. Just draw the envirnoment sphere/box or whatever and make it just large enough to fill the camera view properly.... draw with the Zbuffer write and read turned off. Turn it back on after the draw and everything that is subsequently drawn will all be over top of that backdrop with no need to have insanely huge distances involved. (EDIT: Sorry for the repeat, but it guess the previous post was made while I was typing this)

Also, I THINK that the MeshSphere object does not have texture coordinates on it either, so it may not be showing up because of that.
Dark_Light
Dark_Light
It dont work.. when you move away from the sky it stops drawing.
Even of the zbuffer is off!
somthing stop it from drawing when its far away!
Dark_Light
Dark_Light
Hmm its a pain as i cant show images.

Let me get this strait.

My world is MASIVE! very large indeed. that’s all fine but even if i draw the sky at the edge of my world it going to be to far away when i am in the centre and look out at it.

If i shrink the sky surly it will look shit when i render it? plus it will have to be centred on me else i could still move away from it by to grater distance? and so if its centred in me its going to look shit?

So i am no sure i understand the solution you suggest, surely it will not work?
rjackets
rjackets
name just about any game with some form of skybox and it uses this technique. I don't know if you've ever done any work making Unreal maps but to make a skybox you just make a room that is maybe 128x128 (MUCH smaller than most rooms even) and project that to be the skybox which illustrates that it clearly uses the method. But you are somewhat correct about having to move the sky with the character. Although you'd be best served setting it to surround the CAMERA rather than the player as this removes the need to rescale it when you zoom in/out.
Although to be entirely honest, setting the compare to Always SHOULD work for what you are trying to do anyway.
If you could include some pseudo-code of how your doing the rendering it might help more.

(PS. Why can't you show images?)
Dark_Light
Dark_Light
Ok. il give it a go. will only take a second for me to try.

However i have found a way to get around the Z-buffer. However this look like a BAD idea to me. This works tho

DirectXDevice.Transform.Projection = Matrix.PerspectiveFovLH ((float)(Math.PI / 4),1 ,0f,5000);

Draw sky

DirectXDevice.Transform.Projection = Matrix.PerspectiveFovLH ((float)(Math.PI / 4),1 ,1f,5000);

Draw everything else!

works perfectly.. got a feeling this ant the way to go tho.

i cant post images as i have no web space!

Dark_Light
Dark_Light
hmm looks like moving with the cam is not so bad!..
i have no texture at the moment so i dont know how good is really is yet.

Thanks!
rjackets
rjackets
if it works and you get what your looking for that's fine I guess. The way mentioned above is probably more optimal though. Regardless, the only thing I would question is whether you NEED to do the second DirectXDevice.Transform.Projection setting. I would think that your scene should render okay with the first setting as is. Setting it back and forth may be more expensive than necessary. If it works, just set the projection matrix on initialization/reset and then don't touch it anywhere else.

EDIT: Just saw your reply re: moving with the camera. I would recommend sticking with that if you've got it working.
Dark_Light
Dark_Light
I have both methods working now.

The first (One I came up with)
Looks better as the sky moves ever so slightly when you move. Like in real life. but you NEED to do both the Transform.Projections

the first stops z buffering TOTALY as you cant have "0"
the second puts it back to normal.

But this must be a HIT on the CPU..

the method you suggest works almost as well and its got to be quicker so im going to stick with that one for now..

Thanks.
davidx9
davidx9
Quote:
Original post by Dark_Light
Some of the code i am using while testing

code is from diferent parts of the porject..

myDevice.Transform.Projection = Matrix.PerspectiveFovLH ((float)(Math.PI / 4),this.Width / this.Height ,1f,5000);

myDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer,Color.SkyBlue,1f,0);

DirectXDevice.RenderState.ZBufferFunction = Compare.Always;
SkyDome = Mesh.Sphere(DirectXDevice,8000,12,12);
DirectXDevice.RenderState.CullMode = Cull.None;
SkyDome.DrawSubset(0);
DirectXDevice.RenderState.CullMode = Cull.CounterClockwise;
DirectXDevice.RenderState.ZBufferFunction = Compare.LessEqual;


Any vertex drawn over 5000.0f away from the camera position will be clipped. This could be the cause of your problem.
No bombs, No guns, just an army of game creators...
Dark_Light
Dark_Light
I know. i have solved the prob now BUT
is there any was i can stop clipping objects over 5000??
steg
steg
Can u not change the 5000 to something else ?
If it isn't working, take a bath, have a think and try again...
davidx9
davidx9
I would try to find some articles on Z-Buffers. I don't think you understand them properly. Of course having a large viewing distance could present more pixels being checked for depth, but as I understand it a 24-Bit Z-Buffer simply divides the viewing distance into many possible planes. If you have so much detail that you need more planes, use a larger Z-Buffer, but honestly, your scene management needs a serious re-think if there is even the slightest possibility that you need so much precision.

Each pixel has a Z-value associated with it, it has nothing to do with clipping or drawing distance. It's simply there to see if a pixel needs to be drawn or not, in case something is in the way (ie a lower Z-value).
No bombs, No guns, just an army of game creators...
Tubular
Tubular
Does anyone have a good argument against Dark_Light's method of just resetting the perspective matrix? This was exactly the method I had planned to use in a space combat game that I will be beginning shortly. (Though I haven't actually coded it to see if it works. I'm glad to hear that it does.) The game would have objects with massively different scales, ie tiny ships around 20 meters long, giant ships several kilometers long, semi-realistically sized planets (though hopefully you won't be getting too close to them) and probably even the nearest star. Rather than trying to shrink the 3D objects into the same depth buffer and trying to make them look natural, I figured it would be easier to just render the scene a few times at different scales (with filters on the objects that are too small or too far away to be considered). Shouldn't this work out fine? I'm not sure I see why it would be a hit on the CPU.
I'll still use the standard, origin-centered skybox for the starfield, though. I'm not that crazy. [wow]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.