c++ - Oculus Rift + 点 Sprite + 点大小衰减

标签 c++ opengl oculus virtual-reality point-sprites

我正在编写一个支持 Oculus Rfit 的小项目,我使用点 Sprite 来渲染我的粒子。我根据点 Sprite 与顶点着色器中“相机”的距离计算点 Sprite 的大小(以像素为单位)。在默认屏幕上(不是在 Rift 上)绘图时,尺寸完美,但当我切换到 Rift 时,我注意到这些现象:

The particles on the Left Eye are small and get reduced in size very rapidly. The particles on the Right Eye are huge and do not change in size.

截图: 裂谷禁用:http://i.imgur.com/EoguiF0.jpg 裂谷启用:http://i.imgur.com/4IcBCf0.jpg

这是顶点着色器:

#version 120

attribute vec3 attr_pos;
attribute vec4 attr_col;
attribute float attr_size;

uniform mat4 st_view_matrix;
uniform mat4 st_proj_matrix;
uniform vec2 st_screen_size;

varying vec4 color;

void main()
{
    vec4 local_pos = vec4(attr_pos, 1.0);
    vec4 eye_pos = st_view_matrix * local_pos;
    vec4 proj_vector = st_proj_matrix * vec4(attr_size, 0.0, eye_pos.z, eye_pos.w);
    float proj_size = st_screen_size.x * proj_vector.x / proj_vector.w;

    gl_PointSize = proj_size;
    gl_Position = st_proj_matrix * eye_pos;

    color = attr_col;
}

st_screen_size 统一是视口(viewport)的大小。由于我在 Rift 上渲染时使用单个 frambuffer(每只眼睛一半),st_screen_size 的值应该是(frabuffer_width/2.0,frambuffer_height)。

这是我的绘制调用:

    /*Drawing starts with a call to ovrHmd_BeginFrame.*/
    ovrHmd_BeginFrame(game::engine::ovr_data.hmd, 0);

    /*Start drawing onto our texture render target.*/
    game::engine::ovr_rtarg.bind();
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   //Update the particles.
    game::engine::nuc_manager->update(dt, get_msec());

    /*for each eye... */
    for(unsigned int i = 0 ; i < 2 ; i++){
        ovrEyeType eye = game::engine::ovr_data.hmd->EyeRenderOrder[i];
        /* -- Viewport Transformation --
         * Setup the viewport to draw in the left half of the framebuffer when we're
         * rendering the left eye's view (0, 0, width / 2.0, height), and in the right half
         * of the frambuffer for the right eye's view (width / 2.0, 0, width / 2.0, height)
         */
        int fb_width = game::engine::ovr_rtarg.get_fb_width();
        int fb_height = game::engine::ovr_rtarg.get_fb_height();

        glViewport(eye == ovrEye_Left ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);

      //Send the Viewport size to the shader.
      set_unistate("st_screen_size", Vector2(fb_width /2.0 , fb_height));

        /* -- Projection Transformation --
         * We'll just have to use the projection matrix supplied but he oculus SDK for this eye.
         * Note that libovr matrices are the transpose of what OpenGL expects, so we have to
         * send the transposed ovr projection matrix to the shader.*/
        proj = ovrMatrix4f_Projection(game::engine::ovr_data.hmd->DefaultEyeFov[eye], 0.01, 40000.0, true);

      Matrix4x4 proj_mat;
      memcpy(proj_mat[0], proj.M, 16 * sizeof(float));

      //Send the Projection matrix to the shader.
      set_projection_matrix(proj_mat);

        /* --view/camera tranformation --
         * We need to construct a view matrix by combining all the information provided by
         * the oculus SDK, about the position and orientation of the user's head in the world.
         */
         pose[eye] = ovrHmd_GetHmdPosePerEye(game::engine::ovr_data.hmd, eye);

         camera->reset_identity();

         camera->translate(Vector3(game::engine::ovr_data.eye_rdesc[eye].HmdToEyeViewOffset.x,
          game::engine::ovr_data.eye_rdesc[eye].HmdToEyeViewOffset.y,
          game::engine::ovr_data.eye_rdesc[eye].HmdToEyeViewOffset.z));

         /*Construct a quaternion from the data of the oculus SDK and rotate the view matrix*/
         Quaternion q = Quaternion(pose[eye].Orientation.w, pose[eye].Orientation.x,
                                   pose[eye].Orientation.y, pose[eye].Orientation.z);
         camera->rotate(q.inverse().normalized());


         /*Translate the view matrix with the positional tracking*/
         camera->translate(Vector3(-pose[eye].Position.x, -pose[eye].Position.y, -pose[eye].Position.z));

       camera->rotate(Vector3(0, 1, 0), DEG_TO_RAD(theta));

       //Send the View matrix to the shader.
       set_view_matrix(*camera);



         game::engine::active_stage->render(STAGE_RENDER_SKY | STAGE_RENDER_SCENES | STAGE_RENDER_GUNS |
          STAGE_RENDER_ENEMIES | STAGE_RENDER_PROJECTILES, get_msec());
         game::engine::nuc_manager->render(RENDER_PSYS, get_msec());
       game::engine::active_stage->render(STAGE_RENDER_COCKPIT, get_msec());
    }

    /* After drawing both eyes into the texture render target, revert to drawing directly to the display,
     * and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly, compensated for lens
     * distortion and chromatic abberation onto the HMD screen.
     */
    game::engine::ovr_rtarg.unbind();

    ovrHmd_EndFrame(game::engine::ovr_data.hmd, pose, &game::engine::ovr_data.fb_ovr_tex[0].Texture);

