Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 10 additions & 59 deletions plugins/openvr/src/OpenVrDisplayPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,67 +362,17 @@ glm::mat4 OpenVrDisplayPlugin::getEyeProjection(Eye eye, const glm::mat4& basePr
}
}

inline static glm::mat4 fovToCullingProjection(const std::array<float, 4> fov, const float _near, const float _far) {
const float left = fov[0];
const float right = fov[1];
const float down = fov[2];
const float up = fov[3];

const float width = right - left;
const float height = up - down;

const float m11 = 2 / width;
const float m22 = 2 / height;
const float m33 = -(_far + _near) / (_far - _near);

const float m31 = (right + left) / width;
const float m32 = (up + down) / height;
const float m43 = -(_far * (_near + _near)) / (_far - _near);

// clang-format off
const float mat[16] = {
m11, 0 , 0 , 0,
0 , m22, 0 , 0,
m31, m32, m33, -1,
0 , 0 , m43, 0,
};
// clang-format on

return glm::make_mat4(mat);
}

glm::mat4 OpenVrDisplayPlugin::getCullingProjection(const glm::mat4& baseProjection) const {
if (!_system) {
if (_system) {
ViewFrustum baseFrustum;
baseFrustum.setProjection(baseProjection);
float baseNearClip = baseFrustum.getNearClip();
float baseFarClip = baseFrustum.getFarClip();
// FIXME Calculate the proper combined projection by using GetProjectionRaw values from both eyes
return toGlm(_system->GetProjectionMatrix((vr::EVREye)0, baseNearClip, baseFarClip));
} else {
return baseProjection;
}

std::array<std::array<float, 4>, 2> fovs;

for (auto i = 0; i < 2; i++) {
_system->GetProjectionRaw(
(vr::EVREye)i,
&fovs[i][0],
&fovs[i][1],
&fovs[i][2],
&fovs[i][3]
);
}

// FIXME: OpenVR gives us tan(angle), how can we clamp
// this to within ~170° when multiplied by margin?
// const float maxAngle = 0.9f * PI;
const float margin = 1.0f;

std::array<float, 4> fovMax = {
std::min(fovs[0][0], fovs[1][0]) * margin, // left
std::max(fovs[0][1], fovs[1][1]) * margin, // right
std::min(fovs[0][2], fovs[1][2]) * margin, // bottom (flipped)
std::max(fovs[0][3], fovs[1][3]) * margin, // top (flipped)
};

ViewFrustum frustum;
frustum.setProjection(baseProjection);
return fovToCullingProjection(fovMax, frustum.getNearClip(), frustum.getFarClip());
}

float OpenVrDisplayPlugin::getTargetFrameRate() const {
Expand Down Expand Up @@ -510,7 +460,8 @@ bool OpenVrDisplayPlugin::internalActivate() {
_eyeOffsets[eye] = toGlm(_system->GetEyeToHeadTransform(eye));
_eyeProjections[eye] = toGlm(_system->GetProjectionMatrix(eye, DEFAULT_NEAR_CLIP, DEFAULT_FAR_CLIP));
});
_cullingProjection = getCullingProjection(_eyeProjections[0]);
// FIXME Calculate the proper combined projection by using GetProjectionRaw values from both eyes
_cullingProjection = _eyeProjections[0];
});

// enable async time warp
Expand Down
Loading