Sign in to follow this  

Far away objects

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?)

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i could but i would need to change it so somthing like 20000 and then my z-buffer will be OVER stretched! and stop working correctly

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
davidx9 I know how the z-buffer works.. don’t be so patronising!

Tubular yes that method works! and i cant see any problem with it. BUT it seems like the type of think that i would naturally keep clear of.

I have chosen to stick to the (moving the sky around) method just as that works and i dont have to reset anything to do it.

Good luck with what ever method you try

Share this post


Link to post
Share on other sites

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this