stereocameragenerator.cpp 3.69 KB
Newer Older
Jakob Weiss's avatar
Jakob Weiss committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83


#include "stereocameragenerator.h"

#include "core/datastructures/cameradata.h"
#include "core/datastructures/scopedtypeddata.h"

namespace campvis {

    std::string StereoCameraGenerator::loggerCat_ = "CAMPVis.modules.stereo.StereoCameraGenerator";

    StereoCameraGenerator::StereoCameraGenerator()
        : AbstractProcessor()
        , p_sourceCameraID("SourceCamera", "Source Camera", "camera", DataNameProperty::READ)
        , p_baseline("Baseline", "Baseline [cm?]", 6.5f, 0.001f, 100.0f)
    {
        addProperty(p_sourceCameraID);
        addProperty(p_baseline);
    }

    StereoCameraGenerator::~StereoCameraGenerator()
    {
    }

    void StereoCameraGenerator::init()
    {
        AbstractProcessor::init();
    }

    void StereoCameraGenerator::deinit()
    {
        AbstractProcessor::deinit();
    }

    void StereoCameraGenerator::updateResult(DataContainer & dataContainer)
    {
        ScopedTypedData<CameraData> cam(dataContainer, p_sourceCameraID.getValue());
        if (cam) {
            float eyeSeparation = p_baseline.getValue();
            auto camera = cam->getCamera();
            auto frust = camera.getFrustum();
            auto rightVec = camera.getStrafe();
            rightVec *= 0.5f * eyeSeparation;

            //-----------------------------------------------------------------------
            float focusDistance = focusDistance = camera.getFocalLength();
            float farDist = camera.getFarDist();
            float nearDist = focusDistance - 0.2f*(farDist - focusDistance);

            float ratio = camera.getRatio();
            float radians = ratio * cgt::deg2rad(camera.getFovy()) / 2.0f; // half the horizontal field of view in radians
            float widthAtNear = nearDist * tan(radians); // wd2
            float widthAtFocus = focusDistance * tan(radians);

            float tmp = (widthAtFocus - eyeSeparation * 0.5f) * (focusDistance - nearDist) / focusDistance;

            float ndfl = nearDist / focusDistance;
            float projectionPlaneShift = 0.5f * eyeSeparation * ndfl + cgt::length(rightVec);
            //float projectionPlaneShift = (widthAtFocus - widthAtNear - tmp);

            float left = frust.getLeft() - projectionPlaneShift;
            float right = frust.getRight() - projectionPlaneShift;
            float top = frust.getTop();
            float bottom = frust.getBottom();

            //LDEBUG("Focus Distance for camera.right at " << focusDistance << " left,right at " << left << "," << right << " with a shift of " << projectionPlaneShift);

            cgt::Camera camRight(camera.getPosition() + rightVec,
                camera.getFocus() + rightVec, camera.getUpVector(),
                camera.getFovy(), camera.getRatio(), nearDist, farDist);
            camRight.setFrustum(cgt::Frustum(left, right, bottom, top, nearDist, farDist));
            camRight.setProjectionMode(cgt::Camera::FRUSTUM);


            left = frust.getLeft() + projectionPlaneShift;
            right = frust.getRight() + projectionPlaneShift;
            //top = wd2;
            //bottom = -wd2;

            cgt::Camera camLeft(camera.getPosition() - rightVec,
                camera.getFocus() - rightVec, camera.getUpVector(),
                camera.getFovy(), camera.getRatio(), nearDist, farDist);
            camLeft.setFrustum(cgt::Frustum(left, right, bottom, top, nearDist, farDist));
Jakob Weiss's avatar
Jakob Weiss committed
84
            camLeft.setProjectionMode(cgt::Camera::FRUSTUM);
Jakob Weiss's avatar
Jakob Weiss committed
85
86
87
88
89
90
91
92
93
94

            dataContainer.addData(p_sourceCameraID.getValue() + ".left", new CameraData(camLeft));
            dataContainer.addData(p_sourceCameraID.getValue() + ".right", new CameraData(camRight));
        }
    }

    void StereoCameraGenerator::updateProperties(DataContainer & dataContainer)
    {
    }

Jakob Weiss's avatar
Jakob Weiss committed
95
};