Skip to main content
Up and Coming
May 29, 2014

Oculus Rift SDL2 OpenGL Demo

I created a simple demo to show how to use the latest Oculus SDK with SDL2 and OpenGL using SDK Rendering. You can access the source code here.

UPDATE: I updated the demo to work with the new SDK. Positional tracking is not applied to the camera, but it can easily be added. You can access the source code here. Here are two screenshots for those who are curious.

    18 replies

    Up and Coming
    October 30, 2014
    Just a quick note to say that I was able to compile the lates version of this example against the latest SDK (0.4.3) on Linux with a few minor changes.

    --- SDL_oculus_0_4.c_orig 2014-10-28 21:43:31.259569107 -0600
    +++ SDL_oculus_0_4.c 2014-10-28 21:07:00.959519612 -0600
    @@ -1,9 +1,14 @@
    #define GLEW_STATIC
    #include "GL/glew.h"
    +
    // Uncomment your platform
    //#define OVR_OS_WIN32
    //#define OVR_OS_MAC
    -//#define OVR_OS_LINUX
    +#define OVR_OS_LINUX
    +inline int max ( int a, int b ) { return a > b ? a : b; }
    +
    +#include "OVR.h"
    +
    #include "OVR_CAPI_GL.h"
    #include "Kernel/OVR_Math.h"
    #include "SDL.h"
    @@ -240,9 +245,9 @@
    for (int eyeIndex = 0; eyeIndex < ovrEye_Count; eyeIndex++)
    {
    ovrEyeType eye = hmd->EyeRenderOrder[eyeIndex];
    - eyeRenderPose[eye] = ovrHmd_GetEyePose(hmd, eye);
    + eyeRenderPose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye);

    - Matrix4f MVPMatrix = Matrix4f(ovrMatrix4f_Projection(eyeRenderDesc[eye].Fov, 0.01f, 10000.0f, true)) * Matrix4f::Translation(eyeRenderDesc[eye].ViewAdjust) * Matrix4f(Quatf(eyeRenderPose[eye].Orientation).Inverted());
    + Matrix4f MVPMatrix = Matrix4f(ovrMatrix4f_Projection(eyeRenderDesc[eye].Fov, 0.01f, 10000.0f, true)) * Matrix4f::Translation(eyeRenderDesc[eye].HmdToEyeViewOffset) * Matrix4f(Quatf(eyeRenderPose[eye].Orientation).Inverted());

    glUniformMatrix4fv(MVPMatrixLocation, 1, GL_FALSE, &MVPMatrix.Transposed().M[0][0]);


    Thanks for the example,
    Simon
    New Participant
    November 4, 2014
    I copied this and made it build in Linux (as, I see, others already have as well).

    I also added simple support for it to auto-detect if the rift was rotated or not, if it is on a separate screen. I believe this will also work on a single screen (Twinview in nVidia), but the Rift has to be rotated.

    A copy of the SDK, with this demo and these changes, is here (comments, feedback, PRs, all welcome):
    https://github.com/steaphangreene/oculus_sdk

    Note that, to use it on a seapate screen, you need an updated version of SDL2, which includes https://hg.libsdl.org/SDL/rev/3d2c0f659ad3 - which is not included in any official SDL release yet. In Debian, this is included in version 2.0.2+dfsg1-5 of the packages, currently only in sid/unstable and jessie/testing (I think). I am not sure about the other distributions.

    I believe having the Rift on a separate screen is better when it is always connected. It doesn't confuse your window manager into trying to put normal windows on your rift, and it allows you to turn the rift off when not using it.

    In my case, my Rift is on the second screen of my primary display (:0.1), not rotated, and connected to output HDMI-0. So, I can use these commands to do thing on my rift:

    Start oculusd (I do this in my .xsession)
    DISPLAY=:0.1 ~/bin/oculusd &

    Turn rift off (I do this in my .xsession too, and also whenever I am done playing with the DK2):
    DISPLAY=:0.1 xrandr --output HDMI-0 --off

    Turn rift back on:
    DISPLAY=:0.1 xrandr --output HDMI-0 --auto

    Run this test demo (from in the Samples/SDL2Minimal subdir):
    DISPLAY=:0.1 ./main

    Run the normal OWD (from the main SDK dir):
    DISPLAY=:0.1 make run
    (You have to hit F9 once for it to figure out how to go fullscreen correctly).

    I hope all this helps someone.

    Thanks for writing this simple test program, it was just what I was looking for as a starting point! :)
    Up and Coming
    March 4, 2015
    I also compiled the code with Oculus 0.4.4 API. It can be found here:

    https://github.com/mmostajab/Oculus_SDL
    Participant
    March 5, 2015
    It's working, thanks for the tiny example :), but I had to add
    #include <stddef.h>
    into OVR_CAPI_GL.h.
    Up and Coming
    March 7, 2015
    Thanks for posting this. It works, but in Windows it doesn't work right in direct mode (I'm probably getting 0.5 FPS on average). It works fine in extended mode though. Anyone know why?
    Inspiring
    March 7, 2015
    You're failing to unbind the framebuffer before passing the texture to the Oculus SDK.
    Brad Davis - Developer for High Fidelity Co-author of Oculus Rift in Action
    Up and Coming
    March 7, 2015
    "jherico" wrote:
    You're failing to unbind the framebuffer before passing the texture to the Oculus SDK.


    Thanks for the quick response. I added a call to glBindFramebuffer(GL_FRAMEBUFFER, 0) immediately before the call to ovrHmd_EndFrame(), but nothing changes. It's still taking seconds per frame.
    New Participant
    March 7, 2015
    I have copied the code. Downloaded SDL and GLEW. Connected necessary libraries in VS2013. But when I compile I get
    Error	1	error C1083: Cannot open include file: 'GL/glx.h': No such file or directory

    If I add this glx.h manually, OVRLib asks for additional missing Dx11 libraries. How to solve this?
    UPDATE:
    I guess I just needed to un comment platform definition at top. Now trying to solve glew Linking errors...
    Error	33	error LNK2001: unresolved external symbol _glewExperimental 

    I have added glew32d.lib to linker input.
    UPDATE 2:
    Adding glew.c into my project solved the problem. Dynamic linking causes troubles for me.
    Plus don't forget to switch Rift to "Extended Mode" This may cause troubles as well.
    Nice demo. Nothing that makes code complicated. Thanks to the author!