user7825011
user7825011

Reputation: 43

Qt3d Billboarding

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 &center, 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);

}

Upvotes: 0

Views: 520

Answers (1)

sirop
sirop

Reputation: 180

This

// first
planeTransform->setRotation(QQuaternion::rotationTo(planeTransform-
>translation(), target));

looks very suspicious to me as the docs say:

QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to)

Returns the shortest arc quaternion to rotate from the direction described by the vector from to the direction described by the vector to.

So both arguments have to be orientation directions. And the first argument is then the normal of your plane?

Upvotes: 0

Related Questions