vrex

VRex, Inc.

3 Westchester Plaza

Elmsford, NY 10523

dev@vrex.com



The Interleaved Format


The interleaved format (also known as line alternate) is the base format for all VRex Professional Products (projectors, flat panel displays) and our VR Surfer Classic and Web Pack shutter glasses.

The interleaved format consists of alternating rows of the left and right views. The following image illustrates this:



It is important to note that an interleaved view is comprised of 2 views with half the normal vertical aspect ratio.   When these 2 half-vertical-aspect-ratio views are interleaved, the resulting view appears at the correct aspect ratio.   Therefore, depending on your situation, it may be worthwhile to modify your rendering engine to generate your left and right views with half the normal aspect ratio.

All VRex products expect the interleaved view to start with the right view.   More specifically, the lines corresponding to the right view of an interleaved view should reside on the even scanlines of the display.   The lines of left view, therefore, should fall on the odd scanlines.   This is important to be aware of if your application is running in a window because the window can be moved.


Interleaving when direct pixel access is available
Creating an interleaved view from left and right views that reside in memory (video or local) is fairly trivial.   The following C-language code fragment can be used for this purpose:

// NOTE! This code assumes that "left" and
// "right" are half vertical aspect ratio and
// "final" is pre-allocated and full/normal
// aspect ratio.
//
// "width" and "height" are the width and
// height of the half-vertical-aspect-ratio
// left or right view.
//

int y;
for(y=0; y<height; y++) {
    memcpy(final[y*2], right[y], width);
    memcpy(final[y*2+1], left[y], width);
}



If you can only render full aspect ratio left and right views, don't worry.   You can use the loop above, and simply copy every other line of both the left and the right views into the "final".   By interpolating or blending each adjacent line pair (lines 0+1, 2+3, 4+5, etc) of the left and right views you can create a better quality interleaved image (at a significant performance drop).


Interleaving with OpenGL
In OpenGL, you typically do not have direct pixel access.   Interleaving your left and right views as above involves using ReadPixels and WritePixels and is too time consuming.   A better method exists using only OpenGL calls.   This has the added benefit of being accelerated in hardware.

We will use the OpenGL Stencil buffer to interleave our left and right views.   The first step is to intialize the Stencil buffer with our "mask".   This function only needs to be called once!

GLubyte *mask;

void initStencilMask() {

    glClearStencil(0x0);
    glEnable(GL_STENCIL_TEST);

    glClear(GL_STENCIL_BUFFER_BIT);
    glStencilFunc(GL_ALWAYS, GL_REPLACE, GL_REPLACE);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    if (mask != NULL) {
        delete [] mask ;
        mask = NULL ;
    }

    int w = width / 8;
    if (width % 8) w++;
    GLubyte * myPixels = new GLubyte[w*height];
    mask = myPixels;

    // alternating rows of 0's and 1's
    for(int i=0; i<height; i+=2) {
        memset(myPixels, 0x00, w);
        myPixels += w;
        memset(myPixels, 0xFF, w);
        myPixels += w;
    }

    glRasterPos2f(0.0, 0.0);
    glDrawPixels(width, height, GL_STENCIL_INDEX, GL_BITMAP, mask);
    glFlush();
}

Once the Stencil buffer has been initiated, interleaving left and right views is fairly trivial.   NOTE: you should replace RenderLeftView() and RenderRightView() with the appropriate functions for your application.

void renderStereoView() {

    glDrawBuffer(GL_BACK);

    // render left camera view...
    // note: we do not need to use a stencil mask
    // for the left rendered view.

    RenderLeftView();

    // render right camera view...

    glStencilFunc(GL_EQUAL, 0x1, 0x1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    RenderRightView();

    SwapBuffers();
}

Note that in this case we are rendering our left and right views at the normal, full, aspect ratio.   This method works well for most cases.   However, for scenes that have many thin (1 or 2 pixel thick) horizontal lines, this method of interleaving may be unacceptable.   For this case, you can render with half the vertical aspect ratio, and use glPixelZoom() to "stretch" the view vertically to obtain a full aspect ratio, which is then passed through the stencil buffer.   This method produces higher quality interleaving at a performance cost.

If you have any further questions, feel free to email us at: dev@vrex.com.




Last updated on March 2, 1999
For problems with this website, please contact webmaster@vrex.com
Copyright © 1999 VRex, Inc. All Rights Reserved.