Advertisement

Unity 3D and Kinect wrapper (newer official SDK)

Started by January 23, 2013 03:53 PM
0 comments, last by swiftcoder 12 years, 1 month ago

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 sad.png .

The wrapper is http://wiki.etc.cmu.edu/unity3d/index.php/Microsoft_Kinect_-_Microsoft_SDK

Thanks

[quote name='BobXIV' timestamp='1358956403' post='5024751']
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[/quote]

I've never used C#'s native interop in particular, but in general you would need to figure out both how the C++ compiler is name mangling member functions (varies from compiler to compiler), and what the calling convention is for member functions (is the 'this' pointer stored in a register or passed on the stack, where on the stack would it be passed?). At that point, if you are lucky, you can call the mangled function name as you would a C function, passing the this pointer as necessary.

It would probably be simpler/better to build a small wrapper library in C++ that exposes a set of C wrapper functions, and then call those wrapper functions from C#. The wrapper library could be distributed along with your script...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement