Sign in to follow this  
Flamerie

Projection, Directx, 3D coordinates to 2D screen point

Recommended Posts

Flamerie    122
Hi, I try solve simple 3D cooridnates to 2D screen point. I can do it in simple way, if eye point's x and y are always 0. But when x and y 3D coordinates are not 0, and look at point is not always ahead. I get into problems. Here is what I have tried, I try calculate xPoint and yPoint (2D point). But somehow this goes wrong at for me. But, the code what I try do: ---- // First I set some default values D3DXVECTOR3 vUpVec = D3DXVECTOR3(0,1,0); // up vector D3DXVECTOR3 eyePosition = D3DXVECTOR3(30,10,60); // eye point D3DXVECTOR3 lookAtPosition = D3DXVECTOR3(0,10,-60); // Look at position D3DXVECTOR3 to3DPoint = D3DXVECTOR3(4,0,-60); // I try calculate this 2D point on the screen. float angle = D3DX_PI/4; // Screen angle float screenWidth = 800; // Screen Width float screenHeight = 800; // Screen Height // Next, I try rotate to3DPoint so that It would always look ahead, and eye position would be 0/0/0 (to make easier calculation)... and I fail in this D3DXVECTOR3 eyePosition0 = D3DXVECTOR3(0,0,0); D3DXVECTOR3 lookAtPosition0 = lookAtPosition - eyePosition; D3DXVECTOR3 to3DPoint0 = to3DPoint - eyePosition; D3DXMATRIX matView; D3DXMatrixIdentity(&matView); D3DXMatrixLookAtLH(&matView, &eyePosition0, &lookAtPosition0, &vUpVec ); D3DXVec3TransformCoord(&to3DPoint0, &to3DPoint0, &matView); // Then I do simple math, this should be ok. if I just get to3DPoint0 right. float scaleWidth = (float)screenWidth/tan(angle); float scaleHeight = (float)screenHeight/tan(angle); float xPoint = to3DPoint0.x*scaleWidth/to3DPoint0.z; float yPoint = to3DPoint0.y*screenHeight/to3DPoint0.z; yPoint = yPoint*screenWidth/screenHeight; --- Thanks. If anyone can help me.

Share this post


Link to post
Share on other sites
I think it could be that you are instantly resetting the camera to (0, 0, 0) since you don't change eyePosition0, and yet set the camera when 'rotating'. From my basic calculations eyePosition0 = (30, 0, 120) and lookAtPosition0 = (-26, -10, -120). So, you call D3DXMatrixLookAtLH and set the camera to (0, 0, 0) but are looking at (-26, -10, -120) when your point is at (4, 0, -60) I think. I think what you are doing is making the camera reset to 0, but then looks the opposite way. Maybe I'm on the total wrong wavelength here but I think that could be your problem.

Share this post


Link to post
Share on other sites
pi_equals_3    517
You're already using D3DXMatrixLookAtLH. Why not also use D3DXMatrixPerspectiveFovLH?


D3DXVECTOR3 vUpVec = D3DXVECTOR3(0,1,0); // up vector
D3DXVECTOR3 eyePosition = D3DXVECTOR3(30,10,60); // eye point
D3DXVECTOR3 lookAtPosition = D3DXVECTOR3(0,10,-60); // Look at position
D3DXVECTOR3 to3DPoint = D3DXVECTOR3(4,0,-60); // I try calculate this 2D point on the screen.
float angle = D3DX_PI/4; // Screen angle
float screenWidth = 800; // Screen Width
float screenHeight = 800; // Screen Height

// View Matrix. World space to camaera space
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &eyePosition, &lookAtPosition, &vUpVec);

// Projection Matrix. Camera space to screen space
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, angle, screenWidth/screenHeight, 1.0f, 10000.0f);

// Combined view/projection matrix. World space to screen space
D3DXMATRIX matViewProj;
D3DXMatrixMultiply(&matViewProj, &matView, &matProj);

D3DXVECTOR3 screenPoint;
D3DXVec3Transform(&screenPoint, &to3DPoint, &matViewProj);

// screenPoint.x and screenPoint.y are the 2D screen coordinates

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