OpenXR - Move matrix composing out of VR tweaks

This commit is contained in:
Lubos 2022-10-17 18:31:01 +02:00
parent eb18a87eee
commit d5c9d15ec3
3 changed files with 53 additions and 59 deletions

View File

@ -362,9 +362,43 @@ bool Is2DVRObject(float* projMatrix, bool ortho) {
}
void UpdateVRProjection(float* projMatrix, float* leftEye, float* rightEye) {
VR_TweakProjection(projMatrix, leftEye, VR_PROJECTION_MATRIX_LEFT_EYE);
VR_TweakProjection(projMatrix, rightEye, VR_PROJECTION_MATRIX_RIGHT_EYE);
VR_TweakMirroring(projMatrix);
// Update project matrices
float* dst[] = {leftEye, rightEye};
VRMatrix enums[] = {VR_PROJECTION_MATRIX_LEFT_EYE, VR_PROJECTION_MATRIX_RIGHT_EYE};
for (int index = 0; index < 2; index++) {
ovrMatrix4f hmdProjection = VR_GetMatrix(enums[index]);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if ((hmdProjection.M[i][j] > 0) != (projMatrix[i * 4 + j] > 0)) {
hmdProjection.M[i][j] *= -1.0f;
}
}
}
memcpy(dst[index], hmdProjection.M, 16 * sizeof(float));
}
// Set mirroring of axes
VR_SetConfig(VR_CONFIG_MIRROR_AXIS_X, projMatrix[0] < 0);
VR_SetConfig(VR_CONFIG_MIRROR_AXIS_Y, projMatrix[5] < 0);
VR_SetConfig(VR_CONFIG_MIRROR_AXIS_Z, projMatrix[10] > 0);
if ((projMatrix[0] < 0) && (projMatrix[10] < 0)) { //e.g. Dante's inferno
VR_SetConfig(VR_CONFIG_MIRROR_PITCH, true);
VR_SetConfig(VR_CONFIG_MIRROR_YAW, true);
VR_SetConfig(VR_CONFIG_MIRROR_ROLL, false);
} else if (projMatrix[10] < 0) { //e.g. GTA - Liberty city
VR_SetConfig(VR_CONFIG_MIRROR_PITCH, false);
VR_SetConfig(VR_CONFIG_MIRROR_YAW, false);
VR_SetConfig(VR_CONFIG_MIRROR_ROLL, false);
} else if (projMatrix[5] < 0) { //e.g. PES 2014
VR_SetConfig(VR_CONFIG_MIRROR_PITCH, true);
VR_SetConfig(VR_CONFIG_MIRROR_YAW, true);
VR_SetConfig(VR_CONFIG_MIRROR_ROLL, false);
} else { //e.g. Lego Pirates
VR_SetConfig(VR_CONFIG_MIRROR_PITCH, false);
VR_SetConfig(VR_CONFIG_MIRROR_YAW, true);
VR_SetConfig(VR_CONFIG_MIRROR_ROLL, true);
}
// Set 6DoF scale
float scale = pow(fabs(projMatrix[14]), 1.15f);
@ -378,6 +412,19 @@ void UpdateVRProjection(float* projMatrix, float* leftEye, float* rightEye) {
}
void UpdateVRView(float* leftEye, float* rightEye) {
VR_TweakView(leftEye, VR_VIEW_MATRIX_LEFT_EYE);
VR_TweakView(rightEye, VR_VIEW_MATRIX_RIGHT_EYE);
float* dst[] = {leftEye, rightEye};
VRMatrix enums[] = {VR_VIEW_MATRIX_LEFT_EYE, VR_VIEW_MATRIX_RIGHT_EYE};
for (int index = 0; index < 2; index++) {
// Get view matrix from the game
ovrMatrix4f gameView;
memcpy(gameView.M, dst[index], 16 * sizeof(float));
// Get view matrix from the headset
ovrMatrix4f hmdView = VR_GetMatrix(enums[index]);
// Combine the matrices
ovrMatrix4f renderView = ovrMatrix4f_Multiply(&hmdView, &gameView);
memcpy(dst[index], renderView.M, 16 * sizeof(float));
}
}

View File

@ -1,5 +1,5 @@
#include "VRMath.h"
#include "VRTweaks.h"
#include <iostream>
bool VR_TweakIsMatrixBigScale(float* matrix) {
for (int i = 0; i < 2; i++) {
@ -43,51 +43,3 @@ bool VR_TweakIsMatrixOneTransform(float* matrix) {
}
return true;
}
void VR_TweakMirroring(float* projMatrix) {
VR_SetConfig(VR_CONFIG_MIRROR_AXIS_X, projMatrix[0] < 0);
VR_SetConfig(VR_CONFIG_MIRROR_AXIS_Y, projMatrix[5] < 0);
VR_SetConfig(VR_CONFIG_MIRROR_AXIS_Z, projMatrix[10] > 0);
if ((projMatrix[0] < 0) && (projMatrix[10] < 0)) { //e.g. Dante's inferno
VR_SetConfig(VR_CONFIG_MIRROR_PITCH, true);
VR_SetConfig(VR_CONFIG_MIRROR_YAW, true);
VR_SetConfig(VR_CONFIG_MIRROR_ROLL, false);
} else if (projMatrix[10] < 0) { //e.g. GTA - Liberty city
VR_SetConfig(VR_CONFIG_MIRROR_PITCH, false);
VR_SetConfig(VR_CONFIG_MIRROR_YAW, false);
VR_SetConfig(VR_CONFIG_MIRROR_ROLL, false);
} else if (projMatrix[5] < 0) { //e.g. PES 2014
VR_SetConfig(VR_CONFIG_MIRROR_PITCH, true);
VR_SetConfig(VR_CONFIG_MIRROR_YAW, true);
VR_SetConfig(VR_CONFIG_MIRROR_ROLL, false);
} else { //e.g. Lego Pirates
VR_SetConfig(VR_CONFIG_MIRROR_PITCH, false);
VR_SetConfig(VR_CONFIG_MIRROR_YAW, true);
VR_SetConfig(VR_CONFIG_MIRROR_ROLL, true);
}
}
void VR_TweakProjection(float* src, float* dst, VRMatrix matrix) {
ovrMatrix4f hmdProjection = VR_GetMatrix(matrix);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if ((hmdProjection.M[i][j] > 0) != (src[i * 4 + j] > 0)) {
hmdProjection.M[i][j] *= -1.0f;
}
}
}
memcpy(dst, hmdProjection.M, 16 * sizeof(float));
}
void VR_TweakView(float* view, VRMatrix matrix) {
// Get view matrix from the game
ovrMatrix4f gameView;
memcpy(gameView.M, view, 16 * sizeof(float));
// Get view matrix from the headset
ovrMatrix4f hmdView = VR_GetMatrix(matrix);
// Combine the matrices
ovrMatrix4f renderView = ovrMatrix4f_Multiply(&hmdView, &gameView);
memcpy(view, renderView.M, 16 * sizeof(float));
}

View File

@ -1,12 +1,7 @@
#pragma once
#include "VRRenderer.h"
bool VR_TweakIsMatrixBigScale(float* matrix);
bool VR_TweakIsMatrixIdentity(float* matrix);
bool VR_TweakIsMatrixOneOrtho(float* matrix);
bool VR_TweakIsMatrixOneScale(float* matrix);
bool VR_TweakIsMatrixOneTransform(float* matrix);
void VR_TweakMirroring(float* projMatrix);
void VR_TweakProjection(float* src, float* dst, VRMatrix matrix);
void VR_TweakView(float* view, VRMatrix matrix);