LoginSignup
2
1

More than 5 years have passed since last update.

Visual StudioでOculusがやりたい(角度取得編)

Posted at

そもそもなんでUnityでやらんといけんのじゃ!!
...というよくわからない激怒からVisual StudioでOculusを始めました.

レンダリングは時間がかかるので今回はVisual Studioの簡単な環境構築+角度取得コードを
(Oculusの導入方法はいろんなところにあるので割愛します)

Visual Studioの環境構築

今回私はVisual Studio2013を使用.
Oculus SDKのSamplesの中からslnを起動して見ながらやるといいかも.

以下Visual Studioの設定

追加のインクルードディレクトリ

 $(OVRSDKROOT)\LibOVR\Include
 $(OVRSDKROOT)\LibOVRKernel\Src

$(OVRSDKROOT)はOCULUSをインクルードしたところですがおそらく環境変数として登録されてるのではないかと.

追加のライブラリディレクトリ

$(OVRSDKROOT)\LibOVR\Lib\Windows\Win32\Debug\VS2013
$(OVRSDKROOT)\LibOVRKernel\Lib\Windows\Win32\Debug\VS2013

VS2013のところは環境によって異なります.

追加の入力ファイル

$(OVRSDKROOT)\LibOVRKernel\Lib\Windows\Win32\Debug\VS2013\LibOVRKernel.lib
$(OVRSDKROOT)\LibOVR\Lib\Windows\$(Platform)\$(Configuration)\VS2013\LibOVR.lib

ランタイムライブラリ
自分は/MTdにしています. 設定次第では動かないかも.

プログラム

今回Oculusの姿勢を出力するコンソールプログラムを作りました.
簡単なものなのでここに載せます.

HeadTrack.cpp
#include <stdio.h>
#include <tchar.h>



// TODO: プログラムに必要な追加ヘッダーをここで参照してください。
#include <Windows.h>
#include <opencv.hpp>
#include <highgui.hpp>
#include <iostream>
#include <fstream>
#include <string>

//oculus使用
#include <OVR_CAPI_0_8_0.h>
#include <Kernel/OVR_System.h>
#include "OVR_CAPI.h"
#include "OVR_CAPI_GL.h"

using namespace std;
using namespace OVR;


void Application()
{
    ovrResult result = ovr_Initialize(nullptr);

    if (OVR_FAILURE(result)){
        cout << "ovr_Initialize FAILURE" << endl;
        return;
    }
        //ovrHmd    hmd = nullptr;
        ovrSession session;
        ovrGraphicsLuid luid;
        result = ovr_Create(&session, &luid);

        if (result == ovrSuccess)
        {

            // Get more details about the HMD.
            ovrHmdDesc desc = ovr_GetHmdDesc(session);
            ovrSizei resolution = desc.Resolution;
            //get oculus tracking
            ovr_ConfigureTracking(session, ovrTrackingCap_Orientation |
                ovrTrackingCap_MagYawCorrection |
                ovrTrackingCap_Position, 0);
            while (1){

                // Do something with the HMD.
                // Query the HMD for the current tracking state.
                ovrTrackingState ts = ovr_GetTrackingState(session, ovr_GetTimeInSeconds(),NULL);
                if (ts.StatusFlags & (ovrStatus_OrientationTracked | ovrStatus_PositionTracked))
                {
                    ovrPoseStatef pose = ts.HeadPose;

                    cout << "X=  " << pose.ThePose.Orientation.x << endl;
                    cout << "y=  " << pose.ThePose.Orientation.y << endl;
                    cout << "z=  " << pose.ThePose.Orientation.z << endl;


                    //ファイルに書き込み
                    std::ofstream ofs("C:\\works\\KinectInteraction\\Debug\\HeadPosition.txt");
                    ofs << pose.ThePose.Orientation.y << endl;
                    ofs << pose.ThePose.Orientation.x << endl;
                    ofs << pose.ThePose.Orientation.z << endl;
                    Sleep(100);
                }

                if (cv::waitKey(10) != -1)
                    break;

            }

            ovr_Destroy(session);
        }

        ovr_Shutdown(); 
}

int _tmain(int argc, _TCHAR* argv[])
{

    Application();

    return 0;
}

OpenCV入ってるけど適宜変えてください 笑

Help

たまにロール・ピッチ・ヨー角が反転してもとに戻りません.
サンプルプログラムの場合はすぐに戻るのに...
だれかわかる人がいたら教えてください!

参考

公式サイトのDeveloper Guide
LibOVR Reference Manual

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1