Jump to content
  • Advertisement
Sign in to follow this  
Mr.Moonshine

Screen to world fault

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. Im trying to get world coordinates from screen, using ray picking. I have tryed lots of realisation: D3DXVec3Unproject and Picking sample in SDK. All of ways giving a slight offset from truth.

SCREEN_WIDTH = 800;
SCREEN_HEIGHT = 600;



void PickingRay()
{
D3DXMATRIX MatrixWorld;
D3DXMATRIX MatrixView;
D3DXMATRIX MatrixProjection;

d3ddev->GetTransform(D3DTS_VIEW,&MatrixView);
d3ddev->GetTransform(D3DTS_PROJECTION,&MatrixProjection);
d3ddev->GetTransform(D3DTS_WORLD,&MatrixWorld);
d3ddev->GetViewport(&ViewPort);

D3DXVECTOR3 vPickRayDir;
D3DXVECTOR3 vPickRayOrig;

POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( hWndMain, &ptCursor );

// Compute the vector of the pick ray in screen space
D3DXVECTOR3 v;
v.x = ( ( ( 2.0f * ptCursor.x ) / SCREEN_WIDTH) - 1 ) / MatrixProjection._11;
v.y = -( ( ( 2.0f * ptCursor.y ) / SCREEN_HEIGHT ) - 1 ) / MatrixProjection._22;
v.z = 1.0f;

// Get the inverse view matrix
const D3DXMATRIX matView = MatrixView;
const D3DXMATRIX matWorld = MatrixWorld;
D3DXMATRIX mWorldView = matWorld * matView;
D3DXMATRIX m;
D3DXMatrixInverse( &m, NULL, &mWorldView );

// Transform the screen space pick ray into 3D space
vPickRayDir.x = v.x * m._11 + v.y * m._21 + v.z * m._31;
vPickRayDir.y = v.x * m._12 + v.y * m._22 + v.z * m._32;
vPickRayDir.z = v.x * m._13 + v.y * m._23 + v.z * m._33;
vPickRayOrig.x = m._41;
vPickRayOrig.y = m._42;
vPickRayOrig.z = m._43;
float Z = 0;
float a = (Z-vPickRayOrig.z)/vPickRayDir.z;

float X = vPickRayOrig.x+a*vPickRayDir.x;
float Y = vPickRayOrig.y+a*vPickRayDir.y;
}


I have tryed to use D3DXVec3Project to draw pixel and got it with this offset too. I have runned my app in full screen and got offset too. This offset isnt constant it raise to border, so in center it is the smallest. GetTransform gets matrices, its constant. The render loop:


void render_frame(void)
{
ViewPort.X = 0;
ViewPort.Y = 0;
ViewPort.Width = SCREEN_WIDTH;
ViewPort.Height = SCREEN_HEIGHT;
ViewPort.MinZ = 0.0f;
ViewPort.MaxZ = 1.0f;
d3ddev->SetViewport(&ViewPort);
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3ddev->BeginScene();

// select which vertex format we are using
d3ddev->SetFVF(CUSTOMFVF);

// set the view transform
D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 0.0f, 60.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView

// set the projection transform
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(60), // the horizontal field of view
(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
1.0f, // the near view-plane
1000.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection


// select the vertex buffer to display

D3DXMATRIX matTranslateA; // a matrix to store the translation for triangle A
D3DXMatrixIdentity(&matTranslateA);
d3ddev->SetTransform(D3DTS_WORLD,&matTranslateA);
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 2, 1);

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);
PickingRay();
}


screenshot.jpg


Im sorry, but you need to zoom this picture to see dots, that are effect of D3DXVec3Project from square vertices.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!