这个问题已经困扰我好几天了……我觉得我已经走到了死胡同。我可以只使用广告牌四边形.....但我不想轻易放弃 :) 加点 Sprite 更快。 在 Rift 上渲染时,基于距离变化的点大小衰减背后的数学原理是什么? 是不是没有考虑到什么? 数学不是(但至少)我的强项。 :) 任何见解将不胜感激!

PS:如果需要有关我发布的代码的任何其他信息,我会很乐意提供。

最佳答案

我可以推荐一些故障排除技巧。

首先,修改您的代码以自动编写渲染的第一帧的屏幕截图(或者,如果这不方便,只需使用一个静态 bool 值,它会导致主绘图跳过除第一帧之后的开始/结束帧调用之外的所有内容SDK 有时会弄乱 OpenGL 状态机,如果发生这种情况,那么您所看到的可能是在 ovrHmd_EndFrame() 中完成的工作的结果,在随后通过渲染循环的过程中搞砸了渲染。其他在您的渲染代码中(在粒子渲染之后)可能会无意中恢复所需的状态,这就是渲染的第二只眼睛看起来不错的原因。

其次,我会尝试将渲染的眼睛分成两个帧缓冲区。也许您的代码中有些东西意外地对整个帧缓冲区做了一些事情(比如清除深度缓冲区),这导致了差异。根据顶级代码,您可能正在使用与您预期的帧缓冲区不同的状态运行第二只眼睛。分成两个帧缓冲区会告诉您是否是这种情况。

您可能会运行的另一项测试与第二项测试类似,它是重构您的渲染代码以允许您使用默认帧缓冲区通过此循环,而无需调用 Oculus SDK。这是另一种技术,可帮助您确定问题是出在 SDK 中还是出在您自己的呈现代码中。只需将两个眼睛 View 渲染到屏幕的两半,而不是屏幕外帧缓冲区的两半。

关于c++ - Oculus Rift + 点 Sprite + 点大小衰减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28100073/

相关文章:

c++ - Qt3D QAbstractTextureImage 像素化

c++ - 哈希函数

opengl - 在 OpenGL 中使用 gluPerspective

c++ - 我怎样才能使用 GLU_RGBA 或其他 GLU_ 参数?

c# - 为双边移动统一镜像 Oculus Rift Controller 位置

c++ - 结构、函数和数组

c++ - 使用 clang 编译失败,出现 libstdc++4.4.7 和 -std=c++0x

c# - DirectX 或 OpenGL

c++ - Oculus OVR_CAPI.cpp 错误

performance - 为什么纹理查找比直接计算慢得多?