Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
webstorms's avatar
webstorms
Honored Guest
12 years ago

Calculate displacement of images

How do I calculate the displacement of the image I render to the Oculus Screen for the right and left side?

2 Replies

  • It depends on how the images were generated.

    Software generated images using the methods described in the SDK documentation don't need to be displaced, because the documentation illustrates how to modify the Projection matrix to do the displacement for you.

    Pre-existing 3D imagery (or even non-stereoscopic imagey) should be centered on the screen under the lens. Assuming you have code that will display the image centered on the screen properly in a non-Rift context using the coordinates LEFT, TOP, RIGHT & BOTTOM, then you need to adjust LEFT and RIGHT in order to have the image centered under the lens.

    In OpenGL, the standard screen dimensions are -1,1 in both the X and Y dimensions. So we need to find the offset of the lens center from the center of the viewport in those dimensions. The SDK has an HMDInfo class that provides all you need to do that.

    // Pysical width of the per-eye viewport
    static float eyeScreenWidth = hmdInfo.HScreenSize / 2.0f;

    // The viewport goes from -1,1. We want to get the offset of the lens from the center
    // of the viewport, so we only want to look at the distance from 0, 1, so we divide in
    // half again
    static float halfEyeScreenWidth = eyeScreenWidth / 2.0f;

    // The distance from the center of the WHOLE display panel (NOT the center of the
    // viewport) to the lens axis
    static float lensDistanceFromScreenCenter = hmdInfo.LensSeparationDistance / 2.0f;

    // Now we we want to turn the measurement from
    // millimeters into the range 0, 1
    static float lensDistanceFromViewportEdge = lensDistanceFromScreenCenter /
    halfEyeScreenWidth;

    // Finally, we want the distnace from the center, not the distance from the edge, so
    // subtract the value from 1
    static float lensOffset = 1.0f - lensDistanceFromViewportEdge;


    There you have it. For the left eye, you add lensOffset to LEFT and RIGHT in your rendering coordinates. For the right eye, you subtract it instead. You can apply a zoom factor to the overall coordinates to make the image bigger or smaller, but you should make sure to do this scaling before applying the offset. Ideally, if you know the FOV of the image, you can calculate the appropriate scaling factor so image appears to be the correct size.
  • Thank you, this was very helpful! I just see that 38leinaD has send me a link to some of his code where he uses the method where the matrix does the displacement. I think I might go with that method because of its simplicity.