Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Screen to world fault


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Mr.Moonshine   Members   -  Reputation: 100

Like
0Likes
Like

Posted 24 March 2012 - 01:45 AM

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();
}


Posted Image


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

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS