/// <summary> /// Setup this matrix to view the universe in a certain direction. /// </summary> /// <param name="eyePosition"> /// A <see cref="Vertex3f"/> that specify the eye position, in local coordinates. /// </param> /// <param name="forwardVector"> /// A <see cref="Vertex3f"/> that specify the direction of the view. It will be normalized. /// </param> /// <param name="upVector"> /// A <see cref="Vertex3f"/> that specify the up vector of the view camera abstraction. It will be normalized /// </param> /// <returns> /// It returns a view transformation matrix used to transform the world coordinate, in order to view /// the world from <paramref name="eyePosition"/>, looking at <paramref name="forwardVector"/> having /// an up direction equal to <paramref name="upVector"/>. /// </returns> public void LookAtDirection(Vertex3d eyePosition, Vertex3d forwardVector, Vertex3d upVector) { Vertex3d rightVector; // Normalize forward vector forwardVector.Normalize(); // Normalize up vector (it should already be normalized) upVector.Normalize(); // Compute the right vector (cross-product between forward and up vectors; right is perperndicular to the plane) rightVector = forwardVector ^ upVector; rightVector.Normalize(); // Derive up vector upVector = rightVector ^ forwardVector; // Compute view matrix ModelMatrixDouble lookatMatrix = new ModelMatrixDouble(), positionMatrix = new ModelMatrixDouble(); // Row 0: right vector lookatMatrix[0, 0] = rightVector.x; lookatMatrix[0, 1] = rightVector.y; lookatMatrix[0, 2] = rightVector.z; // Row 1: up vector lookatMatrix[1, 0] = upVector.x; lookatMatrix[1, 1] = upVector.y; lookatMatrix[1, 2] = upVector.z; // Row 2: opposite of forward vector lookatMatrix[2, 0] = -forwardVector.x; lookatMatrix[2, 1] = -forwardVector.y; lookatMatrix[2, 2] = -forwardVector.z; // Eye position positionMatrix.Translate(eyePosition); // Complete look-at matrix Set(positionMatrix * lookatMatrix); }
/// <summary> /// Setup this matrix to view the universe in a certain direction. /// </summary> /// <param name="eyePosition"> /// A <see cref="Vertex3f"/> that specify the eye position, in local coordinates. /// </param> /// <param name="forwardVector"> /// A <see cref="Vertex3f"/> that specify the direction of the view. It will be normalized. /// </param> /// <param name="upVector"> /// A <see cref="Vertex3f"/> that specify the up vector of the view camera abstraction. It will be normalized /// </param> /// <returns> /// It returns a view transformation matrix used to transform the world coordinate, in order to view /// the world from <paramref name="eyePosition"/>, looking at <paramref name="forwardVector"/> having /// an up direction equal to <paramref name="upVector"/>. /// </returns> public void LookAtDirection(Vertex3d eyePosition, Vertex3d forwardVector, Vertex3d upVector) { Vertex3d rightVector; forwardVector.Normalize(); upVector.Normalize(); rightVector = forwardVector ^ upVector; rightVector.Normalize(); if (rightVector.Module() <= 0.0f) { rightVector = Vertex3f.UnitX; } upVector = rightVector ^ forwardVector; // Compute view matrix ModelMatrixDouble lookatMatrix = new ModelMatrixDouble(); // Row 0: right vector lookatMatrix[0, 0] = rightVector.x; lookatMatrix[0, 1] = rightVector.y; lookatMatrix[0, 2] = rightVector.z; // Row 1: up vector lookatMatrix[1, 0] = upVector.x; lookatMatrix[1, 1] = upVector.y; lookatMatrix[1, 2] = upVector.z; // Row 2: opposite of forward vector lookatMatrix[2, 0] = -forwardVector.x; lookatMatrix[2, 1] = -forwardVector.y; lookatMatrix[2, 2] = -forwardVector.z; // Eye position lookatMatrix.Translate(eyePosition); // Complete look-at matrix Set(lookatMatrix); }