Followers 0

# Plane inside view frustum

## 19 posts in this topic

Hello,

I have a following problem: after defining view frustum with D3DXMatrixPerspectiveFovLH and D3DXMatrixLookAtLH I want to draw 2D rectangle on arbitrary z cooridante, parralel with view frustum. Dimesnions should be such that rectangle edges are on the edges of a 2D screen (or window) like a frame.

My app is DX10, using C++.

Edited by D3F84U
0

##### Share on other sites

you want the quad corners to touch the edges of  the upper, left, right and lower frustum planes where they meet.

given z, FOV (field of view), AR (aspect ratio), and rez (screen resolution) - all of which are known,  you can solve for the x,y,z's of the quad's corners.

trig and perhaps equation of a line come to mind as possible methods. there may be an elegant vector solution possible as well.

while i can do math, i find no pleasure in it. so i will defer to one of the math enthusiasts here to prove more details.

there may also be a clever way to do it using the pipeline's capabilities, instead of figuring it out yourself.

.

0

##### Share on other sites

Thanks for the reply, yet my math is quite basic so I'm hoping for more detailed explanation.

0

##### Share on other sites

Knowing the view space Z that you want your screen quad to sit at, We can use a bit of trig to calculate the corners. First get the height and width of the far plane. FOV/2 gives you the angle between view direction and and the top or bottom plane. so tan(fov/2) gives you the slope of that plane in regards to the horizontal plane. multiply that by your Z distance to get the rise, or half height of the quad. multiply that by the aspect ratio to get the half width.

each corner will be some combination of half height, half width, and the center of your plane, which can be found by multipying your forward view space vector by your distance.

all of these points will be in view space, so if you are drawing something on the screen at this point keep that in mind. you may want to transform them into world space view the inverse view transform so they are in world space, or maybe set your view and model transforms to identity before rendering the quad.

1

##### Share on other sites

First of all tanks for the reply. I am little lost on transformations part.

For rendering this quad, I calc. world by multiplying view and projection matrices in that order, no model transformations.

After rendering I get a quad in the middle of the screen that will grow if z grows and is smaller than render window.

If I want to consider model, view and projection transform., how do I get quad dimensions that will after all three transformations are done "touch" view frustum edges?

This is how I calc. half width and height:

	float w, h, zpos;
zpos = 100.0;
h = tan(angle/2.0) * zpos;
w = h * aspect;

0

##### Share on other sites

how much smaller that the render window is it? I don't see where you are making the quad vertices so I'm unsure what is going on there. The fact that your quad grows with Z tells me that it seems to be working as it should, IE: getting larger as the distance from camera increases.

It depends on what you plan to do in the model transform. If you rotate the quad at all, then the quads tangent and bitangent vectors will not be parallel with the camera's and there will be a definite skewing.

0

##### Share on other sites

Hello, I am glad you took some time.

EDITED:

Square increases as distance increases. I kind of don't expect it to work without at least one more transformation to transform it to world space. What I need is local space coordinates that will be transformed to world space. I need those local space coordinates to create object that will always fit in to users screen.

For now my model transformations are non existent, if nothing to make things easier.

Here is more of my code:

OnResize() WM_SIZE
D3D10_VIEWPORT vp;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
vp.Width    = LOWORD(lParam);
vp.Height   = HIWORD(lParam);
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;

float aspect = (float)lparams from size w/h;
float angle = 0.25*M_PI;
D3DXMatrixPerspectiveFovLH(&Proj, angle, aspect, 1.0f, 100.0f);
float w, h, zp;
zp = 50.0;
h = tan(angle/2.0) * zp;
w = h * aspect;
Box.MakeBox(w, h, zp);
}

MakeBox(width, height, z_position)
Vertex Vertices[] =
{
{ D3DXVECTOR3(-_width, -_height, _z_position), WHITE },
{ D3DXVECTOR3(-_width, +_height, _z_position), WHITE },
{ D3DXVECTOR3(+_width, +_height, _z_position), WHITE },
{ D3DXVECTOR3(+_width, -_height, _z_position), WHITE },
};
D3D10_BIND_VERTEX_BUFFER
DWORD Indices[] = {
// front face
0, 1, 2,
0, 2, 3,
};
D3D10_BIND_INDEX_BUFFER
}

// Build the view matrix.
float x = 0.0;
float z = -5.0;
float y = 0.0;
D3DXVECTOR3 pos(x, y, z);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&View, &pos, &target, &up);

WVP = View*Proj;
fx->SetMatrix((float*)&WVP);

D3D10_TECHNIQUE_DESC TechDesc;
Tech->GetDesc(&TechDesc);
for (UINT p = 0; p < TechDesc.Passes; ++p)
{
Tech->GetPassByIndex(p)->Apply(0);
Box.Draw();
}


Edited by D3F84U
0

##### Share on other sites

its beginning to sound as though there may be an easier way to do this - IE some sort of camera space or 2d solution. just exactly what will this be used for?  odds are there's a different easier way to do it. how to do most stuff in games has already been figured out at some point by someone.

1

##### Share on other sites

I don't have a lot of time at the moment, but if the size of the quad is increasing as it gets father away from the camera it is working as it should. Your quad should get bigger as it moves farther away because it needs to be equivalent to the near plane size after the perspective divide. I don't see anything jumping out as wrong to me, but I'll try and take a look on my lunch break in a few hours.

