LearnOpenGL 总结记录<4> Camera , View Matrix

// camera/view transformation
        glm::mat4 view = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first
        float radius = 10.0f;
        float camX   = sin(glfwGetTime()) * radius;
        float camZ   = cos(glfwGetTime()) * radius;
        view = glm::lookAt(glm::vec3(camX, 0.0f, camZ), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
        ourShader.setMat4("view", view);

细节

1. Camera Direction

The next vector required is the camera’s direction e.g. at what direction it is pointing at. For now we let the camera point to the origin of our scene: (0,0,0). Remember that if we subtract two vectors from each other we get a vector that’s the difference of these two vectors? Subtracting the camera position vector from the scene’s origin vector thus results in the direction vector. Since we know that the camera points towards the negative z direction we want the direction vector to point towards the camera’s positive z-axis. If we switch the subtraction order around we now get a vector pointing towards the camera’s positive z-axis:

glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraDirection = glm::normalize(cameraPos - cameraTarget);

The name direction vector is not the best chosen name, since it is actually pointing in the reverse direction of what it is targeting.

 

2.

上面的 view 矩阵就是一个 lookAt 函数:

A great thing about matrices is that if you define a coordinate space using 3 perpendicular (or non-linear) axes you can create a matrix with those 3 axes plus a translation vector and you can transform any vector to that coordinate space by multiplying it with this matrix. This is exactly what the LookAt matrix does and now that we have 3 perpendiclar axes and a position vector to define the camera space we can create our own LookAt matrix:

Where R is the right vector, U is the up vector, D is the direction vector and P is the camera’s position vector. Note that the position vector is inverted since we eventually want to translate the world in the opposite direction of where we want to move. Using this LookAt matrix as our view matrix effectively transforms all the world coordinates to the view space we just defined. The LookAt matrix then does exactly what it says: it creates a view matrix that looks at a given target.

(为什么上面的 LookAt 是 等于 View 矩阵,可以参考:

https://blog.csdn.net/popy007/article/details/5120158

个人理解:

要将顶点变换到相机空间,那么理解为就是,把相机的位置移到原点,旋转相机的自身的3个轴都与世间坐标空间的 x,y,z 轴 重合,顶点是相对于Camera的,所以当顶点执行同样的操作之后,顶点就在相机空间了。

1. 相机平移到原点,也就是 执行(平移 -P,P就是相机位置)。

2. 相机坐标轴与世界坐标空间 x,y,z 轴 重合。(利用坐标转换公式))

 

M-c * P-c = M-w * P-w = P. (同一个点P,世界空间(M-w * P-w) 和 相机空间(M-c * P-c) 的 表示 )

变换:

P-c = M-c ^ (-1) * P-w. (M-w 是世界空间矩阵,单位矩阵)

 

M-c:

[R-x  U-x  D-x  0]

[R-y  U-y  D-y  0]   

[R-z  U-z  D-z 0]

[0  0  0  0]

 

M-c ^ (-1) :

M-c 的转置 得到上面的 LookAt 的 左边的 矩阵。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值