Oculus Rift + 点精灵 + 点大小衰减
Oculus Rift + Point Sprites + Point size attenuation
我正在编写一个支持 Oculus Rfit 的小项目,我使用点精灵来渲染我的粒子。我根据点精灵与顶点着色器中 "camera" 的距离计算点精灵的大小(以像素为单位)。在默认屏幕上(不是在 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统一是视口的大小。由于我在 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);
这个问题已经困扰我好几天了...我觉得我已经走到了死胡同。我可以只使用广告牌四边形.....但我不想轻易放弃 :) 加点精灵更快。
在 Rift 上渲染时,基于距离变化的点大小衰减背后的数学原理是什么?
是不是没有考虑到什么?
数学不是(但至少)我的强项。 :) 任何见解将不胜感激!
PS:如果需要有关我发布的代码的任何其他信息,我很乐意提供。
我可以推荐一些故障排除技巧。
首先,修改您的代码以自动编写渲染的第一帧的屏幕截图(或者如果这不方便,只需使用一个静态布尔值,使主绘图跳过 begin/end 帧以外的所有内容在第一个 运行 通过后调用。SDK 有时会弄乱 OpenGL 状态机,如果发生这种情况,那么您所看到的可能是 ovrHmd_EndFrame() 中完成的工作搞砸了的结果您在后续通过渲染循环时的渲染。渲染代码中的其他内容(在粒子渲染之后)可能会无意中恢复所需的状态,这就是渲染的第二只眼睛看起来不错的原因。
其次,我会尝试将渲染的眼睛分成两个帧缓冲区。也许您的代码中有些东西意外地对整个帧缓冲区做了一些事情(比如清除深度缓冲区),这导致了差异。您可能 运行 通过第二只眼睛看到帧缓冲区的状态与您根据顶级代码所期望的不同。分成两个帧缓冲区会告诉您是否是这种情况。
您可能会进行的另一项测试 运行,与第二项类似,是重构您的渲染代码,以允许您使用默认帧缓冲区通过此循环,而无需调用 Oculus SDK。这是另一种技术,可帮助您确定问题是出在 SDK 中还是出在您自己的呈现代码中。只需将两个眼睛视图渲染到屏幕的两半,而不是屏幕外帧缓冲区的两半。
vec4 local_pos = vec4(attr_pos, 1.0);
vec4 eye_pos = st_view_matrix * local_pos;
vec4 proj_voxel = st_proj_matrix * vec4(attr_size, 0.0, eye_pos.z, eye_pos.w);
float proj_size = st_screen_size.x * proj_voxel.x / proj_voxel.w;</p>
<p>gl_PointSize = proj_size;</pre>
基本上,您首先要将点转换为视图 space 以确定它在视图中的 Z 坐标 space(与观察者的距离),然后构建一个与 X 对齐的向量具有所需粒子大小的轴,并将其投影以查看在投影和视口变换(sortof)时它覆盖了多少像素。
假设您的投影矩阵是对称的,这是完全合理的。在处理裂缝时,这种假设是错误的。我画了一张图来更好地说明问题:
http://i.imgur.com/vm33JUN.jpg
如您所见,当平截头体不对称时(裂缝肯定就是这种情况),使用投影点与屏幕中心的距离将为每只眼睛提供截然不同的值,当然与您要查找的 "correct" 投影大小不同。
您必须改为投影两个点,例如 (0, 0, z, 1) AND (attr_size, 0, z, 1),使用相同的方法,并计算它们的差值在屏幕中 space(在投影、透视划分和视口之后)。
我正在编写一个支持 Oculus Rfit 的小项目,我使用点精灵来渲染我的粒子。我根据点精灵与顶点着色器中 "camera" 的距离计算点精灵的大小(以像素为单位)。在默认屏幕上(不是在 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统一是视口的大小。由于我在 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);
这个问题已经困扰我好几天了...我觉得我已经走到了死胡同。我可以只使用广告牌四边形.....但我不想轻易放弃 :) 加点精灵更快。 在 Rift 上渲染时,基于距离变化的点大小衰减背后的数学原理是什么? 是不是没有考虑到什么? 数学不是(但至少)我的强项。 :) 任何见解将不胜感激!
PS:如果需要有关我发布的代码的任何其他信息,我很乐意提供。
我可以推荐一些故障排除技巧。
首先,修改您的代码以自动编写渲染的第一帧的屏幕截图(或者如果这不方便,只需使用一个静态布尔值,使主绘图跳过 begin/end 帧以外的所有内容在第一个 运行 通过后调用。SDK 有时会弄乱 OpenGL 状态机,如果发生这种情况,那么您所看到的可能是 ovrHmd_EndFrame() 中完成的工作搞砸了的结果您在后续通过渲染循环时的渲染。渲染代码中的其他内容(在粒子渲染之后)可能会无意中恢复所需的状态,这就是渲染的第二只眼睛看起来不错的原因。
其次,我会尝试将渲染的眼睛分成两个帧缓冲区。也许您的代码中有些东西意外地对整个帧缓冲区做了一些事情(比如清除深度缓冲区),这导致了差异。您可能 运行 通过第二只眼睛看到帧缓冲区的状态与您根据顶级代码所期望的不同。分成两个帧缓冲区会告诉您是否是这种情况。
您可能会进行的另一项测试 运行,与第二项类似,是重构您的渲染代码,以允许您使用默认帧缓冲区通过此循环,而无需调用 Oculus SDK。这是另一种技术,可帮助您确定问题是出在 SDK 中还是出在您自己的呈现代码中。只需将两个眼睛视图渲染到屏幕的两半,而不是屏幕外帧缓冲区的两半。
vec4 local_pos = vec4(attr_pos, 1.0); vec4 eye_pos = st_view_matrix * local_pos; vec4 proj_voxel = st_proj_matrix * vec4(attr_size, 0.0, eye_pos.z, eye_pos.w); float proj_size = st_screen_size.x * proj_voxel.x / proj_voxel.w;</p> <p>gl_PointSize = proj_size;</pre>
基本上,您首先要将点转换为视图 space 以确定它在视图中的 Z 坐标 space(与观察者的距离),然后构建一个与 X 对齐的向量具有所需粒子大小的轴,并将其投影以查看在投影和视口变换(sortof)时它覆盖了多少像素。
假设您的投影矩阵是对称的,这是完全合理的。在处理裂缝时,这种假设是错误的。我画了一张图来更好地说明问题:
http://i.imgur.com/vm33JUN.jpg
如您所见,当平截头体不对称时(裂缝肯定就是这种情况),使用投影点与屏幕中心的距离将为每只眼睛提供截然不同的值,当然与您要查找的 "correct" 投影大小不同。
您必须改为投影两个点,例如 (0, 0, z, 1) AND (attr_size, 0, z, 1),使用相同的方法,并计算它们的差值在屏幕中 space(在投影、透视划分和视口之后)。