all of these points will be in view space, so if you are drawing something on the screen at this point keep that in mind. you may want to transform them into world space view  via the inverse view transform so they are in world space, or maybe set your view and model transforms to identity befoe rendering the quad.

EDIT: Since that quad was in view space already your transform should ONLY be the projection matrix. Otherwise, in your make cube function transform each of the vertices by the inverse view transform (the camera's world transform) to get them into world space first. Looking at your zp, you were about 1/2 of the way from the near to far plane(50/99) so your quad is about 1/2 the size it should be.

Edited by Burnt_Fyr
0

##### Share on other sites

its beginning to sound as though there may be an easier way to do this - IE some sort of camera space or 2d solution. just exactly what will this be used for?  odds are there's a different easier way to do it. how to do most stuff in games has already been figured out at some point by someone.

I'm thinking that too. If this is to be a gui, screen space coordinates would be much easier to work with. But the math as laid out should be working just fine.

0

##### Share on other sites

This is an application that renders from 2D buffer on to DirectX. I want to be able to adjust size of the virtual 3D screen showing 2D content to fit DX viewport, maintaining aspect ratio and therefore not always fitting both dimensions.

Virtual screen is at fixed xyz and so far I don't need any other transformations for world expect view and projection

0

##### Share on other sites

I want to be able to adjust size of the virtual 3D screen showing 2D content to fit DX viewport, maintaining aspect ratio and therefore not always fitting both dimensions.

scaled sprite.

you know w,h for both the sprite (2d content) and screen.

just scale the sprite til its as wide / tall as the screen, whichever comes first.

you can then fake moving it closer or farther in camera space z by further isomorphic scaling.

but as stated above the 3d approach will also work, but is more complex.

as usual with games, there are multiple ways to skin a cat. some are easier to implement than others. some run faster than others. the trick is to try to find the one that is as easy to implement as possible AND runs fast enough.

1

##### Share on other sites

just scale the sprite til its as wide / tall as the screen, whichever comes first.

scaling would work if everything was fixed, but both 2D buffer and DX screen change their properties.

0

##### Share on other sites

just scale the sprite til its as wide / tall as the screen, whichever comes first.

scaling would work if everything was fixed, but both 2D buffer and DX screen change their properties.

I'm not sure if you saw my edit above but i think i found your issue. You were essentially using the view transform twice, so were not in the right space.

However seeing that this is to push a 2d surface onto the screen, it makes sense to just start with 2d coordinates, or coordinates that are pretransformed into normalized device coordinates, which range from -1 to +1 on the x,y,z axii, with +z being the direction away from the camera. By starting with coordinate in NDC space, a full screen quad is simply (-1,1),(1,1)(-1,-1),(1,-1). The vertex shader can be disabled, as there is nothing going on there anyway. During the raster step, your NDC coords will get scaled to the viewport, at which point the pixel shader can map your 2d buffer to the quad.

0

##### Share on other sites

the trick is to try to find the one that is as easy to implement as possible AND runs fast enough.

The vertex shader can be disabled, as there is nothing going on there anyway. During the raster step, your NDC coords will get scaled to the viewport, at which point the pixel shader can map your 2d buffer to the quad.

I'll try this as soon as I can. DX is still quite new to me...

0

##### Share on other sites

I ended up leaving everything as is and calculating fixed virtual pixel size factor by trial and error. It is calculated using x dimension if x >= y or using y dimension otherwise multiplied by virtual pixel size factor. viewport and other matrices do the scaling that is always as close to the edge as virtual pixel size is accurate.

Edited by D3F84U
0

##### Share on other sites

I ended up leaving everything as is and calculating fixed virtual pixel size factor by trial and error. It is calculated using x dimension if x >= y or using y dimension otherwise multiplied by virtual pixel size factor. viewport and other matrices do the scaling that is always as close to the edge as virtual pixel size is accurate.

Did you see my post about 6 or 7 up?

When I looked through your code, you were taking the quad the you built, and transforming it by the VP matrix. How ever, this quad was built in view space. so you tried to apply the world to view transform on a plane that was already in viewspace. Your Z pos, of 50, which is almost 1/2 the distance from near to far( 50/99-1)  resulting in your quad being 1/2 the screen size give or take, but still following the rule of getting smaller with a greater Z.

0

##### Share on other sites

Your solution to my original question was on spot. It lead me to conclusion that my assumption that if it is on the edge of view frustum it will be on the edge of the screen was wrong.

0

##### Share on other sites

Now I'm the confused one, if the quad maps the full height and width of the frustum, it will be the full screen. If you go back to the original setup, and pass just the projection transform when rendering the quad it should map to the full screen.

0

##### Share on other sites

Your Z pos, of 50, which is almost 1/2 the distance from near to far( 50/99-1)  resulting in your quad being 1/2 the screen size

D3DXMatrixPerspectiveFovLH(&Proj, angle, aspect, 1.0f, 100.0f);

or

D3DXMatrixPerspectiveFovLH(&Proj, angle, aspect, 1.0f, 10000.0f);

will draw same quad

that might be an error, but as I see it it just sets clipping.

I changed

WVP = View*Proj; to WVP = Proj;

and only change is a bit larger quad but still not close to full screen.

Whole point is to have full screen quad at any distance withing clip planes.

Edited by D3F84U
0

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account