I'm working in a wrapper to the current version of the Kinect official SDK (http://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx) from C# inside Unity. The wrapper works making called through interop to the C API of the SDK. It is working in getting the skeletal information, but I’m having problems in retrieving the depth information of the camera. The problems is because I use the C API and there is no problem in making calls to a C function but to get the depth information I need to Lock a DirectX like Texture and because it is a class in this case I don’t know how to called the member function of Texture. The idea of the code I use is (not the actual code but the basic flow of calls) :
NativeMethods.NuiInitialize(NuiInitializeFlags.UsesDepthAndPlayerIndex |
NuiInitializeFlags.UsesSkeleton | NuiInitializeFlags.UsesDepth |
NuiInitializeFlags.UsesColor);
NativeMethods.NuiImageStreamOpen(NuiImageType.Depth,
NuiImageResolution.resolution320x240, 0, 2, IntPtr.Zero, ref
justDepthStreamHandle);
int hr = NativeMethods.NuiImageStreamGetNextFrame(justDepthStreamHandle, 100,
ref imageFramePtr);
if (hr == 0)
{
NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr,
typeof(NuiImageFrame));
// Here must be a LOCKRECT to imageFrame.pFrameTexture
//
http://msdn.microsoft.com/en-us/library/nuiimagecamera.nui_image_frame.aspx
NuiImageBuffer imageBuf =
(NuiImageBuffer)Marshal.PtrToStructure(imageFrame.pFrameTexture,
typeof(NuiImageBuffer));
DepthBuffer db =
(DepthBuffer)Marshal.PtrToStructure(imageBuf.m_pBuffer,typeof(DepthBuffer));
hr = NativeMethods.NuiImageStreamReleaseFrame(depthStreamHandle,
imageFramePtr);
}
If I do the code showed before the depth image I get is not the data instead it is just junk. My guess is that it is because the texture is not locked before accessing it but the problem is that LockRect is a member function (http://msdn.microsoft.com/en-us/library/jj663804.aspx).
The layout for the variable NuiImageBuffer imageBuf that I'm using is:
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct NuiImageFrame
{
public Int64 liTimeStamp;
public uint dwFrameNumber;
public NuiImageType eImageType;
public NuiImageResolution eResolution;
//[MarshalAsAttribute(UnmanagedType.LPStruct)]
public IntPtr pFrameTexture;
public uint dwFrameFlags_NotUsed;
public NuiImageViewArea ViewArea_NotUsed;
}
Note: the code is based in a wrapper of OpenNI for Unity. There is a Kinect wrapper also based in that OpenNI wrapper but it is from an older version of the Kinect SDK. This wrappers makes the code I show without Locking but it doesn’t work for me .
The wrapper is http://wiki.etc.cmu.edu/unity3d/index.php/Microsoft_Kinect_-_Microsoft_SDK
Thanks