Ejemplo n.º 1
0
OSVR_ReturnCode osvrClientSetRoomRotationUsingHead(OSVR_ClientContext ctx) {
    if (nullptr == ctx) {
        /// Return failure if given a null context
        return OSVR_RETURN_FAILURE;
    }
    auto xform = ctx->getRoomToWorldTransform();

    auto iface = osvr::client::InternalInterfaceOwner{ctx, "/me/head"};
    OSVR_OrientationState state;
    OSVR_TimeValue t;
    /// Give ourselves 200 milliseconds to get a head tracker report.
    using std::chrono::system_clock;
    const auto deadline = system_clock::now() + std::chrono::milliseconds(200);
    do {
        if (OSVR_RETURN_SUCCESS ==
            osvrGetOrientationState(&(*iface), &t, &state)) {
            // OK, we've gotten state successfully: extract yaw, update
            // transform, and return success.
            auto q = osvr::util::eigen_interop::map(state);
            auto yaw = osvr::util::extractYaw(q);
            Eigen::AngleAxisd correction(-yaw, Eigen::Vector3d::UnitY());
            xform.concatPost(Eigen::Isometry3d(correction).matrix());
            ctx->setRoomToWorldTransform(xform);
            return OSVR_RETURN_SUCCESS;
        }
        ctx->update();
    } while (system_clock::now() < deadline);
    return OSVR_RETURN_FAILURE;
}