【问题标题】:Turning a C++ Application into VR [duplicate]将 C++ 应用程序转换为 VR [重复]
【发布时间】:2019-11-18 22:01:23
【问题描述】:

我有一个使用 openGL 和 Cuda 的 C++ 应用程序,并希望将其转换为 VR 应用程序。我搜索了互联网,似乎开发 VR 应用程序的标准方法是使用游戏引擎(统一或虚幻)。有没有其他可能的方法来避免使用其中一种,而只是将我现有的应用程序转换为 VR?

【问题讨论】:

  • VR 没有真正的秘密:只需将同一场景渲染两次,其中一台摄像机稍微偏移另一台摄像机。使用您的 VR 设备的供应商 SDK 获取头显旋转和位置信息。如果您想进行物理交互,请使用 SDK 的输入法获取 3D 位置。 Unreal 和 Unity 为您提供了这一切,但没有人会阻止您自己实现。

标签: game-engine virtual-reality


【解决方案1】:

通常的方法是,在开始渲染一帧时,轮询 VR API 以获取每只眼睛的投影和“相机”转换矩阵,然后使用它,使用 FBO 渲染到纹理中,然后在最后将这些纹理的 ID 传递回 API。使用 OpenVR:

初始化 FBO

vr::EVRInitError err_hmd = vr::VRInitError_None;
m_hmd = vr::VR_Init(&err_hmd, vr::VRApplication_Scene);
if( err_hmd != vr::VRInitError_None ){
    m_hmd = nullptr;
}
if( m_hmd ){
    m_hmd->GetRecommendedRenderTargetSize(&m_hmd_width, &m_hmd_height);
    mat44_from_hmd44(m_prj_l, m_hmd->GetProjectionMatrix(vr::Eye_Left,  0.01f, 10.f) );
    mat44_from_hmd44(m_prj_r, m_hmd->GetProjectionMatrix(vr::Eye_Right, 0.01f, 10.f) );

    mat44_from_hmd34(m_eye_l, m_hmd->GetEyeToHeadTransform(vr::Eye_Left)  );
    mat44_from_hmd34(m_eye_r, m_hmd->GetEyeToHeadTransform(vr::Eye_Right) );

    if( !vr::VRCompositor() ){
        vr::VR_Shutdown();
        m_hmd = nullptr;
    }

    m_timer_vr = startTimer(1000/50);
} else {
}

if( m_hmd_width && m_hmd_height ){
    qDebug() << "resize to" << m_hmd_width << m_hmd_height;
    eye_target_textures.create(m_hmd_width, m_hmd_height);
}

更新 HMD 姿势:

vr::VREvent_t vrev;
while( m_hmd->PollNextEvent(&vrev, sizeof(vrev)) );

// Process SteamVR action state
// UpdateActionState is called each frame to update the state of the actions themselves. The application
// controls which action sets are active with the provided array of VRActiveActionSet_t structs.
vr::VRActiveActionSet_t actionSet = { 0 };
vr::VRInput()->UpdateActionState( &actionSet, sizeof(actionSet), 1 );

vr::TrackedDevicePose_t tdp[ vr::k_unMaxTrackedDeviceCount ];
vr::VRCompositor()->WaitGetPoses(tdp, vr::k_unMaxTrackedDeviceCount, NULL, 0);

mat4x4_translate(m_pose, 0, 0, -1);

for( int i = 0; i < vr::k_unMaxTrackedDeviceCount; ++i ){
    if( !tdp[i].bPoseIsValid ){ continue; }

    switch( m_hmd->GetTrackedDeviceClass(i) ){
    case vr::TrackedDeviceClass_HMD:
        mat44_from_hmd34(m_pose, tdp[i].mDeviceToAbsoluteTracking );
        break;
    }
}

然后在渲染时使用m_posem_eye… 矩阵。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多