Outsourced matrix math code into separate header.

This commit is contained in:
2016-12-20 20:30:42 +01:00
parent 3000e129e3
commit a93bdeb063
2 changed files with 107 additions and 24 deletions
+10 -24
View File
@@ -20,6 +20,7 @@
#include <cmath>
#include <openvr.h>
#include "logging.h"
#include "utils/Matrix.h"
@@ -403,20 +404,11 @@ void OverlayController::RotateUniverseCenter(vr::ETrackingUniverseOrigin univers
} else {
vr::VRChaperoneSetup()->GetWorkingSeatedZeroPoseToRawTrackingPose(&curPos);
}
vr::HmdMatrix34_t rotMat;
vr::HmdMatrix34_t newPos;
float yRot[3][3] = {
{ std::cos(yAngle), 0, std::sin(yAngle) },
{ 0, 1, 0 },
{ -std::sin(yAngle), 0, std::cos(yAngle) }
};
for (unsigned i = 0; i < 3; i++) {
for (unsigned j = 0; j < 3; j++) {
newPos.m[i][j] = 0.0f;
for (unsigned k = 0; k < 3; k++) {
newPos.m[i][j] += yRot[i][k] * curPos.m[k][j];
}
}
}
utils::initRotationMatrix(rotMat, 0, yAngle);
utils::matMul33(newPos, rotMat, curPos);
newPos.m[0][3] = curPos.m[0][3];
newPos.m[1][3] = curPos.m[1][3];
newPos.m[2][3] = curPos.m[2][3];
@@ -434,6 +426,7 @@ void OverlayController::RotateUniverseCenter(vr::ETrackingUniverseOrigin univers
}
}
void OverlayController::AddOffsetToCollisionBounds(unsigned axisId, float offset, bool commit) {
// Apparently Valve sanity-checks the y-coordinates of the collision bounds (and only the y-coordinates)
// I can move the bounds on the xz-plane, I can make the "ceiling" of the chaperone cage lower/higher, but when I
@@ -469,21 +462,14 @@ void OverlayController::RotateCollisionBounds(float angle, bool commit) {
if (collisionBoundsCount > 0) {
vr::HmdQuad_t* collisionBounds = new vr::HmdQuad_t[collisionBoundsCount];
vr::VRChaperoneSetup()->GetWorkingCollisionBoundsInfo(collisionBounds, &collisionBoundsCount);
float yRot[3][3] = {
{ std::cos(angle), 0, std::sin(angle) },
{ 0, 1, 0 },
{ -std::sin(angle), 0, std::cos(angle) }
};
vr::HmdMatrix34_t rotMat;
utils::initRotationMatrix(rotMat, 0, angle);
for (unsigned b = 0; b < collisionBoundsCount; b++) {
for (unsigned c = 0; c < 4; c++) {
auto& corner = collisionBounds[b].vCorners[c];
vr::HmdVector3_t newVal;
for (unsigned i = 0; i < 3; i++) {
newVal.v[i] = 0.0f;
for (unsigned k = 0; k < 3; k++) {
newVal.v[i] += corner.v[k] * yRot[i][k];
};
}
utils::matMul33(newVal, rotMat, corner);
corner = newVal;
}
}
+97
View File
@@ -0,0 +1,97 @@
#pragma once
#include <openvr.h>
#include <cmath>
namespace utils {
inline vr::HmdMatrix34_t& initRotationMatrix(vr::HmdMatrix34_t& matrix, unsigned axisId, float angle) {
switch (axisId) {
case 0: // x-axis
matrix.m[0][0] = 1.0f;
matrix.m[0][1] = 0.0f;
matrix.m[0][2] = 0.0f;
matrix.m[0][3] = 0.0f;
matrix.m[1][0] = 0.0f;
matrix.m[1][1] = std::cos(angle);
matrix.m[1][2] = -std::sin(angle);
matrix.m[1][3] = 0.0f;
matrix.m[2][0] = 0.0f;
matrix.m[2][1] = std::sin(angle);
matrix.m[2][2] = std::cos(angle);
matrix.m[2][3] = 0.0f;
break;
case 1: // y-axis
matrix.m[0][0] = std::cos(angle);
matrix.m[0][1] = 0.0f;
matrix.m[0][2] = std::sin(angle);
matrix.m[0][3] = 0.0f;
matrix.m[1][0] = 0.0f;
matrix.m[1][1] = 1.0f;
matrix.m[1][2] = 0.0f;
matrix.m[1][3] = 0.0f;
matrix.m[2][0] = -std::sin(angle);
matrix.m[2][1] = 0.0f;
matrix.m[2][2] = std::cos(angle);
matrix.m[2][3] = 0.0f;
break;
case 2: // z-axis
matrix.m[0][0] = std::cos(angle);
matrix.m[0][1] = -std::sin(angle);
matrix.m[0][2] = 0.0f;
matrix.m[0][3] = 0.0f;
matrix.m[1][0] = std::sin(angle);
matrix.m[1][1] = std::cos(angle);
matrix.m[1][2] = 0.0f;
matrix.m[1][3] = 0.0f;
matrix.m[2][0] = 0.0f;
matrix.m[2][1] = 0.0f;
matrix.m[2][2] = 1.0f;
matrix.m[2][3] = 0.0f;
break;
default:
break;
}
return matrix;
}
inline vr::HmdMatrix34_t& matMul33(vr::HmdMatrix34_t& result, const vr::HmdMatrix34_t& a, const vr::HmdMatrix34_t& b) {
for (unsigned i = 0; i < 3; i++) {
for (unsigned j = 0; j < 3; j++) {
result.m[i][j] = 0.0f;
for (unsigned k = 0; k < 3; k++) {
result.m[i][j] += a.m[i][k] * b.m[k][j];
}
}
}
return result;
}
inline vr::HmdVector3_t& matMul33(vr::HmdVector3_t& result, const vr::HmdMatrix34_t& a, const vr::HmdVector3_t& b) {
for (unsigned i = 0; i < 3; i++) {
result.v[i] = 0.0f;
for (unsigned k = 0; k < 3; k++) {
result.v[i] += a.m[i][k] * b.v[k];
};
}
return result;
}
inline vr::HmdVector3_t& matMul33(vr::HmdVector3_t& result, const vr::HmdVector3_t& a, const vr::HmdMatrix34_t& b) {
for (unsigned i = 0; i < 3; i++) {
result.v[i] = 0.0f;
for (unsigned k = 0; k < 3; k++) {
result.v[i] += a.v[k] * b.m[k][i];
};
}
return result;
}
} // end namespace utils