NatNet: Creating a Native (C++) Client Application

This guide covers essential points to developing a native client application using the NatNet SDK. The guideline uses sample codes in the SampleClient.cpp application in the \NatNet SDK\Sample folder, please refer to this project as an additional reference.

triangle-exclamation

SDK/API Support Disclaimer

1. Import Library


a. Link the Library

When developing a native NatNet client application, NatNetLib.dll file needs to be linked to the project and placed alongside its executable in order to utilize the library classes and functions. Make sure the project is linked to DLL files with matching architecture (32-bit/64-bit).

b. Include the Header Files

After linking the library, include the header files within your application and import required library declarations. The header files are located in the NatNet SDK/include folder.

#include "NatNetTypes.h"
#include "NatNetClient.h"

2. Connect


a. Create a Client Object

Connection to a NatNet server application is accomplished through an instance of NatNetClient object. The client object is instantiated by calling the NatNetClient constructor with desired connection protocol (Multicast/Unicast) as its argument. Designate a desired connection protocol and instantiate the client object. In the SampleClient example, this step is done within the CreateClient function.

  • ConnectionType_Multicast = 0

  • ConnectionType_Unicast = 1

[C++] SampleClient.cpp : Instantiating NatNetClient


b. Connect to Server

Now that you have instantiated a NatNetClient object, connect the client to the server application at the designated IP address by calling the NatNetClient::Initialize method. The Connect method requires two input arguments: one for the local IP address that the client is running on and one for the server IP address that the data is streamed to. It is important that the client connects to appropriate IP addresses; otherwise, the data will not be received.Once the connection is established, you can use methods within the NatNetClient object to send commands and query data.

[C++] SampleClient.cpp : Connect to the Server


c. Confirm Connection

Now that the NatNetClient object is connected, let’s confirm the connection by querying the server for its descriptions. This can be obtained by calling the NatNetClient::GetServerDescription method and the information gets saved in the provided instance of sServerDescriptions. This is also demonstrated in the CreateClient function of the SampleClient project.

[C++] SampleClient.cpp : Request Server Description


You can also confirm connection by sending a NatNet remote command to the server. NatNet commands are sent by calling the NatNetClient::SendMessageAndWait method with supported NatNet Command as one of its input arguments. The following sample sends a command for querying the number of analog sample for each of the mocap frames. If the client is successfully connected to the server, this method will save the data and return 0.

[C++] SampleClient.cpp : Send NatNet Commands


3. Get DataDescriptions


a. Fetching Data Description

Now that the client application is connected, data descriptions for the streamed capture session can be obtained from the server. This can be done by calling the NatNetClient::GetDataDescriptions method and saving the descriptions list into an instance of sDataDescriptions. From this instance, the client application can figure out how many assets are in the scene as well as their descriptions.This is done by the following line in the SampleClient project:

[C++] SampleClient.cpp : Get Data Descriptions


b. Parsing Data Description

After an sDataDescriptions instance has been saved, data descriptions for each of the assets (marker, rigid body, skeleton, and force plate from the server) can be accessed from it.

[C++] SampleClient.cpp : Parsing Data Descriptions


4. Get FrameOfMocapData


a. Set Callback Functions

Now that we have data descriptions, let's fetch the corresponding frame-specific data. To do this, a callback handler function needs to be set for processing the incoming frames. First, create a callback function that has the correct prototype (input arguments and return types) as declared in the NatNetClient::SetDataCallback method:void (*CallbackFunction)(sFrameOfMocapData* pFrameOfData, void* pUserData)The SampleClient.cpp project sets DataHandler function as the frame handler function.void __cdecl DataHandler(sFrameOfMocapData* data, void* pUserData)

The NatNetClient::SetDataCallback method creates a new thread and assigns the frame handler function. Call this method with the created function and the NatNetClient object as its arguments. In the SampleClient application, this is called within the CreateClient function:

b. Parsing/Handling Frame Data Handling

Once you call the SetDataCallback method to link a data handler callback function, this function will receive a packet of sFrameOfMocapData each time a frame is received. The sFrameOfMocapData contains a single frame data for all of the streamed assets. This allows prompt processing of the capture frames within the handler function.

[C++] SampleClient.cpp : Frame Data Callback Handler


5. Disconnect


When exiting the program, call Uninitialize method using the connected client object and disconnect the client application from the server.

Category:

  • NatNet SDK

Last updated

Was this helpful?