Jump to content
  • Advertisement
Sign in to follow this  
NightCabbage

2d D3DXMatrixOrthoLH layers, depth, order

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

Hey all have been looking around for a while but I can't seem to find any answers to this question. I'm using a D3DXMatrixOrthoLH projection matrix for a 2d game. I'm drawing textured quads as sprites. I would like to be able to order / layer these sprites based on their Z values (or something else if it's better). But when I do a translation on the sprite and move its Z value in front or behind the other sprite, there is no change. They are just rendered in order, completely disregarding their Z values. My ortho matrix is set up like this: D3DXMatrixOrthoLH(&matOrtho, (float)resWidth, (float)resHeight, 1.0f, 10.0f); Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Matruz
Have you enabled the z stencil buffer?


Yup, I tried that - using this:

dxDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
dxDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);

(I think it's on be default anyway? not sure)

Share this post


Link to post
Share on other sites
Aha! You were right :D

I missed the following:

dx_PresParams.EnableAutoDepthStencil = TRUE;
dx_PresParams.AutoDepthStencilFormat = D3DFMT_D16;

So I assume that now my 16-bit z-buffer will be distributed between the values 1.0 and 10.0, given that's what I supplied when I created my Orth projection?

eg. D3DXMatrixOrthoLH(&matOrtho, (float)resWidth, (float)resHeight, 1.0f, 10.0f);

Any reason for the 1.0f and 10.0f?

Couldn't you just use 0.0f and 1.0f?

Share this post


Link to post
Share on other sites
Quote:
(I think it's on be default anyway? not sure)


Nope, its not. I once had a similar problem.

What kind of game you are making, is it a sidescroller?

Share this post


Link to post
Share on other sites
Quote:
Original post by NightCabbage
Any reason for the 1.0f and 10.0f?

Couldn't you just use 0.0f and 1.0f?


Yep, basically I'm just making an engine right now, and I'll be making a side-scroller, and a top down game with the same engine. Not sure which I'll do first. But they'll both only have a few layers of depth.

D3DXMatrixOrthoLH(&matOrtho, (float)resWidth, (float)resHeight, 1.0f, 10.0f);

So what numbers are good to use for the near and far z-buffer range?

As for layers... say if 2 sprites have the same z-index, is tehre a chance that they could flicker between the 2, given they're on the same layer?

Should I give them slightly different z-index values?
eg. 1.00 and 1.01

Or, providing the draw order stays the same, I imagine they should always be drawn the same way each frame...

So say if I wanted about 20 "layers", and I'm going between 1.0 and 10.0, I could just use these z values for the layers:

1.0
1.5
2.0
2.5
3.0
...
9.0
9.5
10.0

?


-EDIT-

Also, should I be using D3DXMatrixOrthoOffCenterLH instead?

Share this post


Link to post
Share on other sites
First, I'm fairly sure you can't use 0.0f for the near clip plane (Try it and see if the debug runtimes complain). I forget why, but it's something to do with the projection matrix not being invertable if zNear = 0.0f.

Quote:
Original post by NightCabbage
So what numbers are good to use for the near and far z-buffer range?
For a 16-bit Z-buffer, you have 65536 unique Z values, with ~70% of those values used in the first ~30% of the Z range (I.e. it's not linear, because you'll notice Z-fighting close up a lot more than you'll notice it far away). I wouldn't use anything more than 1.0f -> 1000.0f with a 16-bit Z-buffer.
However, there's very little reason to use a 16-bit Z-buffer, a 24-bit or 32-bit (If it's available) would be preferable, and then there should be no problem using 1.0f -> 10000.0f or more.

Quote:
Original post by NightCabbage
As for layers... say if 2 sprites have the same z-index, is tehre a chance that they could flicker between the 2, given they're on the same layer?
Yes, You'll see a sort of tearing effect (Check Google images for "z-fighting" to see what I mean) if two triangles have the same Z value.

Quote:
Original post by NightCabbage
Should I give them slightly different z-index values?
eg. 1.00 and 1.01

Or, providing the draw order stays the same, I imagine they should always be drawn the same way each frame...

So say if I wanted about 20 "layers", and I'm going between 1.0 and 10.0, I could just use these z values for the layers:
If you're making an engine, I'd accept Z values from 0 to 1000 or so, and then convert that to your Z range internally (I.e. if you have 1.0f -> 100.0f, multiply each Z value by 0.099 to convert from "engine coordinates" to D3D coordinates. That'll also allow you to change your Z-buffer range at a later date if you want, while still keeping the engine interface the same.

Quote:
Original post by NightCabbage
Also, should I be using D3DXMatrixOrthoOffCenterLH instead?
If you need it, yes. D3DXMatrixOrthoLH gives you a coordinate space with (0, 0) in the centre of the screen. If you want (0, 0) to be the top left, you'll need to use D3DXMatrixOrthoOffCenterLH to do that.

Share this post


Link to post
Share on other sites
Quote:
Original post by NightCabbage
What is support for 24 or 32 bit z-buffers like these days?
The cardcaps spreadsheet with the SDK will show you (Samples\C++\Direct3D\ConfigSystem\CardCaps.pdf or CardCaps.xls). It shows D3DFMT_D24S8 is supported on all DX9 level hardware, D3DFMT_D24X8 is supported on almost all, and D3DFMT_D32 isn't widely supported at all.
If you want to do things "Properly", you should try several formats and see what's supported, choosing the best format available.

Quote:
Original post by NightCabbage
Steve - did you write these tutorials:
http://thetavern.servebeer.com/?p=articles&a=D3DTutorial1

They're excellent :D

Exactly what I'm after!
I did, glad you like them [smile]. I should be adding to them reasonably soon, when I stop being so lazy...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!