Ok so I am getting there!
Below is my Render method and a helper method to get the converted mouse coords
void GameEntity::Render(IDirect3DDevice9* p_d3dDevice){ // select which vertex format we are using p_d3dDevice->SetFVF(CUSTOMFVF); // set the view transform D3DXMATRIX matView; // the view transform matrix D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (0.0f, 8.0f, 25.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 p_d3dDevice->SetTransform(D3DTS_VIEW, &matView); // set the world transform static float index = 0.0f; index += 0.03f; // an ever-increasing float value D3DXMATRIX matRotateY; // a matrix to store the rotation D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix // set the projection transform D3DXMATRIX matProjection; // the projection transform matrix D3DXMatrixPerspectiveFovLH(&matProjection, D3DXToRadian(45), // the horizontal field of view (FLOAT)640 / (FLOAT)480, // aspect ratio 1.0f, // the near view-plane 500.0f); // the far view-plane p_d3dDevice->SetTransform(D3DTS_PROJECTION, &matProjection); D3DXVECTOR3* transformedPoints = 0; transformedPoints = this->SetupLocalMousePoints(p_d3dDevice); if (transformedPoints) { m_xOffset = transformedPoints->x; m_yOffset = transformedPoints->y; } //translate the X and Y of needed D3DXMATRIX matTranslate; D3DXMatrixTranslation(&matTranslate, m_xOffset, m_yOffset, m_zOffset); //ALWAYS, Rotate before Translate p_d3dDevice->SetTransform(D3DTS_WORLD, &(matRotateY * matTranslate)); // set the world transform // select the vertex buffer to display p_d3dDevice->SetStreamSource(0, this->GetVertexBuffer(), 0, sizeof(CUSTOMVERTEX)); // set the texture p_d3dDevice->SetTexture(0, m_triangleTexture); // draw the textured square p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2); p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2); p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2); p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2); p_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2); }D3DXVECTOR3* GameEntity::SetupLocalMousePoints(IDirect3DDevice9* p_d3dDevice){ if (m_mousePoint) { D3DXVECTOR3 result; D3DXVECTOR3 screenPoint; D3DVIEWPORT9 viewport; D3DXMATRIX matProjection; D3DXMATRIX matView; D3DXMATRIX matTranslate; screenPoint.x = m_mousePoint->x; screenPoint.y = m_mousePoint->y; screenPoint.z = 0.0f; p_d3dDevice->GetViewport(&viewport); p_d3dDevice->GetTransform( D3DTS_PROJECTION, &matProjection ); p_d3dDevice->GetTransform( D3DTS_VIEW, &matView ); p_d3dDevice->GetTransform( D3DTS_WORLD, &matTranslate ); //transform the mouse clicked location D3DXVec3Unproject( &result, &screenPoint, &viewport, &matProjection, &matView, &matTranslate ); m_mousePoint = 0; return &result; } return NULL;}
This somewhat works, I think. I say, I think, because I am getting values converted into my world space, but I dont think they are correct.
If I understand how D3DXVec3Unproject works, I need to basically setup the VIEW, PROJECTION and WORLD Transforms first, then convert the users mouse click into the current world space.
I think I am doing this, but if I click in the very top left corner, my X and Y values are like X: -23, Y: 8. I was kind of expecting more like a Y value of +25 or something??
Anyways, with that aside, once I have my X and Y values, is the code (as listed above):
D3DXVECTOR3* transformedPoints = 0;transformedPoints = this->SetupLocalMousePoints(p_d3dDevice);if (transformedPoints){ m_xOffset = transformedPoints->x; m_yOffset = transformedPoints->y;}//translate the X and Y of neededD3DXMATRIX matTranslate;D3DXMatrixTranslation(&matTranslate, m_xOffset, m_yOffset, m_zOffset);
Correct? If I want to simply move the cube to the clicked location?
Thanks!
Mark