Skip to main content
Up and Coming
April 25, 2013

Basic Linux Rift library with OpenGL in C

Ok, don't get too excited. This is a very simple C port of the SDK 1.5 library. I did this mostly to satisfy my own curiosity, but if there is enough interest, I'm willing to make it a more complete project.

Right now all it will do is connect to the Rift, open up a GL window, and move an arrow around to follow the HMD tracker. Now that the new SDK is out, there are GL shaders which I'll work into the simple demo scene for the next pass.

I'm still playing around with this, but tf there is interest, I can put the code up in a private git repo so others can contribute.

Enjoy

-nsb

    73 replies

    New Participant
    May 30, 2013
    If you guys all do "combine libs", I hope that the lib does have Rift API and its own API as well. I would love something that can be both a dropin replacement for the official Rift driver as well as something that can be used with other HMD if they get supported.
    Participant
    May 30, 2013
    "theyain" wrote:
    If you guys all do "combine libs", I hope that the lib does have Rift API and its own API as well. I would love something that can be both a dropin replacement for the official Rift driver as well as something that can be used with other HMD if they get supported.

    I'm not too sure about the legal implications, and I have not enough knowledge of C++ to be sure to have an ABI-compatible lib. I guess one way to do it would be to write a rift-compatible lib on top of something like OpenHMD or LibVR, while following the API reference from the developer center... but I'm personally not that interested.
    Up and Coming
    June 4, 2013
    Checkout ashleysmith's optimized barrel distortion GLSL vertex shader code in this other thread:

    viewtopic.php?f=17&t=1341
    "ashleysmith" wrote:
    Hey, I have been looking into the barrel distortion applied to the final image for the oculus. I was thinking that you could do the barrel distortion in the vertex shader instead of the fragment shaders full screen pass. You would need a highly tessellated model, but this would optimise certain scenes and reduce latency, plus you get hardware aa instead of supersampling.

    I have implemented it and have it working, I took the function from the Render_D3D1X_Device.cpp, implemented it in glsl and applied it to the vertex shaders in our demo. However I needed to do the inverse of a barrel distortion, a pincushion. Does anyone know what the inverse values are for this?

    In the sdk they are defined as Distortion.SetCoefficients(1.0f, 0.22f, 0.24f); for the barrel distortion

    I managed to guess values that give a good look but it would be nice to know where these values come from :D

    Here's the hdmwarp in glsl:

    uniform highp vec2 LensCenter;
    uniform highp vec2 ScreenCenter;
    uniform highp vec2 Scale;
    uniform highp vec2 ScaleIn;
    uniform highp vec4 HmdWarpParam;

    highp vec2 HmdWarp(highp vec2 in01)
    {
    highp vec2 theta = (in01 - LensCenter) * ScaleIn; // Scales to [-1, 1]
    highp float rSq = theta.x * theta.x + theta.y * theta.y;
    highp vec2 theta1 = theta * (HmdWarpParam.x + HmdWarpParam.y * rSq +
    HmdWarpParam.z * rSq * rSq + HmdWarpParam.w * rSq * rSq * rSq);
    return LensCenter + Scale * theta1;
    }


    Note that I am using two different fbo's for each eye instead of the single fbo that I believe is in the sdk.
    Then the way you use this in the fragment shader is:

    highp vec2 tc = HmdWarp(texcoord0);
    highp vec2 offset = clamp(tc, ScreenCenter-vec2(0.5,0.5), ScreenCenter+vec2(0.5, 0.5)) - tc;
    if(offset.x != 0.0 || offset.y != 0.0)
    discard;
    else
    {
    gl_FragColor = texture2D( texture, tc );
    }


    If instead you want to see what it looks like in a vertex shader, do this after you have projected your vertices:

    #ifdef OCULUS_VERTEX_DISTORTION
    gl_Position.xy = (HmdWarp( (gl_Position.xy / gl_Position.w)*0.5+0.5 ) *2.0-1.0 ) * gl_Position.w;
    #endif


    i.e. exactly the same function, but you need to pass in pincushion distortion parameters instead of a barrel (And of course you don't need any fbos). Remember that this only works for highly tessellated models. So if you have a big triangle on screen it will appear un-distored.
    nsbAuthor
    Up and Coming
    June 19, 2013
    At long last I've moved libovr_nsb to github.

    https://github.com/ultranbrown/libovr_nsb

    Work on the example apps is ongoing, as is work on the library itself. Feedback, suggestions, and patches are all welcome.

    I will post updates here as major releases happen.

    Many thanks to atavener, geekmaster and everyone else who helped beat this library into shape.

    -nsb
    Up and Coming
    June 19, 2013
    The switch to hidapi was a wise move. I quit using your code and switched to hidapi myself awhile back. Now I can use your code again. Thanks!
    :D
    This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
    Up and Coming
    June 22, 2013
    "examples/gldemo" (my mess) hasn't been working for nsb. Has anyone else tried it and been having problems? I'd like to figure out what's wrong, but that's always tough when you have no way to reproduce it!

    Also, in the latest commit I've broken out gltools and glstereo which should be useful for building other demos/apps with. I want to improve the interface for glstereo -- it's too simple and chances are it won't do what you want beyond basic demo use. :) Should be easy to "Riftify" other code with it though.
    Participant
    July 2, 2013
    hi all,
    question 1:
    i am working on the libovr_nsb-0.3.0-examples
    i install:libornsb_0.3.0_amd64.zip and hidapi-0.7.0.zip
    if i run the examples: gldemo.c (makefile) is ok
    but output is:
    ./gldemo: error while loading shared libraries:
    libovr_nsb-0.3.0.so: cannot open shared object file: No such file or directory

    if i make sudo ldconfig:
    output:
    /sbin/ldconfig.real: /usr/lib/libgl_matrix-1.2.3.so ist kein symbolischer Link
    /sbin/ldconfig.real: /usr/lib/libovr_nsb-0.2.0.so ist kein symbolischer Link
    (english: *.so is no symbolic link)
    so i have some troubles ??

    question 2:
    i found the libovr_nsb-master.zip
    -> how can i correct install this stuff
    - ./configure
    - make or sudo make

    have somebody a hint for this

    question 3:
    in the moment i have no oculus-rift display
    i can run under win7 the sdk and compile the source-code with
    vs2010 c++ express
    compile and run ok

    now i want do compile the gldemo.c under xubuntu 12.04
    compiling examples: gldemo.c as (from makefile)
    - gcc gldemo.c -o gldemo1 -lovr_nsb -lgl_matrix -lGL -lGLU -lglut -lpthread -lm
    - result:
    In file included from /usr/include/libovr_nsb/OVR_Sensor.h:13:0,
    from /usr/include/libovr_nsb/OVR.h:4,
    from gldemo.c:15:
    /usr/include/libovr_nsb/OVR_Device.h:10:27:
    sch werwiegender Fehler: hidapi/hidapi.h: Datei oder Verzeichnis nicht gefunden
    (english: hidapi/hidapi.h not found)

    now my question
    can i working with gldemo.c without access device
    aka only a dummy device
    is this possible ???

    thanks for hints helps and tricks.

    p.s. i have a raspberryPi with wheezy OS.
    in the moment i can working with
    opengl, opencv and my extern webcam (slow)
    wiimote as input (python) bluetooth
    and i'm interesting make developments with hmd and raspberry Pi.
    2 excuses
    - today i have read pages 3 + 4
    - my bad english
    regards nomad
    nsbAuthor
    Up and Coming
    July 5, 2013
    "nomad" wrote:
    hi all,
    question 1:
    i am working on the libovr_nsb-0.3.0-examples
    i install:libornsb_0.3.0_amd64.zip and hidapi-0.7.0.zip
    if i run the examples: gldemo.c (makefile) is ok
    but output is:
    ./gldemo: error while loading shared libraries:
    libovr_nsb-0.3.0.so: cannot open shared object file: No such file or directory

    if i make sudo ldconfig:
    output:
    /sbin/ldconfig.real: /usr/lib/libgl_matrix-1.2.3.so ist kein symbolischer Link
    /sbin/ldconfig.real: /usr/lib/libovr_nsb-0.2.0.so ist kein symbolischer Link
    (english: *.so is no symbolic link)
    so i have some troubles ??


    If you are building from source, you will need to either do a 'make install' as root to put the libraries in place, or set your LD_LIBRARY_PATH to '<libovr_nsb path>/libovr_nsb/.libs'. and then run the examples.

    "nomad" wrote:

    question 2:
    i found the libovr_nsb-master.zip
    -> how can i correct install this stuff
    - ./configure
    - make or sudo make

    have somebody a hint for this


    To run in place, see above. To install, just 'sudo make' and then 'sudo make install'

    "nomad" wrote:

    question 3:
    in the moment i have no oculus-rift display
    i can run under win7 the sdk and compile the source-code with
    vs2010 c++ express
    compile and run ok

    now i want do compile the gldemo.c under xubuntu 12.04
    compiling examples: gldemo.c as (from makefile)
    - gcc gldemo.c -o gldemo1 -lovr_nsb -lgl_matrix -lGL -lGLU -lglut -lpthread -lm
    - result:
    In file included from /usr/include/libovr_nsb/OVR_Sensor.h:13:0,
    from /usr/include/libovr_nsb/OVR.h:4,
    from gldemo.c:15:
    /usr/include/libovr_nsb/OVR_Device.h:10:27:
    sch werwiegender Fehler: hidapi/hidapi.h: Datei oder Verzeichnis nicht gefunden
    (english: hidapi/hidapi.h not found)


    It looks like hidapi wasn't installed, or at least wasn't found. If you're still building from the main repo, this is confusing as autoconf should have failed before you got this far. If you're building the examples from the separate zip file, you will still need to have libovr_nsb and hidapi installed for it to work.

    "nomad" wrote:

    now my question
    can i working with gldemo.c without access device
    aka only a dummy device
    is this possible ???


    It's certainly possible, though there would be some work involved. If your dummy device operates at the HID level then the driver won't know the difference.

    -nsb
    Participant
    July 6, 2013
    hi nsb,
    thanks for your help and hints
    i give this a try
    thanks
    in the meantime the oculus sdk-0.23 for linux is here
    curious:
    on my first laptop (amd) install ok, run verry bad, the scene is only red color .....
    on my second laptop (intel) all Ok, install and run ok
    have a look on my early postings...
    regards nomad
    Participant
    July 20, 2013
    Quick question about freeglut. when running the only error i get is:

    freeglut (./linuxtest): OpenGL GLS extension not supported by display :10.0

    (yes i have tried using my main display 0.0 and secondary display 0.1, but the same issue arises. Any ideas? I'll look into freeglut itself here, but you all seemed so helpful i figured I should try here in the meantime.

    P.s. for my project I don't really need the code to drive the rift display, but i DO need it to allow the rift to display whatever is being received via HDMI. I really only want the headtracking intputs so I can use them for my specific camera-gimbal-headtracking project.

    Thanks

    -Luke