Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. QCameraLens fieldOfView is ... geometrically in Qt :
Forum Updated to NodeBB v4.3 + New Features

QCameraLens fieldOfView is ... geometrically in Qt :

Scheduled Pinned Locked Moved Solved Game Development
1 Posts 1 Posters 417 Views 1 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.
  • uaniU Offline
    uaniU Offline
    uani
    wrote on last edited by uani
    #1

    I set my Qt3D views camera as

    camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, .01f, cameraFarRestDistance + 2.0f);
    

    which sets the fieldOfView as 45° .

    By geometric projection theory I can calculate the angle the pixel below the mouse pointer is pointing in relative to the center of the screen as

            double x = mouseX + .5 - view->width() / 2.0;
            double z = mouseY + .5 - view->height() / 2.0;
            x *= fieldOfViewX / (view->width() - 1.0);
            z *= fieldOfViewX / (view->width() - 1.0);
            const double pi = 3.1415926535897931;
            double hitX = camera->position().x() + tan(x * pi / 180.0) * (camera->position().y() - planeY);
            double hitZ = camera->position().z() + tan(z * pi / 180.0) * (camera->position().y() - planeY);
            qDebug() << "expected hit at " << hitX << " " << hitZ;
    

    code above for determining the point on a plane whose normal is the y-axis hit by the ray emitting from the camera position in the direction the pixel below the mouse pointer is pointing in.

    (Mouse pointer coordinates adjusted to have the screen center pixel be 0, 0.)

    Comparing QScreenRayCaster with that theoretical value for the given fieldOfView of 45° the following code however lets plane-hits match up to and including the 3rd decimal digit of the hit-coordinate on the plane:

            double x = mouseX - view->width() / 2.0;
            double z = mouseY - view->height() / 2.0;
            x *= 63.75 / (view->width() - 1.0);
            z *= 63.75 / (view->width() - 1.0);
            const double pi = 3.1415926535897931;
            double hitX = camera->position().x() + tan(x * pi / 180.0) * (camera->position().y() - planeY);
            double hitZ = camera->position().z() + tan(z * pi / 180.0) * (camera->position().y() - planeY);
            qDebug() << "expected hit at " << hitX << " " << hitZ;
    

    Note the value at the place of the fieldOfView has "nothing" to do with the value I set to the QCameraLens, even when trying to account for the aspect ratio.
    Also the (+ .5) is missing to have a best match.

    Addendum: the value of 63.75 works for the default window size of 800 x 800 only.
    Solution:

        double distance = camera->position().y() - planeY;
        double factor1 = camera->fieldOfView() * camera->aspectRatio() * 3.1415926535897931 / (180.0 * (viewWidth - 1.0));
        double factor2 = camera->fieldOfView() * 3.1415926535897931 / (180.0 * (viewHeight - 1.0));
        returnValue.x = camera->position().x() + tan((mouseX + .5 - viewWidth / 2.0) * factor1) * distance;
        returnValue.y = camera->position().z() + tan((mouseY + .5 - viewHeight / 2.0) * factor2) * distance;
    

    works for all window sizes.

    Thus the answer to my question as phrased originally in the title:
    QCameraLens fieldOfView obeyed or what is it in Qt geometrically ?
    is
    "yes and it is the vertical fov"

    Here 2 images for visual aid in understanding the geometric projection theory, note they also work when the value of the pixel width of the screen is odd:
    c1e2bd0e-fe7b-4022-86b0-96e8d31da79f-fov.jpg
    0d74167d-8c38-405f-b8ee-ca8fb8fac055-fov2.jpg

    And I try to calculate the direction of the pixel below the mouse pointer myself due to various issues of QScreenRayCaster:
    https://forum.qt.io/topic/140870/qscreenraycaster-does-not-hit-triangles-around-size-1-0f-and-smaller/3
    https://forum.qt.io/topic/140902/qscreenraycaster-depth-of-ray-adjustable-appears-limited-to-50-000
    https://forum.qt.io/topic/140977/qscreenraycaster-addlayer-has-no-effect

    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