Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Can't get gauze (C++), been at it for hours..

mlfarrellmlfarrell
edited October 2016 in Questions And Answers

Please tell me what I'm doing wrong here.. .I'm fried from trying to get this to work..

I can get the vector just fine when I air tap, but in between I can't get any data at all. How the heck do I get a valid SpatialInteractionSourceState when I'm just moving my head around???

PS, the code editor system on this forum is impossible to use...

#include <functional>
#include <algorithm>
#include <iterator>
#include <wrl.h>
#include <windows.perception.spatial.h>
#include "SpatialInputHandler.h"
#include "EventDispatcher.h"

using namespace std;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Input::Spatial;
using namespace Windows::Perception::Spatial;
using namespace Microsoft::WRL;
using namespace std::placeholders;

//ANGLE hack
__declspec(dllimport) ComPtr<ABI::Windows::Perception::IPerceptionTimestamp> AngleHolographicGetCurrentPredictionTimestamp();

namespace vui
{
  namespace holo
  {
    SpatialInputHandler::SpatialInputHandler() 
    {
      eventDispatcher = &(EventDispatcher::dispatcher());

      interactionManager = SpatialInteractionManager::GetForCurrentView();
      locator = SpatialLocator::GetDefault();
      referenceFrame = locator->CreateStationaryFrameOfReferenceAtCurrentLocation();

      sourcePressedEventToken =
        interactionManager->SourcePressed +=
        ref new TypedEventHandler<SpatialInteractionManager^, SpatialInteractionSourceEventArgs^>(
          bind(&SpatialInputHandler::onSourcePressed, this, _1, _2)
          );

      sourceChangedEventToken = interactionManager->SourceUpdated +=
        ref new TypedEventHandler<SpatialInteractionManager ^, SpatialInteractionSourceEventArgs ^>(
          bind(&SpatialInputHandler::onSourceChanged, this, _1, _2)
          );
    }

    SpatialInputHandler::~SpatialInputHandler()
    {
      interactionManager->SourcePressed -= sourcePressedEventToken;
      interactionManager->SourceUpdated -= sourceChangedEventToken;
    }

    static float3 convFloat3(Windows::Foundation::Numerics::float3 wf3)
    {
      return { wf3.x, wf3.y, wf3.z };
    }

    SpatialInputHandler::InputState SpatialInputHandler::poll()
    {
      auto pollResult = doPoll();

      if(pollResult)
      {
        if(pollResult.pressed)
        {
          eventDispatcher->rayMoved({ pollResult.headPosition, pollResult.headForwardVector });
          //eventDispatcher->raySelected({ pollResult.headPosition, pollResult.headForwardVector });
        }
        else
        {
          eventDispatcher->rayMoved({ pollResult.headPosition, pollResult.headForwardVector });
        }
      }

      return pollResult;
    }

    SpatialInputHandler::InputState SpatialInputHandler::doPoll()
    {
      auto ss = checkForInput();
      auto currentCoordinateSystem = referenceFrame->CoordinateSystem;

      if(ss)
      {
        auto spp = ss->TryGetPointerPose(currentCoordinateSystem);
        bool pressed = pressedState;

        pressedState = false;
        return { true, convFloat3(spp->Head->Position), convFloat3(spp->Head->ForwardDirection), convFloat3(spp->Head->UpDirection), pressed };
      }
      else
      {
        ComPtr<ABI::Windows::Perception::IPerceptionTimestamp> timestamp = AngleHolographicGetCurrentPredictionTimestamp();
        if(timestamp)
        {
          Windows::Perception::PerceptionTimestamp ^ts = reinterpret_cast<Windows::Perception::PerceptionTimestamp ^>(timestamp.Get());

          auto sources = interactionManager->GetDetectedSourcesAtTimestamp(ts);
          for(int i = 0; i < sources->Size; i++)
          {
            auto ss = sources->GetAt(i);
            auto spp = ss->TryGetPointerPose(currentCoordinateSystem);

            if(spp)
              vout << convFloat3(spp->Head->ForwardDirection) << endl;
          }
        }
      }

      return { false, float3::zero, float3::zero, float3::zero, false };
    }

    Windows::UI::Input::Spatial::SpatialInteractionSourceState ^ SpatialInputHandler::checkForInput()
    {
      SpatialInteractionSourceState ^ss = sourceState;

      sourceState = nullptr;
      return ss;
    }

    void SpatialInputHandler::onSourcePressed(Windows::UI::Input::Spatial::SpatialInteractionManager ^sender, Windows::UI::Input::Spatial::SpatialInteractionSourceEventArgs ^args)
    {
      sourceState = args->State;
      pressedState = true;
    }

    void SpatialInputHandler::onSourceChanged(Windows::UI::Input::Spatial::SpatialInteractionManager ^sender, Windows::UI::Input::Spatial::SpatialInteractionSourceEventArgs ^args)
    {
      sourceState = args->State;
    }
  }
}
Sign In or Register to comment.