1

I try to use camera's viewVector to make plane face to camera, but it will turn Clockwise or Counterclockwise when Camera rotate to left or right.

Can I make the plane always face to camera without turning Clockwise or Counterclockwise?

begin

camera left rotation : plane turn Counterclockwise

camera right rotation : plane turn clockwise

I think camera->upVector() can help me maybe, but I don't how to use.

My code :

class planeTransformClass : public Qt3DCore::QTransform {

public:
    planeTransformClass( Qt3DCore::QNode *entity = nullptr ) :Qt3DCore::QTransform(entity) {}

signals:
    void faceTo( QVector3D v ) {
        setRotation(QQuaternion::rotationTo(QVector3D(0,1,0), -v));
    } ;
};
// Background
        Qt3DCore::QEntity *planeEntity = new Qt3DCore::QEntity(rootEntity);
        Qt3DExtras::QPlaneMesh *planeMesh = new Qt3DExtras::QPlaneMesh(planeEntity);
        planeMesh->setHeight(2);
        planeMesh->setWidth(2);

        Qt3DExtras::QTextureMaterial planeMaterial = new Qt3DExtras::QTextureMaterial(planeEntity);
        Qt3DRender::QTexture2D *planeTexture = new Qt3DRender::QTexture2D(planeMaterial);

        FlippedTextureImage *planeTextureImage = new FlippedTextureImage(planeTexture);
        planeTextureImage->setSize(QSize(3000, 3000));
        planeTexture->addTextureImage(planeTextureImage);
        planeMaterial->setTexture(planeTexture);
        planeMaterial->setAlphaBlendingEnabled(true);

        // Transform
        planeTransformClass planeTransform = new planeTransformClass(planeEntity);
        planeTransform->setRotationX(90);
        planeTransform->setTranslation(QVector3D(2, 0, 0));

        planeEntity->addComponent(planeMesh);
        planeEntity->addComponent(planeMaterial);
        planeEntity->addComponent(planeTransform);

        // connect camera's viewVectorChanged
        camera->connect( camera, &Qt3DRender::QCamera::viewVectorChanged, 
                 planeTransform, &planeTransformClass::faceTo);
Florian Blume
  • 3,237
  • 17
  • 37
