Understanding the Z-coordinate.

Started by
7 comments, last by Fredericvo 9 years, 8 months ago

hi all.

i do not understand the concept of the Z coordinate in direct3d 11. i tried to google for an explanation but didn't find anything.

when normalised x and y coordinates are used in regards to screen width and height from -1.0f to 1.0f ... what is Z used with when it is 0.0f to 1.0f ?

Advertisement

I'm not sure if Direct3D 11 makes an odd use for the Z coordinate, but everytime you talk about 3D, the Z axis is just another axis, like X and Y. When you work in 2D you still need something to know what should be drawn in front of what, and it's usually done using a value on the Z axis. You should check the convention used for the sign, but assuming negative means "farther" than positive Z you should give less Z coordinate to the background objects and more to the foreground.

In a 2D game you'll probably only use the Z value for what I said before, if you're normalizing vectors you'll probably making some transformation, so you should assume that Z is zero.

If you give an example of what you're doing it would be easier to help.

You draw what's contained in a viewing frustum. The projection to the screen actually doesn't completely forget about the depth of the point that has been projected. Instead, the point gets a value of 0.0 if it is at the near plane, 1.0 if it is at the far plane and somewhere in between if it is inside. This allows you to do basic things like using a z-buffer to make sure nearby things occlude far-away things, and to do advanced things like SSAO.

so it's all about the near-z far-z of the projection matrix?

so it's all about the near-z far-z of the projection matrix?

After projection, yes.

After projection, the x and y coordinates are relative to your viewing frustum (the 2D display screen) and the z is the depth within the screen between the near and far plane.

You shouldn't assume too much about the exact depth within the frustum because there are several different algorithms (z-buffer, w-buffer, 1/Z, logarithmic Z, Irregular Z, Z', etc) that adjust the relative scale between 0.0 and 1.0. That is, a z value of 0.5 might not mean it is exactly halfway between the near and far plane, but you know 0.4 is closer than 0.5.

For beginners the projected z value is not much use beyond an optimization hint. Stuff up close means you don't draw stuff that is behind it, and you can reduce overdraw by drawing the nearest items first. Later you will find all kinds of wonderful algorithms that use and manipulate the depth buffer. You can then use the projected z difference for assorted effects, depth images, advanced lighting techniques, advanced depth of field processing, and more.

You shouldn't assume too much about the exact depth within the frustum


in case of text rendering, how would I draw text at position supplied by x,y,z screen space coordinates to convert to normalised then?

another related question is: I have a viewport as


D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = (float)WINDOW_WIDTH;
viewport.Height = (float)WINDOW_HEIGHT;
 
viewport.MinDepth = 0;
viewport.MaxDepth = 1;

so the viewport depth is 0 to 1. at the same time, my projection matrix is(following tutorials of course):


projection = XMMatrixPerspectiveFovLH(0.4f*3.14f, (float)WINDOW_WIDTH / WINDOW_HEIGHT, 1.0f, 1000.0f);

so it's from 1 to 1000.

Then why on earth would something like this:

Vertex v[] =
{
Vertex( -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f ),
Vertex( -1.0f, +1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f ),
Vertex( +1.0f, +1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f ),
Vertex( +1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f ),
Vertex( -1.0f, -1.0f, +1.0f, 0.0f, 1.0f, 1.0f, 1.0f ),
Vertex( -1.0f, +1.0f, +1.0f, 1.0f, 1.0f, 1.0f, 1.0f ),
Vertex( +1.0f, +1.0f, +1.0f, 1.0f, 0.0f, 1.0f, 1.0f ),
Vertex( +1.0f, -1.0f, +1.0f, 1.0f, 0.0f, 0.0f, 1.0f ),
};


would work? the Z is -1.0f on some vertices.

Note about vertex layout:


//The input-layout description
D3D11_INPUT_ELEMENT_DESC layout[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
};

so it's from 1 to 1000.

Then why on earth would something like this:
http://www.braynzarsoft.net/index.php?p=D3D11TRANS said



Vertex v[] =

    {

        Vertex( -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f ),

        Vertex( -1.0f, +1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f ),

        Vertex( +1.0f, +1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f ),

        Vertex( +1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f ),

        Vertex( -1.0f, -1.0f, +1.0f, 0.0f, 1.0f, 1.0f, 1.0f ),

        Vertex( -1.0f, +1.0f, +1.0f, 1.0f, 1.0f, 1.0f, 1.0f ),

        Vertex( +1.0f, +1.0f, +1.0f, 1.0f, 0.0f, 1.0f, 1.0f ),

        Vertex( +1.0f, -1.0f, +1.0f, 1.0f, 0.0f, 0.0f, 1.0f ),

    };

 
would work? the Z is -1.0f on some vertices.

Because those vertices are in worldspace modelspace and according to that projection matrix your world can contain vertices with z from 1.0f - 1000f to be visible. After transformation your coordinates will be in device space from -1 to 1

Apples & oranges. Note that this box would indeed have a half behind the near clipping plane since as you noticed it has negative values. Usually it will be "placed" in the world at visible positions (world transform)


world can contain vertices with z from 1.0f - 1000f to be visible

my world or my viewing frustum?


After transformation your coordinates will be in device space from -1 to 1

Which transformation? Isn't device space for Z from 0 to 1 ?


Note that this box would indeed have a half behind the near clipping plane since as you noticed it has negative values.

I multiply the vertices by World-View-Projection as in the tutorial. and my view is:


camPosition = XMVectorSet(0.0f, 3.0f, -8.0f, 0.0f);
camTarget = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
camUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);


camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);


Apples & oranges

ohmy.png what?

my world or my viewing frustum?

Your world's objects can be anywhere but yeah vertices comprised in your viewing frustum only are visible.

Which transformation? Isn't device space for Z from 0 to 1 ?

I meant NDC or Normalised Device Coordinates as explanied on this page http://www.directxtutorial.com/lesson.aspx?lessonid=111-4-1

Your screen is flat so Z ceases to exist although not completely. A pixel shader will store corresponding Z value in depth buffer

at that location. That one ranges from 0 - 1 indeed.

what?

Just saying you were comparing 2 dissimilar things.

This topic is closed to new replies.

Advertisement