void Camera::BuildViewMatrix(const Maths::Vector4 &position, const Maths::Vector4 &lookAt, const Maths::Vector4 &up) { Vector4 viewDir = lookAt - position; Vector4 viewSide, viewUp; // store values m_up = up; m_position = position; m_lookAt = lookAt; viewDir.Normalize(); viewUp = up - viewDir * up.DotProduct( viewDir ); viewUp.Normalize(); viewSide = viewDir.CrossProduct( viewUp ); // setup inverse rotation matrix Matrix4 rotation; rotation.SetRows( viewSide, viewUp, Vector4( -viewDir.X, -viewDir.Y, -viewDir.Z, -viewDir.W ), Vector4( 0.0f, 0.0f, 0.0f, 1.0f ) ); //transform translation Vector4 invPos; Matrix4::Transform( rotation, position, invPos ); m_inversePosition = -invPos; // build view matrix Matrix4 view; Matrix4::Multiply( view, rotation, m_viewMatrix ); m_viewMatrix( 0, 3 ) = m_inversePosition.X; m_viewMatrix( 1, 3 ) = m_inversePosition.Y; m_viewMatrix( 2, 3 ) = m_inversePosition.Z; m_viewMatrix( 3, 3 ) = m_inversePosition.W; m_viewDir = viewDir; }
void testCrossProduct(){ Vector4 a; Vector4 b; a.SetVector4(1,2,0,1); b.SetVector4(2,1,1,1); Vector4* c; c = a.CrossProduct(b,a); c->DisplayVector4(); delete c; }