The View-Matrix and Alternative Calculations
Posted
by
P. Avery
on Game Development
See other posts from Game Development
or by P. Avery
Published on 2013-08-28T04:37:07Z
Indexed on
2013/10/22
22:04 UTC
Read the original article
Hit count: 403
I'm working on a radiosity processor in DirectX 9. The process requires that the camera be placed at the center of a mesh face and a 'screenshot' be taken facing 5 different directions...forward...up...down...left...right...
...The problem is that when the mesh face is facing up( look vector: 0, 1, 0 )...a view matrix cannot be determined using standard trigonometry functions:
Matrix4 LookAt( Vector3 eye, Vector3 target, Vector3 up )
{
// The "look-at" vector.
Vector3 zaxis = normal(target - eye);
// The "right" vector.
Vector3 xaxis = normal(cross(up, zaxis));
// The "up" vector.
Vector3 yaxis = cross(zaxis, xaxis);
// Create a 4x4 orientation matrix from the right, up, and at vectors
Matrix4 orientation =
{
xaxis.x, yaxis.x, zaxis.x, 0,
xaxis.y, yaxis.y, zaxis.y, 0,
xaxis.z, yaxis.z, zaxis.z, 0,
0, 0, 0, 1
};
// Create a 4x4 translation matrix by negating the eye position.
Matrix4 translation =
{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
-eye.x, -eye.y, -eye.z, 1
};
// Combine the orientation and translation to compute the view matrix
return ( translation * orientation );
}
The above function comes from http://3dgep.com/?p=1700...
...Is there a mathematical approach to this problem?
Edit: A problem occurs when setting the view matrix to up or down directions, here is an example of the problem when facing down:
D3DXVECTOR4 vPos( 3, 3, 3, 1 ), vEye( 1.5, 3, 3, 1 ), vLook( 0, -1, 0, 1 ), vRight( 1, 0, 0, 1 ), vUp( 0, 0, 1, 1 );
D3DXMATRIX mV, mP;
D3DXMatrixPerspectiveFovLH( &mP, D3DX_PI / 2, 1, 0.5f, 2000.0f );
D3DXMatrixIdentity( &mV );
memcpy( ( void* )&mV._11, ( void* )&vRight, sizeof( D3DXVECTOR3 ) );
memcpy( ( void* )&mV._21, ( void* )&vUp, sizeof( D3DXVECTOR3 ) );
memcpy( ( void* )&mV._31, ( void* )&vLook, sizeof( D3DXVECTOR3 ) );
memcpy( ( void* )&mV._41, ( void* )&(-vEye), sizeof( D3DXVECTOR3 ) );
D3DXVec4Transform( &vPos, &vPos, &( mV * mP ) );
Results: vPos = D3DXVECTOR3( 1.5, -6, -0.5, 0 ) - this vertex is not properly processed by shader as the homogenous w value is 0 it cannot be normalized to a position within device space...
© Game Development or respective owner