Guest User

Untitled

a guest
Jan 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. auto state = m_mouse->GetState();
  2.  
  3. float mouseX = state.x;
  4. float mouseY = state.y;
  5. float m_screenWidth = m_deviceResources->GetScreenViewport().Width;
  6. float m_screenHeight = m_deviceResources->GetScreenViewport().Height;
  7.  
  8. // Normalized device coordinates
  9. float x = (2.0f * mouseX) / m_screenWidth - 1.0f;
  10. float y = 1.0f - (2.0f * mouseY) / m_screenHeight;
  11.  
  12. DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixTranspose(DirectX::XMLoadFloat4x4(&cameraComponentHandle->view));
  13. DirectX::XMMATRIX projectionMatrix = DirectX::XMMatrixTranspose(DirectX::XMLoadFloat4x4(&cameraComponentHandle->projection));
  14. DirectX::XMMATRIX inverseviewproj = DirectX::XMMatrixInverse(nullptr, viewMatrix * projectionMatrix);
  15.  
  16. DirectX::SimpleMath::Vector3 origin = DirectX::SimpleMath::Vector3(x, y, 0);
  17. DirectX::SimpleMath::Vector3 farPoint = DirectX::SimpleMath::Vector3(x, y, 1);
  18.  
  19. DirectX::SimpleMath::Vector3 rayorigin = DirectX::XMVector3TransformCoord(origin, inverseviewproj);
  20. DirectX::SimpleMath::Vector3 rayend = DirectX::XMVector3TransformCoord(farPoint, inverseviewproj);
  21. DirectX::SimpleMath::Vector3 raydirection = (rayend - rayorigin);
  22. raydirection.Normalize();
  23.  
  24. rayComponentHandle->position = rayorigin;
  25. rayComponentHandle->direction = raydirection;
Add Comment
Please, Sign In to add comment