Tag Archives: Software Development

Items related to software development

“Hello World!” for Oculus Rift Development Kit 2 (DK2)

Update: This blog post discusses building a simple C++ app for the Oculus v0.5.0 beta SDK. An updated example which shows how to build a simple C++ console app for the production Oculus Rift CV1/DK2 v1.3.2 SDK can be found here.

This post walks through the basics of building a simple C++ app for the Oculus Rift Development Kit 2 (DK2).  Setup of the development environment is covered first, with the coding of a simple app which reads the device’s sensors and outputs their state following after.

We’re working with the Oculus Rift 0.5.0 SDK for this example and a DK2 device. Everything posted in here is subject to change as the Oculus API matures (that’s no fault of Oculus — we were all told we’re getting a work-in-progress developer kit, and that’s what’s been delivered), so please do let me know if these notes no longer reflect reality for a developer new to the device.

Oculus DK2 Dev Environment Setup

If you’ve not already installed the Oculus Rift SDK, you’ll want to do so now. It is also assumed for Oculus development that the DirectX SDK and Windows SDK are already installed on your computer. We won’t need those SDKs for the initial ‘Hello World!’ completed here but will definitely need them later, so best to fetch those before starting to code.

The DirectX SDK can be retrieved from here:

http://www.microsoft.com/en-us/download/details.aspx?id=6812

And the Windows 8.x SDK (which works fine for Windows 7) from here:

https://msdn.microsoft.com/en-us/windows/desktop/bg162891.aspx

It turns out that the DirectX SDK installer can conflict with the Microsoft Visual C++ 2010 Redistributable, resulting in a cryptic “Error Code: S1023” installation failure. If that happens to you (as it did to me), this StackOverflow post will sort you out.

Microsoft Visual Studio (VS2013) is used as the development environment for the examples shown here. To test that environment after all SDKs are installed (and give us a VS2013 project into which we’ll put our example code) we’ll start out with a traditional ‘Hello World!’ program.

Using the VS2013 File > New > Project… wizard to create a new console app (being sure to turn off the “Precompiled header” checkbox option), we’ll pop into the editor:

and then use the VS2013 Build > Build Solution menu option to create an executable.

With “Hello World!” output to the console when running that exe, we’ve confirmed our development environment is good to go.  Onwards to the DK2!

The Oculus developer documentation is a bit vague on the libraries needed for the basics, and the code examples included with the SDK cover a good amount of DirectX and other extra bits we’re not needing just yet.  Checking the Oculus developer forums, the latest news on libraries tells us that the only ones needed for the linker are:

libovr.lib (libovrd.lib in debug)
Winmm.lib
ws2_32.lib

We’re just working with the Oculus SDK for now, so the libovr.lib and libovrd.lib alone will be sufficient.

Continuing with our simple ‘Hello World!’ project environment, we’ll add in the libovr.lib and libovrd.lib libraries through the VS2013 project’s properties (right-click the project for the Properties dialog selection) under Linker > Input > Additional Dependencies, adding in the file and path for those two .lib files. On my own system, those were under:

e:\OculusSDK\LibOVR\Lib\Windows\Win32\Debug\VS2013\LibOVR.lib
e:\OculusSDK\LibOVR\Lib\Windows\Win32\Release\VS2013\LibOVR.lib

Your own path, of course, will depend upon the installation location of your Oculus SDK.

Next, we’ll include the Oculus SDK.

In VS2013 again, under the project’s properties, Configuration Properties > C/C++ > Additional Include Directories options, we’ll set the path to the SDK’s LibOVR Include, which could be found on my own system at:

e:\OculusSDK\LibOVR\Include

Our development environment is now ready to create our basic Oculus program.

Starting Oculus DK2 Coding

To get a feel for how an Oculus program works, I opened up the Tiny Room sample code included with the SDK and…

i-have-no-idea-what-im-doing-dog

It’s been a while since I last worked in C++ and that sample code felt more unfamiliar than I was comfortable with. Before going any further, it was back to school for me.

The full MIT Open Courseware introduction to C++ class is available online, and stepping away from things to read the full lecture notes and coding up the assignment basics provided just the refresher I needed. If it’s been a while since you’ve coded in C++, I’d recommend completing that course yourself to scrape the rust off your knowledge.

With that done, back to Oculus.

The Oculus Developers Guide provides an example initialization of the device through this snippet:

Let’s put that into working code.

Using the above example for reference, we’ll confirm we can connect to the DK2 by initializing the device and fetching its product name and firmware version.  That’s accomplished with this bit of code:

With the DK2 turned on (look for the orange light on the headset), running the exe compiled from the above gives us the device’s attributes output to console:

So far so good. Now we’ll try reading some sensor data.

Referencing the Oculus Developer Guide again, under the Head Tracking and Sensors section we can see how to start up the DK2’s sensor and read in the tracking state. Putting all that into code with a loop to push out the results gives us:

With the output to console being:

Success!  The DK2’s sensors are being read and updating to reflect the device’s orientation.  ‘Hello World!’ is accomplished.

Now, to get my head around the Tiny World sample code…