1

我正在尝试使用 OpenVR 和 OpenGL 创建简单的应用程序。耳机跟踪和渲染工作正常,但我没有得到控制器的位置。此处https://github.com/ValveSoftware/openvr/wiki/API-Documentation的示例代码有效,并且正在阅读我的控制器。我正在使用从该示例复制的 json 文件。这是我的代码的简化版本:

vr::IVRSystem* vrSystem= vr::VR_Init( &eError, vr::VRApplication_Scene );
vrSystem->GetRecommendedRenderTargetSize( &vrScreenWidth, &vrScreenHeight );
initWinapi();
initOpenGL();
gladLoadGL();
createTexturesForVrRendering(vrScreenWidth,vrScreenHeight);
vrProjection[vr::Eye_Left] = ConvertSteamVRMatrix44ToMatrix4( vrSystem->GetProjectionMatrix( vr::Eye_Left , 0.001, 100.0 ) );
vrProjection[vr::Eye_Right]= ConvertSteamVRMatrix44ToMatrix4( vrSystem->GetProjectionMatrix( vr::Eye_Right, 0.001, 100.0 ) );
vrEyeTransform[vr::Eye_Left] = ConvertSteamVRMatrix34ToMatrix4( vrSystem->GetEyeToHeadTransform( vr::Eye_Left ) );
vrEyeTransform[vr::Eye_Left].invert();
vrEyeTransform[vr::Eye_Right]= ConvertSteamVRMatrix34ToMatrix4( vrSystem->GetEyeToHeadTransform( vr::Eye_Right ) );
vrEyeTransform[vr::Eye_Right].invert();
compileShaders();
vr::VRCompositor();
vr::VRActionSetHandle_t actionsetDemo = vr::k_ulInvalidActionSetHandle;
vr::VRActionHandle_t handLeftPositionAction = vr::k_ulInvalidActionHandle;
vr::VRActionHandle_t handRightPositionAction = vr::k_ulInvalidActionHandle;
vr::VRActionHandle_t m_actionHideCubes= vr::k_ulInvalidActionHandle;
vr::VRInput()->SetActionManifestPath( "C:\\src\\helloWorldVr\\openvr_actions.json" );
vr::VRInput()->GetActionHandle( "/actions/demo/in/HideCubes", &m_actionHideCubes );
vr::VRInput()->GetActionSetHandle( "/actions/demo", &actionsetDemo );
vr::VRInput()->GetActionHandle( "/actions/demo/in/Hand_Left", &handLeftPositionAction );
vr::VRInput()->GetActionHandle( "/actions/demo/in/Hand_Right", &handRightPositionAction );
while( appStillRunning ){
    static vr::TrackedDevicePose_t m_rTrackedDevicePose[vr::k_unMaxTrackedDeviceCount];
    vr::VRCompositor()->WaitGetPoses( m_rTrackedDevicePose, vr::k_unMaxTrackedDeviceCount, NULL, 0 );
    if( m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].bPoseIsValid ){
        vrHeadPosInv = Matrix4(
            m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[0][0], m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[1][0], m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[2][0], 0.0,
            m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[0][1], m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[1][1], m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[2][1], 0.0,
            m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[0][2], m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[1][2], m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[2][2], 0.0,
            m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[0][3], m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[1][3], m_rTrackedDevicePose[vr::k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking.m[2][3], 1.0f
        );
        vrHeadPosInv.invert();
    }

    while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ){
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }

    vr::VREvent_t event;
    while( vrSystem->PollNextEvent( &event, sizeof( event ) ) ){}

    vr::VRActiveActionSet_t actionSet = { 0 };
    actionSet.ulActionSet = actionsetDemo;
    vr::VRInput()->UpdateActionState( &actionSet, sizeof( actionSet ), 1 );

    vr::InputPoseActionData_t poseData;
    vr::EVRInputError res = vr::VRInput()->GetPoseActionDataForNextFrame( handLeftPositionAction, vr::TrackingUniverseStanding, &poseData, sizeof( poseData ), vr::k_ulInvalidInputValueHandle );
    if( res == vr::VRInputError_None ){
        if( poseData.bActive && poseData.pose.bPoseIsValid ){ /// poseData.bActive is always false
            vrHandPos[vr::Eye_Left]= vrHmdMatrix34_t2Matrix4( poseData.pose.mDeviceToAbsoluteTracking );
        }
    }
    res = vr::VRInput()->GetPoseActionDataForNextFrame( handRightPositionAction, vr::TrackingUniverseStanding, &poseData, sizeof( poseData ), vr::k_ulInvalidInputValueHandle );
    if( res == vr::VRInputError_None ){
        if( poseData.bActive && poseData.pose.bPoseIsValid ){ /// poseData.bActive is always false
            vrHandPos[vr::Eye_Right] = vrHmdMatrix34_t2Matrix4( poseData.pose.mDeviceToAbsoluteTracking );
        }
    }

    renderScene();
    vr::Texture_t leftEyeTexture = { (void*)(uintptr_t)vrScreenTexture[0], vr::TextureType_OpenGL, vr::ColorSpace_Gamma };
    vr::VRCompositor()->Submit( vr::Eye_Left, &leftEyeTexture );
    vr::Texture_t rightEyeTexture = { (void*)(uintptr_t)vrScreenTexture[1], vr::TextureType_OpenGL, vr::ColorSpace_Gamma };
    vr::VRCompositor()->Submit( vr::Eye_Right, &rightEyeTexture );
    glFinish();
    SwapBuffers( hdc );
    glClearColor( 0, 0, 0, 1 );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glFlush();
    glFinish();
}
vr::VR_Shutdown();
shutDownOpenGL();

我错过了什么吗?

4

0 回答 0