Basic Functions: initialize, create a node and read data

The following code illustrates the basic functionality of OpenNI. It initializes a Context object, creates a single Depth node, and reads data from it.

XnStatus nRetVal = XN_STATUS_OK;
xn::Context context;
// Initialize context object
nRetVal = context.Init();
// TODO: check error code
// Create a DepthGenerator node
xn::DepthGenerator depth;
nRetVal = depth.Create(context);
// TODO: check error code
// Make it start generating data
nRetVal = context.StartGeneratingAll();
// TODO: check error code
// Main loop
while (bShouldRun)
{
// Wait for new data to be available
nRetVal = context.WaitOneUpdateAll(depth);
if (nRetVal != XN_STATUS_OK)
{
printf("Failed updating data: %s\n", xnGetStatusString(nRetVal));
continue;
}
// Take current depth map
const XnDepthPixel* pDepthMap = depth.GetDepthMap();
// TODO: process depth map
}
// Clean-up
context.Shutdown();
XnStatus
XnUInt32 XnStatus
Definition XnStatus.h:34
XN_STATUS_OK
#define XN_STATUS_OK
Definition XnStatus.h:37
xnGetStatusString
XN_C_API const XnChar *XN_C_DECL xnGetStatusString(const XnStatus Status)
XnDepthPixel
XnUInt16 XnDepthPixel
Definition XnTypes.h:276
xn::Context
Definition XnCppWrapper.h:8594
xn::Context::WaitOneUpdateAll
XnStatus WaitOneUpdateAll(ProductionNode &node)
Updates all generator nodes in the context to their latest available data, first waiting for a specif...
Definition XnCppWrapper.h:9419
xn::Context::Init
XnStatus Init()
Builds the context's general software environment.
Definition XnCppWrapper.h:8707
xn::Context::StartGeneratingAll
XnStatus StartGeneratingAll()
Ensures all created generator nodes are generating data.
Definition XnCppWrapper.h:9161
xn::DepthGenerator
Definition XnCppWrapper.h:4695
xn::DepthGenerator::GetDepthMap
const XnDepthPixel * GetDepthMap() const
Gets the current depth-map. This map is updated after a call to xnWaitAndUpdateData()....
Definition XnCppWrapper.h:4739
xn::DepthGenerator::Create
XnStatus Create(Context &context, Query *pQuery=NULL, EnumerationErrors *pErrors=NULL)
Creates a DepthGenerator node from available production node alternatives.
Definition XnCppWrapper.h:9801