为什么 GetDeviceToAbsoluteTrackingPose(...) 不 return OpenVR 中的 HMD 位置?
Why GetDeviceToAbsoluteTrackingPose(...) doesn't return the HMD position in OpenVR?
我正在尝试实现一个基本的 OpenVR C++ 应用程序。这是我的代码:
#include <iostream>
#include <thread>
#include <chrono>
#include <openvr.h>
int main()
{
vr::EVRInitError eError = vr::VRInitError_None;
vr::IVRSystem * pvr = vr::VR_Init(&eError, vr::VRApplication_Utility);
std::cout << "Error code: " << eError << std::endl;
std::cout << "Is HMD present? " << vr::VR_IsHmdPresent() << std::endl;
std::cout << "Is VR runtime installed? " << vr::VR_IsRuntimeInstalled() << std::endl;
for (uint32_t deviceId = 0; deviceId < vr::k_unMaxTrackedDeviceCount; deviceId++)
{
vr::TrackedDevicePose_t trackedDevicePose;
vr::VRControllerState_t controllerState;
vr::ETrackedDeviceClass deviceClass = pvr->GetTrackedDeviceClass(deviceId);
if (!pvr->IsTrackedDeviceConnected(deviceId))
{
//std::cout << "Device ID " << deviceId << " is not connected." << std::endl;
continue;
}
switch (deviceClass)
{
case vr::ETrackedDeviceClass::TrackedDeviceClass_HMD:
std::cout << "HMD ID: " << deviceId << std::endl;
//for (int i = 0; i < 100; ++i)
{
pvr->GetDeviceToAbsoluteTrackingPose(vr::TrackingUniverseStanding, 0, &trackedDevicePose, deviceId);
//pvr->GetControllerStateWithPose(vr::TrackingUniverseStanding, deviceId, &controllerState,
// sizeof(controllerState), &trackedDevicePose);
vr::HmdMatrix34_t & m34 = trackedDevicePose.mDeviceToAbsoluteTracking;
std::cout << m34.m[0][3] << " " << m34.m[1][3] << " " << m34.m[2][3] << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
break;
default:
break;
}
}
vr::VR_Shutdown();
return 0;
}
输出是:
Error code: 0
Is HMD present? 1
Is VR runtime installed? 1
HMD ID: 0
-1.07374e+08 -1.07374e+08 -1.07374e+08
最后的输出没有改变,即使我 运行 这样做一分钟或更长时间并移动我的耳机 (HTC Vive)。
我已经尝试通过其他案例获得控制器位置:
case vr::ETrackedDeviceClass::TrackedDeviceClass_Controller:
std::cout << "Controller ID: " << deviceId << std::endl;
//for (int i = 0; i < 100; ++i)
{
pvr->GetControllerStateWithPose(vr::TrackingUniverseStanding, deviceId, &controllerState,
sizeof(controllerState), &trackedDevicePose);
vr::HmdMatrix34_t & m34 = trackedDevicePose.mDeviceToAbsoluteTracking;
std::cout << m34.m[0][3] << " " << m34.m[1][3] << " " << m34.m[2][3] << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
break;
它输出
Controller ID: 1
-1.07374e+08 -1.07374e+08 -1.07374e+08
当耳机或控制器关闭时,它们没有输出,所以有些东西在工作,但我不知道出了什么问题。
我正在使用 OpenVR SDK 1.16.8
打印出整个矩阵也给我这个奇怪的结果:
std::cout << m34.m[0][0] << " " << m34.m[1][0] << " " << m34.m[2][0] << std::endl;
std::cout << m34.m[0][1] << " " << m34.m[1][1] << " " << m34.m[2][1] << std::endl;
std::cout << m34.m[0][2] << " " << m34.m[1][2] << " " << m34.m[2][2] << std::endl;
std::cout << m34.m[0][3] << " " << m34.m[1][3] << " " << m34.m[2][3] << std::endl;
-1.07374e+08 -1.07374e+08 -1.07374e+08
-1.07374e+08 -1.07374e+08 -1.07374e+08
-1.07374e+08 -1.07374e+08 -1.07374e+08
-1.07374e+08 -1.07374e+08 -1.07374e+08
好的,我找到了解决方案。
pvr->GetDeviceToAbsoluteTrackingPose(vr::TrackingUniverseStanding, 0, &trackedDevicePose, 1);
最后一个参数应该是 1(在我的例子中)。这是一个数组计数参数,而不是设备 ID。
我正在尝试实现一个基本的 OpenVR C++ 应用程序。这是我的代码:
#include <iostream>
#include <thread>
#include <chrono>
#include <openvr.h>
int main()
{
vr::EVRInitError eError = vr::VRInitError_None;
vr::IVRSystem * pvr = vr::VR_Init(&eError, vr::VRApplication_Utility);
std::cout << "Error code: " << eError << std::endl;
std::cout << "Is HMD present? " << vr::VR_IsHmdPresent() << std::endl;
std::cout << "Is VR runtime installed? " << vr::VR_IsRuntimeInstalled() << std::endl;
for (uint32_t deviceId = 0; deviceId < vr::k_unMaxTrackedDeviceCount; deviceId++)
{
vr::TrackedDevicePose_t trackedDevicePose;
vr::VRControllerState_t controllerState;
vr::ETrackedDeviceClass deviceClass = pvr->GetTrackedDeviceClass(deviceId);
if (!pvr->IsTrackedDeviceConnected(deviceId))
{
//std::cout << "Device ID " << deviceId << " is not connected." << std::endl;
continue;
}
switch (deviceClass)
{
case vr::ETrackedDeviceClass::TrackedDeviceClass_HMD:
std::cout << "HMD ID: " << deviceId << std::endl;
//for (int i = 0; i < 100; ++i)
{
pvr->GetDeviceToAbsoluteTrackingPose(vr::TrackingUniverseStanding, 0, &trackedDevicePose, deviceId);
//pvr->GetControllerStateWithPose(vr::TrackingUniverseStanding, deviceId, &controllerState,
// sizeof(controllerState), &trackedDevicePose);
vr::HmdMatrix34_t & m34 = trackedDevicePose.mDeviceToAbsoluteTracking;
std::cout << m34.m[0][3] << " " << m34.m[1][3] << " " << m34.m[2][3] << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
break;
default:
break;
}
}
vr::VR_Shutdown();
return 0;
}
输出是:
Error code: 0
Is HMD present? 1
Is VR runtime installed? 1
HMD ID: 0
-1.07374e+08 -1.07374e+08 -1.07374e+08
最后的输出没有改变,即使我 运行 这样做一分钟或更长时间并移动我的耳机 (HTC Vive)。
我已经尝试通过其他案例获得控制器位置:
case vr::ETrackedDeviceClass::TrackedDeviceClass_Controller:
std::cout << "Controller ID: " << deviceId << std::endl;
//for (int i = 0; i < 100; ++i)
{
pvr->GetControllerStateWithPose(vr::TrackingUniverseStanding, deviceId, &controllerState,
sizeof(controllerState), &trackedDevicePose);
vr::HmdMatrix34_t & m34 = trackedDevicePose.mDeviceToAbsoluteTracking;
std::cout << m34.m[0][3] << " " << m34.m[1][3] << " " << m34.m[2][3] << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
break;
它输出
Controller ID: 1
-1.07374e+08 -1.07374e+08 -1.07374e+08
当耳机或控制器关闭时,它们没有输出,所以有些东西在工作,但我不知道出了什么问题。
我正在使用 OpenVR SDK 1.16.8
打印出整个矩阵也给我这个奇怪的结果:
std::cout << m34.m[0][0] << " " << m34.m[1][0] << " " << m34.m[2][0] << std::endl;
std::cout << m34.m[0][1] << " " << m34.m[1][1] << " " << m34.m[2][1] << std::endl;
std::cout << m34.m[0][2] << " " << m34.m[1][2] << " " << m34.m[2][2] << std::endl;
std::cout << m34.m[0][3] << " " << m34.m[1][3] << " " << m34.m[2][3] << std::endl;
-1.07374e+08 -1.07374e+08 -1.07374e+08
-1.07374e+08 -1.07374e+08 -1.07374e+08
-1.07374e+08 -1.07374e+08 -1.07374e+08
-1.07374e+08 -1.07374e+08 -1.07374e+08
好的,我找到了解决方案。
pvr->GetDeviceToAbsoluteTrackingPose(vr::TrackingUniverseStanding, 0, &trackedDevicePose, 1);
最后一个参数应该是 1(在我的例子中)。这是一个数组计数参数,而不是设备 ID。