Skip to content

Instantly share code, notes, and snippets.

@PathogenDavid
Last active August 29, 2015 14:03
Show Gist options
  • Save PathogenDavid/d1a1a9c7a17228c1948e to your computer and use it in GitHub Desktop.
Save PathogenDavid/d1a1a9c7a17228c1948e to your computer and use it in GitHub Desktop.
Quick and dirty LibOVR test in C#
using System;
using System.Runtime.InteropServices;
namespace Protoscratch
{
static class Oculus
{
[DllImport("LibOVRDll64.dll", EntryPoint = "ovr_Initialize", CallingConvention=CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]//LibOVR uses 1 byte booleans
public static extern bool Initialize();
[DllImport("LibOVRDll64.dll", EntryPoint = "ovr_Shutdown", CallingConvention=CallingConvention.Cdecl)]
public static extern void Shutdown();
}
enum OculusHmdType
{
None = 0,
DevKit1 = 3,
DevKitHD = 4,
CrystalCove = 5,
DevKit2 = 6,
Other
}
enum OculusEye
{
Left = 0,
Right = 1,
Count = 2
}
[StructLayout(LayoutKind.Sequential)]
struct OculusFovPort
{
float UpTan;
float DownTan;
float LeftTan;
float RightTan;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct OculusHmdDescription
{
private IntPtr Handle;
public OculusHmdType Type;
private IntPtr ProductName;
private IntPtr Manufacturer;
public UInt32 HmdCaps;
public UInt32 SensorCaps;
public UInt32 DistortionCaps;
//Resolution
public Int32 Width;
public Int32 Height;
//WindowPos
public Int32 X;
public Int32 Y;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)OculusEye.Count)]
public OculusFovPort[] DefaultEyeFov;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)OculusEye.Count)]
public OculusFovPort[] MaxEyeFov;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)OculusEye.Count)]
public OculusEye[] EyeRenderOrder;
public IntPtr DisplayDeviceName;//Only for Windows
public int DisplayId;//Only for MacOS
//Note: Letting marshalling happen automagically by defining theses as string didn't work, so I switched it to IntPtr.
public unsafe string GetManufacturer()
{
return Marshal.PtrToStringAnsi(Manufacturer);
}
public unsafe string GetProductName()
{
return Marshal.PtrToStringAnsi(ProductName);
}
};
class OculusHmd : IDisposable
{
//This returns a pointer to ovrHmdStruct, but it is an empty struct so we don't bother creating a struct for it.
[DllImport("LibOVRDll64.dll", EntryPoint = "ovrHmd_Create", CallingConvention=CallingConvention.Cdecl)]
private static extern IntPtr NativeHmdCreate(int hmd);
[DllImport("LibOVRDll64.dll", EntryPoint = "ovrHmd_CreateDebug", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr NativeHmdCreateDebug(OculusHmdType type);
[DllImport("LibOVRDll64.dll", EntryPoint = "ovrHmd_Destroy", CallingConvention=CallingConvention.Cdecl)]
private static extern void NativeHmdDestroy(IntPtr hmd);
[DllImport("LibOVRDll64.dll", EntryPoint = "ovrHmd_GetDesc", CallingConvention=CallingConvention.Cdecl)]
private static extern void NativeGetDescription(IntPtr hmd, ref OculusHmdDescription desc);
private IntPtr nativePointer;
public bool Connected
{
get { return nativePointer != IntPtr.Zero; }
}
public OculusHmd(int hmd)
{
nativePointer = NativeHmdCreate(hmd);
}
public static OculusHmd CreateDebugHmd(OculusHmdType type)
{
return new OculusHmd(NativeHmdCreateDebug(type));
}
public OculusHmdDescription GetDescription()
{
if (!Connected)
{
throw new InvalidOperationException();
}
OculusHmdDescription ret = new OculusHmdDescription();
NativeGetDescription(nativePointer, ref ret);
return ret;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (disposed) { return; }
//Free unmanaged resources:
if (nativePointer != IntPtr.Zero)
{
NativeHmdDestroy(nativePointer);
nativePointer = IntPtr.Zero;
}
disposed = true;
}
~OculusHmd()
{
Dispose(false);
}
}
}
using System;
namespace Protoscratch
{
class Program
{
static void Main(string[] args)
{
OvrTest();
Console.Write("Done.");
Console.ReadLine();
}
/// <summary>
/// This is a (slightly modified) port of the super basic example presented on page 20 of the SDK overview
/// </summary>
static void OvrTest()
{
Oculus.Initialize();
OculusHmd hmd;
//hmd = new OculusHmd(0);
hmd = OculusHmd.CreateDebugHmd(OculusHmdType.DevKit1);
OculusHmdDescription hmdDesc;
if (hmd.Connected)
{
hmdDesc = hmd.GetDescription();
Console.WriteLine("Product: ", hmdDesc.GetProductName());
Console.WriteLine("Manufacturer: ", hmdDesc.GetManufacturer());
}
else
{
Console.WriteLine("No HMD detected!");
}
hmd.Dispose();
Oculus.Shutdown();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment