Skip to main content
Participant
June 15, 2013

OpenGL render setup problem

Hey guys, I am 99% of the way to supporting the Rift in my OpenGL C++ engine but have come across a fundamental problem in how I am rendering the final scene. I am having trouble understanding which part of the transformation process is producing my erroneous result.

I render my scene to a full size framebuffer, first the left side of the buffer, then the right using glViewport and glScissor to limit the influence of the drawing and clearing. So I have a full screen buffer divided in half width wise with two slightly different images in each half.

Then when drawing to the screen, I set the rift shader up as per the tiny room demo then draw a pair of triangles for the left side using texCoords from 0.0 to 0.5 and repeat the process with texCoords 0.5 to 1.0. Everything works great except the result looks like this:



Great! Except the right eye image is backwards... No problem, I can just reverse it using multiple methods (flip texcoords, or in vertex or frag shader). But then I end up with this:



So the render is correct but the shape of the barrel shader has been affected. Each side of the shader is not symmetrical and in my flipping I have also affected this. I KNOW I have made some error in one part of the process and there are also a few parts of the tiny room demo that I don't understand (like why does the vertex shader flip the y texture coordinate? why does it load a matrix with a 2.0 scale for the look and right vectors to render with?).

So any help appreciated, my shaders follow, apologies for the usage of gl_TexCoord[0] etc, I only just learned this was bad mojo.

Vertex shader is passthrough:

void main(void)
{
gl_FrontColor = gl_Color;
gl_Position = gl_Vertex;
gl_TexCoord[0].st = gl_MultiTexCoord0.xy;
}


Pixel shader is pretty much identical to the example:

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

vec2 HmdWarp(vec2 texIn)
{
vec2 theta = (texIn - LensCenter) * ScaleIn;
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(gl_TexCoord[0].xy);
if (any(notEqual(clamp(tc, ScreenCenter-vec2(0.25,0.5), ScreenCenter+vec2(0.25, 0.5)) - tc, vec2(0.0, 0.0))))
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
else
{
gl_FragColor = texture2D(tex, tc);
}
}

    10 replies

    Up and Coming
    June 17, 2013
    OpenHMD or LibVR open source libraries might help you out here, or their code for hints if you're doing it on your own

    recommend read through this discussion thread => http://developer.oculus.com/forums/viewtopic.php?f=20&t=667

    also checkout => http://developer.oculus.com/forums/viewtopic.php?f=17&t=1341
    Participant
    June 17, 2013
    Thanks Harley! I hadn't checked those other threads for a few days.
    Participant
    June 18, 2013
    FYI, my solution to this problem was to multiply my right eye scene modelview matrix by a matrix with a right vector of (-1.0f, 0.0f, 0.0f, 0.0f) and use a similar transformation on the camera matrix so the whole scene is flipped around.
    Participant
    June 22, 2013
    Possibly you forgot to negate the value of LensCenter that you set in the distortion shader for one eye? Just double-check that. You need to be careful about doing random flips - especially matrix flips - it's too easy to get an "even number of sign errors" that looks kinda right, but not quite, and will come back to bite you later.
    Tom Forsyth - OculusVR coder
    Participant
    June 22, 2013
    Thanks tomf, these are things I'm trying to avoid. Where in the TinyRoom demo does the LensCenter value get negated? I see an offset by viewPort size here:


    pPostProcessShader->SetUniform2f("LensCenter", x + (w + Distortion.XCenterOffset * 0.5f)*0.5f, y + h*0.5f);
    Up and Coming
    June 23, 2013
    I believe that the "Distortion.XCenterOffset" value gets negated if you're currently rendering the right eye. This is in:
    RenderTiny_Device.h in the RenderDevice::SetDistortionConfig function.
    Participant
    June 23, 2013
    Thanks tlopes, this was the crux of my problem.
    New Participant
    June 24, 2013
    "tlopes" wrote:
    I believe that the "Distortion.XCenterOffset" value gets negated if you're currently rendering the right eye. This is in:
    RenderTiny_Device.h in the RenderDevice::SetDistortionConfig function.


    Thanks tlopes, I missed this one too and had the same offset problem.
    Up and Coming
    June 24, 2013
    Glad to help guys :)
    New Participant
    March 29, 2014
    Oh my god! I'm also making OpenGL Engine for OpenGL ;)

    You are amazaing. :D