]> git.tdb.fi Git - libs/vr.git/blobdiff - source/oculusriftcombiner.cpp
Fix frustum skew calculation
[libs/vr.git] / source / oculusriftcombiner.cpp
index e6b2908655a9a5d08520124b29e0a7dcbfe3ab21..86d32b031d6b2f590920a6a1ddd649d65071b10a 100644 (file)
@@ -14,12 +14,14 @@ const char vs_source[] =
        "varying vec2 texcoord_r;\n"
        "varying vec2 texcoord_g;\n"
        "varying vec2 texcoord_b;\n"
+       "varying float vignette;\n"
        "void main()\n"
        "{\n"
        "       gl_Position = vec4(gl_Vertex.xy, 0.5, 1.0);\n"
        "       texcoord_r = gl_MultiTexCoord0.xy;\n"
        "       texcoord_g = gl_MultiTexCoord1.xy;\n"
        "       texcoord_b = gl_MultiTexCoord2.xy;\n"
+       "       vignette = gl_MultiTexCoord3.x;\n"
        "}\n";
 
 const char fs_source[] =
@@ -27,18 +29,19 @@ const char fs_source[] =
        "varying vec2 texcoord_r;\n"
        "varying vec2 texcoord_g;\n"
        "varying vec2 texcoord_b;\n"
+       "varying float vignette;\n"
        "void main()\n"
        "{\n"
        "       float r = texture2D(texture, texcoord_r).r;\n"
        "       float g = texture2D(texture, texcoord_g).g;\n"
        "       float b = texture2D(texture, texcoord_b).b;\n"
-       "       gl_FragColor = vec4(r, g, b, 1.0);\n"
+       "       gl_FragColor = vec4(vec3(r, g, b)*vignette, 1.0);\n"
        "}\n";
 
 void create_distortion_mesh(Msp::GL::Mesh &mesh, ovrHmd hmd, ovrEyeType eye, const ovrFovPort &fov)
 {
        ovrDistortionMesh ovr_mesh;
-       ovrHmd_CreateDistortionMesh(hmd, eye, fov, ovrDistortionCap_Chromatic, &ovr_mesh);
+       ovrHmd_CreateDistortionMesh(hmd, eye, fov, ovrDistortionCap_Chromatic|ovrDistortionCap_Vignette, &ovr_mesh);
 
        ovrSizei tex_size = ovrHmd_GetFovTextureSize(hmd, eye, fov, 1.0);
        ovrRecti view_rect;
@@ -57,6 +60,7 @@ void create_distortion_mesh(Msp::GL::Mesh &mesh, ovrHmd hmd, ovrEyeType eye, con
                bld.multitexcoord(0, v.TanEyeAnglesR.x*scale.x+offset.x, 1.0f-(v.TanEyeAnglesR.y*scale.y+offset.y));
                bld.multitexcoord(1, v.TanEyeAnglesG.x*scale.x+offset.x, 1.0f-(v.TanEyeAnglesG.y*scale.y+offset.y));
                bld.multitexcoord(2, v.TanEyeAnglesB.x*scale.x+offset.x, 1.0f-(v.TanEyeAnglesB.y*scale.y+offset.y));
+               bld.multitexcoord(3, v.VignetteFactor);
                bld.vertex(v.ScreenPosNDC.x, v.ScreenPosNDC.y);
        }
 
@@ -74,12 +78,10 @@ namespace VR {
 
 OculusRiftCombiner::OculusRiftCombiner(const OculusRiftDevice &d):
        device(d),
-       left_mesh((GL::VERTEX2, GL::TEXCOORD2,0, GL::TEXCOORD2,1, GL::TEXCOORD2,2)),
-       right_mesh((GL::VERTEX2, GL::TEXCOORD2,0, GL::TEXCOORD2,1, GL::TEXCOORD2,2)),
+       left_mesh((GL::VERTEX2, GL::TEXCOORD2,0, GL::TEXCOORD2,1, GL::TEXCOORD2,2, GL::TEXCOORD1,3)),
+       right_mesh((GL::VERTEX2, GL::TEXCOORD2,0, GL::TEXCOORD2,1, GL::TEXCOORD2,2, GL::TEXCOORD1,3)),
        shprog(vs_source, fs_source)
 {
-       width_div = 2;
-
        const OculusRiftDevice::Private &dev_priv = device.get_private();
        ovrHmd hmd = dev_priv.ovr_hmd;
 
@@ -90,7 +92,7 @@ OculusRiftCombiner::OculusRiftCombiner(const OculusRiftDevice &d):
 
        float inner = max(left_fov.RightTan, right_fov.LeftTan);
        float outer = max(left_fov.LeftTan, right_fov.RightTan);
-       frustum_skew = (inner-outer)*2/(inner+outer);
+       frustum_skew = (inner-outer)/(inner+outer);
 
        left_fov.UpTan = right_fov.UpTan = vertical;
        left_fov.DownTan = right_fov.DownTan = vertical;
@@ -101,7 +103,10 @@ OculusRiftCombiner::OculusRiftCombiner(const OculusRiftDevice &d):
        create_distortion_mesh(right_mesh, hmd, ovrEye_Right, right_fov);
 
        ovrSizei tex_size = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, left_fov, 1.0);
-       oversize = max(tex_size.w*2.0f/hmd->Resolution.w, tex_size.h*1.0f/hmd->Resolution.h);
+       width_factor = tex_size.w*1.0f/hmd->Resolution.w;
+       height_factor = tex_size.h*1.0f/hmd->Resolution.h;
+       float aspect = (inner+outer)/(vertical*2);
+       aspect_factor = aspect*hmd->Resolution.h/hmd->Resolution.w;
 
        shdata.uniform("texture", 0);