Skip to main content
Up and Coming
March 30, 2013

OpenGL full example and shader...

Hello,

Great job with Rift and SDK. It seems awesome!. Waiting for my SDK...

A question:

- It's planned to release an new version of SDK with a minimal example (OculusRoomTiny) for OpenGL (with lens correction shader)?
- When? Soon? (to do this work myself or wait for new release)

Thanks!

Fernando

    36 replies

    Up and Coming
    April 1, 2013
    Fantastic!

    :D :D
    Participant
    April 1, 2013
    Awesome Daniel! As a fellow LWJGL'er, I want my rift nooooow! :)

    Mike
    Game in pre-alpha with support for Oculus Rift DK2 coming in the next patch, State of Fortune
    Up and Coming
    April 1, 2013
    "mike997" wrote:
    Awesome Daniel! As a fellow LWJGL'er, I want my rift nooooow! :)

    Mike


    Hehe; I hope someone will very quickly come up with an JNI wrapper for the C++-based Rift interface :-)
    Up and Coming
    April 4, 2013
    Sweet stuff!

    Have you looked at the sensor at all?
    "It's like Homeworld in first person." Disable Aero and vSync for a completely simulator sickness free experience with 2xHz FPS. Keep the config utility open for tracking to work.
    Participant
    May 4, 2013
    "38leinaD" wrote:

    Anywhere, here is my selfcontained example for the Barrel Distortion Shader. Sorry, I am little quicker with Java. Uses OpenGL via LWJGL.


    Hi, I tried to get your code running but failed with:
    "Lib dir: D:\workspace\OpenGLRiftShader\lwjgl-2.9.0\native\windows
    Info log:
    Fragment shader failed to compile with the following errors:
    ERROR: 0:20: error(#202) No matching overloaded function found HmdWarp
    ERROR: 0:20: error(#160) Cannot convert from 'const float' to 'highp 2-component vector of float'
    ERROR: error(#273) 2 compilation errors. No code generated"

    How can I fix that problem?

    Thanks, Jonek.
    Up and Coming
    July 3, 2013
    I've got a working OpenGL skeleton app on Glfw with Glew. http://jimbomania.com/oculus/OculusGlfwSkeleton-r1722.zip

    It requires CMake, python and Visual Studio to build. If you can get some use out of it, be my guest!
    Up and Coming
    July 4, 2013
    I have got a working example in OpenSceneGraph which uses OpenGL. I have included two shaders, one basic and one with support for correction for chromatic aberration.

    viewtopic.php?f=39&t=1885#p28546
    Participant
    July 31, 2013
    I am trying to adapt 38leinaD's Java code for Processing. The first problem with the fragment shader is that gl_TexCoord[0] is a vec4 and the the function HmdWarp expects a vec2. How did you get this compiled?

    Edit:
    I changed the following line in the fragment shader and now it works in Java. But still not in Processing.
    vec2 tc = HmdWarp(gl_TexCoord[0].xy);

    Any idea?

    Edit: Got it!
    viewtopic.php?f=26&t=789&p=39575#p39884
    New Participant
    September 15, 2013
    Hey 38leinaD, how would I modify your code to display an image?
    Up and Coming
    December 14, 2013
    Posting here as I've no full member rights yet - and this is at least related to openGL and the shader.


    Fast forward to December 2013 - the oculus SDK ships with all the required stuff for openGL in the example code, which is totally cool.

    I have a few questions regarding the setup of the distortion shader. I took the shaders from the tiny room demo and slightly modified them to match our conventions. I ended up with the vertex shader that does practically nothing:

    varying  vec2 oTexCoord;
    void main()
    {
    gl_Position = ftransform();
    gl_FrontColor = gl_Color;
    oTexCoord = gl_MultiTexCoord0.st;
    }


    and the fragment shader that does the distortion given one texture unit:
    uniform vec2 LensCenter;
    uniform vec2 ScreenCenter;
    uniform vec2 Scale;
    uniform vec2 ScaleIn;
    uniform vec4 HmdWarpParam;
    uniform sampler2D Texture0;
    varying vec2 oTexCoord;

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

    void main()
    {
    vec2 tc = HmdWarp(oTexCoord);
    if (!all(equal(clamp(tc, ScreenCenter-vec2(0.25,0.5), ScreenCenter+vec2(0.25,0.5)), tc)))
    gl_FragColor = vec4(0);
    else
    gl_FragColor = texture2D(Texture0, tc);
    }


    As you can see, the fragment shader is identical to the tiny room demo. So no surprises there.

    Now the code to setup the shader takes an OVR::Util::Render::StereoConfig which in turn has been initialized by setHMDInfo() - the only missing parameters to use the code from tiny room are WindowWidth and WindowHeight so the shader setup code is as follows:


    using OVR::Util::Render;

    void set_oculus_shader(StereoConfig& config, StereoEye eye, int WindowWidth, int WindowHeight)
    {
    StereoEyeParams p = config.GetEyeRenderParams(eye);
    DistortionConfig Distortion = config.GetDistortionConfig();

    enum { uLensCenter, uScreenCenter, uScale, uScaleIn, uHmdWarpParam, uTexture0 };
    const char * kUnis[] = { "LensCenter", "ScreenCenter", "Scale", "ScaleIn", "HmdWarpParam", "Texture0", NULL };
    static OGL_shader oculus_shader("oculus.glsl", NULL, "oculus.glsl", NULL, kUnis, NULL);
    oculus_shader.bind();

    float w = float(p.VP.w) / float(WindowWidth),
    h = float(p.VP.h) / float(WindowHeight),
    x = float(p.VP.x) / float(WindowWidth),
    y = float(p.VP.y) / float(WindowHeight);

    float as = float(p.VP.w) / float(p.VP.h);

    // We are using 1/4 of DistortionCenter offset value here, since it is
    // relative to [-1,1] range that gets mapped to [0, 0.5].
    oculus_shader.uniform2f(uLensCenter, x + (w + Distortion.XCenterOffset * 0.5f)*0.5f, y + h*0.5f);
    oculus_shader.uniform2f(uScreenCenter, x + w*0.5f, y + h*0.5f);
    // MA: This is more correct but we would need higher-res texture vertically; we should adopt this
    // once we have asymmetric input texture scale.
    float scaleFactor = 1.0f / Distortion.Scale;

    oculus_shader.uniform2f(uScale, (w/2) * scaleFactor, (h/2) * scaleFactor * as);
    oculus_shader.uniform2f(uScaleIn, (2/w), (2/h) / as);
    oculus_shader.uniform4f(uHmdWarpParam, Distortion.K[0], Distortion.K[1], Distortion.K[2], Distortion.K[3]);
    oculus_shader.uniform1i(uTexture0, 0);
    }


    For now all we have is two textures to present each eye. The textures are obviously the render surfaces of two FBOs.

    The application window is resized to 1280x800, and each of the textures is 1024x1280 in size preserving the aspect ratio per eye.

    So how to get the two textures side-by-side with each getting the correct distortion applied?

    The first straight-forward approach is:
    left_eye_tex->bindTex();
    set_oculus_shader(oculus->getConfig(), OVR::Util::Render::StereoEye_Left, 1280, 800);

    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex2f(0, 0);
    glTexCoord2f(0, 1); glVertex2f(0, 800);
    glTexCoord2f(1, 1); glVertex2f(1280, 800);
    glTexCoord2f(1, 0); glVertex2f(1280, 0);
    glEnd();

    right_eye_tex->bindTex();
    set_oculus_shader(oculus->getConfig(), OVR::Util::Render::StereoEye_Right, 1280, 800);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex2f(0, 0);
    glTexCoord2f(0, 1); glVertex2f(0, 800);
    glTexCoord2f(1, 1); glVertex2f(1280, 800);
    glTexCoord2f(1, 0); glVertex2f(1280, 0);
    glEnd();


    This already almost works, apart from two problems:
    1. The shader seems to expect the texture coordinates for the left picture to go from 0,0 to 0.5,1 and for the right picture from 0.5,0 to 1,1 - but I have two textures each going from 0,0 to 1,0. Consequently, the left eye gets the left half of the left texture, where the right eye gets the right half of the right texture. Which is stupid, both eyes should be getting the entire of their own texture.

    2. Instead of what I'm used from the tiny room demo, I don't get two symmetrically distorted images, but two images distorted in the same direction. This would be very easy to explain if I were allowed to post screenshots here. Think of the shape of the visible area for one eye. Now the shape for the right eye should be a mirror of the shape for the left eye. But instead I'm seeing the same shape translated. I remember reading a post here about the eye distance parameter being inverted for the right eye, so this is probably what I'm missing here.