Position on screen

Started by
8 comments, last by Tayron 15 years, 9 months ago
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
Advertisement
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.
ok, I'll try that..
yup...worked...thanks for the help:D:D
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?
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?
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).
NextWar: The Quest for Earth available now for Windows Phone 7.
Ok, my code is like this:
//light pos on screenD3DXVECTOR3 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 positionspr.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]
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.
well, the z<1.0 worked. dunno why...maybe someone will explain...[grin]

This topic is closed to new replies.

Advertisement