xyLi
  • 27
  • 5
  • Did you have a look at this [GitHub repository](https://github.com/wonder-sk/qt3d-experiments)? There's a billboard example which might be what you're looking for. Although it also changes the size of the plane so I'm not sure if that's what you want. – Florian Blume Jul 22 '20 at 12:41
  • YES, that is what I want. I try to clone project and compile c++ and qml. It can work. But I don't know how to convert it to only c++ code and make it be able to work. – xyLi Jul 22 '20 at 17:11
  • I tried to implement it in C++ but didn't manage to get it to work. I'm really not sure what the issue is. But if it's not absolutely necessary, why don't you just use QML? I once implemented a larger project in C++ only and feel like it would have made my life easier, had I also used QML. – Florian Blume Jul 23 '20 at 11:28
  • How do I use QML in Qt3DWindow? My billboard need to show in it and I don’t have experience in QML. If I use QML , I will need to conform c++ and QML because other module is work in c++. – xyLi Jul 23 '20 at 12:20
  • Well the link to the project uses QML with Qt3D. You can't use it directly in a Qt3DWindow - this means that you hvae to translate existing C++ Qt3D code to QML but its fairly easy. If you have time to wait I mgiht get round to trying to make the project run over the weekend. – Florian Blume Jul 23 '20 at 12:45
  • ok, thanks. I'm sorry to bother you all the times. By the way, can I define RootEntity and Camera then pass to qml? If it is workable, can you make an example project to teach me? – xyLi Jul 23 '20 at 13:35
  • Don't worry it's a bit complicated to understand it all. C++ and QML classes actually have a 1-1 mapping. i.e. behind every QML class, there is a C++ - the actual implementation. Checkout the `main.qml` in the project that I provided in my answer. `Entity` in there stands for `Qt3DCore::QEntity`. The same holds for `Camera` which correspondns to `Qt3DRender::QCamera`. – Florian Blume Jul 23 '20 at 14:11

1 Answers1

1

Solution using geometry shader

There is already a C++ translation this QML code that I tried to translate (using geometry shader):

https://github.com/ismailsunni/qt3d-custom-shader

My own translation can be found here. It works now after solving the issues mentioned in this question.


Solution without geometry shader

I got a different version running based on this bug report. You can find my implementation on GitHub. The billboards don't change their size when the camera moves, i.e. become smaller and larger on screen like every other object, but I hope you can work with that.

On the master branch the images also move for some reason as can be seen in this image:

enter image description here

The no_instanced_rendering branch doesn't use instanced rendering and displays everything correctly.

Edit I'm sorry I'm not using your example but I don't have time anymore to adjust it. Check out the patch related to the bug report I mentioned. The person implemented a material with this effect, you should be able to extract it and make it run.

Florian Blume
  • 3,237
  • 17
  • 37
  • I run it and it shows `"\x00\x00\x80?\x00\x00\x80?\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x80\xBF\x00\x00\x00@\x00\x00\x80@\x00\x00\x00@\x00\x00\x80?\x00\x00\x80?\x00\x00\x80?\x00\x00@@\x00\x00\x00\x00\x00\x00\xE0@\x00\x00\x00@\x00\x00\x80@\x00\x00\x00\x00\x00\x00 A\x00\x00\x00\x00\x00\x00\xA0@\x00\x00@@\x00\x00\x00\x00\x00\x00@@\x00\x00\xC0@\x00\x00\x00\x00\x00\x00@@\x00\x00\x00@\x00\x00\xE0@" Could not read shader source file: ":/shaders/light.inc.frag"` Is my environment wrong? – xyLi Jul 27 '20 at 12:28
  • It is no show error, but the png is not face to me. I think I need the environment. [run result](https://i.imgur.com/nGBCXO2.png) [in frag file shows red line](https://i.imgur.com/cNgWSpr.png) – xyLi Jul 27 '20 at 13:16
  • Qt 5.14.1 and Qt 5.11.2 . But it is no error message. There are comment about cameraPos and cameraUp in vert file. I need to uncomment ? – xyLi Jul 27 '20 at 13:37
  • I try to change modelViewMatrix and solve it. [Original to comment part in vert File](https://i.imgur.com/M7mRGke.png) and [changed part in vert File](https://i.imgur.com/UHnRh9T.png) then **[It WORK~~~~](https://imgur.com/a/A2TQXyN)** – xyLi Jul 27 '20 at 15:47
  • !!!!!!! I don't notice that **the objects also move a bit** !!!!!!! It is about the matrix Calculation. How do I fix it? (QAQ) – xyLi Jul 27 '20 at 16:15
  • I fixed it. I guess that `modelViewMatrix` can't be changed in my environment. **[So I use this to change it](https://i.imgur.com/YieowL5.png)** **[And it work finally](https://imgur.com/a/VLiyRiu)** Thanks. Next step, I will try to fix its scale. I hope I would be successful. – xyLi Jul 28 '20 at 11:33
  • Both of them can work. I just use `mat4 billboardMatrix = modelViewMatrix;` and `billboardMatrix[0][0~3] = modelMatrix[0][0~3];` and `billboardMatrix[2][0~3] = modelMatrix[2][0~3];` then `gl_Position = projectionMatrix * billboardMatrix * offsetPos;` – xyLi Jul 28 '20 at 13:20
  • Original code is `modelViewMatrix[0] = modelMatrix[0];` 、 `modelViewMatrix[2] = modelMatrix[2];` then `gl_Position = projectionMatrix * modelViewMatrix * offsetPos;` in vert file. I think they mean the same code. – xyLi Jul 28 '20 at 13:24
  • @xyLi check out my update answer - there already exists a C++ port of the QML code which is working properly! – Florian Blume Jul 28 '20 at 13:51
  • It shows `QOpenGLShader::compile(Fragment): ERROR: 0:? : 'variable' : is removed in Forward Compatible context gl_FragColor`. Can the freg file be writed like `no_instanced_rendering`'s ? – xyLi Jul 28 '20 at 15:45
  • Which one? https://github.com/ismailsunni/qt3d-custom-shader this one? – Florian Blume Jul 28 '20 at 16:01
  • [no_instanced_rendering/billboards.frag](https://github.com/florianblume/qt3d-billboards-size-changing/blob/no_instanced_rendering/billboards.frag) This. The error maybe show that I can't use `gl_FragColor`. – xyLi Jul 29 '20 at 09:52
  • I don't know where this issue comes from but I suggest you use the code that I mentioned in my updated answer: https://github.com/ismailsunni/qt3d-custom-shader. It's a working C++ port of the [QML code](https://github.com/wonder-sk/qt3d-experiments/tree/master/billboards) that I have suggested to you in the first place. – Florian Blume Jul 29 '20 at 10:28
  • ohohoh~~~ I fixed again. [In your **qt3d-billboards/blob/master/billboards.frag**](https://github.com/florianblume/qt3d-billboards/blob/master/billboards.frag) **[I show this](https://i.imgur.com/o9ox0En.png)** and [It be fixed.](https://i.imgur.com/OzCiMFW.png) – xyLi Jul 29 '20 at 11:09
  • Yeah well I didn't bother fixing the texture in the shader because it was a showcase for billboarding ;) – Florian Blume Jul 29 '20 at 17:21