簡體   English   中英

OpenVR-IVRSystem :: GetControllerState始終返回空結構

[英]OpenVR - IVRSystem::GetControllerState always returns empty structs

我一直在關注Kamran Bigdely-Shamloo關於如何從HTC Vive獲取位置信息的文章 ,並且到目前為止效果很好。 我的下一步是“監聽”按鈕按下。 我已經閱讀了文檔,並且在這里說,我需要做的就是查詢IVRSystem::GetControllerState ,它將返回一個

“使用控制器的當前狀態構造”

但是,此結構始終包含值為0的變量。 main函數的while (true)循環中調用以下函數。

bool CMainApplication::HandleInput()
{
SDL_Event sdlEvent;
bool bRet = false;

while ( SDL_PollEvent( &sdlEvent ) != 0 )
{
    if ( sdlEvent.type == SDL_QUIT )
    {
        bRet = true;
    }
    else if ( sdlEvent.type == SDL_KEYDOWN )
    {
        if ( sdlEvent.key.keysym.sym == SDLK_ESCAPE 
             || sdlEvent.key.keysym.sym == SDLK_q )
        {
            bRet = true;
        }
        if( sdlEvent.key.keysym.sym == SDLK_c )
        {
            m_bShowCubes = !m_bShowCubes;
        }
    }
}

// Process SteamVR events
// Periodically returns an event of type 404 ("VREvent_SceneApplicationChanged      = 404, // data is process - The App actually drawing the scene changed (usually to or from the compositor)"
vr::VREvent_t event;
vr::VREvent_Controller_t controllerEvent;
std::chrono::milliseconds ms4;
while( m_pHMD->PollNextEvent( &event, sizeof( event ) ) )
{
    ms4 = std::chrono::duration_cast<std::chrono::milliseconds>(
        std::chrono::system_clock::now().time_since_epoch()
        );
    ProcessVREvent( &event);
    printPositionalData(&event, ms4);
}

vr::VRControllerState_t state;

// Check every device attached, usually it's four devices
// Second if statement is never reached
for (int i = 0; i < 1000; i++) {
    if (m_pHMD->GetControllerState(i, &state, sizeof(state))) {
        dprintf("%d", i);
            if (state.ulButtonPressed != 0 || state.unPacketNum != 0 || state.ulButtonTouched != 0) {
                dprintf("Some action?");
            }
    }
}
dprintf("\n");

// Process SteamVR action state
// UpdateActionState is called each frame to update the state of the actions themselves. The application
// controls which action sets are active with the provided array of VRActiveActionSet_t structs.
vr::VRActiveActionSet_t actionSet = { 0 };
actionSet.ulActionSet = m_actionsetDemo;
vr::VRInput()->UpdateActionState( &actionSet, sizeof(actionSet), 1 );

m_bShowCubes = !GetDigitalActionState( m_actionHideCubes );

vr::VRInputValueHandle_t ulHapticDevice;
if ( GetDigitalActionRisingEdge( m_actionTriggerHaptic, &ulHapticDevice ) )
{
    if ( ulHapticDevice == m_rHand[Left].m_source )
    {
        vr::VRInput()->TriggerHapticVibrationAction( m_rHand[Left].m_actionHaptic, 0, 1, 4.f, 1.0f, vr::k_ulInvalidInputValueHandle );
    }
    if ( ulHapticDevice == m_rHand[Right].m_source )
    {
        vr::VRInput()->TriggerHapticVibrationAction( m_rHand[Right].m_actionHaptic, 0, 1, 4.f, 1.0f, vr::k_ulInvalidInputValueHandle );
    }
}

vr::InputAnalogActionData_t analogData;
if ( vr::VRInput()->GetAnalogActionData( m_actionAnalongInput, &analogData, sizeof( analogData ), vr::k_ulInvalidInputValueHandle ) == vr::VRInputError_None && analogData.bActive )
{
    m_vAnalogValue[0] = analogData.x;
    m_vAnalogValue[1] = analogData.y;
}

m_rHand[Left].m_bShowController = true;
m_rHand[Right].m_bShowController = true;

vr::VRInputValueHandle_t ulHideDevice;
if ( GetDigitalActionState( m_actionHideThisController, &ulHideDevice ) )
{
    if ( ulHideDevice == m_rHand[Left].m_source )
    {
        m_rHand[Left].m_bShowController = false;
    }
    if ( ulHideDevice == m_rHand[Right].m_source )
    {
        m_rHand[Right].m_bShowController = false;
    }
}

for ( EHand eHand = Left; eHand <= Right; ((int&)eHand)++ )
{
    vr::InputPoseActionData_t poseData;
    if ( vr::VRInput()->GetPoseActionData( m_rHand[eHand].m_actionPose, vr::TrackingUniverseStanding, 0, &poseData, sizeof( poseData ), vr::k_ulInvalidInputValueHandle ) != vr::VRInputError_None
        || !poseData.bActive || !poseData.pose.bPoseIsValid )
    {
        m_rHand[eHand].m_bShowController = false;
    }
    else
    {
        m_rHand[eHand].m_rmat4Pose = ConvertSteamVRMatrixToMatrix4( poseData.pose.mDeviceToAbsoluteTracking );

        vr::InputOriginInfo_t originInfo;
        if ( vr::VRInput()->GetOriginTrackedDeviceInfo( poseData.activeOrigin, &originInfo, sizeof( originInfo ) ) == vr::VRInputError_None 
            && originInfo.trackedDeviceIndex != vr::k_unTrackedDeviceIndexInvalid )
        {
            std::string sRenderModelName = GetTrackedDeviceString( originInfo.trackedDeviceIndex, vr::Prop_RenderModelName_String );
            if ( sRenderModelName != m_rHand[eHand].m_sRenderModelName )
            {
                m_rHand[eHand].m_pRenderModel = FindOrLoadRenderModel( sRenderModelName.c_str() );
                m_rHand[eHand].m_sRenderModelName = sRenderModelName;
            }
        }
    }
}

return bRet;

m_pHMD的初始化如下:

vr::IVRSystem *m_pHMD;
....
m_pHMD = vr::VR_Init( &eError, vr::VRApplication_Background );

我一定在做錯事,因為在以下代碼段中,僅在前四個迭代中傳遞了if語句,這應該是控制器,虛擬跟蹤器,頭戴式耳機和燈塔。 這告訴我可以訪問這些狀態,但是無法以某種方式讀取信息。

for (int i = 0; i < 1000; i++) {
    if (m_pHMD->GetControllerState(i, &state, sizeof(state))) {
        dprintf("%d", i);
            if (state.ulButtonPressed != 0 || state.unPacketNum != 0 || state.ulButtonTouched != 0) {
                dprintf("Some action?");
            }
    }

我無法想象它是一個錯誤,所以我猜是我的配置有問題,或者我做錯了查詢。 任何幫助是極大的贊賞!

顯然我犯了兩個錯誤。 錯誤#1是我使用了錯誤的示例文件。 我使用了OpenVr示例文件夾中的hellovr_opengl,但是hellovr_dx12是有效的解決方案。 它也必須具有不同類型的配置,因為我將一個正在工作的函數復制到了hellovr_opengl項目中,所以它不起作用! 然后,我復制了添加到hellovr_dx12項目中的函數,並能夠獲取Vive控制器的控制器狀態。

但是,我想獲取Vive Tracker的狀態,該狀態無效。 經過一番谷歌搜索后,我發現當前SteamVR驅動程序存在問題 ,因此我將其還原為beta hoftix,它為我解決了Vive Tracker問題。

你必須打電話;
vr :: VRInput()-> SetActionManifestPath

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM