Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Q3D] Setting aspect ration has no effect on an image
Forum Updated to NodeBB v4.3 + New Features

[Q3D] Setting aspect ration has no effect on an image

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 293 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VictorT
    wrote on last edited by VictorT
    #1

    Good day!

    Again trying to make something NOT out of box I'm facing the following essue.
    I was trying to change aspect ration of a camera like

    Qt3DRender::QCamera *camera = view.camera();
        camera->lens()->setPerspectiveProjection(45.0f, 100.0f/*16.0f/9.0f*/, 0.1f, 1000.0f);
        camera->lens()->setProjectionType(Qt3DRender::QCameraLens::ProjectionType::PerspectiveProjection);
        camera->setPosition(QVector3D(0, 0, 40.0f));
        camera->setViewCenter(QVector3D(0, 0, 0));
    

    But apperently it has no effect what-so-ever. It's lke this parameters is totally irrelevant to image formation.
    Could some one explain me how can I set aspect ration or any other method to set "uniform mat4 mvp;" if i have to use shader anyway.

    Is there any concise reference on how CameraLens passes its value to shaders? The code is VERY big and intricated I'd rather not go too deep into it.

    Thanks!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VictorT
      wrote on last edited by
      #2

      Ok... Things went worse
      This part works fine

      Qt3DRender::QCamera *cameraEntity = view->camera();
      
          cameraEntity->lens()->setPerspectiveProjection(45.0f, /*100.0f*/16.0f/9.0f, 0.1f, 1000.0f);
          cameraEntity->setPosition(QVector3D(0, 0, 20.0f));
          cameraEntity->setUpVector(QVector3D(0, 1, 0));
          cameraEntity->setViewCenter(QVector3D(0, 0, 0));
          cameraEntity->setNearPlane(0.01);
          cameraEntity->setFarPlane(100);
      

      If I add right after this

       float viewAngle = 2.0f*atan2(((float)imgHeight)/2.0/fy, 1.0)*180.0f/M_PI;
          float aspectRation = ((float)imgWidth)/((float)imgHeight);
          cameraEntity->lens()->setPerspectiveProjection(viewAngle,/*20.0f*/1.0f/aspectRation, 0.1f, 1000.0f);
          qDebug()<<cameraEntity->lens()->projectionMatrix();
      

      everything still goes fine.

      Now If I add this (again right after the last part of code)

      QMatrix4x4 m = cameraEntity->lens()->projectionMatrix();
          cameraEntity->lens()->setProjectionMatrix(m);
      

      everything is still visible but the image does not adapt to the screen size any more (it just stretches along with the window). It's very inconvinient but I think I can coup with it.

      Finally if I add the following code last then everything just disappears

      QMatrix4x4 projectionMatrix = cameraEntity->lens()->projectionMatrix();;
      //    projectionMatrix.setToIdentity();
          float farPlane = 10;
          float nearPlane = 0.01;
      
          {
              float bottom = ((float)(0.0f-camCenterY))/fy*nearPlane;
              float top = ((float)(imgHeight-camCenterY))/fy*nearPlane;
              float left = ((float)(0.0f-camCenterX))/fx*nearPlane;
              float right = ((float)((float)imgWidth-camCenterX))/fx*nearPlane;
              // Construct the projection.
      // this code is copied from QMatrix4x4
              float clip = farPlane - nearPlane;
      
              float radians = qDegreesToRadians(viewAngle / 2.0f);
              float sine = std::sin(radians);
              float cotan = std::cos(radians) / sine;
      
              projectionMatrix(0,0) = cotan * aspectRation;
              projectionMatrix(0,1) = 0.0f;
              projectionMatrix(0,2) = 0.0f;
              projectionMatrix(0,3) = 0.0f;
              projectionMatrix(1,0) = 0.0f;
              projectionMatrix(1,1) = cotan;
              projectionMatrix(1,2) = 0.0f;
              projectionMatrix(1,3) = 0.0f;
              projectionMatrix(2,0) = 0.0f;//?
              projectionMatrix(2,1) = 0.0f;
              projectionMatrix(2,2) = -(nearPlane + farPlane) / clip;
              projectionMatrix(2,3) = -(2.0f * nearPlane * farPlane) / clip;
              projectionMatrix(3,0) = 0.0f;
              projectionMatrix(3,1) = 0.0f;
              projectionMatrix(3,2) = -1.0f;
              projectionMatrix(3,3) = 0.0f;
      
          }
      
      
          cameraEntity->lens()->setProjectionMatrix(projectionMatrix);
      

      I DO NOT UNDERSTAND WHY THIS HAPPENS! This part is complitely illogical. The projection matrix is the same in all cases (apart when the windowis resizes).
      Dear developrs could you explain what I'm doing wrong and how can i fix it?

      kshegunovK 1 Reply Last reply
      0
      • V Offline
        V Offline
        VictorT
        wrote on last edited by VictorT
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • V VictorT

          Ok... Things went worse
          This part works fine

          Qt3DRender::QCamera *cameraEntity = view->camera();
          
              cameraEntity->lens()->setPerspectiveProjection(45.0f, /*100.0f*/16.0f/9.0f, 0.1f, 1000.0f);
              cameraEntity->setPosition(QVector3D(0, 0, 20.0f));
              cameraEntity->setUpVector(QVector3D(0, 1, 0));
              cameraEntity->setViewCenter(QVector3D(0, 0, 0));
              cameraEntity->setNearPlane(0.01);
              cameraEntity->setFarPlane(100);
          

          If I add right after this

           float viewAngle = 2.0f*atan2(((float)imgHeight)/2.0/fy, 1.0)*180.0f/M_PI;
              float aspectRation = ((float)imgWidth)/((float)imgHeight);
              cameraEntity->lens()->setPerspectiveProjection(viewAngle,/*20.0f*/1.0f/aspectRation, 0.1f, 1000.0f);
              qDebug()<<cameraEntity->lens()->projectionMatrix();
          

          everything still goes fine.

          Now If I add this (again right after the last part of code)

          QMatrix4x4 m = cameraEntity->lens()->projectionMatrix();
              cameraEntity->lens()->setProjectionMatrix(m);
          

          everything is still visible but the image does not adapt to the screen size any more (it just stretches along with the window). It's very inconvinient but I think I can coup with it.

          Finally if I add the following code last then everything just disappears

          QMatrix4x4 projectionMatrix = cameraEntity->lens()->projectionMatrix();;
          //    projectionMatrix.setToIdentity();
              float farPlane = 10;
              float nearPlane = 0.01;
          
              {
                  float bottom = ((float)(0.0f-camCenterY))/fy*nearPlane;
                  float top = ((float)(imgHeight-camCenterY))/fy*nearPlane;
                  float left = ((float)(0.0f-camCenterX))/fx*nearPlane;
                  float right = ((float)((float)imgWidth-camCenterX))/fx*nearPlane;
                  // Construct the projection.
          // this code is copied from QMatrix4x4
                  float clip = farPlane - nearPlane;
          
                  float radians = qDegreesToRadians(viewAngle / 2.0f);
                  float sine = std::sin(radians);
                  float cotan = std::cos(radians) / sine;
          
                  projectionMatrix(0,0) = cotan * aspectRation;
                  projectionMatrix(0,1) = 0.0f;
                  projectionMatrix(0,2) = 0.0f;
                  projectionMatrix(0,3) = 0.0f;
                  projectionMatrix(1,0) = 0.0f;
                  projectionMatrix(1,1) = cotan;
                  projectionMatrix(1,2) = 0.0f;
                  projectionMatrix(1,3) = 0.0f;
                  projectionMatrix(2,0) = 0.0f;//?
                  projectionMatrix(2,1) = 0.0f;
                  projectionMatrix(2,2) = -(nearPlane + farPlane) / clip;
                  projectionMatrix(2,3) = -(2.0f * nearPlane * farPlane) / clip;
                  projectionMatrix(3,0) = 0.0f;
                  projectionMatrix(3,1) = 0.0f;
                  projectionMatrix(3,2) = -1.0f;
                  projectionMatrix(3,3) = 0.0f;
          
              }
          
          
              cameraEntity->lens()->setProjectionMatrix(projectionMatrix);
          

          I DO NOT UNDERSTAND WHY THIS HAPPENS! This part is complitely illogical. The projection matrix is the same in all cases (apart when the windowis resizes).
          Dear developrs could you explain what I'm doing wrong and how can i fix it?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4
          camera->lens()->setAspectRatio(aspectRatio); 
          

          What are you wanting to do exactly, though?
          As for the uniforms, here's a list of the ones automatically populated for you:
          https://doc.qt.io/qt-5/qt3drender-qshaderprogram.html

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VictorT
            wrote on last edited by
            #5

            I jus try to anderstand the logic behide the way the pojection matrix is being formed in Qt3D. It's really poor documented and the code surfing is time consuming. For the moment I've solve my issue. Not quite elegantly but yet....

            What I was trying to do back then is assigning the projection matrix myself to fit the properties of my camera. Eventually I did it but it has taken my dayoff to dig the code.

            When it comes to Framegraph it even worse....
            Anyway anything NOT out of box is an automatic sentence to mining the code up to the end of your life your project life. It starts getting on my nerves.

            Only QML examples gives some explanation to the way things has to be done. Yet the QL and C++ versions are far from being isomorphic since QML is basically a configurator for C++ classes with thousands of wrappers to make it work.

            I've made the code above worked bt the problem is I don't know how yet. So the essue can be marked solve unfortunatly without a benifit to anyone

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved