camera frustum disoriented

Started by
6 comments, last by MJP 15 years, 4 months ago
I have a calculated frustum points. Set the world transformation with the camera's inverse view matrix and this is the result: On the left, the camera is looking straight at the building. Apparently when i switch view and look at the rendered camera frustum, its disoriented and its position is off(right image). Not sure where I went wrong.
Advertisement
I think there is some error perhaps happened:

1. Camera's position is looks bad(From your image^^).
D3DXMATRIX mtxViewStraightInverse;
D3DXMatrixInverse(&mtxViewStraightInverse, NULL, &mtxViewStraight);
D3DXVECTOR3 camPostion = D3DXVECTOR3(mtxViewStraightInverse._41, mtxViewStraightInverse._42, mtxViewStraightInverse._43);

2. The projection matrix is not match.

3. If the frustum is calculated with the viewport paramater, but the viewport is not match.
Hi Armterla

Thanks for trying to help.

But I think the camera's position should be fine, as well as the projection and view matrix.

I use the code from DXUT - FirstPersonCamera.

Also the frustum points are calculated using the view and projection matrix.

Thanks.

Why don't you post your code for calculating the frustum coordinates?
Hi Guys

Thanks for trying to help. This code is from the PSSM tutorial, which in turn is converted from the frustum culling OpenGL tutorial:

http://www.lighthouse3d.com/opengl/viewfrustum/

	D3DXVECTOR3 vZ = m_vLookAt - m_vEye;	D3DXVec3Normalize( &vZ, &vZ );	D3DXVECTOR3 vX;	D3DXVec3Cross( &vX, GetWorldUp(), &vZ );	D3DXVec3Normalize( &vX, &vX );	D3DXVECTOR3 vY;	D3DXVec3Cross( &vY, &vZ, &vX );	float fNearPlaneHeight = tanf(m_fFOV * 0.5f) * m_fNearPlane;	float fNearPlaneWidth = fNearPlaneHeight * m_fAspect;	float fFarPlaneHeight = tanf(m_fFOV * 0.5f) * m_fFarPlane;	float fFarPlaneWidth = fFarPlaneHeight * m_fAspect;	D3DXVECTOR3 vNearPlaneCenter = m_vEye + vZ * m_fNearPlane;	D3DXVECTOR3 vFarPlaneCenter = m_vEye + vZ * m_fFarPlane;	// Near	mFrustumPoints[0]=D3DXVECTOR3(vNearPlaneCenter - vX*fNearPlaneWidth - vY*fNearPlaneHeight); // LR	mFrustumPoints[1]=D3DXVECTOR3(vNearPlaneCenter - vX*fNearPlaneWidth + vY*fNearPlaneHeight); // UR	mFrustumPoints[2]=D3DXVECTOR3(vNearPlaneCenter + vX*fNearPlaneWidth + vY*fNearPlaneHeight); // UL	mFrustumPoints[3]=D3DXVECTOR3(vNearPlaneCenter + vX*fNearPlaneWidth - vY*fNearPlaneHeight); // LL	// Far	mFrustumPoints[4]=D3DXVECTOR3(vFarPlaneCenter - vX*fFarPlaneWidth - vY*fFarPlaneHeight); // LR	mFrustumPoints[5]=D3DXVECTOR3(vFarPlaneCenter - vX*fFarPlaneWidth + vY*fFarPlaneHeight); // UR	mFrustumPoints[6]=D3DXVECTOR3(vFarPlaneCenter + vX*fFarPlaneWidth + vY*fFarPlaneHeight); // UL	mFrustumPoints[7]=D3DXVECTOR3(vFarPlaneCenter + vX*fFarPlaneWidth - vY*fFarPlaneHeight); // LL


I think I may just need to rotate the frustum but I want to understand if there anything that went wrong.

Thanks!
Probably the easiest way to do it is to calculate the frustum coordinate in view-space every time your projection parameters change, and then transform them by the camera's world matrix every time its position or orientation changes. Like this:

void OnProjectionChanged(){        float fNearPlaneHeight = tanf(m_fFOV * 0.5f) * m_fNearPlane;	float fNearPlaneWidth = fNearPlaneHeight * m_fAspect;	float fFarPlaneHeight = tanf(m_fFOV * 0.5f) * m_fFarPlane;	float fFarPlaneWidth = fFarPlaneHeight * m_fAspect;	D3DXVECTOR3 vNearPlaneCenter = D3DXVECTOR3(0, 0, fNearPlane);	D3DXVECTOR3 vFarPlaneCenter = D3DXVECTOR3(0, 0, fFarPlane);	// Near	mFrustumPointsVS[0]=D3DXVECTOR3(vNearPlaneCenter - fNearPlaneWidth - fNearPlaneHeight); // LR	mFrustumPointsVS[1]=D3DXVECTOR3(vNearPlaneCenter - fNearPlaneWidth + fNearPlaneHeight); // UR	mFrustumPointsVS[2]=D3DXVECTOR3(vNearPlaneCenter + vfNearPlaneWidth + fNearPlaneHeight); // UL	mFrustumPointsVS[3]=D3DXVECTOR3(vNearPlaneCenter + fNearPlaneWidth - fNearPlaneHeight); // LL	// Far	mFrustumPointsVS[4]=D3DXVECTOR3(vFarPlaneCenter - fFarPlaneWidth - fFarPlaneHeight); // LR	mFrustumPointsVS[5]=D3DXVECTOR3(vFarPlaneCenter - fFarPlaneWidth + fFarPlaneHeight); // UR	mFrustumPointsVS[6]=D3DXVECTOR3(vFarPlaneCenter + fFarPlaneWidth + fFarPlaneHeight); // UL	mFrustumPointsVS[7]=D3DXVECTOR3(vFarPlaneCenter + fFarPlaneWidth - fFarPlaneHeight); // LL}void OnCameraMoveOrRotate(){    // Camera's world matrix is inverse of the view matrix    D3DXMATRIXA16 cameraWorldMatrix;    D3DXMatrixInverse(&cameraWorldMatrix, NULL, &mViewMatrix);    // Transform frustum coordinates to world space    D3DXVec3TransformCoordArray(mFrustumPointsWS, sizeof(D3DXVECTOR3), mFrustumPointsVS, sizeof(D3DXVECTOR3), &cameraWorldMatrix, 8);}
Oops, sorry.

The calculated frustum points are already in word space, so I shouldn't be doing any transformation at all and just set the world transformation to identity.

Sorry about that, but many thanks!




Quote:Original post by mickeyren
The calculated frustum points are already in word space, so I shouldn't be doing any transformation at all and just set the world transformation to identity.


Right, but it looked like you weren't properly accounting for rotation in your code (which is why I suggested an alternative).

This topic is closed to new replies.

Advertisement