I'm trying to implement billboarding in Qt 5.8 C++. I want to make a entity face the camera whenever I move around. I got a camera, first person controls and a plane entity with QTransform, QDiffuseMapMaterial and QPlaneMesh. I' ve made some attempts without the matrix multiplication I found in openGL-tutorials because I thought there might be an easier solution in Qt3D.
In the first approach, the plane faces to the camera (more or less), but there is no up-vector, so it often is upside down.
In the second approach I used QMatrix4x4::lookAt(const QVector3D &eye, const QVector3D ¢er, const QVector3D &up) because I sounds like exactly what I need, but as soon as I move, the plane disappears. I tried some more stuff with QQuaterions but I'm a bit overwhelmed by the math and the possibilities
// camera
Qt3DRender::QCamera* camera = view->camera();
camera->lens()->setPerspectiveProjection(90.0f, 16.0f/9.0f, 0.1f, 1000.0f);
camera->setPosition(QVector3D(2.0f, 2.0f, 2.0f));
camera->setUpVector(QVector3D(0, 1, 0));
camera->setViewCenter(QVector3D(0, 0, 0));
// controls
Qt3DExtras::QFirstPersonCameraController* camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
camController->setCamera(camera);
// emmits the new camera position to rotate function
QObject::connect(camera, &Qt3DRender::QCamera::positionChanged, plane, &Plane::rotate);
// Plane::rotate
void Plane::rotate(QVector3D target)
{
// first
planeTransform->setRotation(QQuaternion::rotationTo(planeTransform->translation(), target));
// second
QMatrix4x4 matrix = planeTransform->matrix();
matrix.lookAt(planeTransform->translation(),target,QVector3D(0,1,0));
planeTransform->setMatrix(matrix);
}