矩阵投影

摄像机的矩阵


https://www.cnblogs.com/w-wfy/p/7243459.html

//关于矩阵我建议还是看一下上面这个网址 理解一下什么是unity里的矩阵


代码如下:




       static Matrix4x4 PerspectiveOffCenter(float left, float right, float bottom, float top, float near, float far)

       {

           float x = 2.0F * near / (right - left);

           float y = 2.0F * near / (top - bottom);

           float a = (right + left) / (right - left);

           float b = (top + bottom) / (top - bottom);

           float c = -(far + near) / (far - near);

           float d = -(2.0F * far * near) / (far - near);

           float e = -1.0F;

           Matrix4x4 m = new Matrix4x4();

           m[0, 0] = x;

           m[0, 1] = 0;

           m[0, 2] = a;

           m[0, 3] = 0;

           m[1, 0] = 0;

           m[1, 1] = y;

           m[1, 2] = b;

           m[1, 3] = 0;

           m[2, 0] = 0;

           m[2, 1] = 0;

           m[2, 2] = c;

           m[2, 3] = d;

           m[3, 0] = 0;

           m[3, 1] = 0;

           m[3, 2] = e;

           m[3, 3] = 0;

           return m;

       }

void UpdateLeftRightEyeCameraAndProjMatrix()

       {

           //更新投影矩阵

           //左眼

           float Cam_y = Left_Cam.transform.position.y;

           float left = (-x_left - Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;

           float right = (-x_left + Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;

           float top = (-z_left + Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;

           float bottom = (-z_left - Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;


           Matrix4x4 m = PerspectiveOffCenter(left, right, bottom, top, Left_Cam.nearClipPlane, Left_Cam.farClipPlane);


           Left_Cam.projectionMatrix = m;



           //主摄像机 右眼

           Cam_y = Right_Cam.transform.position.y;

           left = (-x_right - Len_Rect / 2) / Cam_y * Right_Cam.nearClipPlane;

           right = (-x_right + Len_Rect / 2) / Cam_y * Right_Cam.nearClipPlane;

           top = (-z_right + Len_Rect / 2) / Cam_y * Right_Cam.nearClipPlane;

           bottom = (-z_right - Len_Rect / 2) / Cam_y * Right_Cam.nearClipPlane;


           m = PerspectiveOffCenter(left, right, bottom, top, Right_Cam.nearClipPlane, Right_Cam.farClipPlane);


           Right_Cam.projectionMatrix = m;

       }



说一下  以左眼为例子

//左眼

           float Cam_y = Left_Cam.transform.position.y;

           float left = (-x_left - Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;

           float right = (-x_left + Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;

           float top = (-z_left + Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;

           float bottom = (-z_left - Len_Rect / 2) / Cam_y * Left_Cam.nearClipPlane;


Matrix4x4 m = PerspectiveOffCenter(left, right, bottom, top, Left_Cam.nearClipPlane, Left_Cam.farClipPlane);


           Left_Cam.projectionMatrix = m;


-x_Left是我左眼的坐标的x值,Len_Rect是我矩阵投影的边长,计算完再乘一个比例就是我近切面的左边坐标,right top bottom 相同,关系如下图:求float left

这样两个摄像头的矩阵投影是重合的


### 旋转矩阵 在三维空间中,旋转矩阵是一种用于描述物体围绕某个坐标轴进行旋转的工具。通过将多个基本旋转矩阵按特定顺序相乘,可以得到一个组合旋转矩阵[^2]。例如,如果需要实现依次绕 \(z\) 轴、\(y\) 轴和 \(x\) 轴的旋转,则最终的组合旋转矩阵可由这三个基础旋转矩阵相乘得出。 对于任意给定的向量 \(\vec{v}\),将其左乘该组合旋转矩阵 \(R\) 即可完成相应的旋转变换操作。这种基于矩阵的方式不仅简化了复杂几何变换的过程,还使得程序设计更加高效与直观。 #### 组合旋转矩阵示例 假设分别定义了绕各主要坐标轴的标准旋转子矩阵: \[ R_z(\theta) = \begin{bmatrix} \cos{\theta} & -\sin{\theta} & 0 \\ \sin{\theta} & \cos{\theta} & 0 \\ 0 & 0 & 1 \end{bmatrix}, R_y(\phi)=... \] 完整的三步连续旋转表达式为: ```python import numpy as np def rotation_matrix(theta, phi, psi): theta_rad = np.radians(theta) phi_rad = np.radians(phi) psi_rad = np.radians(psi) Rx = np.array([[1, 0, 0], [0, np.cos(psi_rad), -np.sin(psi_rad)], [0, np.sin(psi_rad), np.cos(psi_rad)]]) Ry = ... ``` 上述代码片段展示了如何构建一个通用的三维旋转函数 `rotation_matrix` 来生成所需的旋转矩阵。 --- ### 投影矩阵 (Projection Matrix) 投影矩阵主要用于从三维场景映射至二维屏幕的过程中保留必要的视觉信息并丢弃无关维度的数据。它分为两大类:正交投影(Orthographic Projection) 和透视投影(Perspective Projection)[^4]。 - **正交投影**: 它保持平行线不汇聚的特点,在工程制图等领域广泛采用; - **透视投影**: 更接近人类眼睛观察世界的真实效果——近大远小原则被体现出来。其核心原理在于模拟镜头焦距作用下的缩放效应以及深度裁剪范围设定等参数调整[^3]。 具体来说,为了创建标准的OpenGL兼容型透视投影矩阵P_persp,通常会涉及以下几个关键变量设置: 1. Field Of View Angle (\(fovY\)): 垂直视野角度大小; 2. Aspect Ratio: 屏幕宽高比例关系; 3. Near Plane Distance (\(nearClip\)) and Far Plane Distance (\(farClip\)): 指定可见距离区间边界值. 以下是利用Python+Numpy库实现的一个简单的透视投影矩阵构造器: ```python from math import tan, pi def perspective_projection(fovy_degrees=90., aspect_ratio=16./9., near_clip=.1, far_clip=100.): fovy_radians = fovy_degrees * pi / 180. tangent_half_fov = tan(fovy_radians / 2.) P_persp = np.zeros((4, 4)) P_persp[0][0] = 1./(aspect_ratio * tangent_half_fov) P_persp[1][1] = 1./tangent_half_fov P_persp[2][2] = -(far_clip + near_clip)/(far_clip - near_clip) P_persp[2][3] = (-2.*far_clip*near_clip)/(far_clip-near_clip) P_persp[3][2] = -1. return P_persp ``` 此段脚本提供了灵活定制化选项以便适应不同应用场景需求的同时也遵循现代GPU管线架构下普遍接受的形式规范. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值