/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal
{
using System;
using UnityEngine;
using System.Runtime.InteropServices;
/// HMD Eye offset Native API .
public partial class NativeHMD
{
/// Handle of the hmd.
private UInt64 m_HmdHandle;
/// Gets the handle of the hmd.
/// The hmd handle.
public UInt64 HmdHandle
{
get
{
return m_HmdHandle;
}
}
/// Creates a new bool.
/// True if it succeeds, false if it fails.
public bool Create()
{
NativeResult result = NativeApi.NRHMDCreate(ref m_HmdHandle);
NativeErrorListener.Check(result, this, "Create");
return result == NativeResult.Success;
}
/// Pauses this object.
/// True if it succeeds, false if it fails.
public bool Pause()
{
NativeResult result = NativeApi.NRHMDPause(m_HmdHandle);
NativeErrorListener.Check(result, this, "Pause");
return result == NativeResult.Success;
}
/// Resumes this object.
/// True if it succeeds, false if it fails.
public bool Resume()
{
NativeResult result = NativeApi.NRHMDResume(m_HmdHandle);
NativeErrorListener.Check(result, this, "Resume");
return result == NativeResult.Success;
}
/// Gets device pose from head.
/// The device type.
/// The device pose from head.
public Pose GetDevicePoseFromHead(NativeDevice device)
{
return GetDevicePoseFromHead((int)device);
}
/// Gets device pose from head.
/// The device type.
/// The device pose from head.
public Pose GetDevicePoseFromHead(int device)
{
Pose outDevicePoseFromHead = Pose.identity;
NativeMat4f mat4f = new NativeMat4f(Matrix4x4.identity);
NativeResult result = NativeApi.NRHMDGetEyePoseFromHead(m_HmdHandle, device, ref mat4f);
if (result == NativeResult.Success)
{
ConversionUtility.ApiPoseToUnityPose(mat4f, out outDevicePoseFromHead);
}
return outDevicePoseFromHead;
}
/// Gets projection matrix.
/// [in,out] The out eyes projection matrix.
/// The znear.
/// The zfar.
/// True if it succeeds, false if it fails.
public bool GetProjectionMatrix(ref EyeProjectMatrixData outEyesProjectionMatrix, float znear, float zfar)
{
NativeFov4f fov = new NativeFov4f();
NativeResult result_left = NativeApi.NRHMDGetEyeFovInCoord(m_HmdHandle, (int)NativeDevice.LEFT_DISPLAY, ref fov);
NativeErrorListener.Check(result_left, this, "GetProjectionMatrix-L");
NRDebugger.Info("[GetProjectionMatrix] LEFT_DISPLAY: {0}", fov.ToString());
outEyesProjectionMatrix.LEyeMatrix = ConversionUtility.GetProjectionMatrixFromFov(fov, znear, zfar).ToUnityMat4f();
NativeResult result_right = NativeApi.NRHMDGetEyeFovInCoord(m_HmdHandle, (int)NativeDevice.RIGHT_DISPLAY, ref fov);
NativeErrorListener.Check(result_right, this, "GetProjectionMatrix-R");
NRDebugger.Info("[GetProjectionMatrix] RIGHT_DISPLAY: {0}", fov.ToString());
outEyesProjectionMatrix.REyeMatrix = ConversionUtility.GetProjectionMatrixFromFov(fov, znear, zfar).ToUnityMat4f();
NativeResult result_RGB = NativeApi.NRHMDGetEyeFovInCoord(m_HmdHandle, (int)NativeDevice.RGB_CAMERA, ref fov);
NativeErrorListener.Check(result_RGB, this, "GetProjectionMatrix-RGB");
outEyesProjectionMatrix.RGBEyeMatrix = ConversionUtility.GetProjectionMatrixFromFov(fov, znear, zfar).ToUnityMat4f();
return (result_left == NativeResult.Success && result_right == NativeResult.Success && result_RGB == NativeResult.Success);
}
[Obsolete("Use 'GetEyeFovInCoord' to replace.")]
public NativeFov4f GetEyeFov(NativeEye eye)
{
NativeFov4f fov = new NativeFov4f();
NativeApi.NRHMDGetEyeFov(m_HmdHandle, (int)eye, ref fov);
return fov;
}
public NativeFov4f GetEyeFovInCoord(NativeDevice eye)
{
NativeFov4f fov = new NativeFov4f();
NativeApi.NRHMDGetEyeFovInCoord(m_HmdHandle, (int)eye, ref fov);
return fov;
}
/// Gets camera intrinsic matrix.
/// The eye.
/// [in,out] The camera intrinsic matix.
/// True if it succeeds, false if it fails.
public bool GetCameraIntrinsicMatrix(int eye, ref NativeMat3f CameraIntrinsicMatix)
{
var result = NativeApi.NRHMDGetCameraIntrinsicMatrix(m_HmdHandle, (int)eye, ref CameraIntrinsicMatix);
return result == NativeResult.Success;
}
/// Gets camera distortion.
/// The eye.
/// A variable-length parameters list containing distortion.
/// True if it succeeds, false if it fails.
public bool GetCameraDistortion(int eye, ref NRDistortionParams distortion)
{
var result = NativeApi.NRHMDGetCameraDistortionParams(m_HmdHandle, eye, ref distortion);
return result == NativeResult.Success;
}
/// Gets eye resolution.
/// The eye.
/// The eye resolution.
public NativeResolution GetEyeResolution(int eye)
{
NativeResolution resolution = new NativeResolution(1920, 1080);
#if UNITY_EDITOR
return resolution;
#else
var result = NativeApi.NRHMDGetEyeResolution(m_HmdHandle, eye, ref resolution);
NativeErrorListener.Check(result, this, "GetEyeResolution");
return resolution;
#endif
}
/// Gets device type of running device.
/// The device type.
public NRDeviceType GetDeviceType()
{
NRDeviceType deviceType = NRDeviceType.NrealLight;
NativeApi.NRHMDGetDeviceType(m_HmdHandle, ref deviceType);
return deviceType;
}
/// Gets device type of running device.
/// The request feature.
/// Is the feature supported.
public bool IsFeatureSupported(NRSupportedFeature feature)
{
bool result = false;
NativeApi.NRHMDIsFeatureSupported(m_HmdHandle, feature, ref result);
return result;
}
/// Destroys this object.
/// True if it succeeds, false if it fails.
public bool Destroy()
{
NativeResult result = NativeApi.NRHMDDestroy(m_HmdHandle);
NativeErrorListener.Check(result, this, "Destroy");
return result == NativeResult.Success;
}
/// A native api.
private struct NativeApi
{
/// Nrhmd create.
/// [in,out] Handle of the out hmd.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDCreate(ref UInt64 out_hmd_handle);
/// Nrhmd get device type.
/// Handle of the hmd.
/// [in,out] The out device type.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDGetDeviceType(UInt64 hmd_handle, ref NRDeviceType out_device_type);
///
/// Check whether the current feature is supported.
///
/// Handle of the out hmd.
/// Current feature.
/// Result of whether the current feature is supported.
///
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDIsFeatureSupported(UInt64 hmd_handle, NRSupportedFeature feature, ref bool out_is_supported);
/// Nrhmd pause.
/// Handle of the hmd.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDPause(UInt64 hmd_handle);
/// Nrhmd resume.
/// Handle of the hmd.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDResume(UInt64 hmd_handle);
/// Nrhmd get eye pose from head.
/// Handle of the hmd.
/// The eye.
/// [in,out] The out eye pose from head.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDGetEyePoseFromHead(UInt64 hmd_handle, int eye, ref NativeMat4f outEyePoseFromHead);
/// Nrhmd get eye fov.
/// Handle of the hmd.
/// The eye.
/// [in,out] The out eye fov.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
[Obsolete]
public static extern NativeResult NRHMDGetEyeFov(UInt64 hmd_handle, int eye, ref NativeFov4f out_eye_fov);
/// Nrhmd get eye fov.
/// Handle of the hmd.
/// The eye.
/// [in,out] The out eye fov.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDGetEyeFovInCoord(UInt64 hmd_handle, int eye, ref NativeFov4f out_eye_fov);
/// Nrhmd get camera intrinsic matrix.
/// Handle of the hmd.
/// The eye.
/// [in,out] The out intrinsic matrix.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDGetCameraIntrinsicMatrix(
UInt64 hmd_handle, int eye, ref NativeMat3f out_intrinsic_matrix);
/// Nrhmd get camera distortion parameters.
/// Handle of the hmd.
/// The eye.
/// A variable-length parameters list containing out parameters.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDGetCameraDistortionParams(
UInt64 hmd_handle, int eye, ref NRDistortionParams out_params);
/// Nrhmd get eye resolution.
/// Handle of the hmd.
/// The eye.
/// [in,out] The out eye resolution.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDGetEyeResolution(UInt64 hmd_handle, int eye, ref NativeResolution out_eye_resolution);
/// Nrhmd destroy.
/// Handle of the hmd.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRHMDDestroy(UInt64 hmd_handle);
};
}
}