简体   繁体   中英

OpenVR - IVRSystem::GetControllerState always returns empty structs

I've been following Kamran Bigdely-Shamloo's article on how to get positional information from the HTC Vive and it has worked well so far. My next step was to "listen" to button presses. I've read the documentation and it says here that all I need to do is query IVRSystem::GetControllerState and it'll return a

"struct with the current state of the controller"

This struct however always contains variables that have the 0 value. The following function is called in a while (true) loop from the main function.

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 is initalized as follows:

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

I must be doing something wrong because in the following snippet, the if statement is passed only for the first four iterations, which should be a controller, the vive tracker, the headset and the lighthouses. This tells me that I am able to access these states, but I am somehow not able to read the information.

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?");
            }
    }

I can't imagine its a bug, so my guess is that my configuration is faulty, or I'm doing the wrong query. Any help is greatly appreciated!

Apparently I was making two mistakes. Mistake #1 was that I was using the wrong sample file. I used hellovr_opengl from the OpenVr sample folder, but instead hellovr_dx12 was the working solution. It must have a different kind of configration as well, since I copied a function which was working into the hellovr_opengl project and there, it did not work! Then I copied the functions I added to the hellovr_dx12 project and was able to get the controller states of the Vive controller.

However, I wanted to get the states of the Vive Tracker, which didn't work. After some googling I found out about an issue with the current SteamVR driver , so I reverted it back to a beta hoftix, which solved the Vive Tracker issue for me.

You have to call;
vr::VRInput()->SetActionManifestPath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM