Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Draworder calculation depending on camera

Started by sms-timing Dec 24, 2008 at 3:53 AM 0 replies 650+ views
Original Post
sms-timing
sms-timing
Hi How can I calculate the draworder depending on the current camera. I have a "Vector3" with the position of the object. I need to know the distance to the camera, so I can sort my items before drawing them. I have issues with the alphablending thing. Kind regards Alexander
Evil Steve
Evil Steve
What language and DirectX version?

You can get the view matrix, and read the position from it. Assuming C++ / DX9:
float GetDistanceToCamera(const D3DXVECTOR3& vPos){   D3DMATRIX matView;   HRESULT hResult = m_pDevice->GetTransform(D3DTS_VIEW, &matView);   if(FAILED(hResult))      return 9999.0f;   D3DXVECTOR3 vCameraPos;   vCameraPos.x = matView._41;   vCameraPos.y = matView._42;   vCameraPos.z = matView._43;   D3DXVECTOR3 vDistance = vPos - vCameraPos;   return D3DXVec3Length(&vDistance);}
That code is untested though. It's also worth noting that the usual way to do this is to have a camera class that contains the current camera orientation and frustum planes for clipping.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.