mainLoop hmd glfwHdl glhdl (eyeTexture,texobj,fbo) eyeRD tex frameNo = do
  pollGLFW
  --threadDelay   10000
  --threadDelay 1000
  --threadDelay 1000000
  dt <- getDeltTime glfwHdl
  exitflg' <- getExitReqGLFW glfwHdl

  --ts <- ovrHmd_GetTrackingState hmd =<< ovr_GetTimeInSeconds
  --traceIO $ show ts
  ovrHmd_BeginFrame hmd frameNo
  bindFramebuffer Framebuffer $= fbo
  let (winW,winH) = winSize
  withViewport (Position 0 0) (Size (fromIntegral winW) (fromIntegral winH)) $
    clear [GL.ColorBuffer, GL.DepthBuffer]
  (poseL:poseR:_) <- ovrHmd_GetEyePoses hmd frameNo $ map hmdToEyeViewOffset eyeRD 
  renderPose <- forM [(ovrEye_Left,0,poseL),(ovrEye_Right,1,poseR)]
                  $ \ (eyeType,i,pose) -> do
    --pose <- ovrHmd_GetHmdPosePerEye hmd eyeType
    (OvrMatrix4f m) <- ovrMatrix4f_Projection (fov (eyeRD !! i)) 0.1 20 True
    let pm = (\ [v1,v2,v3,v4] -> V4 v1 v2 v3 v4) $
                 map (\ l -> (\ [a,b,c,d] -> V4 a b c d) $ map realToFrac l ) m
        (OvrQuatf qx qy qz qw) = orientation pose
    --traceIO $ "pose : " ++ (show eyeType) ++ " : " ++ (show pose)
    --textureBinding Texture2D $= Just texobj
    let fov' = fov $ head eyeRD 
        vPos = if eyeType == ovrEye_Left
                then Position 0 0
                else Position 1182 0
    withViewport vPos (Size 1182 1461) $ render glhdl tex pm (qx,qy,qz,qw)
    flush
    return pose
  bindFramebuffer Framebuffer $= defaultFramebufferObject      
  --traceIO $ "renderPose = " ++ (show renderPose) 
  --traceIO $ "eyeTexture = " ++ (show eyeTexture) 
  ovrHmd_EndFrame hmd renderPose eyeTexture
  --swapBuff glfwHdl
  --msg <- ovrHmd_GetLastError hmd
  --traceIO $ "GetLastError 3 = " ++ msg ++ " Msg End"
  --printError
  if exitflg' 
    then return ()
    else mainLoop hmd glfwHdl glhdl (eyeTexture,texobj,fbo) eyeRD tex (frameNo + 1)
