如何求解镜面反射后的虚拟相机(需保留四元数Roll属性)
问题:求解镜面反射后的虚拟相机(保留Roll属性)
现有一相机,用四元数[w, x, y, z]和位置[x, y, z]表示,该相机朝向由法向量和面上一点定义的镜面(也可表示为平面方程AX + BY + CZ + D = 0)。需要求解该相机经镜面反射后的虚拟相机,要求保留四元数的Roll属性——例如直视镜面时,反射后的四元数视角应保持相同Roll,不会上下颠倒。
可以通过射线与平面相交法求解虚拟相机位置,但不确定该方法对求解四元数是否有帮助。以下为已实现的虚拟相机位置求解代码(相比射线追踪法,该方法更适配平行或背向镜面的场景):
// 平面方程 ax + by + cz + d = 0 float plane_equation[4]; // 存储a, b, c, d plane_equation[0] = mirror_normal[0]; plane_equation[1] = mirror_normal[1]; plane_equation[2] = mirror_normal[2]; float mults[3]; mults[0] = plane_equation[0] * mirror_position[0]; mults[1] = plane_equation[1] * mirror_position[1]; mults[2] = plane_equation[2] * mirror_position[2]; plane_equation[3] = -1 * (mults[0] + mults[1] + mults[2]); // 利用平面方程求解反射后的虚拟相机位置 // 推导过程: // a(p0 + aT) + b(p1 + bT) + c(p2 + cT) + d = 0 // 展开得:ap0 + bp1 + cp2 + T(a² + b² + c²) + d = 0 // 令U = ap0 + bp1 + cp2,V = a² + b² + c²,则U + TV + d = 0 // 解得T = (-d - U) / V float t_parts[2]; // U:相机位置代入平面方程前三项的结果 t_parts[0] = plane_equation[0] * cam_pos[0] + plane_equation[1] * cam_pos[1] + plane_equation[2] * cam_pos[2]; // V:平面法向量的模长平方 t_parts[1] = plane_equation[0] * plane_equation[0] + plane_equation[1] * plane_equation[1] + plane_equation[2] * plane_equation[2]; float t = (-plane_equation[3] - t_parts[0]) / t_parts[1]; // t为相机到镜面交点的参数,2t得到虚拟相机位置(沿法向量方向移动2t距离) virtual_cam_pos[0] = 2 * t * plane_equation[0] + cam_pos[0]; virtual_cam_pos[1] = 2 * t * plane_equation[1] + cam_pos[1]; virtual_cam_pos[2] = 2 * t * plane_equation[2] + cam_pos[2];
内容的提问来源于stack exchange,提问作者user11428571
相关产品推荐
相关产品推荐

