Sign in to follow this  
Tayron

Position on screen

Recommended Posts

I'm trying to create a lens flare effect, but I need the position on the screen (2D) of the light, in DirectX. I have a basic scene that contains a light and a camera. Any ideas? Some sample code, please?:D

Share this post


Link to post
Share on other sites
Well, if you think about it, all you really need to do is multiply whatever 3D point you're concerned with by the view matrix, and then the projection matrix, and that should give you the point projected into the 2D screen plane (which is what you're after). I think there's also a D3DX function that can do that for you. It's called something like D3DXVec3Project or something along those lines.

Share this post


Link to post
Share on other sites
Quote:
Original post by The Senshi
Well, if you think about it, all you really need to do is multiply whatever 3D point you're concerned with by the view matrix, and then the projection matrix, and that should give you the point projected into the 2D screen plane (which is what you're after).


Wouldn't the point then be in clip-space, and to get to screen-space you'd need to apply the viewport transform?

Share this post


Link to post
Share on other sites
well, I get some sort of bug....if I look at the light, the sprite draws as it should, but if I turn 180 and look away, the sprite still shows up.....any ideas why?

Share this post


Link to post
Share on other sites
Your Z might be getting messed up. In D3D, after projection the vertex is said to be in clip space. To get from clip space to screen space, a homogeneous divide (divide by w) is performed before the viewport transform. If you don't perform this divide by w, your z is likely to be incorrect (and thus resulting in incorrect clipping).

Share this post


Link to post
Share on other sites
Ok, my code is like this:

//light pos on screen
D3DXVECTOR3 out,in;
in.x=lgt.x;
in.y=lgt.y;
in.z=lgt.z;
D3DVIEWPORT9 view;
g_App.GetDevice()->GetViewport(&view);
D3DXMATRIX matProj,matWorld;
g_App.GetDevice()->GetTransform(D3DTS_PROJECTION,&matProj);
g_App.GetDevice()->GetTransform( D3DTS_WORLD, &matWorld);
D3DXVec3Project(&out,&in,&view,&matProj,&V,&matWorld);

//set sprite position
spr.set_pos(out.x-128,out.y-128);




lgt is my light (from my light class) and spr is the sprite (from my sprite class)..
could you point out the code I need to add, please?[grin]

Share this post


Link to post
Share on other sites
Here's the method of my GraphicsDevice class that does this transformation (Pro is the projection matrix).


D3DXVECTOR3 GraphicsDevice::WorldToScreen(const D3DXVECTOR3 &Pos,const D3DXMATRIX &View)
{
D3DXMATRIX M=View*Pro;
D3DXVECTOR3 V=Pos,R;

D3DXVec3TransformCoord(&V,&V,&M);

float Hw=float(Params.BackBufferWidth)/2.0f;
float Hh=float(Params.BackBufferHeight)/2.0f;

R.x=Hw+(Hw*V.x);
R.y=Hh-(Hh*V.y);
R.z=V.z;

return R;
}


It is basically doing the same thing as yours except I'm doing the viewport transform manually (probably pretty stupid, come to think about it).

I found the same problem with the sprite appearing when I turned 180 degrees. However, I found that if I examined the Z compononet of the returned D3DXVECTOR3 above, and only drew the sprite if Z was less than 1.0f, the problem was solved.

Dunno how this works. Expect someone can explain it.

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this