Public Group

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

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

## Recommended Posts

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 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 on other sites
You're already using D3DXMatrixLookAtLH. Why not also use D3DXMatrixPerspectiveFovLH?

D3DXVECTOR3 vUpVec = D3DXVECTOR3(0,1,0); // up vectorD3DXVECTOR3 eyePosition = D3DXVECTOR3(30,10,60); // eye pointD3DXVECTOR3 lookAtPosition = D3DXVECTOR3(0,10,-60); // Look at positionD3DXVECTOR3 to3DPoint = D3DXVECTOR3(4,0,-60); // I try calculate this 2D point on the screen.float angle = D3DX_PI/4; // Screen anglefloat screenWidth = 800; // Screen Widthfloat screenHeight = 800; // Screen Height// View Matrix. World space to camaera spaceD3DXMATRIX matView;D3DXMatrixLookAtLH(&matView, &eyePosition, &lookAtPosition, &vUpVec);// Projection Matrix. Camera space to screen spaceD3DXMATRIX matProj;D3DXMatrixPerspectiveFovLH(&matProj, angle, screenWidth/screenHeight, 1.0f, 10000.0f);// Combined view/projection matrix. World space to screen spaceD3DXMATRIX matViewProj;D3DXMatrixMultiply(&matViewProj, &matView, &matProj);D3DXVECTOR3 screenPoint;D3DXVec3Transform(&screenPoint, &to3DPoint, &matViewProj);// screenPoint.x and screenPoint.y are the 2D screen coordinates

##### Share on other sites
Quote:
 Original post by pi_equals_3You're already using D3DXMatrixLookAtLH. Why not also use D3DXMatrixPerspectiveFovLH?

thanks, that works fine.

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 13
• 15
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632962
• Total Posts
3009498
• ### Who's Online (See full list)

There are no registered users currently online

×