Archived

This topic is now archived and is closed to further replies.

2D in front of and behind 3D

This topic is 5574 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

When setting renderstates, viewports, view and projection transformations etcetera, it''s important to group them together, right? Now, I have this scene, where the viewport is the entire window, and I have an orthogonal frustum set to 1.0 by 1.0. So if I render a 1 by 1 panel, it fits the entire viewport. This is used as a background image. Then, over that, I render some 3D things, requiring the z-buffer and such. After that, I render some small 3D things in a separate viewport in the top right of the window, and then, I want a 2D graphic in front of it all, in the entire viewport. The best way I can see to do this is first rendering the 2D bits, both the foreground and background (leaving the viewport and the frustum the same, and the z-buffer off, for instance), then turning on the z-buffer and setting a perspective projection instead of an orthogonal one etc to render the 3D bits, and after that, setting the small viewport to render the small 3D bits. This way, I''d have minimal state/viewport/etcetera changes. However, the only way I can get the foreground 2D image to be in the foreground is by rendering it -after- I''ve rendered everything else, or it won''t appear in the foreground. To cut a long story short, I want to either first render 2D and then 3D, or first render 3D and then 2D. But the only way I seem to be able to get it working is first 2D, then 3D, then 2D again, which causes me to set renderstates and viewports which had already been set before I started rendering the 3D. Any ideas on what I should be doing?

Share this post


Link to post
Share on other sites
You can probably do this if you are clever about depth buffering/testing, but sometimes it just makes more sense to render things as background, "midground", and foreground with all the changes. Are you actually experiencing problems, or are you just trying to be very efficient?

I can imagine scenarios where the "all or nothing" approach might be slower. For instance, you might be rendering your foreground with depth testing turned off because you know you want everything to show. Clever depth testing might allow you to do what your asking, but it might end up being costlier than what you''re doing now.

In short... Sometimes it''s possible to work very hard toward a bad solution. This might not be such a case, but watch out.

Author, "Real Time Rendering Tricks and Techniques in DirectX", "Focus on Curves and Surfaces"

Share this post


Link to post
Share on other sites
You shouldn''t have any more renderstate changes when doing 2D/3D/2D than you do with 2D/3D or 3D/2D. At the end of each frame eveything is set up for doing full-screen 2D (because you just plotted yur foreground 2D stuff), and that''s the same set of states that you need for the beginning of the next frame. Apart from the first frame, you just need to do :

Draw 2D background
Set 3D states
Draw 3D midground
Set 2D states
Draw 2D foreground

On the first frame you''ll need to set your 2D states initially but after that you only change the states twice a frame.

Share this post


Link to post
Share on other sites
Hmm, indeed! Thanks a lot to you both. Indeed, I''m not experiencing any problems, I''m just trying to be as efficient as possible.

You''re right, pieman. I should think of it more as a repeating chain of things, and not as one separate function. The end of the function is of course -also- the beginning of the same function, since it is looped. I guess I''d only have to set the state required for the background during initialization. I still have a lot to learn about when to render what, apparently.

Thanks for the tip, by the way, CrazedGenius. You''re right, some times it may be a lot of work to come up with what turns out to be a bad solution. I''ll have to keep that in mind, too.

Anyway, thanks to you both!

Share this post


Link to post
Share on other sites