Im trying to display a mesh in front of the camera and FACING the camera ( I'm developing an small editor in wpf using helix3d).
Pretty much something like this question
I load my mesh into a ModelVisual3D
and I set it 20 units in front of the camera with this code:
var camera = ((PerspectiveCamera) viewport.Camera);
var distanceFromCamera = 20;
var direction = camera.LookDirection;
direction.Normalize();
direction = direction * distanceFromCamera;
var meshPosition = camera.Position + direction;
var compositeTransform = new Transform3DGroup();
compositeTransform.Children.Add(new RotationTransform(???));
compositeTransform.Children.Add(new TranslateTransform3D(meshPosition));
ModelVisual3D mesh = LoadMesh(...);
mesh.Transform = compositeTransform
It already shows correctly in front of the camera, but I'm missing the rotation, how can I extract the rotation of the camera and set it to my mesh so is facing the camera?
Edit
I tried to use the following function to get the rotation matrix but is not working either:
static Matrix3D CalculateLookAt(Vector3D eye, Vector3D at, Vector3D up)
{
if (Math.Abs(up.Length - 0.0) < double.Epsilon) up = new Vector3D(0, 1, 0);
var zaxis = (at - eye);
zaxis.Normalize();
var xaxis = Vector3D.CrossProduct(up, zaxis);
xaxis.Normalize();
var yaxis = Vector3D.CrossProduct(zaxis, xaxis);
return new Matrix3D(
xaxis.X, yaxis.X, zaxis.X, 0,
xaxis.Y, yaxis.Y, zaxis.Y, 0,
xaxis.Z, yaxis.Z, zaxis.Z, 0,
Vector3D.DotProduct(xaxis, -eye), Vector3D.DotProduct(yaxis, -eye), Vector3D.DotProduct(zaxis, -eye), 1
);
}