Problem with width and height of quads

Started by
13 comments, last by Dev_NightDreamer 11 years, 8 months ago
I didn't really need screen coordinate. But I need 3D coordinates, which I don't have. biggrin.png
My example was a quad which should perfectly fits the screen.
My screens size is 1280x720 pixels.
The vertices of my quad are A(-1/-1/10), B(1/-1/10), C(1/1/10) and D(-1/1/10) (for point(x/y/z)).
But it's too big (~3 times as big as my screen), so, which numbers I have to choose for x/y/z to have my quad as 1280x720 pixel?
Advertisement
Never mind all these reasons, I guess I'm just confused what you really need here, so may just as well answer the question without asking for any reason :)

Assuming that you have a symmetric view volume, which you do if you don't know what a non-symmetric one is, then you can easily calculate the size of the "screen" at a certain depth:

width = tan(fov/2)*aspect*depth;
height = tan(fov/2)*depth;

Here, fov is the vertical field of view, aspect is the aspect ratio of the dimensions of the screen, and depth is the desired depth at which you want to calculate the coordinate range of the screen. The result is that the coordinates spans from -width to width in the horizontal direction, and from -height to height in the vertical direction.

So if you want the quad to span the entire screen, then the corners should be [-width, -height, depth], [width, -height, depth], [width, height, depth] and [-width, height, depth].

You can also calculate other coordinates based on fractions of the resolution and the width and height values of course. For example, if you want the quad to span the lower quadrant only, then draw from [-width, -height, depth] to [0, 0, depth].
Thank you very much. That is just what I needed to know! smile.png

How do I calculate the vertically fov ? My Code is now:

float width = tan((D3DX_PI/4)/2)*(float)(1280/720)*(1.0f);
float height = tan((D3DX_PI/4)/2)*(1.0f);
// Set up vertex buffer
D3DVERTEX quad[4] = {
{D3DXVECTOR3(-width, -height, -1.0f), D3DXVECTOR2(0.0f, 1.0f)},
{D3DXVECTOR3(-width, height, -1.0f), D3DXVECTOR2(0.0f, 0.0f)},
{D3DXVECTOR3( width, -height, -1.0f), D3DXVECTOR2(1.0f, 1.0f)},
{D3DXVECTOR3( width, height, -1.0f), D3DXVECTOR2(1.0f, 0.0f)},
};

It has the same height as the screen, but the width is too small to cover the whole screen.

x4hp4o.png
The vertical field of view is the same field of view parameter you pass to D3DXMatrixPerspectiveFovLH. My numbers should match the window exactly, ignoring the inherent floating point errors, so I cannot say why it doesn't completely span the whole screen.

However, since you were talking about perspective effects, one thing you should keep in mind is that the width and height values are only valid for the single depth value used to calculate them. If you're drawing, say, a cube (as in your crate example you also mentioned), then the cube extends in the depth direction as well. If you want the cube to span the whole window, you have to be clear on what part of the cube has to span the size of the screen; the front, the back or somewhere inside the the cube.

Likewise, if you have a perspective effect you also have varying depth on different places of the quad. If the cube is tilted backwards at the top, then the top of the quad is further away and the height range becomes larger; thus, the quad appears smaller unless you increase its height.

D3DXMATRIXA16 projection;
D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI/4, (float)width / (float)height, 0.0f, 100.0f);
d3d9Device->SetTransform(D3DTS_PROJECTION,&projection);

Okay, so I have the same fov like in my Init(). huh.png

Yeah, thanks for the tipp. smile.png

EDIT:

I found the problem, namely that for the aspect I don't have to write (float)(1280/720) but (1280.0f/720.0f). dry.png
Now the quad fills the entire screen. biggrin.png

Thank you for helping! smile.png

This topic is closed to new replies.

Advertisement