mainLoop hmd glfwHdl glhdl (eyeTexture,texobj,fbo) eyeRD tex frameNo = do
  pollGLFW
  --threadDelay   10000
  --threadDelay 1000
  --threadDelay 1000000
  dt <- getDeltTime glfwHdl
  exitflg' <- getExitReqGLFW glfwHdl

  --ts <- ovrHmd_GetTrackingState hmd =<< ovr_GetTimeInSeconds
  --traceIO $ show ts
  ovrHmd_BeginFrame hmd frameNo
  bindFramebuffer Framebuffer $= fbo
  let (winW,winH) = winSize
  withViewport (Position 0 0) (Size (fromIntegral winW) (fromIntegral winH)) $
    clear [GL.ColorBuffer, GL.DepthBuffer]
  (poseL:poseR:_) <- ovrHmd_GetEyePoses hmd frameNo $ map hmdToEyeViewOffset eyeRD 
  renderPose <- forM [(ovrEye_Left,0,poseL),(ovrEye_Right,1,poseR)]
                  $ \ (eyeType,i,pose) -> do
    --pose <- ovrHmd_GetHmdPosePerEye hmd eyeType
    (OvrMatrix4f m) <- ovrMatrix4f_Projection (fov (eyeRD !! i)) 0.1 20 True
    let pm = (\ [v1,v2,v3,v4] -> V4 v1 v2 v3 v4) $
                 map (\ l -> (\ [a,b,c,d] -> V4 a b c d) $ map realToFrac l ) m
        (OvrQuatf qx qy qz qw) = orientation pose
    --traceIO $ "pose : " ++ (show eyeType) ++ " : " ++ (show pose)
    --textureBinding Texture2D $= Just texobj
    let fov' = fov $ head eyeRD 
        vPos = if eyeType == ovrEye_Left
                then Position 0 0
                else Position 1182 0
    withViewport vPos (Size 1182 1461) $ render glhdl tex pm (qx,qy,qz,qw)
    flush
    return pose
  bindFramebuffer Framebuffer $= defaultFramebufferObject      
  --traceIO $ "renderPose = " ++ (show renderPose) 
  --traceIO $ "eyeTexture = " ++ (show eyeTexture) 
  ovrHmd_EndFrame hmd renderPose eyeTexture
  --swapBuff glfwHdl
  --msg <- ovrHmd_GetLastError hmd
  --traceIO $ "GetLastError 3 = " ++ msg ++ " Msg End"
  --printError
  if exitflg' 
    then return ()
    else mainLoop hmd glfwHdl glhdl (eyeTexture,texobj,fbo) eyeRD tex (frameNo + 1)
mainLoop hmd glfwHdl glhdl (eyeTexture,texobj,fbo) eyeRD tex frameNo = do
  pollGLFW
  --threadDelay   10000
  --threadDelay 1000
  --threadDelay 1000000
  dt <- getDeltTime glfwHdl
  exitflg' <- getExitReqGLFW glfwHdl

  --ts <- ovrHmd_GetTrackingState hmd =<< ovr_GetTimeInSeconds
  --traceIO $ show ts
  ovrHmd_BeginFrame hmd frameNo
  bindFramebuffer Framebuffer $= fbo
  let (winW,winH) = winSize
  withViewport (Position 0 0) (Size (fromIntegral winW) (fromIntegral winH)) $
    clear [GL.ColorBuffer, GL.DepthBuffer]
  (poseL:poseR:_) <- ovrHmd_GetEyePoses hmd frameNo $ map hmdToEyeViewOffset eyeRD 
  renderPose <- forM [(ovrEye_Left,0,poseL),(ovrEye_Right,1,poseR)]
                  $ \ (eyeType,i,pose) -> do
    --pose <- ovrHmd_GetHmdPosePerEye hmd eyeType
    (OvrMatrix4f m) <- ovrMatrix4f_Projection (fov (eyeRD !! i)) 0.1 20 True
    let pm = (\ [v1,v2,v3,v4] -> V4 v1 v2 v3 v4) $
                 map (\ l -> (\ [a,b,c,d] -> V4 a b c d) $ map realToFrac l ) m
        (OvrQuatf qx qy qz qw) = orientation pose
    --traceIO $ "pose : " ++ (show eyeType) ++ " : " ++ (show pose)
    --textureBinding Texture2D $= Just texobj
    let fov' = fov $ head eyeRD 
        vPos = if eyeType == ovrEye_Left
                then Position 0 0
                else Position 1182 0
    withViewport vPos (Size 1182 1461) $ render glhdl tex pm (qx,qy,qz,qw)
    flush
    return pose
  bindFramebuffer Framebuffer $= defaultFramebufferObject      
  --traceIO $ "renderPose = " ++ (show renderPose) 
  --traceIO $ "eyeTexture = " ++ (show eyeTexture) 
  ovrHmd_EndFrame hmd renderPose eyeTexture
  --swapBuff glfwHdl
  --msg <- ovrHmd_GetLastError hmd
  --traceIO $ "GetLastError 3 = " ++ msg ++ " Msg End"
  --printError
  if exitflg' 
    then return ()
    else mainLoop hmd glfwHdl glhdl (eyeTexture,texobj,fbo) eyeRD tex (frameNo + 1)