Friday, May 2, 2014

Project Virtua Demo - Using Just the Rift

This is a quick tutorial on how to get a simple Oculus Rift demo up and running with Project Virtua.  The library is designed to be quick and easy to use, with little to no extra work on the developer or users end.  We will also be using the MiddleMan library included with Project Virtua to create the basic window scene and handle the model rendering for our basic floor, though I will not cover their usage in this tutorial (see the Github code at the end for that, it's pretty easy to use).

Setting Up the Project Files

To start, make sure that the Project Virtua libraries are properly setup in your project, as well as the Oculus Rift SDK headers.  This will ensure that you can properly access the methods from the library.  You can find a quick tutorial on setting up the Project Virtua files here: http://projectvirtua.wikia.com/wiki/Installing.  Once that is done, you are all set to start getting into the code!

Creating an Oculus Rift Object

As I said previously, I'm not going to go over the code for creating the window and object models, but it's very easy to do so using this library.  The real meat of this is creating the Oculus Rift object to use in your code.  Doing so is very easy still:

      OculusRift rift(false, testWindow.renderingContext, testWindow.windowHandle, testWindow.deviceContext);  
This will create an Oculus Rift object named "Rift", that does not create a fake Oculus Rift when disconnected, uses our testWindow's rendering context, window handle, and device context.  To check that the Oculus Rift was properly detected, simply use rift.isConnected() to get the boolean status.  When rendering, you can use the following code:

           //Attempts to render to OculusRift  
           if (rift.StartRender())  
           {  
                //Renders left eye  
                rift.StartEyeRender(Left, viewOffsetMatrix);  
                {  
                     rift.getPerspectiveMatrix(Left, perspectiveMatrix);  
                     drawGLScene(program, perspectiveMatrix, (viewOffsetMatrix * viewMatrix));  
                }  
                rift.EndEyeRender(Left);  
                //Renders right eye  
                rift.StartEyeRender(Right, viewOffsetMatrix);  
                {  
                     rift.getPerspectiveMatrix(Right, perspectiveMatrix);  
                     drawGLScene(program, perspectiveMatrix, (viewOffsetMatrix * viewMatrix));  
                }  
                rift.EndEyeRender(Right);  
                //Frame buffering is not handled automatically!  
                pv_glBindFramebuffer(PV_GL_FRAMEBUFFER, 0);  
                glDisable(GL_DEPTH_TEST);  
                rift.EndRender();  
                glEnable(GL_DEPTH_TEST);  
                // Ocuus Rift sets clear depth to 0, reset that here.  
                glClearDepth(1);  
           }  
The final scene rendered without the Rift as well.
Notice that rift.StartRender() also returns a boolean indicating whether the Oculus Rift was detected.  This will help to reduce the number of calls that you need to make when rendering.  The process from there on is pretty simple, get the view matrix offset for each eye via StartEyeRender, and then render the scene for each eye.

Source Code

You can find the source code for this test project here, as well as the resources files here.  While this tutorial was fairly quick, I hope it will help people get setup and using Project Virtua for the Oculus Rift side.  I plan to try and do one or two more of these to show off some of the other nice features soon.

Feel free to post any comments or questions below!