• Create Account

## Plane inside view frustum

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

19 replies to this topic

### #1D3F84U  Members

121
Like
0Likes
Like

Posted 20 April 2014 - 04:40 AM

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, 20 April 2014 - 07:36 AM.

### #2Norman Barrows  Members

6339
Like
0Likes
Like

Posted 20 April 2014 - 05:57 PM

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.

.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #3D3F84U  Members

121
Like
0Likes
Like

Posted 20 April 2014 - 06:27 PM

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

### #4Burnt_Fyr  Members

1656
Like
1Likes
Like

Posted 21 April 2014 - 08:35 AM

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.

### #5D3F84U  Members

121
Like
0Likes
Like

Posted 22 April 2014 - 02:56 AM

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;


### #6Burnt_Fyr  Members

1656
Like
0Likes
Like

Posted 22 April 2014 - 06:47 AM

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.

### #7D3F84U  Members

121
Like
0Likes
Like

Posted 22 April 2014 - 08:04 AM

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();
}



#### Attached Thumbnails

Edited by D3F84U, 22 April 2014 - 08:40 AM.

### #8Norman Barrows  Members

6339
Like
1Likes
Like

Posted 22 April 2014 - 09:44 AM

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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #9Burnt_Fyr  Members

1656
Like
0Likes
Like

Posted 22 April 2014 - 09:54 AM

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, 22 April 2014 - 10:30 AM.

### #10Burnt_Fyr  Members

1656
Like
0Likes
Like

Posted 22 April 2014 - 09:56 AM

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.

### #11D3F84U  Members

121
Like
0Likes
Like

Posted 22 April 2014 - 09:58 AM

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

### #12Norman Barrows  Members

6339
Like
1Likes
Like

Posted 22 April 2014 - 10:49 AM

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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #13D3F84U  Members

121
Like
0Likes
Like

Posted 22 April 2014 - 11:27 AM

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.

### #14Burnt_Fyr  Members

1656
Like
0Likes
Like

Posted 22 April 2014 - 02:58 PM

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.

### #15D3F84U  Members

121
Like
0Likes
Like

Posted 23 April 2014 - 04:37 AM

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...

### #16D3F84U  Members

121
Like
0Likes
Like

Posted 26 April 2014 - 04:42 AM

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, 26 April 2014 - 04:44 AM.

### #17Burnt_Fyr  Members

1656
Like
0Likes
Like

Posted 26 April 2014 - 06:59 AM

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.

### #18D3F84U  Members

121
Like
0Likes
Like

Posted 26 April 2014 - 07:44 AM

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.

### #19Burnt_Fyr  Members

1656
Like
0Likes
Like

Posted 26 April 2014 - 08:01 AM

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.

### #20D3F84U  Members

121
Like
0Likes
Like

Posted 26 April 2014 - 08:31 AM

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);

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, 26 April 2014 - 08:32 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.