Position on screen

This topic is 3846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 on other sites
ok, I'll try that..

Share on other sites
yup...worked...thanks for the help:D:D

Share on other sites
Quote:
 Original post by The SenshiWell, 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 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 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 on other sites
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)..

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 on other sites
well, the z<1.0 worked. dunno why...maybe someone will explain...[grin]

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• Forum Statistics

• Total Topics
634050
• Total Posts
3015242
×