Jump to content
  • Advertisement
Sign in to follow this  
D3F84U

Plane inside view frustum

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

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

Share this post


Link to post
Share on other sites
Advertisement

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.

 

.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

@Burnt_Fyr

 

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;
	MakeQuad(w, h, zp);

Share this post


Link to post
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.

Share this post


Link to post
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;

    RSSetViewports(1, &vp);


	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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!