Messing with the depth buffer for skybox effect

Started by
4 comments, last by cephalo 9 years ago

So I want to create a sort of dynamic skybox, with a standard cube map as the backround, and quads depicting orbiting planets and other celestial phenomena.

I tried creating a dynamic cube map, and before I succeeded I decided that this approach is really very expensive for what I'm trying to do, as there's only ever going to be maybe ten billboards floating around the sky. Rendering the 6 faces of a cube map over that is overkill I think. Just clearing the 6 faces had a significant impact on my framerate.

What I'd like to do instead, is draw the planets as I normally would draw a quad, but have them:

pass in front or behind each other correctly according to distance,

be in front of the background skybox.

be behind everything else.

Can I do that with certain DepthStencilState settings, or does anyone else have any other ideas?

EDIT: If I make my projection matrix too deep, I'm going to have z-fighting issues with the other components in the scene, therefore I can't just make it 'real' by putting the planets far away.

Advertisement

What if you just clear the z-buffer after drawing your planets, but before drawing everything else? You could even futz with the projection matrix at the same time, and, as you say, "make it real" by drawing the planets at their proper distance.

Geoff

Well, I think that changing the projection matrix will lengthen the area where things are drawn, but compress the data in the depth buffer, such that things that are far away will have a Z value that is much closer, and occlude things that are closer.

I've never used a stencil buffer. Can I use it for this?

1. Draw the main scene, setting the stencil buffer somehow for every pixel that is drawn.

2.Draw the planets wherever, ignoring the depth buffer but only where the stencil buffer is not set.

3. Then draw the background skybox.

EDIT: Hey you're right, if I draw the planets and skybox first and then clear the z buffer, I should get exactly the output I want. I guess the only complaint would be that often the skybox takes up very little of the screen, and is therefore usually drawn last.

You don't mention whether you're using any blending (transparent) for objects. If so, the background has to be drawn first.

1. Clear the depth buffer to 1.0.

2. Sort objects beyond the far plane farthest to closest.

3. Set depth testing to LESS_EQUAL.

4. Use a vertex shader that outputs position as pos.xyzz to force a depth of 1.0.

5. Render the "background" objects as sorted (far-to-near) - everything beyond the far plane renders with depth = 1.0, but overdrawing anything "beyond" it.

6. Set depth testing to LESS, and render the rest of your scene (objects between near and far) normally (i.e., using normal depth calcs and testing).

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

EDIT: Hey you're right, if I draw the planets and skybox first and then clear the z buffer, I should get exactly the output I want. I guess the only complaint would be that often the skybox takes up very little of the screen, and is therefore usually drawn last.

You're worried about the performance of drawing the full skybox, when usually it would be mostly blocked by objects in front? I'm hardly an expert on graphics performance but I wouldn't think it would be a big deal.

If you're really worried, you could experiment by drawing the skybox a bunch of times per frame and see how many you can do before it affects frame rate. If the answer is in the single digits, maybe worry about it... if in the hundreds, safe to ignore. That would be easy enough to do, and should quickly assuage your fears without doing serious profiling or anything.

Geoff

I do have some transparent objects, and those are rendered after my plain sky box.

I suppose I could do this:

1. render the non-transparent parts of the scene (90% of everything).

2. set depth to LESS_EQUAL

3 render the plain skybox with xyww

4 render the sorted planets with xyww

5 set depth to LESS

6 render the transparents (water etc.)

Thanks everyone, I know what to do.

This topic is closed to new replies.

Advertisement