OVR: Show mirror of single eye only
[mercenary-reloaded.git] / src / libovr / ovr.c
index bfab6c4..8a9007f 100755 (executable)
@@ -101,7 +101,7 @@ static int mirror_height;
 static ovrEyeRenderDesc eyeRenderDesc[2];
 static ovrPosef hmdToEyeViewPose[2];
 static ovrPosef headPose;
-static ovrPosef handPoses[2];
+static ovrPosef handPose;
 static ovrInputState inputState;
 static ovrLayerEyeFov layer;
 static int multisampling;
@@ -209,10 +209,11 @@ int init_ovr(int _multisampling)
        ovrMirrorTextureDesc desc;
        memset(&desc, 0, sizeof(desc));
        desc.Width = mirror_width = TextureSize[0].w;
-       desc.Height = mirror_height = TextureSize[0].h / 2;
+       desc.Height = mirror_height = TextureSize[0].h;
        desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
        desc.MirrorOptions =
-               ovrMirrorOption_PostDistortion |
+//             ovrMirrorOption_PostDistortion |
+               ovrMirrorOption_LeftEyeOnly |
                ovrMirrorOption_IncludeGuardian |
                ovrMirrorOption_IncludeNotifications |
                ovrMirrorOption_IncludeSystemGui;
@@ -245,6 +246,8 @@ void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, i
        ovrResult result;
        float yaw, pitch, roll;
        double x, y, z;
+       unsigned int hand_mask = (ovrStatus_OrientationTracked | ovrStatus_PositionTracked);
+       unsigned int hand_flags = (ovrStatus_OrientationTracked | ovrStatus_PositionTracked);
 
        /* Get both eye poses simultaneously, with IPD offset already included. */
        double displayMidpointSeconds = ovr_GetPredictedDisplayTime(session, frameIndex);
@@ -252,17 +255,14 @@ void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, i
        ovr_CalcEyePoses(hmdState.HeadPose.ThePose, hmdToEyeViewPose, layer.RenderPose);
        /* Grab hand poses useful for rendering head/hand or controller representation */
        headPose = hmdState.HeadPose.ThePose;
-       if (hmdState.HandStatusFlags[ovrHand_Left] == (ovrStatus_OrientationTracked | ovrStatus_PositionTracked))
-               handPoses[ovrHand_Left]  = hmdState.HandPoses[ovrHand_Left].ThePose;
-       else
-               handPoses[ovrHand_Left] = headPose;
-       if (hmdState.HandStatusFlags[ovrHand_Right] == (ovrStatus_OrientationTracked | ovrStatus_PositionTracked))
-               handPoses[ovrHand_Right] = hmdState.HandPoses[ovrHand_Right].ThePose;
-       else
-               handPoses[ovrHand_Right] = headPose;
-       x = handPoses[ovrHand_Right].Position.x;
-       y = handPoses[ovrHand_Right].Position.y;
-       z = handPoses[ovrHand_Right].Position.z;
+       handPose = headPose;
+//     if ((hmdState.HandStatusFlags[ovrHand_Left] & hand_mask) == hand_flags)
+//             handPose = hmdState.HandPoses[ovrHand_Left].ThePose;
+       if ((hmdState.HandStatusFlags[ovrHand_Right] & hand_mask) == hand_flags)
+               handPose = hmdState.HandPoses[ovrHand_Right].ThePose;
+       x = handPose.Position.x;
+       y = handPose.Position.y;
+       z = handPose.Position.z;
        x += hand_x_reset;
        y += hand_y_reset;
        z += hand_z_reset;
@@ -273,7 +273,7 @@ void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, i
        *head_yaw = yaw;
        *head_pitch = pitch;
        *head_roll = roll;
-       ovrOrientation2yawpitchroll(handPoses[ovrHand_Right].Orientation, &yaw, &pitch, &roll);
+       ovrOrientation2yawpitchroll(handPose.Orientation, &yaw, &pitch, &roll);
        *hand_right_yaw = yaw;
        *hand_right_pitch = pitch;
        *hand_right_roll = roll;
@@ -335,6 +335,8 @@ void begin_render_ovr(void)
                print_info("Failed to begin frame (error %d)\n", result);
 }
 
+static int initial_observer_reset = 1;
+
 void begin_render_ovr_eye(int eye, double *camera_x, double *camera_y, double *camera_z)
 {
        int curIndex;
@@ -365,10 +367,14 @@ void begin_render_ovr_eye(int eye, double *camera_x, double *camera_y, double *c
 
        /* reset to game's observer, if requrested by user */
        observer_x = x;
-       x += observer_x_reset;
        observer_y = y;
-       y += observer_y_reset;
        observer_z = z;
+       if (initial_observer_reset) {
+               initial_observer_reset = 0;
+               reset_observer_ovr();
+       }
+       x += observer_x_reset;
+       y += observer_y_reset;
        z += observer_z_reset;
 
        glRotatef(-roll / M_PI * 180.0,0,0,1);
@@ -413,22 +419,22 @@ void end_render_ovr(void)
 void render_mirror_ovr(int view_width, int view_height)
 {
        int view_x = 0, view_y = 0;
-       int new_width, new_height;
+       int new_height; //, new_width;
 
        /* avoid division by zero, if one dimension is too small */
        if (view_width < 1 || view_height < 1)
                return;
 
        /* calculate a viewport that has apect of TextureSize */
-       if (view_height * mirror_width > view_width * mirror_height) {
+//     if (view_height * mirror_width > view_width * mirror_height) {
                new_height = view_width * mirror_height / mirror_width;
                view_y = view_y + view_height / 2 - new_height / 2;
                view_height = new_height;
-       } else if (view_height * mirror_width < view_width * mirror_height) {
-               new_width = view_height * mirror_width / mirror_height;
-               view_x = view_x + view_width / 2 - new_width / 2;
-               view_width = new_width;
-       }
+//     } else if (view_height * mirror_width < view_width * mirror_height) {
+//             new_width = view_height * mirror_width / mirror_height;
+//             view_x = view_x + view_width / 2 - new_width / 2;
+//             view_width = new_width;
+//     }
 
        /* avoid views that are too small */
        if (view_width < 1 || view_height < 1)
@@ -444,7 +450,7 @@ void render_mirror_ovr(int view_width, int view_height)
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
        glBlitFramebuffer(0, mirror_height, mirror_width, 0,
                view_x, view_y, view_x + view_width, view_y + view_height,
-               GL_COLOR_BUFFER_BIT, GL_NEAREST);
+               GL_COLOR_BUFFER_BIT, GL_LINEAR);
        glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
 }
 
@@ -458,6 +464,18 @@ void reset_observer_ovr(void)
        hand_z_reset = -observer_z;
 }
 
+int should_quit_ovr(void)
+{
+       ovrSessionStatus sessionStatus;
+       ovrResult result;
+
+       result = ovr_GetSessionStatus(session, &sessionStatus);
+
+       if (OVR_SUCCESS(result))
+               return sessionStatus.ShouldQuit;
+       return 0;
+}
+
 void exit_ovr(void)
 {
        int eye;